001: package org.ofbiz.rules.parse.tokens;
002:
003: /**
004: * <p><b>Title:</b> Token
005: * <p><b>Description:</b> None
006: * <p>Copyright (c) 1999 Steven J. Metsker.
007: * <p>Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
008: *
009: * <p>Permission is hereby granted, free of charge, to any person obtaining a
010: * copy of this software and associated documentation files (the "Software"),
011: * to deal in the Software without restriction, including without limitation
012: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
013: * and/or sell copies of the Software, and to permit persons to whom the
014: * Software is furnished to do so, subject to the following conditions:
015: *
016: * <p>The above copyright notice and this permission notice shall be included
017: * in all copies or substantial portions of the Software.
018: *
019: * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
020: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
021: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
022: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
023: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
024: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
025: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
026: *
027: * <br>
028: * A token represents a logical chunk of a string. For
029: * example, a typical tokenizer would break the string
030: * <code>"1.23 <= 12.3"</code> into three tokens: the number
031: * 1.23, a less-than-or-equal symbol, and the number 12.3. A
032: * token is a receptacle, and relies on a tokenizer to decide
033: * precisely how to divide a string into tokens.
034: *
035: * @author Steven J. Metsker
036: * @version 1.0
037: */
038: public class Token {
039: protected TokenType ttype;
040: protected String sval;
041: protected double nval;
042:
043: /**
044: * A constant indicating that the end of the stream has
045: * been read.
046: */
047: public static final TokenType TT_EOF = new TokenType("eof");
048:
049: /**
050: * A constant indicating that there are no more tokens
051: */
052: public static final Token EOF = new Token(TT_EOF, "", 0);
053:
054: /**
055: * A constant indicating that a token is a number,
056: * like 3.14
057: */
058: public static final TokenType TT_NUMBER = new TokenType("number");
059:
060: /**
061: * A constant indicating a token is a word, like "cat"
062: */
063: public static final TokenType TT_WORD = new TokenType("word");
064:
065: /**
066: * A constant indicating that a token is a symbol
067: * like "<=".
068: */
069: public static final TokenType TT_SYMBOL = new TokenType("symbol");
070:
071: /**
072: * A constant indicating that a token is a quoted string,
073: * like "Launch Mi".
074: */
075: public static final TokenType TT_QUOTED = new TokenType("quoted");
076:
077: /**
078: * Constructs a token from the given char.
079: *
080: * @param char the char
081: *
082: * @return a token constructed from the given char
083: */
084: public Token(char c) {
085: this (TT_SYMBOL, new String(new char[] { c }), 0);
086: }
087:
088: /**
089: * Constructs a token from the given number.
090: *
091: * @param double the number
092: *
093: * @return a token constructed from the given number
094: */
095: public Token(double nval) {
096: this (TT_NUMBER, "", nval);
097: }
098:
099: /**
100: * Constructs a token from the given string.
101: *
102: * @param String the string
103: *
104: * @return a token constructed from the given string
105: */
106: public Token(String sval) {
107: this (TT_WORD, sval, 0);
108: }
109:
110: /**
111: * Constructs a token of the indicated type and associated
112: * string or numeric values.
113: *
114: * @param TokenType the type of the token, typically one
115: * of the constants this class defines
116: *
117: * @param string the string value of the token, typically
118: * null except for WORD and QUOTED tokens
119: *
120: * @param double the numeric value of the token, typically
121: * 0 except for NUMBER tokens
122: *
123: * @return a token
124: */
125: public Token(TokenType ttype, String sval, double nval) {
126: this .ttype = ttype;
127: this .sval = sval;
128: this .nval = nval;
129: }
130:
131: /**
132: * Returns true if the supplied object is an equivalent token.
133: *
134: * @param object the object to compare
135: *
136: * @return true, if the supplied object is of the same type
137: * and value
138: */
139: public boolean equals(Object o) {
140: if (!(o instanceof Token))
141: return false;
142: Token t = (Token) o;
143:
144: if (ttype != t.ttype) {
145: return false;
146: }
147: if (ttype == TT_NUMBER) {
148: return nval == t.nval;
149: }
150: if (sval == null || t.sval == null) {
151: return false;
152: }
153: return sval.equals(t.sval);
154: }
155:
156: /**
157: * Returns true if the supplied object is an equivalent token,
158: * given mellowness about case in strings and characters.
159: *
160: * @param object the object to compare
161: *
162: * @return true, if the supplied object is of the same type
163: * and value. This method disregards case when
164: * comparing the string value of tokens.
165: */
166: public boolean equalsIgnoreCase(Object o) {
167: if (!(o instanceof Token))
168: return false;
169: Token t = (Token) o;
170:
171: if (ttype != t.ttype) {
172: return false;
173: }
174: if (ttype == TT_NUMBER) {
175: return nval == t.nval;
176: }
177: if (sval == null || t.sval == null) {
178: return false;
179: }
180: return sval.equalsIgnoreCase(t.sval);
181: }
182:
183: /**
184: * Returns true if this token is a number.
185: *
186: * @return true, if this token is a number
187: */
188: public boolean isNumber() {
189: return ttype == TT_NUMBER;
190: }
191:
192: /**
193: * Returns true if this token is a quoted string.
194: *
195: * @return true, if this token is a quoted string
196: */
197: public boolean isQuotedString() {
198: return ttype == TT_QUOTED;
199: }
200:
201: /**
202: * Returns true if this token is a symbol.
203: *
204: * @return true, if this token is a symbol
205: */
206: public boolean isSymbol() {
207: return ttype == TT_SYMBOL;
208: }
209:
210: /**
211: * Returns true if this token is a word.
212: *
213: * @return true, if this token is a word.
214: */
215: public boolean isWord() {
216: return ttype == TT_WORD;
217: }
218:
219: /**
220: * Returns the numeric value of this token.
221: *
222: * @return the numeric value of this token
223: */
224: public double nval() {
225: return nval;
226: }
227:
228: /**
229: * Returns the string value of this token.
230: *
231: * @return the string value of this token
232: */
233: public String sval() {
234: return sval;
235: }
236:
237: /**
238: * Return a textual description of this object.
239: *
240: * @return a textual description of this object
241: */
242: public String toString() {
243: if (ttype == TT_EOF) {
244: return "EOF";
245: }
246: return value().toString();
247: }
248:
249: /**
250: * Returns the type of this token.
251: *
252: * @return the type of this token, typically one of the
253: * constants this class defines
254: */
255: public TokenType ttype() {
256: return ttype;
257: }
258:
259: /**
260: * Returns an object that represents the value of this token.
261: *
262: * @return an object that represents the value of this token
263: */
264: public Object value() {
265: if (ttype == TT_NUMBER) {
266: return new Double(nval);
267: }
268: if (ttype == TT_EOF) {
269: return EOF;
270: }
271: if (sval != null) {
272: return sval;
273: }
274: return ttype;
275: }
276: }
|