01: package org.ofbiz.rules.parse.tokens;
02:
03: import java.io.*;
04:
05: /**
06: * <p><b>Title:</b> Quote State
07: * <p><b>Description:</b> None
08: * <p>Copyright (c) 1999 Steven J. Metsker.
09: * <p>Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
10: *
11: * <p>Permission is hereby granted, free of charge, to any person obtaining a
12: * copy of this software and associated documentation files (the "Software"),
13: * to deal in the Software without restriction, including without limitation
14: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15: * and/or sell copies of the Software, and to permit persons to whom the
16: * Software is furnished to do so, subject to the following conditions:
17: *
18: * <p>The above copyright notice and this permission notice shall be included
19: * in all copies or substantial portions of the Software.
20: *
21: * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
25: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
26: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
27: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28: *
29: * <br>
30: * A quoteState returns a quoted string token from a reader.
31: * This state will collect characters until it sees a match
32: * to the character that the tokenizer used to switch to
33: * this state. For example, if a tokenizer uses a double-
34: * quote character to enter this state, then <code>
35: * nextToken()</code> will search for another double-quote
36: * until it finds one or finds the end of the reader.
37: *
38: * @author Steven J. Metsker
39: * @version 1.0
40: */
41: public class QuoteState extends TokenizerState {
42: protected char charbuf[] = new char[16];
43:
44: /**
45: * Fatten up charbuf as necessary.
46: */
47: protected void checkBufLength(int i) {
48: if (i >= charbuf.length) {
49: char nb[] = new char[charbuf.length * 2];
50:
51: System.arraycopy(charbuf, 0, nb, 0, charbuf.length);
52: charbuf = nb;
53: }
54: }
55:
56: /**
57: * Return a quoted string token from a reader. This method
58: * will collect characters until it sees a match to the
59: * character that the tokenizer used to switch to this
60: * state.
61: *
62: * @return a quoted string token from a reader
63: */
64: public Token nextToken(PushbackReader r, int cin, Tokenizer t)
65: throws IOException {
66:
67: int i = 0;
68:
69: charbuf[i++] = (char) cin;
70: int c;
71:
72: do {
73: c = r.read();
74: if (c < 0) {
75: c = cin;
76: }
77: checkBufLength(i);
78: charbuf[i++] = (char) c;
79: } while (c != cin);
80:
81: String sval = String.copyValueOf(charbuf, 0, i);
82:
83: return new Token(Token.TT_QUOTED, sval, 0);
84: }
85: }
|