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