public Rule compileExpression()

in src/main/java/org/apache/log4j/rule/ExpressionRule.java [127:177]


    public Rule compileExpression(final String expression) {
      RuleFactory factory = RuleFactory.getInstance();

      Stack stack = new Stack();
      InFixToPostFix.CustomTokenizer tokenizer = new InFixToPostFix.CustomTokenizer(expression);

          while (tokenizer.hasMoreTokens()) {
            //examine each token
            String token = tokenizer.nextToken();
              if (token.startsWith("'") || token.startsWith("\"")) {
                String quoteChar = token.substring(0, 1);
                token = token.substring(1);
                while (!token.endsWith(quoteChar) && tokenizer.hasMoreTokens()) {
                  token = token + " " + tokenizer.nextToken();
                }
                if (token.length() > 0) {
                  token = token.substring(0, token.length() - 1);
                }
              } else {
                //if a symbol is found, pop 2 off the stack,
                  // evaluate and push the result
                if (factory.isRule(token)) {
                  Rule r = factory.getRule(token, stack);
                  stack.push(r);
                  //null out the token so we don't try to push it below
                  token = null;
                }
            }
              //variables or constants are pushed onto the stack
              if (token != null && token.length() > 0) {
                  stack.push(token);
              }
          }

          if ((stack.size() == 1) && (!(stack.peek() instanceof Rule))) {
            //while this may be an attempt at creating an expression,
            //for ease of use, convert this single entry to a partial-text
            //match on the MSG field
            Object o = stack.pop();
            stack.push("MSG");
            stack.push(o);
            return factory.getRule("~=", stack);
          }

          //stack should contain a single rule if the expression is valid
          if ((stack.size() != 1) || (!(stack.peek() instanceof Rule))) {
            throw new IllegalArgumentException("invalid expression: " + expression);
          } else {
            return (Rule) stack.pop();
          }
        }