KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > runtime > parser > node > ASTGENode


1 package org.apache.velocity.runtime.parser.node;
2
3 /*
4  * Copyright 2000-2001,2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.velocity.context.InternalContextAdapter;
20 import org.apache.velocity.runtime.parser.Parser;
21 import org.apache.velocity.exception.MethodInvocationException;
22
23 public class ASTGENode extends SimpleNode
24 {
25     public ASTGENode(int id)
26     {
27         super(id);
28     }
29
30     public ASTGENode(Parser p, int id)
31     {
32         super(p, id);
33     }
34
35     /** Accept the visitor. **/
36     public Object JavaDoc jjtAccept(ParserVisitor visitor, Object JavaDoc data)
37     {
38         return visitor.visit(this, data);
39     }
40
41     public boolean evaluate( InternalContextAdapter context)
42         throws MethodInvocationException
43     {
44         /*
45          * get the two args
46          */

47
48         Object JavaDoc left = jjtGetChild(0).value( context );
49         Object JavaDoc right = jjtGetChild(1).value( context );
50
51         /*
52          * if either is null, lets log and bail
53          */

54
55         if (left == null || right == null)
56         {
57             rsvc.error( ( left == null ? "Left" : "Right" )
58                            + " side ("
59                            + jjtGetChild( (left == null? 0 : 1) ).literal()
60                            + ") of '>=' operation has null value."
61                            + " Operation not possible. "
62                            + context.getCurrentTemplateName() + " [line "
63                            + getLine()
64                            + ", column " + getColumn() + "]");
65             return false;
66         }
67         
68         /*
69          * if not an Integer, not much we can do either
70          */

71
72         if ( !( left instanceof Integer JavaDoc ) || !( right instanceof Integer JavaDoc ))
73         {
74             rsvc.error( ( !( left instanceof Integer JavaDoc ) ? "Left" : "Right" )
75                            + " side of '>=' operation is not a valid type. "
76                            + " It is a " + ( !( left instanceof Integer JavaDoc ) ? left.getClass() : right.getClass() )
77                            + ". Currently only integers (1,2,3...) and Integer type is supported. "
78                            + context.getCurrentTemplateName() + " [line " + getLine()
79                            + ", column " + getColumn() + "]");
80  
81             return false;
82         }
83
84         return ( (Integer JavaDoc) left).intValue() >= ((Integer JavaDoc) right).intValue();
85      
86     }
87
88     public Object JavaDoc value(InternalContextAdapter context)
89         throws MethodInvocationException
90     {
91         boolean val = evaluate(context);
92
93         return val ? Boolean.TRUE : Boolean.FALSE;
94     }
95
96 }
97
Popular Tags