Source code: com/tripi/asp/NumberNode.java
1 /**
2 * ArrowHead ASP Server
3 * This is a source file for the ArrowHead ASP Server - an 100% Java
4 * VBScript interpreter and ASP server.
5 *
6 * For more information, see http://www.tripi.com/arrowhead
7 *
8 * Copyright (C) 2002 Terence Haddock
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 */
25 package com.tripi.asp;
26
27 /**
28 * NumberNode class represents a number parsed from the ASP source file.
29 * Usually contains either the Integer class or the Double class.
30 *
31 * @author Terence Haddock
32 * @version 0.9
33 */
34 public class NumberNode extends DefaultNode
35 {
36 /** Number this node contains */
37 Object number;
38
39 /**
40 * Constructor.
41 * @param number Number this node should contain.
42 */
43 public NumberNode(Object number)
44 {
45 this.number = number;
46 }
47
48 /**
49 * Get the number this node contains.
50 * @return number
51 */
52 public Object getNumber()
53 {
54 return number;
55 }
56
57 /**
58 * Dumps this node's string representation.
59 * @see DefaultNode#dump
60 */
61 public void dump()
62 {
63 System.out.print(number);
64 }
65
66 /**
67 * Executes this node within the specified context. This class evaluates
68 * to the number this class contains.
69 * @param context AspContext under which to execute this node.
70 * @return Number this node contains.
71 * @see Node#execute(AspContext)
72 */
73 public Object execute(AspContext context)
74 {
75 return number;
76 }
77
78 /**
79 * Create a number node from a decimal string.
80 * @param str String to create number node from.
81 */
82 public static NumberNode fromDoubleToken(String str)
83 {
84 /* See if it should be an integer or float */
85 if (str.indexOf(".") == -1)
86 {
87 /* Should be an integer */
88 long longValue = Long.parseLong(str);
89 if (longValue < Integer.MAX_VALUE)
90 {
91 return new NumberNode(new Integer((int)longValue));
92 } else {
93 return new NumberNode(new Long(longValue));
94 }
95 } else {
96 /* Should be a floating point */
97 return new NumberNode(new Double(str));
98 }
99 }
100
101 /**
102 * Create a number node from a hex string.
103 * @param str Hex string to create number node from.
104 */
105 public static NumberNode fromHexToken(String str)
106 {
107 if (!str.startsWith("&H"))
108 {
109 throw new AspRuntimeException("Invalid hex string: " + str);
110 }
111 long longValue = Long.parseLong(str.substring(2), 16);
112 if (longValue < Integer.MAX_VALUE)
113 return new NumberNode(new Integer((int)longValue));
114 else
115 return new NumberNode(new Long(longValue));
116 }
117 };
118