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