01: package org.jicengine.expression;
02:
03: import org.jicengine.operation.Operation;
04:
05: /**
06: *
07: *
08: * <p>
09: * Copyright (C) 2004 Timo Laitinen
10: * </p>
11: * @author Timo Laitinen
12: * @created 2004-09-20
13: * @since JICE-0.10
14: *
15: */
16:
17: public class CompositeParser implements Parser {
18:
19: private Parser[] parsers;
20: private boolean unparsedExpressionIsError = true;
21:
22: public CompositeParser(Parser[] parsers) {
23: this .parsers = parsers;
24: }
25:
26: public Operation parse(String expression) throws SyntaxException {
27: if (expression == null) {
28: throw new SyntaxException("Expression was null");
29: }
30:
31: expression = expression.trim();
32: if (expression.length() == 0) {
33: throw new SyntaxException(
34: "Empty expression can't be evaluated.");
35: }
36:
37: // try the parsers one at a time.
38: // we assume that the first parser to return a non-null value
39: // was the right parser.
40: Parser parser;
41: Operation result = null;
42: for (int i = 0; i < parsers.length; i++) {
43: parser = parsers[i];
44: try {
45: result = parser.parse(expression);
46: if (result != null) {
47: // ok
48: break;
49: }
50: } catch (SyntaxException e) {
51: throw e;
52: } catch (Exception e2) {
53: throw new SyntaxException("Problems parsing '"
54: + expression + "' with parser " + parser, e2);
55: }
56: }
57:
58: if (result == null) {
59: if (this .unparsedExpressionIsError) {
60: throw new SyntaxException("Illegal expression: '"
61: + expression + "'");
62: } else {
63: return null;
64: }
65: } else {
66: return result;
67: }
68:
69: }
70: }
|