View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jelly.tags.core;
17  
18  import org.apache.commons.jelly.JellyTagException;
19  import org.apache.commons.jelly.TagSupport;
20  import org.apache.commons.jelly.XMLOutput;
21  
22  
23  /***
24   * A tag which conditionally evaluates its body if
25   * none of its preceeding sibling {@link CaseTag <case>}
26   * tags have been evaluated.
27   *
28   * This tag must be contained within the body of some
29   * {@link SwitchTag <switch>} tag.
30   *
31   * @see SwitchTag
32   *
33   * @author Rodney Waldhoff
34   * @version $Revision: 155420 $ $Date: 2005-02-26 14:06:03 +0100 (Sat, 26 Feb 2005) $
35   */
36  public class DefaultTag extends TagSupport {
37  
38      public DefaultTag() {
39      }
40  
41      // Tag interface
42      //-------------------------------------------------------------------------
43  
44      public void setFallThru(boolean fallThru) {
45          this.fallThru = fallThru;
46      }
47  
48      public void doTag(XMLOutput output) throws JellyTagException {
49          SwitchTag tag = (SwitchTag)findAncestorWithClass(SwitchTag.class);
50          if(null == tag) {
51              throw new JellyTagException("This tag must be enclosed inside a <switch> tag" );
52          }
53          if(tag.hasDefaultBeenEncountered()) {
54              throw new JellyTagException("Only one <default> tag is allowed per <switch>.");
55          }
56          tag.defaultEncountered();
57          if(tag.isFallingThru() || (!tag.hasSomeCaseMatched())) {
58              tag.caseMatched();
59              tag.setFallingThru(fallThru);
60              invokeBody(output);
61          }
62      }
63  
64      // Attributes
65      //-------------------------------------------------------------------------
66      private boolean fallThru = false;
67  
68  }