001: /*
002: * Copyright (c) 2003 The Visigoth Software Society. All rights
003: * reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * 2. Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowledgement:
019: * "This product includes software developed by the
020: * Visigoth Software Society (http://www.visigoths.org/)."
021: * Alternately, this acknowledgement may appear in the software itself,
022: * if and wherever such third-party acknowledgements normally appear.
023: *
024: * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
025: * project contributors may be used to endorse or promote products derived
026: * from this software without prior written permission. For written
027: * permission, please contact visigoths@visigoths.org.
028: *
029: * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
030: * nor may "FreeMarker" or "Visigoth" appear in their names
031: * without prior written permission of the Visigoth Software Society.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of the Visigoth Software Society. For more
049: * information on the Visigoth Software Society, please see
050: * http://www.visigoths.org/
051: */
052:
053: package freemarker.core;
054:
055: import java.util.*;
056: import java.io.IOException;
057: import freemarker.template.*;
058:
059: /**
060: * An instruction representing a switch-case structure.
061: */
062: final class SwitchBlock extends TemplateElement {
063:
064: private Case defaultCase;
065: private Expression testExpression;
066:
067: /**
068: * @param testExpression the expression to be tested.
069: */
070: SwitchBlock(Expression testExpression) {
071: this .testExpression = testExpression;
072: nestedElements = new LinkedList();
073: }
074:
075: /**
076: * @param cas a Case element.
077: */
078: void addCase(Case cas) {
079: if (cas.isDefault) {
080: defaultCase = cas;
081: }
082: nestedElements.add(cas);
083: }
084:
085: void accept(Environment env) throws TemplateException, IOException {
086: boolean processedCase = false;
087: Iterator iterator = nestedElements.iterator();
088: try {
089: while (iterator.hasNext()) {
090: Case cas = (Case) iterator.next();
091: boolean processCase = false;
092:
093: // Fall through if a previous case tested true.
094: if (processedCase) {
095: processCase = true;
096: } else if (!cas.isDefault) {
097: // Otherwise, if this case isn't the default, test it.
098: ComparisonExpression equalsOp = new ComparisonExpression(
099: testExpression, cas.expression, "==");
100: processCase = equalsOp.isTrue(env);
101: }
102: if (processCase) {
103: env.visit(cas);
104: processedCase = true;
105: }
106: }
107:
108: // If we didn't process any nestedElements, and we have a default,
109: // process it.
110: if (!processedCase && defaultCase != null) {
111: env.visit(defaultCase);
112: }
113: } catch (BreakInstruction.Break br) {
114: }
115: }
116:
117: public String getCanonicalForm() {
118: StringBuffer buf = new StringBuffer("<#switch ");
119: buf.append(testExpression.getCanonicalForm());
120: buf.append(">");
121: for (int i = 0; i < nestedElements.size(); i++) {
122: Case cas = (Case) nestedElements.get(i);
123: buf.append(cas.getCanonicalForm());
124: }
125: if (defaultCase != null) {
126: buf.append(defaultCase.getCanonicalForm());
127: }
128: buf.append("</#switch>");
129: return buf.toString();
130: }
131:
132: public String getDescription() {
133: return "switch " + testExpression;
134: }
135: }
|