1 package net.sf.logdistiller;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import java.io.Serializable;
18 import java.util.HashSet;
19 import java.util.Set;
20
21 import org.apache.commons.lang.ArrayUtils;
22 import org.apache.commons.lang.StringUtils;
23 import org.jdom.Element;
24
25
26
27
28 public class Condition
29 implements Serializable
30 {
31 private static final long serialVersionUID = 9085774334169039381L;
32
33 private final Match[] matches;
34
35 public static final Condition ACCEPT_ALL = new Condition( null, new Match[0] );
36
37
38
39
40 private final Set<String> tags;
41
42 public Condition( String tags, Match[] matches )
43 {
44 if ( tags != null )
45 {
46 this.tags = new HashSet<String>();
47 String[] values = tags.split( "," );
48 for ( int i = values.length - 1; i > 0; i-- )
49 {
50 String tag = values[i].trim();
51 if ( tag.length() > 0 )
52 {
53 this.tags.add( tag );
54 }
55 }
56 }
57 else
58 {
59 this.tags = null;
60 }
61 this.matches = (Match[]) ArrayUtils.clone( matches );
62 }
63
64 public boolean match( LogEvent le )
65 {
66 int count = matches.length;
67 for ( int i = 0; i < count; i++ )
68 {
69 if ( !matches[i].match( le ) )
70 {
71 return false;
72 }
73 }
74 return true;
75 }
76
77
78
79
80
81
82 public boolean hasTag( String tag )
83 {
84 return ( tags == null ) ? false : tags.contains( tag );
85 }
86
87 public Element dump()
88 {
89 Element elmt = new Element( "condition" );
90 if ( tags != null )
91 {
92 elmt.setAttribute( "tags", StringUtils.join( tags, ", " ) );
93 };
94 int count = ( matches == null ) ? 0 : matches.length;
95 for ( int i = 0; i < count; i++ )
96 {
97 elmt.addContent( matches[i].dump() );
98 }
99 return elmt;
100 }
101 }