00001 package ceylan.parser;
00002
00003 import java.text.SimpleDateFormat;
00004 import java.util.Arrays;
00005 import java.util.Date;
00006 import java.util.HashMap;
00007 import java.util.List;
00008 import java.util.regex.Matcher;
00009 import java.util.regex.Pattern;
00010
00011 import com.lightysoft.logmx.business.ParsedEntry;
00012 import com.lightysoft.logmx.mgr.LogFileParser;
00013
00014
00015
00030 public class CeylanTraceParser extends LogFileParser
00031 {
00032
00034 private ParsedEntry entry = null;
00035
00036
00038 private final static SimpleDateFormat DatePattern = new SimpleDateFormat(
00039 "dd/MM/yyyy HH:mm:ss" ) ;
00040
00041
00043 private final static Pattern TraceBeginPattern =
00044 Pattern.compile("^<\\d++\\.\\d++\\.\\d++>\\|.*$");
00045
00046
00049 private StringBuilder entryMsgBuffer = null;
00050
00051
00053 private static final String ExecutionTimeKey = "Wallclock Time" ;
00054
00056 private static final String EmitterLocationKey = "Emitter Location" ;
00057
00059 private static final String CategoryKey = "Categorization" ;
00060
00061 private static final String[] ArrayOfKeys =
00062 { ExecutionTimeKey, EmitterLocationKey, CategoryKey } ;
00063
00065 private static final List<String> KeysOfUserDefinedFields =
00066 Arrays.asList( ArrayOfKeys ) ;
00067
00068
00069
00074 public String getParserName()
00075 {
00076 return "Ceylan Trace Parser" ;
00077 }
00078
00079
00084 public String getSupportedFileType()
00085 {
00086 return "Ceylan trace files" ;
00087 }
00088
00089
00094 protected void parseLine(String line) throws Exception
00095 {
00096
00097
00098 if ( line == null )
00099 {
00100 recordPreviousEntryIfExists() ;
00101 return ;
00102 }
00103
00104 Matcher matcher = TraceBeginPattern.matcher(line) ;
00105
00106 if ( matcher.matches() )
00107 {
00108
00109
00110 prepareNewEntry();
00111
00112
00113
00114
00115 String[] fields = line.split( "\\|" ) ;
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 entry.setThread( fields[0].trim() ) ;
00146 entry.setEmitter( fields[2].trim() + "." + fields[1].trim() ) ;
00147 entry.setDate( fields[3].trim() );
00148 entry.setLevel( fields[7].trim() ) ;
00149
00150
00151 String remainingFields = "" ;
00152
00153 Integer remainingFieldsCount = fields.length - 8 ;
00154
00155
00156 for ( Integer i = 0; i < remainingFieldsCount; i++ )
00157 {
00158 remainingFields += fields[i+8].trim() ;
00159 if ( i != remainingFieldsCount-1 )
00160 remainingFields += "|" ;
00161 }
00162
00163
00164
00165
00166
00167
00168 entryMsgBuffer.append( remainingFields ) ;
00169
00170
00171 entry.getUserDefinedFields().put( ExecutionTimeKey,
00172 fields[4].trim() ) ;
00173
00174 entry.getUserDefinedFields().put( EmitterLocationKey,
00175 fields[5].trim() ) ;
00176
00177 entry.getUserDefinedFields().put( CategoryKey,
00178 fields[6].trim() ) ;
00179
00180 }
00181 else if (entry != null)
00182 {
00183
00184
00185 entryMsgBuffer.append('\n').append(line);
00186
00187 }
00188
00189 }
00190
00191
00198 @Override
00199 public List<String> getUserDefinedFields() {
00200 return KeysOfUserDefinedFields ;
00201 }
00202
00203
00211 public Date getRelativeEntryDate(ParsedEntry pEntry) throws Exception
00212 {
00213
00214 final String executionTimeString = pEntry.getUserDefinedFields().get(
00215 ExecutionTimeKey ).toString() ;
00216
00217 return new Date( Integer.parseInt( executionTimeString ) ) ;
00218 }
00219
00220
00226 public Date getAbsoluteEntryDate(ParsedEntry pEntry) throws Exception
00227 {
00228
00229 return DatePattern.parse( pEntry.getDate() ) ;
00230
00231 }
00232
00233
00239 private void recordPreviousEntryIfExists() throws Exception
00240 {
00241
00242 if (entry != null)
00243 {
00244 entry.setMessage( entryMsgBuffer.toString() );
00245 addEntry(entry);
00246 }
00247
00248 }
00249
00250
00256 private void prepareNewEntry() throws Exception
00257 {
00258
00259 recordPreviousEntryIfExists();
00260 entry = createNewEntry();
00261 entryMsgBuffer = new StringBuilder(80) ;
00262
00263
00264 entry.setUserDefinedFields( new HashMap<String, Object>(1) );
00265
00266 }
00267
00268 }