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("sessions") ) {
|
---|
113 | currentSequence = new LinkedList<JFCEvent>();
|
---|
114 | }
|
---|
115 | if (qName.equals("newsession")) {
|
---|
116 | Console.traceln("start of session");
|
---|
117 | if (currentSequence != null && !currentSequence.isEmpty()) {
|
---|
118 | sequences.add(currentSequence);
|
---|
119 | }
|
---|
120 | currentSequence = new LinkedList<JFCEvent>();
|
---|
121 | } else if (qName.equals("event")) {
|
---|
122 | currentEvent = new JFCEvent(atts.getValue("id"));
|
---|
123 | paramSource = ParamSource.EVENT;
|
---|
124 | } else if (qName.equals("param")) {
|
---|
125 | if (paramSource == ParamSource.EVENT) {
|
---|
126 | currentEvent.addParameter(atts.getValue("name"),
|
---|
127 | atts.getValue("value"));
|
---|
128 | } else if (paramSource == ParamSource.SOURCE) {
|
---|
129 | currentEvent.addSourceInformation(atts.getValue("name"),
|
---|
130 | atts.getValue("value"));
|
---|
131 | } else if (paramSource == ParamSource.PARENT) {
|
---|
132 | currentEvent.addParentInformation(atts.getValue("name"),
|
---|
133 | atts.getValue("value"));
|
---|
134 | }
|
---|
135 | } else if (qName.equals("source")) {
|
---|
136 | paramSource = ParamSource.SOURCE;
|
---|
137 | } else if (qName.equals("parent")) {
|
---|
138 | paramSource = ParamSource.PARENT;
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | /*
|
---|
143 | * (non-Javadoc)
|
---|
144 | *
|
---|
145 | * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String,
|
---|
146 | * java.lang.String, java.lang.String)
|
---|
147 | */
|
---|
148 | @Override
|
---|
149 | public void endElement(String uri, String localName, String qName)
|
---|
150 | throws SAXException {
|
---|
151 | if (qName.equals("event")) {
|
---|
152 | currentSequence.add(currentEvent);
|
---|
153 | } else if (qName.equals("source")) {
|
---|
154 | paramSource = ParamSource.EVENT;
|
---|
155 | } else if (qName.equals("parent")) {
|
---|
156 | paramSource = ParamSource.SOURCE;
|
---|
157 | } else if (qName.equals("sessions")) {
|
---|
158 | if(currentSequence!=null && !currentSequence.isEmpty() ) {
|
---|
159 | sequences.add(currentSequence);
|
---|
160 | }
|
---|
161 | currentSequence = null;
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * <p>
|
---|
167 | * Parses a log file written by the JFCMonitor and creates a collection of
|
---|
168 | * event sequences.
|
---|
169 | * </p>
|
---|
170 | *
|
---|
171 | * @param filename
|
---|
172 | * name and path of the log file
|
---|
173 | */
|
---|
174 | public void parseFile(String filename) {
|
---|
175 | if (filename == null) {
|
---|
176 | throw new InvalidParameterException("filename must not be null");
|
---|
177 | }
|
---|
178 |
|
---|
179 | SAXParserFactory spf = SAXParserFactory.newInstance();
|
---|
180 | spf.setValidating(true);
|
---|
181 |
|
---|
182 | SAXParser saxParser = null;
|
---|
183 | InputSource inputSource = null;
|
---|
184 | try {
|
---|
185 | saxParser = spf.newSAXParser();
|
---|
186 | inputSource = new InputSource(new InputStreamReader(
|
---|
187 | new FileInputStream(filename), "UTF-16"));
|
---|
188 | } catch (UnsupportedEncodingException e) {
|
---|
189 | e.printStackTrace();
|
---|
190 | } catch (ParserConfigurationException e) {
|
---|
191 | e.printStackTrace();
|
---|
192 | } catch (SAXException e) {
|
---|
193 | e.printStackTrace();
|
---|
194 | } catch (FileNotFoundException e) {
|
---|
195 | e.printStackTrace();
|
---|
196 | }
|
---|
197 | if (inputSource != null) {
|
---|
198 | inputSource.setSystemId("file://"
|
---|
199 | + new File(filename).getAbsolutePath());
|
---|
200 | try {
|
---|
201 | if (saxParser == null) {
|
---|
202 | throw new RuntimeException("SAXParser creation failed");
|
---|
203 | }
|
---|
204 | saxParser.parse(inputSource, this);
|
---|
205 | } catch (SAXParseException e) {
|
---|
206 | Console.printerrln("Failure parsing file in line "
|
---|
207 | + e.getLineNumber() + ", column " + e.getColumnNumber()
|
---|
208 | + ".");
|
---|
209 | e.printStackTrace();
|
---|
210 | } catch (SAXException e) {
|
---|
211 | e.printStackTrace();
|
---|
212 | } catch (IOException e) {
|
---|
213 | e.printStackTrace();
|
---|
214 | }
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | }
|
---|