1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
75
76 private Expression valueExpression = null;
77 private boolean fallThru = false;
78
79 }