1
2 package com.gridsystems.beanfilter;
3
4 import java.io.*;
5
6 class FilterParser implements FilterParserConstants {
7
8 private Class dataClass;
9
10 public FilterParser(String src, Class dataClass) {
11 this(new StringBufferInputStream(src));
12 this.dataClass = dataClass;
13 }
14
15 final public EvalNode compile() throws ParseException, EvalException {
16 EvalNode node = null;
17 node = subexpr(this.dataClass);
18 jj_consume_token(0);
19 {if (true) return node;}
20 throw new Error("Missing return statement in function");
21 }
22
23 final public EvalNode subexpr(Class parentClass) throws ParseException, EvalException {
24 EvalValue val = null;
25 val = value(parentClass);
26 {if (true) return new BooleanNode(val);}
27 throw new Error("Missing return statement in function");
28 }
29
30 final public EvalValue value(Class parentClass) throws ParseException, EvalException {
31 EvalValue lval = null, rval = null;
32 int op = -1;
33 lval = preInternalValue(parentClass);
34 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
35 case AND:
36 case OR:
37 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
38 case AND:
39 jj_consume_token(AND);
40 break;
41 case OR:
42 jj_consume_token(OR);
43 break;
44 default:
45 jj_la1[0] = jj_gen;
46 jj_consume_token(-1);
47 throw new ParseException();
48 }
49 op = token.kind;
50 rval = value(parentClass);
51 break;
52 default:
53 jj_la1[1] = jj_gen;
54 ;
55 }
56 if (op == AND) {
57 {if (true) return new AndNode(token, lval, rval);}
58 }
59 if (op == OR) {
60 {if (true) return new OrNode(token, lval, rval);}
61 }
62 {if (true) return lval;}
63 throw new Error("Missing return statement in function");
64 }
65
66 final public EvalValue preInternalValue(Class parentClass) throws ParseException, EvalException {
67 EvalValue lval = null, rval = null;
68 int op = -1;
69 lval = internalValue(parentClass);
70 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
71 case MATCHES:
72 case LIKE:
73 case CONTAINS:
74 case IN:
75 case EQ:
76 case NE:
77 case GT:
78 case LT:
79 case LE:
80 case GE:
81 op = operator();
82 rval = preInternalValue(parentClass);
83 break;
84 default:
85 jj_la1[2] = jj_gen;
86 ;
87 }
88 switch(op) {
89 case -1:
90 {if (true) return lval;}
91 case CONTAINS:
92 case IN:
93 {if (true) return new CollectionNode(lval, op, rval);}
94 default:
95 if (lval instanceof CollectionValue || rval instanceof CollectionValue) {
96 {if (true) return new CollectionNode(lval, op, rval);}
97 } else {
98 {if (true) return new CompareNode(lval, op, rval, parentClass);}
99 }
100 }
101 throw new Error("Missing return statement in function");
102 }
103
104 final public EvalValue internalValue(Class parentClass) throws ParseException, EvalException {
105 EvalValue f;
106 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
107 case LPAREN:
108 jj_consume_token(LPAREN);
109 f = value(parentClass);
110 jj_consume_token(RPAREN);
111 {if (true) return new ParenthesisValue(token, f);}
112 break;
113 case TRUE:
114 case FALSE:
115 case NULL:
116 case NUMBER:
117 case STRING:
118 f = constValue();
119 {if (true) return f;}
120 break;
121 case NOT:
122 case SIZEOF:
123 case SELECT:
124 case THIS:
125 case IDENTIFIER:
126 f = varValue(parentClass);
127 {if (true) return f;}
128 break;
129 case LBRACKET:
130 jj_consume_token(LBRACKET);
131 CollectionValue col = new CollectionValue(token);
132 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
133 case NOT:
134 case TRUE:
135 case FALSE:
136 case NULL:
137 case SIZEOF:
138 case SELECT:
139 case THIS:
140 case NUMBER:
141 case STRING:
142 case IDENTIFIER:
143 case LPAREN:
144 list(col, parentClass);
145 break;
146 default:
147 jj_la1[3] = jj_gen;
148 ;
149 }
150 jj_consume_token(RBRACKET);
151 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
152 case ALLITEMS:
153 case ANYITEMS:
154 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
155 case ALLITEMS:
156 jj_consume_token(ALLITEMS);
157 col.setTypeList(ALLITEMS);
158 break;
159 case ANYITEMS:
160 jj_consume_token(ANYITEMS);
161 col.setTypeList(ANYITEMS);
162 break;
163 default:
164 jj_la1[4] = jj_gen;
165 jj_consume_token(-1);
166 throw new ParseException();
167 }
168 break;
169 default:
170 jj_la1[5] = jj_gen;
171 ;
172 }
173 {if (true) return col;}
174 break;
175 default:
176 jj_la1[6] = jj_gen;
177 jj_consume_token(-1);
178 throw new ParseException();
179 }
180 throw new Error("Missing return statement in function");
181 }
182
183 final public EvalValue nolistvalue(Class parentClass) throws ParseException, EvalException {
184 EvalValue f;
185 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
186 case LPAREN:
187 jj_consume_token(LPAREN);
188 f = value(parentClass);
189 jj_consume_token(RPAREN);
190 {if (true) return new ParenthesisValue(token, f);}
191 break;
192 case TRUE:
193 case FALSE:
194 case NULL:
195 case NUMBER:
196 case STRING:
197 f = constValue();
198 {if (true) return f;}
199 break;
200 case NOT:
201 case SIZEOF:
202 case SELECT:
203 case THIS:
204 case IDENTIFIER:
205 f = varValue(parentClass);
206 {if (true) return f;}
207 break;
208 default:
209 jj_la1[7] = jj_gen;
210 jj_consume_token(-1);
211 throw new ParseException();
212 }
213 throw new Error("Missing return statement in function");
214 }
215
216 final public EvalValue constValue() throws ParseException {
217 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
218 case STRING:
219 jj_consume_token(STRING);
220 {if (true) return new StringValue(token, token.image);}
221 break;
222 case TRUE:
223 jj_consume_token(TRUE);
224 {if (true) return new BooleanValue(token, true);}
225 break;
226 case FALSE:
227 jj_consume_token(FALSE);
228 {if (true) return new BooleanValue(token, false);}
229 break;
230 case NUMBER:
231 jj_consume_token(NUMBER);
232 {if (true) return new NumValue(token, token.image);}
233 break;
234 case NULL:
235 jj_consume_token(NULL);
236 {if (true) return new NullValue(token);}
237 break;
238 default:
239 jj_la1[8] = jj_gen;
240 jj_consume_token(-1);
241 throw new ParseException();
242 }
243 throw new Error("Missing return statement in function");
244 }
245
246 final public EvalValue varValue(Class parentClass) throws ParseException, EvalException {
247 EvalValue base,field = null;
248 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
249 case NOT:
250 jj_consume_token(NOT);
251 base = value(parentClass);
252 {if (true) return new NotNode(base);}
253 break;
254 case SIZEOF:
255 jj_consume_token(SIZEOF);
256 jj_consume_token(LPAREN);
257 base = value(parentClass);
258 jj_consume_token(RPAREN);
259 {if (true) return new SizeOfValue(token, base);}
260 break;
261 case SELECT:
262
263 base = selectwhere(parentClass);
264 {if (true) return base;}
265 break;
266 case THIS:
267 case IDENTIFIER:
268
269 base = arrayOrVariable(parentClass);
270 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
271 case DOT:
272 jj_consume_token(DOT);
273 field = varValue(base.getClassValue(parentClass));
274 break;
275 default:
276 jj_la1[9] = jj_gen;
277 ;
278 }
279 if(field == null) {
280 {if (true) return base;}
281 } else {
282 {if (true) return new FieldValue(token, base, field);}
283 }
284 break;
285 default:
286 jj_la1[10] = jj_gen;
287 jj_consume_token(-1);
288 throw new ParseException();
289 }
290 throw new Error("Missing return statement in function");
291 }
292
293 final public SelectWhereValue selectwhere(Class parentClass) throws ParseException, EvalException {
294 EvalValue selectVal; EvalNode evalWhere; Token whereTok; Token selectTok;
295 Class originalDataClass = this.dataClass;
296 jj_consume_token(SELECT);
297 selectTok=token;
298 selectVal = value(parentClass);
299 jj_consume_token(WHERE);
300 whereTok=token;
301 Class selectClass = selectVal.getClassValue(parentClass);
302 this.dataClass = selectClass;
303 evalWhere = subexpr(selectClass);
304 this.dataClass = originalDataClass;
305 {if (true) return new SelectWhereValue(selectTok, selectVal, whereTok , evalWhere, parentClass);}
306 throw new Error("Missing return statement in function");
307 }
308
309 final public EvalValue arrayOrVariable(Class parentClass) throws ParseException, EvalException {
310 EvalValue value;
311 Token arrayType;
312 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
313 case IDENTIFIER:
314 jj_consume_token(IDENTIFIER);
315 break;
316 case THIS:
317 jj_consume_token(THIS);
318 break;
319 default:
320 jj_la1[11] = jj_gen;
321 jj_consume_token(-1);
322 throw new ParseException();
323 }
324 if(token.kind == FilterParserConstants.THIS) {
325 value = new ThisValue(token, this.dataClass);
326 } else {
327 value = new VariableValue(token, token.image, parentClass);
328 }
329 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
330 case LBRACKET:
331 jj_consume_token(LBRACKET);
332 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
333 case NUMBER:
334 jj_consume_token(NUMBER);
335 break;
336 case ALLITEMS:
337 jj_consume_token(ALLITEMS);
338 break;
339 case ANYITEMS:
340 jj_consume_token(ANYITEMS);
341 break;
342 default:
343 jj_la1[12] = jj_gen;
344 jj_consume_token(-1);
345 throw new ParseException();
346 }
347 arrayType = token;
348 jj_consume_token(RBRACKET);
349 ArrayValue arrayValue = new ArrayValue(token, value, arrayType, parentClass);
350 {if (true) return arrayValue;}
351 break;
352 default:
353 jj_la1[13] = jj_gen;
354 ;
355 }
356 {if (true) return value;}
357 throw new Error("Missing return statement in function");
358 }
359
360 final public void list(CollectionValue col, Class parentClass) throws ParseException, EvalException {
361 EvalValue value;
362 value = nolistvalue(parentClass);
363 col.add(value, parentClass);
364 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
365 case COMMA:
366 jj_consume_token(COMMA);
367 list(col, parentClass);
368 break;
369 default:
370 jj_la1[14] = jj_gen;
371 ;
372 }
373 }
374
375 final public int operator() throws ParseException {
376 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
377 case EQ:
378 jj_consume_token(EQ);
379 break;
380 case NE:
381 jj_consume_token(NE);
382 break;
383 case LT:
384 jj_consume_token(LT);
385 break;
386 case LE:
387 jj_consume_token(LE);
388 break;
389 case GT:
390 jj_consume_token(GT);
391 break;
392 case GE:
393 jj_consume_token(GE);
394 break;
395 case MATCHES:
396 jj_consume_token(MATCHES);
397 break;
398 case LIKE:
399 jj_consume_token(LIKE);
400 break;
401 case CONTAINS:
402 jj_consume_token(CONTAINS);
403 break;
404 case IN:
405 jj_consume_token(IN);
406 break;
407 default:
408 jj_la1[15] = jj_gen;
409 jj_consume_token(-1);
410 throw new ParseException();
411 }
412 {if (true) return token.kind;}
413 throw new Error("Missing return statement in function");
414 }
415
416 public FilterParserTokenManager token_source;
417 SimpleCharStream jj_input_stream;
418 public Token token, jj_nt;
419 private int jj_ntk;
420 private int jj_gen;
421 final private int[] jj_la1 = new int[16];
422 static private int[] jj_la1_0;
423 static private int[] jj_la1_1;
424 static {
425 jj_la1_0();
426 jj_la1_1();
427 }
428 private static void jj_la1_0() {
429 jj_la1_0 = new int[] {0xc0,0xc0,0x1e00,0x62d36100,0xc0000,0xc0000,0x62d36100,0x62d36100,0x2816000,0x8000,0x20520100,0x20400000,0x8c0000,0x0,0x0,0x1e00,};
430 }
431 private static void jj_la1_1() {
432 jj_la1_1 = new int[] {0x0,0x0,0x1f8,0x0,0x0,0x0,0x1,0x0,0x0,0x0,0x0,0x0,0x0,0x1,0x4,0x1f8,};
433 }
434
435 public FilterParser(java.io.InputStream stream) {
436 this(stream, null);
437 }
438 public FilterParser(java.io.InputStream stream, String encoding) {
439 try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
440 token_source = new FilterParserTokenManager(jj_input_stream);
441 token = new Token();
442 jj_ntk = -1;
443 jj_gen = 0;
444 for (int i = 0; i < 16; i++) jj_la1[i] = -1;
445 }
446
447 public void ReInit(java.io.InputStream stream) {
448 ReInit(stream, null);
449 }
450 public void ReInit(java.io.InputStream stream, String encoding) {
451 try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
452 token_source.ReInit(jj_input_stream);
453 token = new Token();
454 jj_ntk = -1;
455 jj_gen = 0;
456 for (int i = 0; i < 16; i++) jj_la1[i] = -1;
457 }
458
459 public FilterParser(java.io.Reader stream) {
460 jj_input_stream = new SimpleCharStream(stream, 1, 1);
461 token_source = new FilterParserTokenManager(jj_input_stream);
462 token = new Token();
463 jj_ntk = -1;
464 jj_gen = 0;
465 for (int i = 0; i < 16; i++) jj_la1[i] = -1;
466 }
467
468 public void ReInit(java.io.Reader stream) {
469 jj_input_stream.ReInit(stream, 1, 1);
470 token_source.ReInit(jj_input_stream);
471 token = new Token();
472 jj_ntk = -1;
473 jj_gen = 0;
474 for (int i = 0; i < 16; i++) jj_la1[i] = -1;
475 }
476
477 public FilterParser(FilterParserTokenManager tm) {
478 token_source = tm;
479 token = new Token();
480 jj_ntk = -1;
481 jj_gen = 0;
482 for (int i = 0; i < 16; i++) jj_la1[i] = -1;
483 }
484
485 public void ReInit(FilterParserTokenManager tm) {
486 token_source = tm;
487 token = new Token();
488 jj_ntk = -1;
489 jj_gen = 0;
490 for (int i = 0; i < 16; i++) jj_la1[i] = -1;
491 }
492
493 final private Token jj_consume_token(int kind) throws ParseException {
494 Token oldToken;
495 if ((oldToken = token).next != null) token = token.next;
496 else token = token.next = token_source.getNextToken();
497 jj_ntk = -1;
498 if (token.kind == kind) {
499 jj_gen++;
500 return token;
501 }
502 token = oldToken;
503 jj_kind = kind;
504 throw generateParseException();
505 }
506
507 final public Token getNextToken() {
508 if (token.next != null) token = token.next;
509 else token = token.next = token_source.getNextToken();
510 jj_ntk = -1;
511 jj_gen++;
512 return token;
513 }
514
515 final public Token getToken(int index) {
516 Token t = token;
517 for (int i = 0; i < index; i++) {
518 if (t.next != null) t = t.next;
519 else t = t.next = token_source.getNextToken();
520 }
521 return t;
522 }
523
524 final private int jj_ntk() {
525 if ((jj_nt=token.next) == null)
526 return (jj_ntk = (token.next=token_source.getNextToken()).kind);
527 else
528 return (jj_ntk = jj_nt.kind);
529 }
530
531 private java.util.Vector jj_expentries = new java.util.Vector();
532 private int[] jj_expentry;
533 private int jj_kind = -1;
534
535 public ParseException generateParseException() {
536 jj_expentries.removeAllElements();
537 boolean[] la1tokens = new boolean[41];
538 for (int i = 0; i < 41; i++) {
539 la1tokens[i] = false;
540 }
541 if (jj_kind >= 0) {
542 la1tokens[jj_kind] = true;
543 jj_kind = -1;
544 }
545 for (int i = 0; i < 16; i++) {
546 if (jj_la1[i] == jj_gen) {
547 for (int j = 0; j < 32; j++) {
548 if ((jj_la1_0[i] & (1<<j)) != 0) {
549 la1tokens[j] = true;
550 }
551 if ((jj_la1_1[i] & (1<<j)) != 0) {
552 la1tokens[32+j] = true;
553 }
554 }
555 }
556 }
557 for (int i = 0; i < 41; i++) {
558 if (la1tokens[i]) {
559 jj_expentry = new int[1];
560 jj_expentry[0] = i;
561 jj_expentries.addElement(jj_expentry);
562 }
563 }
564 int[][] exptokseq = new int[jj_expentries.size()][];
565 for (int i = 0; i < jj_expentries.size(); i++) {
566 exptokseq[i] = (int[])jj_expentries.elementAt(i);
567 }
568 return new ParseException(token, exptokseq, tokenImage);
569 }
570
571 final public void enable_tracing() {
572 }
573
574 final public void disable_tracing() {
575 }
576
577 }