1 | package de.ugoe.cs.eventbensch.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.List;
|
---|
12 |
|
---|
13 | import javax.xml.parsers.ParserConfigurationException;
|
---|
14 | import javax.xml.parsers.SAXParser;
|
---|
15 | import javax.xml.parsers.SAXParserFactory;
|
---|
16 |
|
---|
17 | import org.xml.sax.Attributes;
|
---|
18 | import org.xml.sax.InputSource;
|
---|
19 | import org.xml.sax.SAXException;
|
---|
20 | import org.xml.sax.SAXParseException;
|
---|
21 | import org.xml.sax.helpers.DefaultHandler;
|
---|
22 |
|
---|
23 | import de.ugoe.cs.eventbensch.jfc.data.JFCEvent;
|
---|
24 | import de.ugoe.cs.util.console.Console;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * <p>
|
---|
28 | * This class provides functionality to parse XML log files generated by the
|
---|
29 | * JFCMonitor of EventBench. The result of parsing a file is a collection of
|
---|
30 | * event sequences.
|
---|
31 | * </p>
|
---|
32 | *
|
---|
33 | * @author Steffen Herbold
|
---|
34 | * @version 1.0
|
---|
35 | */
|
---|
36 | public class JFCLogParser extends DefaultHandler {
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * <p>
|
---|
40 | * Collection of event sequences that is contained in the log file, which is
|
---|
41 | * parsed.
|
---|
42 | * </p>
|
---|
43 | */
|
---|
44 | Collection<List<JFCEvent>> sequences;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * <p>
|
---|
48 | * Returns the collection of event sequences that is obtained from parsing
|
---|
49 | * log files.
|
---|
50 | * </p>
|
---|
51 | *
|
---|
52 | * @return collection of event sequences
|
---|
53 | */
|
---|
54 | public Collection<List<JFCEvent>> getSequences() {
|
---|
55 | return sequences;
|
---|
56 | }
|
---|
57 |
|
---|
58 | /*
|
---|
59 | * (non-Javadoc)
|
---|
60 | *
|
---|
61 | * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String,
|
---|
62 | * java.lang.String, java.lang.String, org.xml.sax.Attributes)
|
---|
63 | */
|
---|
64 | public void startElement(String uri, String localName, String qName,
|
---|
65 | Attributes atts) throws SAXException {
|
---|
66 | if (qName.equals("newsession")) {
|
---|
67 | Console.traceln("start of session");
|
---|
68 | // TODO implement handling: newsession node
|
---|
69 | } else if (qName.equals("event")) {
|
---|
70 | // TODO implement handling: start of event node
|
---|
71 | } else if (qName.equals("param")) {
|
---|
72 | // TODO implement handling: param node
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | /*
|
---|
77 | * (non-Javadoc)
|
---|
78 | *
|
---|
79 | * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String,
|
---|
80 | * java.lang.String, java.lang.String)
|
---|
81 | */
|
---|
82 | @Override
|
---|
83 | public void endElement(String uri, String localName, String qName)
|
---|
84 | throws SAXException {
|
---|
85 | if (qName.equals("event")) {
|
---|
86 | // TODO implement handling: end of event node
|
---|
87 | } else if (qName.equals("source")) {
|
---|
88 | // TODO implement handling: end of source node
|
---|
89 | } else if (qName.equals("parent")) {
|
---|
90 | // TODO implement handling: end of parent node
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * <p>
|
---|
96 | * Parses a log file written by the JFCMonitor and creates a collection of
|
---|
97 | * event sequences.
|
---|
98 | * </p>
|
---|
99 | *
|
---|
100 | * @param filename
|
---|
101 | * name and path of the log file
|
---|
102 | */
|
---|
103 | public void parseFile(String filename) {
|
---|
104 | if (filename == null) {
|
---|
105 | throw new InvalidParameterException("filename must not be null");
|
---|
106 | }
|
---|
107 |
|
---|
108 | SAXParserFactory spf = SAXParserFactory.newInstance();
|
---|
109 | spf.setValidating(true);
|
---|
110 |
|
---|
111 | SAXParser saxParser = null;
|
---|
112 | InputSource inputSource = null;
|
---|
113 | try {
|
---|
114 | saxParser = spf.newSAXParser();
|
---|
115 | inputSource = new InputSource(new InputStreamReader(
|
---|
116 | new FileInputStream(filename), "UTF-16"));
|
---|
117 | } catch (UnsupportedEncodingException e) {
|
---|
118 | e.printStackTrace();
|
---|
119 | } catch (ParserConfigurationException e) {
|
---|
120 | e.printStackTrace();
|
---|
121 | } catch (SAXException e) {
|
---|
122 | e.printStackTrace();
|
---|
123 | } catch (FileNotFoundException e) {
|
---|
124 | e.printStackTrace();
|
---|
125 | }
|
---|
126 | if (inputSource != null) {
|
---|
127 | inputSource.setSystemId("file://"
|
---|
128 | + new File(filename).getAbsolutePath());
|
---|
129 | try {
|
---|
130 | if (saxParser == null) {
|
---|
131 | throw new RuntimeException("SAXParser creation failed");
|
---|
132 | }
|
---|
133 | saxParser.parse(inputSource, this);
|
---|
134 | } catch (SAXParseException e) {
|
---|
135 | Console.printerrln("Failure parsing file in line "
|
---|
136 | + e.getLineNumber() + ", column " + e.getColumnNumber()
|
---|
137 | + ".");
|
---|
138 | e.printStackTrace();
|
---|
139 | } catch (SAXException e) {
|
---|
140 | e.printStackTrace();
|
---|
141 | } catch (IOException e) {
|
---|
142 | e.printStackTrace();
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | }
|
---|