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  import java.math.BigInteger;
23  
24  import org.apache.velocity.context.InternalContextAdapter;
25  import org.apache.velocity.exception.TemplateInitException;
26  import org.apache.velocity.runtime.parser.Parser;
27  
28  /**
29   * Handles integer numbers.  The value will be either an Integer, a Long, or a BigInteger.
30   *
31   * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
32   */
33  public class ASTIntegerLiteral extends SimpleNode
34  {
35  
36      // This may be of type Integer, Long or BigInteger
37      private Number value = null;
38  
39      /**
40       * @param id
41       */
42      public ASTIntegerLiteral(int id)
43      {
44          super(id);
45      }
46  
47      /**
48       * @param p
49       * @param id
50       */
51      public ASTIntegerLiteral(Parser p, int id)
52      {
53          super(p, id);
54      }
55  
56  
57      /**
58       * @see org.apache.velocity.runtime.parser.node.SimpleNode#jjtAccept(org.apache.velocity.runtime.parser.node.ParserVisitor, java.lang.Object)
59       */
60      public Object jjtAccept(ParserVisitor visitor, Object data)
61      {
62          return visitor.visit(this, data);
63      }
64  
65      /**
66       * @see org.apache.velocity.runtime.parser.node.SimpleNode#init(org.apache.velocity.context.InternalContextAdapter, java.lang.Object)
67       */
68      public Object init( InternalContextAdapter context, Object data)
69          throws TemplateInitException
70      {
71          /*
72           *  init the tree correctly
73           */
74  
75          super.init( context, data );
76  
77          /**
78           * Determine the size of the item and make it an Integer, Long, or BigInteger as appropriate.
79           */
80           String str = getFirstToken().image;
81           try
82           {
83               value = new Integer( str );
84           }
85           catch ( NumberFormatException E1 )
86           {
87              try
88              {
89  
90                  value = new Long( str );
91  
92              }
93              catch ( NumberFormatException E2 )
94              {
95  
96                  // if there's still an Exception it will propogate out
97                  value = new BigInteger( str );
98              }
99          }
100 
101         return data;
102     }
103 
104     /**
105      * @see org.apache.velocity.runtime.parser.node.SimpleNode#value(org.apache.velocity.context.InternalContextAdapter)
106      */
107     public Object value( InternalContextAdapter context)
108     {
109         return value;
110     }
111 
112 }