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 multiplication |
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: ASTMulNode.java,v 1.7.8.1 2004/03/03 23:22:59 geirm Exp $ |
33 | */ |
34 | public class ASTMulNode extends SimpleNode |
35 | { |
36 | public ASTMulNode(int id) |
37 | { |
38 | super(id); |
39 | } |
40 | |
41 | public ASTMulNode(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 product of the two args. Returns null if either arg is null |
54 | * or if either arg is not an integer |
55 | */ |
56 | public Object value( InternalContextAdapter context ) |
57 | throws MethodInvocationException |
58 | { |
59 | /* |
60 | * get the two args |
61 | */ |
62 | |
63 | Object left = jjtGetChild(0).value( context ); |
64 | Object right = jjtGetChild(1).value( context ); |
65 | |
66 | /* |
67 | * if either is null, lets log and bail |
68 | */ |
69 | |
70 | if (left == null || right == null) |
71 | { |
72 | rsvc.error( ( left == null ? "Left" : "Right" ) + " side (" |
73 | + jjtGetChild( (left == null? 0 : 1) ).literal() |
74 | + ") of multiplication operation has null value." |
75 | + " Operation not possible. " |
76 | + context.getCurrentTemplateName() + " [line " + getLine() |
77 | + ", column " + getColumn() + "]"); |
78 | return null; |
79 | } |
80 | |
81 | /* |
82 | * if not an Integer, not much we can do either |
83 | */ |
84 | |
85 | if ( !( left instanceof Integer ) || !( right instanceof Integer )) |
86 | { |
87 | rsvc.error( ( !( left instanceof Integer ) ? "Left" : "Right" ) |
88 | + " side of multiplication operation is not a valid type. " |
89 | + "Currently only integers (1,2,3...) and Integer type is supported. " |
90 | + context.getCurrentTemplateName() + " [line " + getLine() |
91 | + ", column " + getColumn() + "]"); |
92 | |
93 | return null; |
94 | } |
95 | |
96 | return new Integer( ( (Integer) left ).intValue() * ( (Integer) right ).intValue() ); |
97 | } |
98 | } |
99 | |
100 | |
101 | |
102 | |