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