1 | package de.ugoe.cs.eventbench.windows;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileInputStream;
|
---|
5 | import java.io.FileNotFoundException;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.InputStreamReader;
|
---|
8 | import java.io.UnsupportedEncodingException;
|
---|
9 | import java.security.InvalidParameterException;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.LinkedList;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.SortedMap;
|
---|
14 | import java.util.TreeMap;
|
---|
15 |
|
---|
16 | import javax.xml.parsers.ParserConfigurationException;
|
---|
17 | import javax.xml.parsers.SAXParser;
|
---|
18 | import javax.xml.parsers.SAXParserFactory;
|
---|
19 |
|
---|
20 | import org.xml.sax.Attributes;
|
---|
21 | import org.xml.sax.InputSource;
|
---|
22 | import org.xml.sax.SAXException;
|
---|
23 | import org.xml.sax.SAXParseException;
|
---|
24 | import org.xml.sax.helpers.DefaultHandler;
|
---|
25 |
|
---|
26 | import de.ugoe.cs.eventbench.windows.data.WindowTree;
|
---|
27 | import de.ugoe.cs.eventbench.windows.data.WindowsEvent;
|
---|
28 | import de.ugoe.cs.eventbench.windows.data.WindowsMessage;
|
---|
29 | import de.ugoe.cs.util.StringTools;
|
---|
30 | import de.ugoe.cs.util.console.Console;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * <p>
|
---|
34 | * This class provides functionality to parse XML log files generated by the
|
---|
35 | * MFCUsageMonitor of EventBench. The result of parsing a file is a collection
|
---|
36 | * of event sequences. It uses the {@link SequenceSplitter} and the
|
---|
37 | * {@link EventGenerator} as well as custom defined {@link MessageHandler} for
|
---|
38 | * the parsing.
|
---|
39 | * </p>
|
---|
40 | *
|
---|
41 | * @author Steffen Herbold
|
---|
42 | * @version 1.0
|
---|
43 | */
|
---|
44 | public class MFCLogParser extends DefaultHandler {
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * <p>
|
---|
48 | * If a custom message handler is used, this field contains its handle.
|
---|
49 | * Otherwise this field is {@code null}.
|
---|
50 | * </p>
|
---|
51 | */
|
---|
52 | private MessageHandler currentHandler;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * <p>
|
---|
56 | * Handle to the message that is currently parsed.
|
---|
57 | * </p>
|
---|
58 | */
|
---|
59 | private WindowsMessage currentMessage;
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * <p>
|
---|
63 | * {@link SequenceSplitter} instance used by the {@link MFCLogParser}.
|
---|
64 | * </p>
|
---|
65 | */
|
---|
66 | private SequenceSplitter sequenceSplitter;
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * <p>
|
---|
70 | * Collection of event sequences that is contained in the log file, which is
|
---|
71 | * parsed.
|
---|
72 | * </p>
|
---|
73 | */
|
---|
74 | private Collection<List<WindowsEvent>> sequences;
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * <p>
|
---|
78 | * Debugging variable that allows the analysis which message type occurs how
|
---|
79 | * often in the log file. Can be used to enhance the message filter.
|
---|
80 | * </p>
|
---|
81 | */
|
---|
82 | private SortedMap<Integer, Integer> typeCounter;
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * <p>
|
---|
86 | * Debugging variable that enables the counting of the occurrences of each
|
---|
87 | * message. Used in combination with {@link #typeCounter}.
|
---|
88 | * </p>
|
---|
89 | */
|
---|
90 | private boolean countMessageOccurences;
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * <p>
|
---|
94 | * Constructor. Creates a new LogParser that does not count message
|
---|
95 | * occurrences.
|
---|
96 | * </p>
|
---|
97 | */
|
---|
98 | public MFCLogParser() {
|
---|
99 | this(false);
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * <p>
|
---|
104 | * Constructor. Creates a new LogParser.
|
---|
105 | * </p>
|
---|
106 | *
|
---|
107 | * @param countMessageOccurences
|
---|
108 | * if true, the occurrences of each message type in the log is
|
---|
109 | * counted.
|
---|
110 | */
|
---|
111 | public MFCLogParser(boolean countMessageOccurences) {
|
---|
112 | sequenceSplitter = new SequenceSplitter();
|
---|
113 | sequences = new LinkedList<List<WindowsEvent>>();
|
---|
114 | currentHandler = null;
|
---|
115 | this.countMessageOccurences = countMessageOccurences;
|
---|
116 | if (countMessageOccurences) {
|
---|
117 | typeCounter = new TreeMap<Integer, Integer>();
|
---|
118 | }
|
---|
119 |
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * <p>
|
---|
124 | * Returns the collection of event sequences that is obtained from parsing
|
---|
125 | * log files.
|
---|
126 | * </p>
|
---|
127 | *
|
---|
128 | * @return collection of event sequences
|
---|
129 | */
|
---|
130 | public Collection<List<WindowsEvent>> getSequences() {
|
---|
131 | return sequences;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /*
|
---|
135 | * (non-Javadoc)
|
---|
136 | *
|
---|
137 | * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String,
|
---|
138 | * java.lang.String, java.lang.String, org.xml.sax.Attributes)
|
---|
139 | */
|
---|
140 | @Override
|
---|
141 | public void startElement(String uri, String localName, String qName,
|
---|
142 | Attributes atts) throws SAXException {
|
---|
143 | if (qName.equals("session")) {
|
---|
144 | Console.traceln("start of session");
|
---|
145 | sequenceSplitter = new SequenceSplitter();
|
---|
146 | } else if (qName.equals("msg")) {
|
---|
147 | String msgType = atts.getValue("type");
|
---|
148 | int msgInt = -1;
|
---|
149 | try {
|
---|
150 | msgInt = Integer.parseInt(msgType);
|
---|
151 |
|
---|
152 | if (countMessageOccurences) {
|
---|
153 | Integer currentCount = typeCounter.get(msgInt);
|
---|
154 | if (currentCount == null) {
|
---|
155 | typeCounter.put(msgInt, 1);
|
---|
156 | } else {
|
---|
157 | typeCounter.put(msgInt, currentCount + 1);
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | if (msgInt == MessageDefs.WM_CREATE) {
|
---|
162 | currentHandler = new HandlerCreate();
|
---|
163 | currentHandler.onStartElement();
|
---|
164 | } else if (msgInt == MessageDefs.WM_DESTROY) {
|
---|
165 | currentHandler = new HandlerDestroy();
|
---|
166 | currentHandler.onStartElement();
|
---|
167 | } else if (msgInt == MessageDefs.WM_SETTEXT) {
|
---|
168 | currentHandler = new HandlerSetText();
|
---|
169 | currentHandler.onStartElement();
|
---|
170 | } else {
|
---|
171 | currentMessage = new WindowsMessage(msgInt);
|
---|
172 | }
|
---|
173 | } catch (NumberFormatException e) {
|
---|
174 | Console.printerrln("Invalid message type: type not a number");
|
---|
175 | e.printStackTrace();
|
---|
176 | }
|
---|
177 | } else if (qName.equals("param")) {
|
---|
178 | if (currentHandler != null) {
|
---|
179 | currentHandler.onParameter(atts.getValue("name"),
|
---|
180 | atts.getValue("value"));
|
---|
181 | } else {
|
---|
182 | currentMessage.addParameter(atts.getValue("name"),
|
---|
183 | atts.getValue("value"));
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | /*
|
---|
189 | * (non-Javadoc)
|
---|
190 | *
|
---|
191 | * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String,
|
---|
192 | * java.lang.String, java.lang.String)
|
---|
193 | */
|
---|
194 | @Override
|
---|
195 | public void endElement(String uri, String localName, String qName)
|
---|
196 | throws SAXException {
|
---|
197 | if (qName.equals("msg")) {
|
---|
198 | if (currentHandler != null) {
|
---|
199 | currentHandler.onEndElement();
|
---|
200 | currentHandler = null;
|
---|
201 | } else {
|
---|
202 | try {
|
---|
203 | currentMessage.setTarget(WindowTree.getInstance());
|
---|
204 | sequenceSplitter.addMessage(currentMessage);
|
---|
205 | } catch (InvalidParameterException e) {
|
---|
206 | Console.traceln(e.getMessage() + " WindowsMessage "
|
---|
207 | + currentMessage + " ignored.");
|
---|
208 | }
|
---|
209 | }
|
---|
210 | } else if (qName.equals("session")) {
|
---|
211 | sequenceSplitter.endSession();
|
---|
212 | List<WindowsEvent> seq = sequenceSplitter.getSequence();
|
---|
213 | if( seq!=null && !seq.isEmpty() ) {
|
---|
214 | sequences.add(seq);
|
---|
215 | }
|
---|
216 | Console.traceln("end of session");
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * <p>
|
---|
222 | * Parses a given log file created by the MFCMonitor and adds its contents
|
---|
223 | * to the collection of event sequences.
|
---|
224 | * </p>
|
---|
225 | *
|
---|
226 | * @param filename
|
---|
227 | * name and path of the log file
|
---|
228 | */
|
---|
229 | public void parseFile(String filename) {
|
---|
230 | if (filename == null) {
|
---|
231 | throw new InvalidParameterException("filename must not be null");
|
---|
232 | }
|
---|
233 |
|
---|
234 | SAXParserFactory spf = SAXParserFactory.newInstance();
|
---|
235 | spf.setValidating(true);
|
---|
236 |
|
---|
237 | SAXParser saxParser = null;
|
---|
238 | InputSource inputSource = null;
|
---|
239 | try {
|
---|
240 | saxParser = spf.newSAXParser();
|
---|
241 | inputSource = new InputSource(new InputStreamReader(
|
---|
242 | new FileInputStream(filename)));//, "UTF-8"));
|
---|
243 | //} catch (UnsupportedEncodingException e) {
|
---|
244 | // e.printStackTrace();
|
---|
245 | } catch (ParserConfigurationException e) {
|
---|
246 | e.printStackTrace();
|
---|
247 | } catch (SAXException e) {
|
---|
248 | e.printStackTrace();
|
---|
249 | } catch (FileNotFoundException e) {
|
---|
250 | e.printStackTrace();
|
---|
251 | }
|
---|
252 | if (inputSource != null) {
|
---|
253 | inputSource.setSystemId("file://"
|
---|
254 | + new File(filename).getAbsolutePath());
|
---|
255 | try {
|
---|
256 | if (saxParser == null) {
|
---|
257 | throw new RuntimeException("SAXParser creation failed");
|
---|
258 | }
|
---|
259 | saxParser.parse(inputSource, this);
|
---|
260 | } catch (SAXParseException e) {
|
---|
261 | Console.printerrln("Failure parsing file in line "
|
---|
262 | + e.getLineNumber() + ", column " + e.getColumnNumber()
|
---|
263 | + ".");
|
---|
264 | e.printStackTrace();
|
---|
265 | } catch (SAXException e) {
|
---|
266 | e.printStackTrace();
|
---|
267 | } catch (IOException e) {
|
---|
268 | e.printStackTrace();
|
---|
269 | }
|
---|
270 | }
|
---|
271 | if (countMessageOccurences) {
|
---|
272 | Console.println("Message statistics:");
|
---|
273 | Console.println(typeCounter.toString()
|
---|
274 | .replace(" ", StringTools.ENDLINE)
|
---|
275 | .replaceAll("[\\{\\}]", ""));
|
---|
276 | }
|
---|
277 | }
|
---|
278 | }
|
---|