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.awt.Color;
21  import java.io.Serializable;
22  import java.util.Map;
23  
24  import org.apache.log4j.spi.LoggingEvent;
25  
26  
27  /***
28   * A Rule class which also holds a color.
29   *
30   * @author Scott Deboy (sdeboy@apache.org)
31   */
32  public class ColorRule extends AbstractRule implements Serializable {
33      /***
34       * Serialization id.
35       */
36    static final long serialVersionUID = -794434783372847773L;
37  
38      /***
39       * Wrapped rule.
40       */
41    private final Rule rule;
42      /***
43       * Foreground color.
44       */
45    private final Color foregroundColor;
46      /***
47       * Background color.
48       */
49    private final Color backgroundColor;
50      /***
51       * Expression.
52       */
53    private final String expression;
54  
55      /***
56       * Create new instance.
57       * @param expression expression.
58       * @param rule rule.
59       * @param backgroundColor background color.
60       * @param foregroundColor foreground color.
61       */
62    public ColorRule(final String expression,
63                     final Rule rule,
64                     final Color backgroundColor,
65                     final Color foregroundColor) {
66      super();
67      this.expression = expression;
68      this.rule = rule;
69      this.backgroundColor = backgroundColor;
70      this.foregroundColor = foregroundColor;
71    }
72  
73      /***
74       * Get rule.
75       * @return underlying rule.
76       */
77    public Rule getRule() {
78        return rule;
79    }
80  
81      /***
82       * Get foreground color.
83       * @return foreground color.
84       */
85    public Color getForegroundColor() {
86      return foregroundColor;
87    }
88  
89      /***
90       * Get background color.
91       * @return background color.
92       */
93    public Color getBackgroundColor() {
94      return backgroundColor;
95    }
96  
97      /***
98       * Get expression.
99       * @return expression.
100      */
101   public String getExpression() {
102       return expression;
103   }
104 
105     /***
106      * {@inheritDoc}
107      */
108   public boolean evaluate(final LoggingEvent event, Map matches) {
109     //no need for color rules to build matches
110     return (rule != null && rule.evaluate(event, null));
111   }
112 
113     /***
114      * {@inheritDoc}
115      */
116   public String toString() {
117       StringBuffer buf = new StringBuffer("color rule - expression: ");
118       buf.append(expression);
119       buf.append(", rule: ");
120       buf.append(rule);
121       buf.append(" bg: ");
122       buf.append(backgroundColor);
123       buf.append(" fg: ");
124       buf.append(foregroundColor);
125       return buf.toString();
126   }
127 }