1 package net.sf.logdistiller;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import java.io.IOException;
18 import java.io.PrintWriter;
19 import java.io.StringWriter;
20 import java.text.ParseException;
21 import java.util.regex.Matcher;
22
23 import net.sf.logdistiller.LogType.AttributeInfo;
24
25 import org.apache.commons.lang.ArrayUtils;
26
27
28
29
30
31
32
33
34
35
36
37 public abstract class LogEvent
38 {
39 private final Factory factory;
40
41 private final String rawLog;
42
43
44 private String[] attributes;
45
46
47 private Extension[] extensions;
48
49 protected LogEvent( Factory factory, String rawLog )
50 {
51 this.factory = factory;
52 this.rawLog = rawLog;
53 this.extensions = new Extension[factory.getDescription().getExtensions().length];
54 }
55
56
57
58
59
60
61 protected void setAttributes( String[] attributes )
62 {
63 int count = factory.getDescription().getAttributesCount();
64 if ( attributes.length != count )
65 {
66 throw new IllegalArgumentException( "expected " + count + " attributes, got " + attributes.length );
67 }
68 this.attributes = attributes;
69 }
70
71 protected void checkInitialized()
72 {
73 if ( attributes == null )
74 {
75 throw new IllegalStateException( "log event not initialized: call setAttributes() to initialize it" );
76 }
77 }
78
79
80
81
82
83
84 public String getRawLog()
85 {
86 return rawLog;
87 }
88
89
90
91
92
93
94
95 public String getAttribute( int pos )
96 {
97 checkInitialized();
98 return attributes[pos];
99 }
100
101
102
103
104
105
106 public String[] getAttributes()
107 {
108 checkInitialized();
109 return (String[]) ArrayUtils.clone( attributes );
110 }
111
112
113
114
115
116
117 public int getAttributesCount()
118 {
119 checkInitialized();
120 return attributes.length;
121 }
122
123
124
125
126
127
128
129 public String getAttribute( String attributeName )
130 {
131 int index = factory.getDescription().getAttributeIndex( attributeName );
132 if ( index < 0 )
133 {
134 throw new IllegalArgumentException( "unknown provided attribute '" + attributeName + "'" );
135 }
136 return getAttribute( index );
137 }
138
139 public String getValue( AttributeInfo info )
140 {
141 if ( info.extended )
142 {
143 Extension extension = extensions[info.index];
144 if ( extension == null )
145 {
146 extension = new Extension( factory.getDescription().getExtensions()[info.index] );
147 extensions[info.index] = extension;
148 }
149 return extension.getValue( info.regexpGroup );
150 }
151 return getAttribute( info.index );
152 }
153
154 public Factory getFactory()
155 {
156 return factory;
157 }
158
159 public void dump( PrintWriter out )
160 {
161 out.println( "- raw log: " + getRawLog() );
162 for ( int i = 0; i < getAttributesCount(); i++ )
163 {
164 out.println( "- " + getFactory().getDescription().getAttributeName( i ) + ": " + getAttribute( i ) );
165 }
166 }
167
168 public String dump()
169 {
170 StringWriter sw = new StringWriter();
171 PrintWriter out = new PrintWriter( sw, false );
172 dump( out );
173 out.close();
174 return sw.toString();
175 }
176
177
178
179
180
181
182
183 public String getTimestamp()
184 {
185 int index = factory.getDescription().getTimestampAttribute();
186 return ( index > 0 ) ? getAttribute( index ) : "-";
187 }
188
189
190
191
192 public static abstract class Factory
193 {
194 public final static String NEWLINE = System.getProperty( "line.separator" );
195
196 protected final LogType.Description description;
197
198 protected final String logSource;
199
200 private LogEvent lastEvent;
201
202 private LogEvent pushedbackEvent;
203
204 protected Factory( LogType.Description description, String logSource )
205 {
206 this.description = description;
207 this.logSource = logSource;
208 }
209
210
211
212
213
214
215
216
217 public LogEvent nextEvent()
218 throws IOException, ParseException
219 {
220 LogEvent nextEvent = null;
221 if ( pushedbackEvent != null )
222 {
223 nextEvent = pushedbackEvent;
224 pushedbackEvent = null;
225 }
226 else
227 {
228 try
229 {
230 nextEvent = readNextEvent();
231 }
232 catch ( ParseException pe )
233 {
234 ParseException pe2 = new ParseException( "error in '" + logSource + "' " + pe.getMessage(),
235 pe.getErrorOffset() );
236 pe2.initCause( pe );
237 throw pe2;
238 }
239 catch ( RuntimeException re )
240 {
241 throw new RuntimeException( "error in '" + logSource + "' " + re.getMessage(), re );
242 }
243 }
244 lastEvent = nextEvent;
245 return nextEvent;
246 }
247
248 public void pushbackLastEvent()
249 throws IllegalStateException
250 {
251 if ( pushedbackEvent != null )
252 {
253 throw new IllegalStateException( "The last event has already been pushed back" );
254 }
255 if ( lastEvent == null )
256 {
257 throw new IllegalStateException( "There is no last event to push back" );
258 }
259 pushedbackEvent = lastEvent;
260 }
261
262 public LogType.Description getDescription()
263 {
264 return description;
265 }
266
267 public String getLogSource()
268 {
269 return logSource;
270 }
271
272 protected abstract LogEvent readNextEvent()
273 throws IOException, ParseException;
274 }
275
276
277
278
279
280
281 private class Extension
282 {
283
284
285
286 private final String[] values;
287
288 public Extension( Attributes.Extension definition )
289 {
290 values = new String[definition.getProvides().size()];
291 Matcher matcher = definition.getRegexp().matcher( getAttribute( definition.getSource() ) );
292 if ( matcher.find() )
293 {
294 int count = matcher.groupCount();
295 for ( int i = 0; i < values.length; i++ )
296 {
297 String value = ( i < count ) ? matcher.group( i + 1 ) : "";
298 values[i] = ( value == null ) ? "" : value;
299 }
300 }
301 else
302 {
303 for ( int i = 0; i < values.length; i++ )
304 {
305 values[i] = "";
306 }
307 }
308 }
309
310 public String getValue( int index )
311 {
312 return values[index];
313 }
314 }
315 }