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.Date;
056: import freemarker.template.*;
057:
058: /**
059: * A class that handles comparisons.
060: * @author <a href="mailto:jon@revusky.com">Jonathan Revusky</a>
061: * @author Attila Szegedi
062: */
063:
064: final class ComparisonExpression extends BooleanExpression {
065:
066: static final int EQUALS = 1;
067: static final int NOT_EQUALS = 2;
068: static final int LESS_THAN = 3;
069: static final int GREATER_THAN = 4;
070: static final int LESS_THAN_EQUALS = 5;
071: static final int GREATER_THAN_EQUALS = 6;
072:
073: private final Expression left;
074: private final Expression right;
075: private final int operation;
076: private final String opString;
077:
078: ComparisonExpression(Expression left, Expression right,
079: String opString) {
080: this .left = left;
081: this .right = right;
082: opString = opString.intern();
083: this .opString = opString;
084: if (opString == "==" || opString == "=") {
085: operation = EQUALS;
086: } else if (opString == "!=") {
087: operation = NOT_EQUALS;
088: } else if (opString == "gt" || opString == "\\gt"
089: || opString == ">" || opString == ">") {
090: operation = GREATER_THAN;
091: } else if (opString == "gte" || opString == "\\gte"
092: || opString == ">=" || opString == ">=") {
093: operation = GREATER_THAN_EQUALS;
094: } else if (opString == "lt" || opString == "\\lt"
095: || opString == "<" || opString == "<") {
096: operation = LESS_THAN;
097: } else if (opString == "lte" || opString == "\\lte"
098: || opString == "<=" || opString == "<=") {
099: operation = LESS_THAN_EQUALS;
100: } else {
101: throw new RuntimeException("Unknown comparison operator "
102: + opString);
103: }
104: }
105:
106: /*
107: * WARNING! This algorithm is duplicated in SequenceBuiltins.modelsEqual.
108: * Thus, if you update this method, then you have to update that too!
109: */
110: boolean isTrue(Environment env) throws TemplateException {
111: TemplateModel ltm = left.getAsTemplateModel(env);
112: TemplateModel rtm = right.getAsTemplateModel(env);
113: if (env != null && env.isClassicCompatible()) {
114: if (ltm == null) {
115: ltm = TemplateScalarModel.EMPTY_STRING;
116: }
117: if (rtm == null) {
118: rtm = TemplateScalarModel.EMPTY_STRING;
119: }
120: }
121: assertNonNull(ltm, left, env);
122: assertNonNull(rtm, right, env);
123: int comp = 0;
124: if (ltm instanceof TemplateNumberModel
125: && rtm instanceof TemplateNumberModel) {
126: Number first = EvaluationUtil.getNumber(
127: (TemplateNumberModel) ltm, left, env);
128: Number second = EvaluationUtil.getNumber(
129: (TemplateNumberModel) rtm, right, env);
130: ArithmeticEngine ae = env != null ? env
131: .getArithmeticEngine() : getTemplate()
132: .getArithmeticEngine();
133: comp = ae.compareNumbers(first, second);
134: } else if (ltm instanceof TemplateDateModel
135: && rtm instanceof TemplateDateModel) {
136: TemplateDateModel ltdm = (TemplateDateModel) ltm;
137: TemplateDateModel rtdm = (TemplateDateModel) rtm;
138: int ltype = ltdm.getDateType();
139: int rtype = rtdm.getDateType();
140: if (ltype != rtype) {
141: throw new TemplateException(
142: "Can not compare dates of different type. Left date is of "
143: + TemplateDateModel.TYPE_NAMES
144: .get(ltype)
145: + " type, right date is of "
146: + TemplateDateModel.TYPE_NAMES
147: .get(rtype) + " type.", env);
148: }
149: if (ltype == TemplateDateModel.UNKNOWN) {
150: throw new TemplateException(
151: "Left date is of UNKNOWN type, and can not be compared.",
152: env);
153: }
154: if (rtype == TemplateDateModel.UNKNOWN) {
155: throw new TemplateException(
156: "Right date is of UNKNOWN type, and can not be compared.",
157: env);
158: }
159:
160: Date first = EvaluationUtil.getDate(ltdm, left, env);
161: Date second = EvaluationUtil.getDate(rtdm, right, env);
162: comp = first.compareTo(second);
163: } else if (ltm instanceof TemplateScalarModel
164: && rtm instanceof TemplateScalarModel) {
165: if (operation != EQUALS && operation != NOT_EQUALS) {
166: throw new TemplateException("Can not use operator "
167: + opString + " on string values.", env);
168: }
169: String first = EvaluationUtil.getString(
170: (TemplateScalarModel) ltm, left, env);
171: String second = EvaluationUtil.getString(
172: (TemplateScalarModel) rtm, right, env);
173: comp = env.getCollator().compare(first, second);
174: } else if (ltm instanceof TemplateBooleanModel
175: && rtm instanceof TemplateBooleanModel) {
176: if (operation != EQUALS && operation != NOT_EQUALS) {
177: throw new TemplateException("Can not use operator "
178: + opString + " on boolean values.", env);
179: }
180: boolean first = ((TemplateBooleanModel) ltm).getAsBoolean();
181: boolean second = ((TemplateBooleanModel) rtm)
182: .getAsBoolean();
183: comp = (first ? 1 : 0) - (second ? 1 : 0);
184: }
185: // Here we handle compatibility issues
186: else if (env.isClassicCompatible()) {
187: String first = left.getStringValue(env);
188: String second = right.getStringValue(env);
189: comp = env.getCollator().compare(first, second);
190: } else {
191: throw new TemplateException(
192: "The only legal comparisons are between two numbers, two strings, or two dates.\n"
193: + "Left hand operand is a "
194: + ltm.getClass().getName()
195: + "\n"
196: + "Right hand operand is a "
197: + rtm.getClass().getName() + "\n", env);
198: }
199: switch (operation) {
200: case EQUALS:
201: return comp == 0;
202: case NOT_EQUALS:
203: return comp != 0;
204: case LESS_THAN:
205: return comp < 0;
206: case GREATER_THAN:
207: return comp > 0;
208: case LESS_THAN_EQUALS:
209: return comp <= 0;
210: case GREATER_THAN_EQUALS:
211: return comp >= 0;
212: default:
213: throw new TemplateException("unknown operation", env);
214: }
215: }
216:
217: public String getCanonicalForm() {
218: return left.getCanonicalForm() + ' ' + opString + ' '
219: + right.getCanonicalForm();
220: }
221:
222: boolean isLiteral() {
223: return constantValue != null
224: || (left.isLiteral() && right.isLiteral());
225: }
226:
227: Expression _deepClone(String name, Expression subst) {
228: return new ComparisonExpression(left.deepClone(name, subst),
229: right.deepClone(name, subst), opString);
230: }
231: }
|