001: package org.ofbiz.rules.parse.tokens;
002:
003: import org.ofbiz.rules.parse.*;
004:
005: /**
006: * <p><b>Title:</b> Token Assembly
007: * <p><b>Description:</b> None
008: * <p>Copyright (c) 1999 Steven J. Metsker.
009: * <p>Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
010: *
011: * <p>Permission is hereby granted, free of charge, to any person obtaining a
012: * copy of this software and associated documentation files (the "Software"),
013: * to deal in the Software without restriction, including without limitation
014: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
015: * and/or sell copies of the Software, and to permit persons to whom the
016: * Software is furnished to do so, subject to the following conditions:
017: *
018: * <p>The above copyright notice and this permission notice shall be included
019: * in all copies or substantial portions of the Software.
020: *
021: * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
022: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
023: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
024: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
025: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
026: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
027: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
028: *
029: * <br>
030: * A TokenAssembly is an Assembly whose elements are Tokens.
031: * Tokens are, roughly, the chunks of text that a <code>
032: * Tokenizer</code> returns.
033: *
034: * @author Steven J. Metsker
035: * @version 1.0
036: */
037: public class TokenAssembly extends Assembly {
038:
039: /**
040: * the "string" of tokens this assembly will consume
041: */
042: protected TokenString tokenString;
043:
044: /**
045: * Constructs a TokenAssembly on a TokenString constructed
046: * from the given String.
047: *
048: * @param string the string to consume
049: *
050: * @return a TokenAssembly that will consume a tokenized
051: * version of the supplied String
052: */
053: public TokenAssembly(String s) {
054: this (new TokenString(s));
055: }
056:
057: /**
058: * Constructs a TokenAssembly on a TokenString constructed
059: * from the given Tokenizer.
060: *
061: * @param Tokenizer the tokenizer to consume tokens
062: * from
063: *
064: * @return a TokenAssembly that will consume a tokenized
065: * version of the supplied Tokenizer
066: */
067: public TokenAssembly(Tokenizer t) {
068: this (new TokenString(t));
069: }
070:
071: /**
072: * Constructs a TokenAssembly from the given TokenString.
073: *
074: * @param tokenString the tokenString to consume
075: *
076: * @return a TokenAssembly that will consume the supplied
077: * TokenString
078: */
079: public TokenAssembly(TokenString tokenString) {
080: this .tokenString = tokenString;
081: }
082:
083: /**
084: * Returns a textual representation of the amount of this
085: * tokenAssembly that has been consumed.
086: *
087: * @param delimiter the mark to show between consumed
088: * elements
089: *
090: * @return a textual description of the amount of this
091: * assembly that has been consumed
092: */
093: public String consumed(String delimiter) {
094: StringBuffer buf = new StringBuffer();
095:
096: for (int i = 0; i < elementsConsumed(); i++) {
097: if (i > 0) {
098: buf.append(delimiter);
099: }
100: buf.append(tokenString.tokenAt(i));
101: }
102: return buf.toString();
103: }
104:
105: /**
106: * Returns the default string to show between elements
107: * consumed or remaining.
108: *
109: * @return the default string to show between elements
110: * consumed or remaining
111: */
112: public String defaultDelimiter() {
113: return "/";
114: }
115:
116: /**
117: * Returns the number of elements in this assembly.
118: *
119: * @return the number of elements in this assembly
120: */
121: public int length() {
122: return tokenString.length();
123: }
124:
125: /**
126: * Returns the next token.
127: *
128: * @return the next token from the associated token string.
129: *
130: * @exception ArrayIndexOutOfBoundsException if there are no
131: * more tokens in this tokenizer's string.
132: */
133: public Object nextElement() {
134: return tokenString.tokenAt(index++);
135: }
136:
137: /**
138: * Shows the next object in the assembly, without removing it
139: *
140: * @return the next object
141: *
142: */
143: public Object peek() {
144: if (index < length()) {
145: return tokenString.tokenAt(index);
146: } else {
147: return null;
148: }
149: }
150:
151: /**
152: * Returns a textual representation of the amount of this
153: * tokenAssembly that remains to be consumed.
154: *
155: * @param delimiter the mark to show between consumed
156: * elements
157: *
158: * @return a textual description of the amount of this
159: * assembly that remains to be consumed
160: */
161: public String remainder(String delimiter) {
162: StringBuffer buf = new StringBuffer();
163:
164: for (int i = elementsConsumed(); i < tokenString.length(); i++) {
165:
166: if (i > elementsConsumed()) {
167: buf.append(delimiter);
168: }
169: buf.append(tokenString.tokenAt(i));
170: }
171: return buf.toString();
172: }
173: }
|