01: /*
02: * Copyright 2002,2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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-27 00:06:03 +1100 (Sun, 27 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)
54: throws MissingAttributeException, JellyTagException {
55: if (null == this .valueExpression) {
56: throw new MissingAttributeException("value");
57: }
58: SwitchTag tag = (SwitchTag) findAncestorWithClass(SwitchTag.class);
59: if (null == tag) {
60: throw new JellyTagException(
61: "This tag must be enclosed inside a <switch> tag");
62: }
63: if (tag.hasDefaultBeenEncountered()) {
64: throw new JellyTagException(
65: "<default> should be the last tag within a <switch>");
66: }
67: Object value = valueExpression.evaluate(context);
68: if (tag.isFallingThru()
69: || (null == tag.getValue() && null == value)
70: || (null != tag.getValue() && tag.getValue().equals(
71: value))) {
72: tag.caseMatched();
73: tag.setFallingThru(fallThru);
74: invokeBody(output);
75: }
76: }
77:
78: // Attributes
79: //-------------------------------------------------------------------------
80: private Expression valueExpression = null;
81: private boolean fallThru = false;
82:
83: }
|