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 jjtAccept(ParserVisitor visitor, Object 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 value( InternalContextAdapter context) |
58 | throws MethodInvocationException |
59 | { |
60 | /* |
61 | * get the two args |
62 | */ |
63 | |
64 | Object left = jjtGetChild(0).value( context ); |
65 | Object 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 ) || !( right instanceof Integer )) |
89 | { |
90 | rsvc.error( ( !( left instanceof Integer ) ? "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) 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( ( (Integer) left ).intValue() / ( (Integer) right ).intValue() ); |
113 | } |
114 | } |