001: /* Generated By:JavaCC: Do not edit this line. SymbolParser.java */
002: package net.sourceforge.sqlunit.parsers;
003:
004: import java.io.StringReader;
005: import java.util.Map;
006: import java.util.HashMap;
007:
008: /**
009: * Contains specifications and grammar for parsing Strings and replacing
010: * embedded ant-style variables with values from the SymbolTable.
011: * @author Sujit Pal (sujit.pal@cnet.com)
012: * @version $Revision: 1.1 $
013: */
014: public class SymbolParser implements SymbolParserConstants {
015:
016: private static StringBuffer resbuf;
017: private static Map symbolMap;
018: private static boolean partialOk = false;
019:
020: /**
021: * This is the method that will be called from the client.
022: * @param str the String with embedded ant-style variables.
023: * @param syms a Map of symbols in the SymbolTable.
024: * @param partialOk if true, exception will not be thrown if
025: * variable is not found, it is assumed that this will be passed
026: * in later.
027: * @throws ParseException if there was a problem parsing the String,
028: * or if a variable could not be found in the SymbolTable.
029: * @return a String with the variables replaced by their values.
030: */
031: public static String parse(String str, Map syms, boolean partialOk)
032: throws ParseException {
033: resbuf = new StringBuffer();
034: StringReader reader = new StringReader(str);
035: SymbolParser parser = new SymbolParser(reader);
036: SymbolParser.symbolMap = syms;
037: SymbolParser.partialOk = partialOk;
038: parser.Substitute();
039: return resbuf.toString();
040: }
041:
042: final public void Substitute() throws ParseException {
043: Token t;
044: boolean inVar = false;
045: StringBuffer varbuf = null;
046: label_1: while (true) {
047: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
048: case START_VAR:
049: case CODE_CHAR:
050: case END_VAR:
051: case VAR_CHAR:
052: ;
053: break;
054: default:
055: jj_la1[0] = jj_gen;
056: break label_1;
057: }
058: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
059: case CODE_CHAR:
060: jj_consume_token(CODE_CHAR);
061: t = getToken(0);
062: resbuf.append(t.image);
063: break;
064: case START_VAR:
065: jj_consume_token(START_VAR);
066: inVar = true;
067: varbuf = new StringBuffer();
068: break;
069: case END_VAR:
070: jj_consume_token(END_VAR);
071: String var = varbuf.toString();
072: String varValue = (String) SymbolParser.symbolMap
073: .get("${" + var + "}");
074: if (varValue == null) {
075: if (SymbolParser.partialOk) {
076: varValue = "${" + var + "}";
077: } else {
078: {
079: if (true)
080: throw new ParseException(
081: "Variable ${"
082: + var
083: + "} not defined in SymbolTable");
084: }
085: }
086: }
087: resbuf.append(varValue);
088: inVar = false;
089: break;
090: case VAR_CHAR:
091: jj_consume_token(VAR_CHAR);
092: t = getToken(0);
093: varbuf.append(t.image);
094: break;
095: default:
096: jj_la1[1] = jj_gen;
097: jj_consume_token(-1);
098: throw new ParseException();
099: }
100: }
101: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
102: case 0:
103: jj_consume_token(0);
104: break;
105: default:
106: jj_la1[2] = jj_gen;
107: ;
108: }
109: }
110:
111: public SymbolParserTokenManager token_source;
112: SimpleCharStream jj_input_stream;
113: public Token token, jj_nt;
114: private int jj_ntk;
115: private int jj_gen;
116: final private int[] jj_la1 = new int[3];
117: static private int[] jj_la1_0;
118: static {
119: jj_la1_0();
120: }
121:
122: private static void jj_la1_0() {
123: jj_la1_0 = new int[] { 0x1e, 0x1e, 0x1, };
124: }
125:
126: public SymbolParser(java.io.InputStream stream) {
127: jj_input_stream = new SimpleCharStream(stream, 1, 1);
128: token_source = new SymbolParserTokenManager(jj_input_stream);
129: token = new Token();
130: jj_ntk = -1;
131: jj_gen = 0;
132: for (int i = 0; i < 3; i++)
133: jj_la1[i] = -1;
134: }
135:
136: public void ReInit(java.io.InputStream stream) {
137: jj_input_stream.ReInit(stream, 1, 1);
138: token_source.ReInit(jj_input_stream);
139: token = new Token();
140: jj_ntk = -1;
141: jj_gen = 0;
142: for (int i = 0; i < 3; i++)
143: jj_la1[i] = -1;
144: }
145:
146: public SymbolParser(java.io.Reader stream) {
147: jj_input_stream = new SimpleCharStream(stream, 1, 1);
148: token_source = new SymbolParserTokenManager(jj_input_stream);
149: token = new Token();
150: jj_ntk = -1;
151: jj_gen = 0;
152: for (int i = 0; i < 3; i++)
153: jj_la1[i] = -1;
154: }
155:
156: public void ReInit(java.io.Reader stream) {
157: jj_input_stream.ReInit(stream, 1, 1);
158: token_source.ReInit(jj_input_stream);
159: token = new Token();
160: jj_ntk = -1;
161: jj_gen = 0;
162: for (int i = 0; i < 3; i++)
163: jj_la1[i] = -1;
164: }
165:
166: public SymbolParser(SymbolParserTokenManager tm) {
167: token_source = tm;
168: token = new Token();
169: jj_ntk = -1;
170: jj_gen = 0;
171: for (int i = 0; i < 3; i++)
172: jj_la1[i] = -1;
173: }
174:
175: public void ReInit(SymbolParserTokenManager tm) {
176: token_source = tm;
177: token = new Token();
178: jj_ntk = -1;
179: jj_gen = 0;
180: for (int i = 0; i < 3; i++)
181: jj_la1[i] = -1;
182: }
183:
184: final private Token jj_consume_token(int kind)
185: throws ParseException {
186: Token oldToken;
187: if ((oldToken = token).next != null)
188: token = token.next;
189: else
190: token = token.next = token_source.getNextToken();
191: jj_ntk = -1;
192: if (token.kind == kind) {
193: jj_gen++;
194: return token;
195: }
196: token = oldToken;
197: jj_kind = kind;
198: throw generateParseException();
199: }
200:
201: final public Token getNextToken() {
202: if (token.next != null)
203: token = token.next;
204: else
205: token = token.next = token_source.getNextToken();
206: jj_ntk = -1;
207: jj_gen++;
208: return token;
209: }
210:
211: final public Token getToken(int index) {
212: Token t = token;
213: for (int i = 0; i < index; i++) {
214: if (t.next != null)
215: t = t.next;
216: else
217: t = t.next = token_source.getNextToken();
218: }
219: return t;
220: }
221:
222: final private int jj_ntk() {
223: if ((jj_nt = token.next) == null)
224: return (jj_ntk = (token.next = token_source.getNextToken()).kind);
225: else
226: return (jj_ntk = jj_nt.kind);
227: }
228:
229: private java.util.Vector jj_expentries = new java.util.Vector();
230: private int[] jj_expentry;
231: private int jj_kind = -1;
232:
233: public ParseException generateParseException() {
234: jj_expentries.removeAllElements();
235: boolean[] la1tokens = new boolean[5];
236: for (int i = 0; i < 5; i++) {
237: la1tokens[i] = false;
238: }
239: if (jj_kind >= 0) {
240: la1tokens[jj_kind] = true;
241: jj_kind = -1;
242: }
243: for (int i = 0; i < 3; i++) {
244: if (jj_la1[i] == jj_gen) {
245: for (int j = 0; j < 32; j++) {
246: if ((jj_la1_0[i] & (1 << j)) != 0) {
247: la1tokens[j] = true;
248: }
249: }
250: }
251: }
252: for (int i = 0; i < 5; i++) {
253: if (la1tokens[i]) {
254: jj_expentry = new int[1];
255: jj_expentry[0] = i;
256: jj_expentries.addElement(jj_expentry);
257: }
258: }
259: int[][] exptokseq = new int[jj_expentries.size()][];
260: for (int i = 0; i < jj_expentries.size(); i++) {
261: exptokseq[i] = (int[]) jj_expentries.elementAt(i);
262: }
263: return new ParseException(token, exptokseq, tokenImage);
264: }
265:
266: final public void enable_tracing() {
267: }
268:
269: final public void disable_tracing() {
270: }
271:
272: }
|