001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jelly.tags.core;
017:
018: import org.apache.commons.jelly.JellyTagException;
019: import org.apache.commons.jelly.MissingAttributeException;
020: import org.apache.commons.jelly.TagSupport;
021: import org.apache.commons.jelly.XMLOutput;
022: import org.apache.commons.jelly.expression.Expression;
023:
024: /**
025: * Executes the child <case> tag whose value equals my on attribute.
026: * Executes a child <default> tag when present and no <case> tag has
027: * yet matched.
028: *
029: * @see CaseTag
030: * @see DefaultTag
031: *
032: * @author Rodney Waldhoff
033: * @version $Revision: 155420 $ $Date: 2005-02-27 00:06:03 +1100 (Sun, 27 Feb 2005) $
034: */
035: public class SwitchTag extends TagSupport {
036:
037: public SwitchTag() {
038: }
039:
040: // Tag interface
041: //-------------------------------------------------------------------------
042:
043: /**
044: * Sets the value to switch on.
045: * Note that the {@link Expression} is evaluated only once, when the
046: * <switch> tag is evaluated.
047: * @param on the value to switch on
048: */
049: public void setOn(Expression on) {
050: this .on = on;
051: }
052:
053: public void doTag(XMLOutput output)
054: throws MissingAttributeException, JellyTagException {
055: this .defaultEncountered = false;
056: this .someCaseMatched = false;
057: this .fallingThru = false;
058:
059: if (null == on) {
060: throw new MissingAttributeException("on");
061: } else {
062: value = on.evaluate(context);
063: invokeBody(output);
064: }
065: }
066:
067: // Protected properties
068: //-------------------------------------------------------------------------
069: protected boolean hasSomeCaseMatched() {
070: return this .someCaseMatched;
071: }
072:
073: protected void caseMatched() {
074: this .someCaseMatched = true;
075: }
076:
077: protected boolean isFallingThru() {
078: return this .fallingThru;
079: }
080:
081: protected void setFallingThru(boolean fallingThru) {
082: this .fallingThru = fallingThru;
083: }
084:
085: protected Object getValue() {
086: return value;
087: }
088:
089: protected boolean hasDefaultBeenEncountered() {
090: return defaultEncountered;
091: }
092:
093: protected void defaultEncountered() {
094: this .defaultEncountered = true;
095: }
096:
097: // Attributes
098: //-------------------------------------------------------------------------
099: private boolean someCaseMatched = false;
100: private boolean fallingThru = false;
101: private boolean defaultEncountered = false;
102: private Expression on = null;
103: private Object value = null;
104:
105: }
|