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:
026: import org.apache.velocity.exception.MethodInvocationException;
027: import org.apache.velocity.util.TemplateNumber;
028:
029: /**
030: * Handles arg1 < arg2<br><br>
031: *
032: * Only subclasses of Number can be compared.<br><br>
033: *
034: * Please look at the Parser.jjt file which is
035: * what controls the generation of this class.
036: *
037: * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
038: * @author <a href="mailto:pero@antaramusic.de">Peter Romianowski</a>
039: */
040:
041: public class ASTLTNode extends SimpleNode {
042: /**
043: * @param id
044: */
045: public ASTLTNode(int id) {
046: super (id);
047: }
048:
049: /**
050: * @param p
051: * @param id
052: */
053: public ASTLTNode(Parser p, int id) {
054: super (p, id);
055: }
056:
057: /**
058: * @see org.apache.velocity.runtime.parser.node.SimpleNode#jjtAccept(org.apache.velocity.runtime.parser.ParserVisitor, java.lang.Object)
059: */
060: public Object jjtAccept(ParserVisitor visitor, Object data) {
061: return visitor.visit(this , data);
062: }
063:
064: /**
065: * @see org.apache.velocity.runtime.parser.node.SimpleNode#evaluate(org.apache.velocity.context.InternalContextAdapter)
066: */
067: public boolean evaluate(InternalContextAdapter context)
068: throws MethodInvocationException {
069: /*
070: * get the two args
071: */
072:
073: Object left = jjtGetChild(0).value(context);
074: Object right = jjtGetChild(1).value(context);
075:
076: /*
077: * if either is null, lets log and bail
078: */
079:
080: if (left == null || right == null) {
081: log.error((left == null ? "Left" : "Right") + " side ("
082: + jjtGetChild((left == null ? 0 : 1)).literal()
083: + ") of '<' operation has null value."
084: + " Operation not possible. "
085: + context.getCurrentTemplateName() + " [line "
086: + getLine() + ", column " + getColumn() + "]");
087: return false;
088: }
089:
090: /*
091: * convert to Number if applicable
092: */
093: if (left instanceof TemplateNumber) {
094: left = ((TemplateNumber) left).getAsNumber();
095: }
096: if (right instanceof TemplateNumber) {
097: right = ((TemplateNumber) right).getAsNumber();
098: }
099:
100: /*
101: * Only compare Numbers
102: */
103:
104: if (!(left instanceof Number) || !(right instanceof Number)) {
105: log.error((!(left instanceof Number) ? "Left" : "Right")
106: + " side of '>=' operation is not a valid Number. "
107: + context.getCurrentTemplateName() + " [line "
108: + getLine() + ", column " + getColumn() + "]");
109:
110: return false;
111: }
112:
113: return MathUtils.compare((Number) left, (Number) right) == -1;
114:
115: }
116:
117: /**
118: * @see org.apache.velocity.runtime.parser.node.SimpleNode#value(org.apache.velocity.context.InternalContextAdapter)
119: */
120: public Object value(InternalContextAdapter context)
121: throws MethodInvocationException {
122: boolean val = evaluate(context);
123:
124: return val ? Boolean.TRUE : Boolean.FALSE;
125: }
126:
127: }
|