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.MissingAttributeException;
20  import org.apache.commons.jelly.TagSupport;
21  import org.apache.commons.jelly.XMLOutput;
22  import org.apache.commons.jelly.expression.Expression;
23  
24  /***
25   * A tag which conditionally evaluates its body if
26   * my {@link #setValue value} attribute equals my ancestor
27   * {@link SwitchTag <switch>} tag's
28   * {@link SwitchTag#setOn "on"} attribute.
29   *
30   * This tag must be contained within the body of some
31   * {@link SwitchTag <switch>} tag.
32   *
33   * @see SwitchTag
34   *
35   * @author Rodney Waldhoff
36   * @version $Revision: 155420 $ $Date: 2005-02-26 14:06:03 +0100 (Sat, 26 Feb 2005) $
37   */
38  public class CaseTag extends TagSupport {
39  
40      public CaseTag() {
41      }
42  
43      // Tag interface
44      //-------------------------------------------------------------------------
45      public void setValue(Expression value) {
46          this.valueExpression = value;
47      }
48  
49      public void setFallThru(boolean fallThru) {
50          this.fallThru = fallThru;
51      }
52  
53      public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
54          if(null == this.valueExpression) {
55              throw new MissingAttributeException("value");
56          }
57          SwitchTag tag = (SwitchTag)findAncestorWithClass(SwitchTag.class);
58          if(null == tag) {
59              throw new JellyTagException("This tag must be enclosed inside a <switch> tag" );
60          }
61          if(tag.hasDefaultBeenEncountered()) {
62              throw new JellyTagException("<default> should be the last tag within a <switch>" );
63          }
64          Object value = valueExpression.evaluate(context);
65          if(tag.isFallingThru() ||
66             (null == tag.getValue() && null == value) ||
67             (null != tag.getValue() && tag.getValue().equals(value))) {
68              tag.caseMatched();
69              tag.setFallingThru(fallThru);
70              invokeBody(output);
71          }
72      }
73  
74      // Attributes
75      //-------------------------------------------------------------------------
76      private Expression valueExpression = null;
77      private boolean fallThru = false;
78  
79  }