001: package org.ofbiz.rules.parse.tokens;
002:
003: import java.io.*;
004: import java.util.*;
005:
006: /**
007: * <p><b>Title:</b> Token String
008: * <p><b>Description:</b> None
009: * <p>Copyright (c) 1999 Steven J. Metsker.
010: * <p>Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
011: *
012: * <p>Permission is hereby granted, free of charge, to any person obtaining a
013: * copy of this software and associated documentation files (the "Software"),
014: * to deal in the Software without restriction, including without limitation
015: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
016: * and/or sell copies of the Software, and to permit persons to whom the
017: * Software is furnished to do so, subject to the following conditions:
018: *
019: * <p>The above copyright notice and this permission notice shall be included
020: * in all copies or substantial portions of the Software.
021: *
022: * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
023: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
024: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
025: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
026: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
027: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
028: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
029: *
030: * <br>
031: * A TokenString is like a String, but it is a series of
032: * Tokens rather than a series of chars. Once a TokenString is
033: * created, it is "immutable", meaning it cannot change. This
034: * lets you freely copy TokenStrings without worrying about
035: * their state.
036: *
037: * @author Steven J. Metsker
038: * @version 1.0
039: */
040: public class TokenString {
041:
042: /**
043: * the tokens in this tokenString
044: */
045: protected Token tokens[];
046:
047: /**
048: * Constructs a tokenString from the supplied tokens.
049: *
050: * @param tokens the tokens to use
051: *
052: * @return a tokenString constructed from the supplied
053: * tokens
054: */
055: public TokenString(Token[] tokens) {
056: this .tokens = tokens;
057: }
058:
059: /**
060: * Constructs a tokenString from the supplied string.
061: *
062: * @param string the string to tokenize
063: *
064: * @return a tokenString constructed from tokens read from
065: * the supplied string
066: */
067: public TokenString(String s) {
068: this (new Tokenizer(s));
069: }
070:
071: /**
072: * Constructs a tokenString from the supplied reader and
073: * tokenizer.
074: *
075: * @param Tokenizer the tokenizer that will produces the
076: * tokens
077: *
078: * @return a tokenString constructed from the tokenizer's
079: * tokens
080: */
081: public TokenString(Tokenizer t) {
082: List v = new ArrayList();
083:
084: try {
085: while (true) {
086: Token tok = t.nextToken();
087:
088: if (tok.ttype() == Token.TT_EOF) {
089: break;
090: }
091: v.add(tok);
092: }
093: ;
094: } catch (IOException e) {
095: throw new InternalError("Problem tokenizing string: " + e);
096: }
097: tokens = (Token[]) v.toArray(new Token[v.size()]);
098: }
099:
100: /**
101: * Returns the number of tokens in this tokenString.
102: *
103: * @return the number of tokens in this tokenString
104: */
105: public int length() {
106: return tokens.length;
107: }
108:
109: /**
110: * Returns the token at the specified index.
111: *
112: * @param index the index of the desired token
113: *
114: * @return token the token at the specified index
115: */
116: public Token tokenAt(int i) {
117: return tokens[i];
118: }
119:
120: /**
121: * Returns a string representation of this tokenString.
122: *
123: * @return a string representation of this tokenString
124: */
125: public String toString() {
126: StringBuffer buf = new StringBuffer();
127:
128: for (int i = 0; i < tokens.length; i++) {
129: if (i > 0) {
130: buf.append(" ");
131: }
132: buf.append(tokens[i]);
133: }
134: return buf.toString();
135: }
136: }
|