KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 /**
21  * Handles integer addition of nodes
22  *
23  * Please look at the Parser.jjt file which is
24  * what controls the generation of this class.
25  *
26  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
27  * @author <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
28  * @version $Id: ASTAddNode.java,v 1.8.8.1 2004/03/03 23:22:58 geirm Exp $
29 */

30
31 import org.apache.velocity.context.InternalContextAdapter;
32 import org.apache.velocity.runtime.parser.Parser;
33
34 import org.apache.velocity.exception.MethodInvocationException;
35
36 public class ASTAddNode extends SimpleNode
37 {
38     public ASTAddNode(int id)
39     {
40         super(id);
41     }
42
43     public ASTAddNode(Parser p, int id)
44     {
45         super(p, id);
46     }
47
48     /** Accept the visitor. **/
49     public Object JavaDoc jjtAccept(ParserVisitor visitor, Object JavaDoc data)
50     {
51         return visitor.visit(this, data);
52     }
53
54     /**
55      * computes the sum of the two nodes. Currently only integer operations are
56      * supported.
57      * @return Integer object with value, or null
58      */

59     public Object JavaDoc value( InternalContextAdapter context)
60         throws MethodInvocationException
61     {
62         /*
63          * get the two addends
64          */

65
66         Object JavaDoc left = jjtGetChild(0).value( context );
67         Object JavaDoc right = jjtGetChild(1).value( context );
68
69         /*
70          * if either is null, lets log and bail
71          */

72
73         if (left == null || right == null)
74         {
75             rsvc.error( ( left == null ? "Left" : "Right" )
76                            + " side ("
77                            + jjtGetChild( (left == null? 0 : 1) ).literal()
78                            + ") of addition operation has null value."
79                            + " Operation not possible. "
80                            + context.getCurrentTemplateName() + " [line " + getLine()
81                            + ", column " + getColumn() + "]");
82             return null;
83         }
84         
85         /*
86          * if not an Integer, not much we can do either
87          */

88
89         if ( !( left instanceof Integer JavaDoc ) || !( right instanceof Integer JavaDoc ))
90         {
91             rsvc.error( ( !( left instanceof Integer JavaDoc ) ? "Left" : "Right" )
92                            + " side of addition operation is not a valid type. "
93                            + "Currently only integers (1,2,3...) and Integer type is supported. "
94                            + context.getCurrentTemplateName() + " [line " + getLine()
95                            + ", column " + getColumn() + "]");
96  
97             return null;
98         }
99
100         return new Integer JavaDoc( ( (Integer JavaDoc) left ).intValue() + ( (Integer JavaDoc) right ).intValue() );
101     }
102
103 }
104
105
106
107
108
Popular Tags