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 org.apache.log4j.spi.LoggingEvent;
21  import org.apache.log4j.spi.LoggingEventFieldResolver;
22  
23  import java.util.Stack;
24  
25  
26  /***
27   * A Rule class implementing case-insensitive
28   * partial-text matches against two strings.
29   *
30   * @author Scott Deboy (sdeboy@apache.org)
31   */
32  public class PartialTextMatchRule extends AbstractRule {
33      /***
34       * Serialization ID.
35       */
36    static final long serialVersionUID = 6963284773637727558L;
37      /***
38       * Resolver.
39       */
40    private static final LoggingEventFieldResolver RESOLVER =
41      LoggingEventFieldResolver.getInstance();
42      /***
43       * Field.
44       */
45    private final String field;
46      /***
47       * Value.
48       */
49    private final String value;
50  
51      /***
52       * Create new instance.
53       * @param field field
54       * @param value value
55       */
56    private PartialTextMatchRule(final String field, final String value) {
57      super();
58      if (!RESOLVER.isField(field)) {
59        throw new IllegalArgumentException(
60          "Invalid partial text rule - " + field + " is not a supported field");
61      }
62  
63      this.field = field;
64      this.value = value;
65    }
66  
67      /***
68       * Create new instance.
69       * @param field field
70       * @param value value
71       * @return new instance
72       */
73    public static Rule getRule(final String field, final String value) {
74      return new PartialTextMatchRule(field, value);
75    }
76  
77      /***
78       * Create new instance from top two elements of stack.
79       * @param stack stack
80       * @return new instance
81       */
82    public static Rule getRule(final Stack stack) {
83      if (stack.size() < 2) {
84        throw new IllegalArgumentException(
85          "invalid partial text rule - expected two parameters but received "
86          + stack.size());
87      }
88  
89      String p2 = stack.pop().toString();
90      String p1 = stack.pop().toString();
91  
92      return new PartialTextMatchRule(p1, p2);
93    }
94  
95      /*** {@inheritDoc} */
96    public boolean evaluate(final LoggingEvent event) {
97      Object p2 = RESOLVER.getValue(field, event);
98  
99      return ((p2 != null) && (value != null)
100     && (p2.toString().toLowerCase().indexOf(value.toLowerCase()) > -1));
101   }
102 }