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.ArrayList;
056: import java.util.Collections;
057: import java.util.Iterator;
058: import java.util.List;
059: import java.util.ListIterator;
060: import freemarker.template.*;
061: import freemarker.template.utility.Collections12;
062: import java.io.IOException;
063:
064: final class ListLiteral extends Expression {
065:
066: final ArrayList values;
067:
068: ListLiteral(ArrayList values) {
069: this .values = values;
070: values.trimToSize();
071: }
072:
073: TemplateModel _getAsTemplateModel(Environment env)
074: throws TemplateException {
075: SimpleSequence list = new SimpleSequence(values.size());
076: for (Iterator it = values.iterator(); it.hasNext();) {
077: Expression exp = (Expression) it.next();
078: TemplateModel tm = exp.getAsTemplateModel(env);
079: assertNonNull(tm, exp, env);
080: list.add(tm);
081: }
082: return list;
083: }
084:
085: /**
086: * For the benefit of method calls, return the list of arguments as a list
087: * of values.
088: */
089: List getValueList(Environment env) throws TemplateException {
090: int size = values.size();
091: switch (size) {
092: case 0: {
093: return Collections.EMPTY_LIST;
094: }
095: case 1: {
096: return Collections12.singletonList(((Expression) values
097: .get(0)).getStringValue(env));
098: }
099: default: {
100: List result = new ArrayList(values.size());
101: for (ListIterator iterator = values.listIterator(); iterator
102: .hasNext();) {
103: Expression exp = (Expression) iterator.next();
104: result.add(exp.getStringValue(env));
105: }
106: return result;
107: }
108: }
109: }
110:
111: /**
112: * For the benefit of extended method calls, return the list of arguments as a
113: * list of template models.
114: */
115: List getModelList(Environment env) throws TemplateException {
116: int size = values.size();
117: switch (size) {
118: case 0: {
119: return Collections.EMPTY_LIST;
120: }
121: case 1: {
122: return Collections12.singletonList(((Expression) values
123: .get(0)).getAsTemplateModel(env));
124: }
125: default: {
126: List result = new ArrayList(values.size());
127: for (ListIterator iterator = values.listIterator(); iterator
128: .hasNext();) {
129: Expression exp = (Expression) iterator.next();
130: result.add(exp.getAsTemplateModel(env));
131: }
132: return result;
133: }
134: }
135: }
136:
137: public String getCanonicalForm() {
138: StringBuffer buf = new StringBuffer("[");
139: int size = values.size();
140: for (int i = 0; i < size; i++) {
141: Expression value = (Expression) values.get(i);
142: buf.append(value.getCanonicalForm());
143: if (i != size - 1) {
144: buf.append(",");
145: }
146: }
147: buf.append("]");
148: return buf.toString();
149: }
150:
151: boolean isLiteral() {
152: if (constantValue != null) {
153: return true;
154: }
155: for (int i = 0; i < values.size(); i++) {
156: Expression exp = (Expression) values.get(i);
157: if (!exp.isLiteral()) {
158: return false;
159: }
160: }
161: return true;
162: }
163:
164: // A hacky routine used by VisitNode and RecurseNode
165:
166: TemplateSequenceModel evaluateStringsToNamespaces(Environment env)
167: throws TemplateException {
168: TemplateSequenceModel val = (TemplateSequenceModel) getAsTemplateModel(env);
169: SimpleSequence result = new SimpleSequence(val.size());
170: for (int i = 0; i < values.size(); i++) {
171: if (values.get(i) instanceof StringLiteral) {
172: String s = ((StringLiteral) values.get(i))
173: .getAsString();
174: try {
175: Environment.Namespace ns = env.importLib(s, null);
176: result.add(ns);
177: } catch (IOException ioe) {
178: throw new TemplateException(
179: "Could not import library '" + s + "', "
180: + ioe.getMessage(), env);
181: }
182: } else {
183: result.add(val.get(i));
184: }
185: }
186: return result;
187: }
188:
189: Expression _deepClone(String name, Expression subst) {
190: ArrayList clonedValues = (ArrayList) values.clone();
191: for (ListIterator iter = clonedValues.listIterator(); iter
192: .hasNext();) {
193: iter.set(((Expression) iter.next()).deepClone(name, subst));
194: }
195: return new ListLiteral(clonedValues);
196: }
197:
198: }
|