1 | package de.ugoe.cs.eventbench.jfc;
|
---|
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 |
|
---|
14 | import javax.xml.parsers.ParserConfigurationException;
|
---|
15 | import javax.xml.parsers.SAXParser;
|
---|
16 | import javax.xml.parsers.SAXParserFactory;
|
---|
17 |
|
---|
18 | import org.xml.sax.Attributes;
|
---|
19 | import org.xml.sax.InputSource;
|
---|
20 | import org.xml.sax.SAXException;
|
---|
21 | import org.xml.sax.SAXParseException;
|
---|
22 | import org.xml.sax.helpers.DefaultHandler;
|
---|
23 |
|
---|
24 | import de.ugoe.cs.eventbench.jfc.data.JFCEvent;
|
---|
25 | import de.ugoe.cs.util.console.Console;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * <p>
|
---|
29 | * This class provides functionality to parse XML log files generated by the
|
---|
30 | * JFCMonitor of EventBench. The result of parsing a file is a collection of
|
---|
31 | * event sequences.
|
---|
32 | * </p>
|
---|
33 | *
|
---|
34 | * @author Steffen Herbold
|
---|
35 | * @version 1.0
|
---|
36 | */
|
---|
37 | public class JFCLogParser extends DefaultHandler {
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * <p>
|
---|
41 | * Collection of event sequences that is contained in the log file, which is
|
---|
42 | * parsed.
|
---|
43 | * </p>
|
---|
44 | */
|
---|
45 | private Collection<List<JFCEvent>> sequences;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * <p>
|
---|
49 | * Internal handle to the event that is currently being parsed.
|
---|
50 | * </p>
|
---|
51 | */
|
---|
52 | private JFCEvent currentEvent;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * <p>
|
---|
56 | * Internal handle to the event sequence that is currently being parsed.
|
---|
57 | * </p>
|
---|
58 | */
|
---|
59 | private List<JFCEvent> currentSequence = null;
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * <p>
|
---|
63 | * Enumeration to differentiate if a parameter belongs to an event, a source
|
---|
64 | * or the parent of a source.
|
---|
65 | * </p>
|
---|
66 | *
|
---|
67 | * @author Steffen Herbold
|
---|
68 | * @version 1.0
|
---|
69 | */
|
---|
70 | private enum ParamSource {
|
---|
71 | EVENT, SOURCE, PARENT
|
---|
72 | };
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * <p>
|
---|
76 | * Specifies whether the parameters that are currently being read belong the
|
---|
77 | * the event, the source or the parent.
|
---|
78 | * </p>
|
---|
79 | */
|
---|
80 | ParamSource paramSource = null;
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * <p>
|
---|
84 | * Constructor. Creates a new JFCLogParser.
|
---|
85 | * </p>
|
---|
86 | */
|
---|
87 | public JFCLogParser() {
|
---|
88 | sequences = new LinkedList<List<JFCEvent>>();
|
---|
89 | currentSequence = new LinkedList<JFCEvent>();
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * <p>
|
---|
94 | * Returns the collection of event sequences that is obtained from parsing
|
---|
95 | * log files.
|
---|
96 | * </p>
|
---|
97 | *
|
---|
98 | * @return collection of event sequences
|
---|
99 | */
|
---|
100 | public Collection<List<JFCEvent>> getSequences() {
|
---|
101 | return sequences;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /*
|
---|
105 | * (non-Javadoc)
|
---|
106 | *
|
---|
107 | * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String,
|
---|
108 | * java.lang.String, java.lang.String, org.xml.sax.Attributes)
|
---|
109 | */
|
---|
110 | public void startElement(String uri, String localName, String qName,
|
---|
111 | Attributes atts) throws SAXException {
|
---|
112 | if (qName.equals("newsession")) {
|
---|
113 | Console.traceln("start of session");
|
---|
114 | if (currentSequence != null && !currentSequence.isEmpty()) {
|
---|
115 | sequences.add(currentSequence);
|
---|
116 | currentSequence = new LinkedList<JFCEvent>();
|
---|
117 | }
|
---|
118 | } else if (qName.equals("event")) {
|
---|
119 | currentEvent = new JFCEvent(atts.getValue("id"));
|
---|
120 | paramSource = ParamSource.EVENT;
|
---|
121 | } else if (qName.equals("param")) {
|
---|
122 | if (paramSource == ParamSource.EVENT) {
|
---|
123 | currentEvent.addParameter(atts.getValue("name"),
|
---|
124 | atts.getValue("value"));
|
---|
125 | } else if (paramSource == ParamSource.SOURCE) {
|
---|
126 | currentEvent.addSourceInformation(atts.getValue("name"),
|
---|
127 | atts.getValue("value"));
|
---|
128 | } else if (paramSource == ParamSource.PARENT) {
|
---|
129 | currentEvent.addParentInformation(atts.getValue("name"),
|
---|
130 | atts.getValue("value"));
|
---|
131 | }
|
---|
132 | } else if (qName.equals("source")) {
|
---|
133 | paramSource = ParamSource.SOURCE;
|
---|
134 | } else if (qName.equals("parent")) {
|
---|
135 | paramSource = ParamSource.PARENT;
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | /*
|
---|
140 | * (non-Javadoc)
|
---|
141 | *
|
---|
142 | * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String,
|
---|
143 | * java.lang.String, java.lang.String)
|
---|
144 | */
|
---|
145 | @Override
|
---|
146 | public void endElement(String uri, String localName, String qName)
|
---|
147 | throws SAXException {
|
---|
148 | if (qName.equals("event")) {
|
---|
149 | currentSequence.add(currentEvent);
|
---|
150 | } else if (qName.equals("source")) {
|
---|
151 | paramSource = ParamSource.EVENT;
|
---|
152 | } else if (qName.equals("parent")) {
|
---|
153 | paramSource = ParamSource.SOURCE;
|
---|
154 | } else if (qName.equals("sessions")) {
|
---|
155 | if(currentSequence!=null && !currentSequence.isEmpty() ) {
|
---|
156 | sequences.add(currentSequence);
|
---|
157 | }
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * <p>
|
---|
163 | * Parses a log file written by the JFCMonitor and creates a collection of
|
---|
164 | * event sequences.
|
---|
165 | * </p>
|
---|
166 | *
|
---|
167 | * @param filename
|
---|
168 | * name and path of the log file
|
---|
169 | */
|
---|
170 | public void parseFile(String filename) {
|
---|
171 | if (filename == null) {
|
---|
172 | throw new InvalidParameterException("filename must not be null");
|
---|
173 | }
|
---|
174 |
|
---|
175 | SAXParserFactory spf = SAXParserFactory.newInstance();
|
---|
176 | spf.setValidating(true);
|
---|
177 |
|
---|
178 | SAXParser saxParser = null;
|
---|
179 | InputSource inputSource = null;
|
---|
180 | try {
|
---|
181 | saxParser = spf.newSAXParser();
|
---|
182 | inputSource = new InputSource(new InputStreamReader(
|
---|
183 | new FileInputStream(filename), "UTF-16"));
|
---|
184 | } catch (UnsupportedEncodingException e) {
|
---|
185 | e.printStackTrace();
|
---|
186 | } catch (ParserConfigurationException e) {
|
---|
187 | e.printStackTrace();
|
---|
188 | } catch (SAXException e) {
|
---|
189 | e.printStackTrace();
|
---|
190 | } catch (FileNotFoundException e) {
|
---|
191 | e.printStackTrace();
|
---|
192 | }
|
---|
193 | if (inputSource != null) {
|
---|
194 | inputSource.setSystemId("file://"
|
---|
195 | + new File(filename).getAbsolutePath());
|
---|
196 | try {
|
---|
197 | if (saxParser == null) {
|
---|
198 | throw new RuntimeException("SAXParser creation failed");
|
---|
199 | }
|
---|
200 | saxParser.parse(inputSource, this);
|
---|
201 | } catch (SAXParseException e) {
|
---|
202 | Console.printerrln("Failure parsing file in line "
|
---|
203 | + e.getLineNumber() + ", column " + e.getColumnNumber()
|
---|
204 | + ".");
|
---|
205 | e.printStackTrace();
|
---|
206 | } catch (SAXException e) {
|
---|
207 | e.printStackTrace();
|
---|
208 | } catch (IOException e) {
|
---|
209 | e.printStackTrace();
|
---|
210 | }
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | }
|
---|