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