1 package net.sf.logdistiller;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import java.io.*;
18 import java.util.Collections;
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.regex.Pattern;
22
23 import org.jdom.Element;
24
25
26
27
28
29
30 public class Match
31 implements Serializable
32 {
33 private static final long serialVersionUID = -1809458932445222708L;
34
35 private final String attribute;
36
37 private final boolean not;
38
39 private final Function function;
40
41 private transient LogType.AttributeInfo attributeInfo;
42
43 public Match( String attribute, String type, String reference )
44 {
45 this.attribute = attribute;
46 this.not = type.startsWith( "!" );
47 if ( not )
48 {
49 type = type.substring( 1 );
50 }
51 this.function = Match.Function.getInstance( type, reference );
52 }
53
54 public boolean match( LogEvent le )
55 {
56
57 if ( attributeInfo == null )
58 {
59 LogType.Description description = le.getFactory().getDescription();
60 attributeInfo = description.getAttributeInfo( attribute );
61 }
62
63 String value = le.getValue( attributeInfo );
64 boolean result = function.checkValue( value );
65 return ( not ? !result : result );
66 }
67
68 public Element dump()
69 {
70 Element elmt = new Element( "match" );
71 elmt.setAttribute( "attribute", attribute );
72 elmt.setAttribute( "type", ( not ? "!" : "" ) + function.getId() );
73 elmt.setText( function.reference );
74 return elmt;
75 }
76
77 private static abstract class Function
78 implements Serializable
79 {
80 private static final long serialVersionUID = 1072441995042764838L;
81
82 public final static Map<String, Class<? extends Function>> FUNCTIONS;
83 static
84 {
85 Map<String, Class<? extends Function>> map = new HashMap<String, Class<? extends Function>>();
86 map.put( Contains.ID, Contains.class );
87 map.put( Equals.ID, Equals.class );
88 map.put( StartsWith.ID, StartsWith.class );
89 map.put( EndsWith.ID, EndsWith.class );
90 map.put( Regexp.ID, Regexp.class );
91 FUNCTIONS = Collections.unmodifiableMap( map );
92 }
93
94 public static Function getInstance( String type, String reference )
95 {
96 Class<? extends Function> functionClass = FUNCTIONS.get( type );
97 if ( functionClass == null )
98 {
99 throw new IllegalArgumentException( "function type not supported '" + type + "'" );
100 }
101 try
102 {
103 Function function = functionClass.newInstance();
104 function.setReference( reference );
105 return function;
106 }
107 catch ( IllegalAccessException iae )
108 {
109
110 throw new RuntimeException( "an impossible condition has arrived...", iae );
111 }
112 catch ( InstantiationException ie )
113 {
114
115 throw new RuntimeException( "an impossible condition has arrived...", ie );
116 }
117 }
118
119 protected String reference;
120
121 public void setReference( String reference )
122 {
123 this.reference = reference;
124 }
125
126 public abstract String getId();
127
128
129
130
131
132
133
134 public abstract boolean checkValue( String value );
135 }
136
137 protected static class Contains
138 extends Function
139 {
140 private static final long serialVersionUID = 4613611844726287358L;
141
142 public static final String ID = "contains";
143
144 public String getId()
145 {
146 return ID;
147 }
148
149 public boolean checkValue( String value )
150 {
151 return ( value.indexOf( reference ) >= 0 );
152 }
153 }
154
155 protected static class Equals
156 extends Function
157 {
158 private static final long serialVersionUID = -6539918940865536415L;
159
160 public static final String ID = "equals";
161
162 public String getId()
163 {
164 return ID;
165 }
166
167 public boolean checkValue( String value )
168 {
169 return reference.equals( value );
170 }
171 }
172
173 protected static class StartsWith
174 extends Function
175 {
176 private static final long serialVersionUID = 5014914695776363132L;
177
178 public static final String ID = "startsWith";
179
180 public String getId()
181 {
182 return ID;
183 }
184
185 public boolean checkValue( String value )
186 {
187 return value.startsWith( reference );
188 }
189 }
190
191 protected static class EndsWith
192 extends Function
193 {
194 private static final long serialVersionUID = 1819477635452601804L;
195
196 public static final String ID = "endsWith";
197
198 public String getId()
199 {
200 return ID;
201 }
202
203 public boolean checkValue( String value )
204 {
205 return value.endsWith( reference );
206 }
207 }
208
209 protected static class Regexp
210 extends Function
211 {
212 private static final long serialVersionUID = -2067188055591690252L;
213
214 public static final String ID = "regexp";
215
216 public String getId()
217 {
218 return ID;
219 }
220
221 private transient Pattern pattern;
222
223 public void setReference( String reference )
224 {
225 super.setReference( reference );
226 updatePattern();
227 }
228
229 private void updatePattern()
230 {
231 pattern = Pattern.compile( reference );
232 }
233
234 private void readObject( ObjectInputStream in )
235 throws IOException, ClassNotFoundException
236 {
237 in.defaultReadObject();
238 updatePattern();
239 }
240
241 public boolean checkValue( String value )
242 {
243 return pattern.matcher( value ).find();
244 }
245 }
246 }