001: /*
002:
003: * Copyright (c) 2006 The Visigoth Software Society. All rights
004:
005: * reserved.
006:
007: *
008:
009: * Redistribution and use in source and binary forms, with or without
010:
011: * modification, are permitted provided that the following conditions
012:
013: * are met:
014:
015: *
016:
017: * 1. Redistributions of source code must retain the above copyright
018:
019: * notice, this list of conditions and the following disclaimer.
020:
021: *
022:
023: * 2. Redistributions in binary form must reproduce the above copyright
024:
025: * notice, this list of conditions and the following disclaimer in
026:
027: * the documentation and/or other materials provided with the
028:
029: * distribution.
030:
031: *
032:
033: * 3. The end-user documentation included with the redistribution, if
034:
035: * any, must include the following acknowledgement:
036:
037: * "This product includes software developed by the
038:
039: * Visigoth Software Society (http://www.visigoths.org/)."
040:
041: * Alternately, this acknowledgement may appear in the software itself,
042:
043: * if and wherever such third-party acknowledgements normally appear.
044:
045: *
046:
047: * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
048:
049: * project contributors may be used to endorse or promote products derived
050:
051: * from this software without prior written permission. For written
052:
053: * permission, please contact visigoths@visigoths.org.
054:
055: *
056:
057: * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
058:
059: * nor may "FreeMarker" or "Visigoth" appear in their names
060:
061: * without prior written permission of the Visigoth Software Society.
062:
063: *
064:
065: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
066:
067: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
068:
069: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
070:
071: * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
072:
073: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
074:
075: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
076:
077: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
078:
079: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
080:
081: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
082:
083: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
084:
085: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
086:
087: * SUCH DAMAGE.
088:
089: * ====================================================================
090:
091: *
092:
093: * This software consists of voluntary contributions made by many
094:
095: * individuals on behalf of the Visigoth Software Society. For more
096:
097: * information on the Visigoth Software Society, please see
098:
099: * http://www.visigoths.org/
100:
101: */
102:
103: package freemarker.core;
104:
105: import freemarker.template.*;
106:
107: class DefaultToExpression extends Expression {
108:
109: private static final TemplateCollectionModel EMPTY_COLLECTION = new SimpleCollection(
110: new java.util.ArrayList(0));
111:
112: static private class EmptyStringAndSequence
113:
114: implements TemplateScalarModel, TemplateSequenceModel,
115: TemplateHashModelEx {
116:
117: public String getAsString() {
118:
119: return "";
120:
121: }
122:
123: public TemplateModel get(int i) {
124:
125: return null;
126:
127: }
128:
129: public TemplateModel get(String s) {
130:
131: return null;
132:
133: }
134:
135: public int size() {
136:
137: return 0;
138:
139: }
140:
141: public boolean isEmpty() {
142:
143: return true;
144:
145: }
146:
147: public TemplateCollectionModel keys() {
148:
149: return EMPTY_COLLECTION;
150:
151: }
152:
153: public TemplateCollectionModel values() {
154:
155: return EMPTY_COLLECTION;
156:
157: }
158:
159: }
160:
161: static final TemplateModel EMPTY_STRING_AND_SEQUENCE = new EmptyStringAndSequence();
162:
163: private Expression lhs, rhs;
164:
165: DefaultToExpression(Expression lhs, Expression rhs) {
166:
167: this .lhs = lhs;
168:
169: this .rhs = rhs;
170:
171: }
172:
173: TemplateModel _getAsTemplateModel(Environment env)
174: throws TemplateException {
175:
176: TemplateModel left = null;
177:
178: try {
179:
180: left = lhs.getAsTemplateModel(env);
181:
182: } catch (InvalidReferenceException ire) {
183:
184: if (!(lhs instanceof ParentheticalExpression)) {
185:
186: throw ire;
187:
188: }
189:
190: }
191:
192: if (left != null)
193: return left;
194:
195: if (rhs == null)
196: return EMPTY_STRING_AND_SEQUENCE;
197:
198: return rhs.getAsTemplateModel(env);
199:
200: }
201:
202: boolean isLiteral() {
203:
204: return false;
205:
206: }
207:
208: Expression _deepClone(String name, Expression subst) {
209:
210: if (rhs == null) {
211:
212: return new DefaultToExpression(lhs.deepClone(name, subst),
213: null);
214:
215: }
216:
217: return new DefaultToExpression(lhs.deepClone(name, subst), rhs
218: .deepClone(name, subst));
219:
220: }
221:
222: public String getCanonicalForm() {
223:
224: if (rhs == null) {
225:
226: return lhs.getCanonicalForm() + "!";
227:
228: }
229:
230: return lhs.getCanonicalForm() + "!" + rhs.getCanonicalForm();
231:
232: }
233:
234: }
|