View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.log4j.rule;
19  
20  import java.util.Collection;
21  import java.util.LinkedList;
22  import java.util.Stack;
23  
24  /***
25   * A Factory class which, given a string representation of the rule,
26   * and a context stack, will
27   * return a Rule ready for evaluation against events.
28   * If an operator is requested that isn't supported, 
29   * an IllegalArgumentException is thrown.
30   *
31   * @author Scott Deboy (sdeboy@apache.org)
32   */
33  public final class RuleFactory {
34      /***
35       * Singleton instance.
36       */
37    private static final RuleFactory FACTORY = new RuleFactory();
38      /***
39       * Rules.
40       */
41    private static final Collection RULES = new LinkedList();
42      /***
43       * AND operator literal.
44       */
45    private static final String AND_RULE = "&&";
46      /***
47       * OR operator literal.
48       */
49    private static final String OR_RULE = "||";
50      /***
51       * NOT operator literal.
52       */
53    private static final String NOT_RULE = "!";
54      /***
55       * Inequality operator literal.
56       */
57    private static final String NOT_EQUALS_RULE = "!=";
58      /***
59       * Equality operator literal.
60       */
61    private static final String EQUALS_RULE = "==";
62      /***
63       * Partial match operator literal.
64       */
65    private static final String PARTIAL_TEXT_MATCH_RULE = "~=";
66      /***
67       * Like operator literal.
68       */
69    private static final String LIKE_RULE = "like";
70      /***
71       * Exists operator literal.
72       */
73    private static final String EXISTS_RULE = "exists";
74      /***
75       * Less than operator literal.
76       */
77    private static final String LESS_THAN_RULE = "<";
78      /***
79       * Greater than operator literal.
80       */
81    private static final String GREATER_THAN_RULE = ">";
82      /***
83       * Less than or equal operator literal.
84       */
85    private static final String LESS_THAN_EQUALS_RULE = "<=";
86      /***
87       * Greater than or equal operator literal.
88       */
89    private static final String GREATER_THAN_EQUALS_RULE = ">=";
90  
91    static {
92      RULES.add(AND_RULE);
93      RULES.add(OR_RULE);
94      RULES.add(NOT_RULE);
95      RULES.add(NOT_EQUALS_RULE);
96      RULES.add(EQUALS_RULE);
97      RULES.add(PARTIAL_TEXT_MATCH_RULE);
98      RULES.add(LIKE_RULE);
99      RULES.add(EXISTS_RULE);
100     RULES.add(LESS_THAN_RULE);
101     RULES.add(GREATER_THAN_RULE);
102     RULES.add(LESS_THAN_EQUALS_RULE);
103     RULES.add(GREATER_THAN_EQUALS_RULE);
104   }
105 
106     /***
107      * Create instance.
108      */
109   private RuleFactory() {
110         super();
111     }
112 
113     /***
114      * Get instance.
115      * @return rule factory instance.
116      */
117   public static RuleFactory getInstance() {
118       return FACTORY;
119   }
120 
121     /***
122      * Determine if specified string is a known operator.
123      * @param symbol string
124      * @return true if string is a known operator
125      */
126   public boolean isRule(final String symbol) {
127     return ((symbol != null) && (RULES.contains(symbol.toLowerCase())));
128   }
129 
130     /***
131      * Create rule from applying operator to stack.
132      * @param symbol symbol
133      * @param stack stack
134      * @return new instance
135      */
136   public Rule getRule(final String symbol, final Stack stack) {
137     if (AND_RULE.equals(symbol)) {
138       return AndRule.getRule(stack);
139     }
140 
141     if (OR_RULE.equals(symbol)) {
142       return OrRule.getRule(stack);
143     }
144 
145     if (NOT_RULE.equals(symbol)) {
146       return NotRule.getRule(stack);
147     }
148 
149     if (NOT_EQUALS_RULE.equals(symbol)) {
150       return NotEqualsRule.getRule(stack);
151     }
152 
153     if (EQUALS_RULE.equals(symbol)) {
154       return EqualsRule.getRule(stack);
155     }
156 
157     if (PARTIAL_TEXT_MATCH_RULE.equals(symbol)) {
158       return PartialTextMatchRule.getRule(stack);
159     }
160 
161     if (RULES.contains(LIKE_RULE) && LIKE_RULE.equalsIgnoreCase(symbol)) {
162       return LikeRule.getRule(stack);
163     }
164 
165     if (EXISTS_RULE.equalsIgnoreCase(symbol)) {
166       return ExistsRule.getRule(stack);
167     }
168 
169     if (LESS_THAN_RULE.equals(symbol)) {
170       return InequalityRule.getRule(LESS_THAN_RULE, stack);
171     }
172 
173     if (GREATER_THAN_RULE.equals(symbol)) {
174       return InequalityRule.getRule(GREATER_THAN_RULE, stack);
175     }
176 
177     if (LESS_THAN_EQUALS_RULE.equals(symbol)) {
178       return InequalityRule.getRule(LESS_THAN_EQUALS_RULE, stack);
179     }
180 
181     if (GREATER_THAN_EQUALS_RULE.equals(symbol)) {
182       return InequalityRule.getRule(GREATER_THAN_EQUALS_RULE, stack);
183     }
184     throw new IllegalArgumentException("Invalid rule: " + symbol);
185   }
186 }