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 org.apache.velocity.context.InternalContextAdapter;
023: import org.apache.velocity.exception.TemplateInitException;
024: import org.apache.velocity.runtime.parser.Parser;
025: import org.apache.velocity.runtime.parser.ParserVisitor;
026:
027: import java.math.BigInteger;
028:
029: /**
030: * Handles integer numbers. The value will be either an Integer, a Long, or a BigInteger.
031: *
032: * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
033: */
034: public class ASTIntegerLiteral extends SimpleNode {
035:
036: // This may be of type Integer, Long or BigInteger
037: private Number value = null;
038:
039: /**
040: * @param id
041: */
042: public ASTIntegerLiteral(int id) {
043: super (id);
044: }
045:
046: /**
047: * @param p
048: * @param id
049: */
050: public ASTIntegerLiteral(Parser p, int id) {
051: super (p, id);
052: }
053:
054: /**
055: * @see org.apache.velocity.runtime.parser.node.SimpleNode#jjtAccept(org.apache.velocity.runtime.parser.ParserVisitor, java.lang.Object)
056: */
057: public Object jjtAccept(ParserVisitor visitor, Object data) {
058: return visitor.visit(this , data);
059: }
060:
061: /**
062: * @see org.apache.velocity.runtime.parser.node.SimpleNode#init(org.apache.velocity.context.InternalContextAdapter, java.lang.Object)
063: */
064: public Object init(InternalContextAdapter context, Object data)
065: throws TemplateInitException {
066: /*
067: * init the tree correctly
068: */
069:
070: super .init(context, data);
071:
072: /**
073: * Determine the size of the item and make it an Integer, Long, or BigInteger as appropriate.
074: */
075: String str = getFirstToken().image;
076: try {
077: value = new Integer(str);
078: } catch (NumberFormatException E1) {
079: try {
080:
081: value = new Long(str);
082:
083: } catch (NumberFormatException E2) {
084:
085: // if there's still an Exception it will propogate out
086: value = new BigInteger(str);
087: }
088: }
089:
090: return data;
091: }
092:
093: /**
094: * @see org.apache.velocity.runtime.parser.node.SimpleNode#value(org.apache.velocity.context.InternalContextAdapter)
095: */
096: public Object value(InternalContextAdapter context) {
097: return value;
098: }
099:
100: }
|