KickJava   Java API By Example, From Geeks To Geeks.

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


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

34 public class ASTDivNode extends SimpleNode
35 {
36     public ASTDivNode(int id)
37     {
38         super(id);
39     }
40
41     public ASTDivNode(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      * computes the result of the division. Currently limited to
54      * Integers.
55      * @return Integer(value) or null
56      */

57     public Object JavaDoc value( InternalContextAdapter context)
58         throws MethodInvocationException
59     {
60         /*
61          * get the two args
62          */

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

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

87
88         if ( !( left instanceof Integer JavaDoc ) || !( right instanceof Integer JavaDoc ))
89         {
90             rsvc.error( ( !( left instanceof Integer JavaDoc ) ? "Left" : "Right" )
91                            + " side of division operation is not a valid type. "
92                            + "Currently only integers (1,2,3...) and Integer type is supported. "
93                            + context.getCurrentTemplateName() + " [line " + getLine()
94                            + ", column " + getColumn() + "]");
95  
96             return null;
97         }
98
99         /*
100          * check for divide by 0
101          */

102
103         if ( ( (Integer JavaDoc) right).intValue() == 0 )
104         {
105             rsvc.error( "Right side of division operation is zero. Must be non-zero. "
106                            + context.getCurrentTemplateName() + " [line " + getLine()
107                            + ", column " + getColumn() + "]");
108  
109             return null;
110         }
111
112         return new Integer JavaDoc( ( (Integer JavaDoc) left ).intValue() / ( (Integer JavaDoc) right ).intValue() );
113     }
114 }
115
Popular Tags