View Javadoc

1   package org.apache.velocity.runtime.parser.node;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  /**
23   * Handles number addition of nodes.<br><br>
24   *
25   * Please look at the Parser.jjt file which is
26   * what controls the generation of this class.
27   *
28   * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
29   * @author <a href="mailto:pero@antaramusic.de">Peter Romianowski</a>
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: ASTAddNode.java 463298 2006-10-12 16:10:32Z henning $
33   */
34  import org.apache.velocity.context.InternalContextAdapter;
35  import org.apache.velocity.runtime.parser.Parser;
36  import org.apache.velocity.runtime.parser.ParserVisitor;
37  
38  import org.apache.velocity.exception.MethodInvocationException;
39  
40  import org.apache.velocity.util.TemplateNumber;
41  
42  /**
43   *
44   */
45  public class ASTAddNode extends SimpleNode
46  {
47      /**
48       * @param id
49       */
50      public ASTAddNode(int id)
51      {
52          super(id);
53      }
54  
55      /**
56       * @param p
57       * @param id
58       */
59      public ASTAddNode(Parser p, int id)
60      {
61          super(p, id);
62      }
63  
64      /**
65       * @see org.apache.velocity.runtime.parser.node.SimpleNode#jjtAccept(org.apache.velocity.runtime.parser.ParserVisitor, java.lang.Object)
66       */
67      public Object jjtAccept(ParserVisitor visitor, Object data)
68      {
69          return visitor.visit(this, data);
70      }
71  
72      /**
73       *  computes the sum of the two nodes.
74       * @param context
75       *  @return result or null
76       * @throws MethodInvocationException
77       */
78      public Object value( InternalContextAdapter context)
79          throws MethodInvocationException
80      {
81          /*
82           *  get the two addends
83           */
84  
85          Object left = jjtGetChild(0).value(context);
86          Object right = jjtGetChild(1).value(context);
87  
88          /*
89           *  if either is null, lets log and bail
90           */
91  
92          if (left == null || right == null)
93          {
94              log.error((left == null ? "Left" : "Right")
95                             + " side ("
96                             + jjtGetChild( (left == null? 0 : 1) ).literal()
97                             + ") of addition operation has null value."
98                             + " Operation not possible. "
99                             + context.getCurrentTemplateName() + " [line " + getLine()
100                            + ", column " + getColumn() + "]");
101             return null;
102         }
103 
104         /*
105          *  convert to Number if applicable
106          */
107         if (left instanceof TemplateNumber)
108         {
109            left = ( (TemplateNumber) left).getAsNumber();
110         }
111         if (right instanceof TemplateNumber)
112         {
113            right = ( (TemplateNumber) right).getAsNumber();
114         }
115 
116         /*
117          * Arithmetic operation.
118          */
119         if (left instanceof Number && right instanceof Number)
120         {
121             return MathUtils.add((Number)left, (Number)right);
122         }
123 
124         /*
125          * shall we try for strings?
126          */
127         if (left instanceof String || right instanceof String)
128         {
129             return left.toString().concat(right.toString());
130         }
131         /*
132          *  if not a Number or Strings, not much we can do right now
133          */
134         log.error((!(left instanceof Number || left instanceof String) ? "Left" : "Right")
135                        + " side of addition operation is not a valid type. "
136                        + "Currently only Strings, numbers (1,2,3...) and Number type are supported. "
137                        + context.getCurrentTemplateName() + " [line " + getLine()
138                        + ", column " + getColumn() + "]");
139 
140         return null;
141     }
142 }
143 
144 
145 
146