KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 import org.apache.velocity.exception.MethodInvocationException;
23
24 /**
25  * Handles the equivalence operator
26  *
27  * <arg1> == <arg2>
28  *
29  * This operator requires that the LHS and RHS are both of the
30  * same Class.
31  *
32  * @version $Id: ASTEQNode.java,v 1.9.4.1 2004/03/03 23:22:58 geirm Exp $
33  */

34 public class ASTEQNode extends SimpleNode
35 {
36     public ASTEQNode(int id)
37     {
38         super(id);
39     }
40
41     public ASTEQNode(Parser p, int id)
42     {
43         super(p, id);
44     }
45
46     /** Accept the visitor. **/
47     public Object JavaDoc jjtAccept(ParserVisitor visitor, Object JavaDoc data)
48     {
49         return visitor.visit(this, data);
50     }
51
52     /**
53      * Calculates the value of the logical expression
54      *
55      * arg1 == arg2
56      *
57      * All class types are supported. Uses equals() to
58      * determine equivalence. This should work as we represent
59      * with the types we already support, and anything else that
60      * implements equals() to mean more than identical references.
61      *
62      *
63      * @param context internal context used to evaluate the LHS and RHS
64      * @return true if equivalent, false if not equivalent,
65      * false if not compatible arguments, or false
66      * if either LHS or RHS is null
67      */

68     public boolean evaluate( InternalContextAdapter context)
69         throws MethodInvocationException
70     {
71         Object JavaDoc left = jjtGetChild(0).value(context);
72         Object JavaDoc right = jjtGetChild(1).value(context);
73
74         /*
75          * they could be null if they are references and not in the context
76          */

77
78         if (left == null || right == null)
79         {
80             rsvc.error( ( left == null ? "Left" : "Right" )
81                            + " side ("
82                            + jjtGetChild( (left == null? 0 : 1) ).literal()
83                            + ") of '==' operation "
84                            + "has null value. "
85                            + "If a reference, it may not be in the context."
86                            + " Operation not possible. "
87                            + context.getCurrentTemplateName() + " [line " + getLine()
88                            + ", column " + getColumn() + "]");
89             return false;
90         }
91
92         /*
93          * check to see if they are the same class. I don't think this is slower
94          * as I don't think that getClass() results in object creation, and we can
95          * extend == to handle all classes
96          */

97
98         if (left.getClass().equals( right.getClass() ) )
99         {
100             return left.equals( right );
101         }
102         else
103         {
104             rsvc.error("Error in evaluation of == expression."
105                           + " Both arguments must be of the same Class."
106                           + " Currently left = " + left.getClass() + ", right = "
107                           + right.getClass() + ". "
108                           + context.getCurrentTemplateName() + " [line " + getLine()
109                           + ", column " + getColumn() + "] (ASTEQNode)");
110         }
111
112         return false;
113     }
114
115     public Object JavaDoc value(InternalContextAdapter context)
116         throws MethodInvocationException
117     {
118         boolean val = evaluate(context);
119
120         return val ? Boolean.TRUE : Boolean.FALSE;
121     }
122
123 }
124
Popular Tags