001: package org.apache.velocity.runtime.parser.node;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.math.BigDecimal;
023:
024: import org.apache.velocity.context.InternalContextAdapter;
025: import org.apache.velocity.exception.TemplateInitException;
026: import org.apache.velocity.runtime.parser.Parser;
027: import org.apache.velocity.runtime.parser.ParserVisitor;
028:
029: /**
030: * Handles floating point numbers. The value will be either a Double
031: * or a BigDecimal.
032: *
033: * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
034: */
035: public class ASTFloatingPointLiteral extends SimpleNode {
036:
037: // This may be of type Double or BigDecimal
038: private Number value = null;
039:
040: /**
041: * @param id
042: */
043: public ASTFloatingPointLiteral(int id) {
044: super (id);
045: }
046:
047: /**
048: * @param p
049: * @param id
050: */
051: public ASTFloatingPointLiteral(Parser p, int id) {
052: super (p, id);
053: }
054:
055: /**
056: * @see org.apache.velocity.runtime.parser.node.SimpleNode#jjtAccept(org.apache.velocity.runtime.parser.ParserVisitor, java.lang.Object)
057: */
058: public Object jjtAccept(ParserVisitor visitor, Object data) {
059: return visitor.visit(this , data);
060: }
061:
062: /**
063: * Initialization method - doesn't do much but do the object
064: * creation. We only need to do it once.
065: * @param context
066: * @param data
067: * @return The data object.
068: * @throws TemplateInitException
069: */
070: public Object init(InternalContextAdapter context, Object data)
071: throws TemplateInitException {
072: /*
073: * init the tree correctly
074: */
075:
076: super .init(context, data);
077:
078: /**
079: * Determine the size of the item and make it a Double or BigDecimal as appropriate.
080: */
081: String str = getFirstToken().image;
082: try {
083: value = new Double(str);
084:
085: } catch (NumberFormatException E1) {
086:
087: // if there's still an Exception it will propogate out
088: value = new BigDecimal(str);
089:
090: }
091:
092: return data;
093: }
094:
095: /**
096: * @see org.apache.velocity.runtime.parser.node.SimpleNode#value(org.apache.velocity.context.InternalContextAdapter)
097: */
098: public Object value(InternalContextAdapter context) {
099: return value;
100: }
101:
102: }
|