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 freemarker.template.*;
057:
058: /**
059: * A unary operator that uses the string value of an expression as a hash key.
060: * It associates with the <tt>Identifier</tt> or <tt>Dot</tt> to its left.
061: */
062: final class DynamicKeyName extends Expression {
063:
064: private final Expression nameExpression;
065: private final Expression target;
066:
067: DynamicKeyName(Expression target, Expression nameExpression) {
068: this .target = target;
069: this .nameExpression = nameExpression;
070: }
071:
072: TemplateModel _getAsTemplateModel(Environment env)
073: throws TemplateException {
074: TemplateModel targetModel = target.getAsTemplateModel(env);
075: assertNonNull(targetModel, target, env);
076: if (nameExpression instanceof Range) {
077: return dealWithRangeKey(targetModel,
078: (Range) nameExpression, env);
079: }
080: TemplateModel keyModel = nameExpression.getAsTemplateModel(env);
081: if (keyModel == null) {
082: if (env.isClassicCompatible()) {
083: keyModel = TemplateScalarModel.EMPTY_STRING;
084: } else {
085: assertNonNull(keyModel, nameExpression, env);
086: }
087: }
088: if (keyModel instanceof TemplateNumberModel) {
089: int index = EvaluationUtil.getNumber(keyModel,
090: nameExpression, env).intValue();
091: return dealWithNumericalKey(targetModel, index, env);
092: }
093: if (keyModel instanceof TemplateScalarModel) {
094: String key = EvaluationUtil
095: .getString((TemplateScalarModel) keyModel,
096: nameExpression, env);
097: return dealWithStringKey(targetModel, key, env);
098: }
099: throw invalidTypeException(keyModel, nameExpression, env,
100: "number, range, or string");
101: }
102:
103: private TemplateModel dealWithNumericalKey(
104: TemplateModel targetModel, int index, Environment env)
105: throws TemplateException {
106: if (targetModel instanceof TemplateSequenceModel) {
107: TemplateSequenceModel tsm = (TemplateSequenceModel) targetModel;
108: int size = Integer.MAX_VALUE;
109: try {
110: size = tsm.size();
111: } catch (Exception e) {
112: }
113: return index < size ? tsm.get(index) : null;
114: }
115:
116: try {
117: String s = target.getStringValue(env);
118: try {
119: return new SimpleScalar(s.substring(index, index + 1));
120: } catch (RuntimeException re) {
121: throw new TemplateException("", re, env);
122: }
123: } catch (NonStringException e) {
124: throw invalidTypeException(targetModel, target, env,
125: "number, sequence, or string");
126: }
127: }
128:
129: private TemplateModel dealWithStringKey(TemplateModel targetModel,
130: String key, Environment env) throws TemplateException {
131: if (targetModel instanceof TemplateHashModel) {
132: return ((TemplateHashModel) targetModel).get(key);
133: }
134: throw invalidTypeException(targetModel, target, env, "hash");
135: }
136:
137: private TemplateModel dealWithRangeKey(TemplateModel targetModel,
138: Range range, Environment env) throws TemplateException {
139: int start = EvaluationUtil.getNumber(range.left, env)
140: .intValue();
141: int end = 0;
142: boolean hasRhs = range.hasRhs();
143: if (hasRhs) {
144: end = EvaluationUtil.getNumber(range.right, env).intValue();
145: }
146: if (targetModel instanceof TemplateSequenceModel) {
147: TemplateSequenceModel sequence = (TemplateSequenceModel) targetModel;
148: if (!hasRhs)
149: end = sequence.size() - 1;
150: if (start < 0) {
151: String msg = range.left.getStartLocation()
152: + "\nNegative starting index for range, is "
153: + range;
154: throw new TemplateException(msg, env);
155: }
156: if (end < 0) {
157: String msg = range.left.getStartLocation()
158: + "\nNegative ending index for range, is "
159: + range;
160: throw new TemplateException(msg, env);
161: }
162: if (start >= sequence.size()) {
163: String msg = range.left.getStartLocation()
164: + "\nLeft side index of range out of bounds, is "
165: + start
166: + ", but the sequence has only "
167: + sequence.size()
168: + " element(s) "
169: + "(note that indices are 0 based, and ranges are inclusive).";
170: throw new TemplateException(msg, env);
171: }
172: if (end >= sequence.size()) {
173: String msg = range.right.getStartLocation()
174: + "\nRight side index of range out of bounds, is "
175: + end
176: + ", but the sequence has only "
177: + sequence.size()
178: + " element(s)."
179: + "(note that indices are 0 based, and ranges are inclusive).";
180: throw new TemplateException(msg, env);
181: }
182: ArrayList list = new ArrayList(1 + Math.abs(start - end));
183: if (start > end) {
184: for (int i = start; i >= end; i--) {
185: list.add(sequence.get(i));
186: }
187: } else {
188: for (int i = start; i <= end; i++) {
189: list.add(sequence.get(i));
190: }
191: }
192: return new SimpleSequence(list);
193: }
194:
195: try {
196: String s = target.getStringValue(env);
197: if (!hasRhs)
198: end = s.length() - 1;
199: if (start < 0) {
200: String msg = range.left.getStartLocation()
201: + "\nNegative starting index for range "
202: + range + " : " + start;
203: throw new TemplateException(msg, env);
204: }
205: if (end < 0) {
206: String msg = range.left.getStartLocation()
207: + "\nNegative ending index for range " + range
208: + " : " + end;
209: throw new TemplateException(msg, env);
210: }
211: if (start > s.length()) {
212: String msg = range.left.getStartLocation()
213: + "\nLeft side of range out of bounds, is: "
214: + start + "\nbut string " + targetModel
215: + " has " + s.length() + " elements.";
216: throw new TemplateException(msg, env);
217: }
218: if (end > s.length()) {
219: String msg = range.right.getStartLocation()
220: + "\nRight side of range out of bounds, is: "
221: + end + "\nbut string " + targetModel
222: + " is only " + s.length() + " characters.";
223: throw new TemplateException(msg, env);
224: }
225: try {
226: return new SimpleScalar(s.substring(start, end + 1));
227: } catch (RuntimeException re) {
228: String msg = "Error " + getStartLocation();
229: throw new TemplateException(msg, re, env);
230: }
231: } catch (NonStringException e) {
232: throw invalidTypeException(target.getAsTemplateModel(env),
233: target, env, "number, scalar, or sequence");
234: }
235: }
236:
237: public String getCanonicalForm() {
238: return target.getCanonicalForm() + "["
239: + nameExpression.getCanonicalForm() + "]";
240: }
241:
242: boolean isLiteral() {
243: return constantValue != null
244: || (target.isLiteral() && nameExpression.isLiteral());
245: }
246:
247: Expression _deepClone(String name, Expression subst) {
248: return new DynamicKeyName(target.deepClone(name, subst),
249: nameExpression.deepClone(name, subst));
250: }
251: }
|