KickJava   Java API By Example, From Geeks To Geeks.

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


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 modulus division
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:geirm@optonline.net">Geir Magnusson Jr.</a>
27  * @version $Id: ASTModNode.java,v 1.6.10.1 2004/03/03 23:22:59 geirm Exp $
28 */

29 import org.apache.velocity.context.InternalContextAdapter;
30 import org.apache.velocity.runtime.parser.Parser;
31
32 import org.apache.velocity.exception.MethodInvocationException;
33
34 public class ASTModNode extends SimpleNode
35 {
36     public ASTModNode(int id)
37     {
38         super(id);
39     }
40
41     public ASTModNode(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     public Object JavaDoc value( InternalContextAdapter context)
53         throws MethodInvocationException
54     {
55         /*
56          * get the two args
57          */

58
59         Object JavaDoc left = jjtGetChild(0).value( context );
60         Object JavaDoc right = jjtGetChild(1).value( context );
61
62         /*
63          * if either is null, lets log and bail
64          */

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

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

95
96         if ( ( (Integer JavaDoc) right).intValue() == 0 )
97         {
98             rsvc.error( "Right side of modulus operation is zero. Must be non-zero. "
99                            + context.getCurrentTemplateName() + " [line " + getLine()
100                            + ", column " + getColumn() + "]");
101  
102             return null;
103         }
104
105         return new Integer JavaDoc( ( (Integer JavaDoc) left ).intValue() % ( (Integer JavaDoc) right ).intValue() );
106     }
107 }
108
109
Popular Tags