001: package org.apache.velocity.runtime.parser.node;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.velocity.context.InternalContextAdapter;
023: import org.apache.velocity.runtime.parser.Parser;
024: import org.apache.velocity.runtime.parser.ParserVisitor;
025: import org.apache.velocity.exception.MethodInvocationException;
026: import org.apache.velocity.util.TemplateNumber;
027:
028: /**
029: * Handles arg1 > arg2<br><br>
030: *
031: * Only subclasses of Number can be compared.<br><br>
032: *
033: * Please look at the Parser.jjt file which is
034: * what controls the generation of this class.
035: *
036: * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
037: * @author <a href="mailto:pero@antaramusic.de">Peter Romianowski</a>
038: */
039:
040: public class ASTGTNode extends SimpleNode {
041: /**
042: * @param id
043: */
044: public ASTGTNode(int id) {
045: super (id);
046: }
047:
048: /**
049: * @param p
050: * @param id
051: */
052: public ASTGTNode(Parser p, int id) {
053: super (p, id);
054: }
055:
056: /**
057: * @see org.apache.velocity.runtime.parser.node.SimpleNode#jjtAccept(org.apache.velocity.runtime.parser.ParserVisitor, java.lang.Object)
058: */
059: public Object jjtAccept(ParserVisitor visitor, Object data) {
060: return visitor.visit(this , data);
061: }
062:
063: /**
064: * @see org.apache.velocity.runtime.parser.node.SimpleNode#evaluate(org.apache.velocity.context.InternalContextAdapter)
065: */
066: public boolean evaluate(InternalContextAdapter context)
067: throws MethodInvocationException {
068: /*
069: * get the two args
070: */
071:
072: Object left = jjtGetChild(0).value(context);
073: Object right = jjtGetChild(1).value(context);
074:
075: /*
076: * if either is null, lets log and bail
077: */
078:
079: if (left == null || right == null) {
080: log.error((left == null ? "Left" : "Right") + " side ("
081: + jjtGetChild((left == null ? 0 : 1)).literal()
082: + ") of '>' operation has null value."
083: + " Operation not possible. "
084: + context.getCurrentTemplateName() + " [line "
085: + getLine() + ", column " + getColumn() + "]");
086: return false;
087: }
088:
089: /*
090: * convert to Number if applicable
091: */
092: if (left instanceof TemplateNumber) {
093: left = ((TemplateNumber) left).getAsNumber();
094: }
095: if (right instanceof TemplateNumber) {
096: right = ((TemplateNumber) right).getAsNumber();
097: }
098:
099: /*
100: * Only compare Numbers
101: */
102:
103: if (!(left instanceof Number) || !(right instanceof Number)) {
104: log.error((!(left instanceof Number) ? "Left" : "Right")
105: + " side of '>=' operation is not a Numbere. "
106: + context.getCurrentTemplateName() + " [line "
107: + getLine() + ", column " + getColumn() + "]");
108:
109: return false;
110: }
111:
112: return MathUtils.compare((Number) left, (Number) right) == 1;
113:
114: }
115:
116: /**
117: * @see org.apache.velocity.runtime.parser.node.SimpleNode#value(org.apache.velocity.context.InternalContextAdapter)
118: */
119: public Object value(InternalContextAdapter context)
120: throws MethodInvocationException {
121: boolean val = evaluate(context);
122:
123: return val ? Boolean.TRUE : Boolean.FALSE;
124: }
125:
126: }
|