source: trunk/EventBenchConsole/src/de/ugoe/cs/eventbensch/jfc/JFCLogParser.java @ 299

Last change on this file since 299 was 299, checked in by sherbold, 12 years ago
  • added command parseJFC for parsing log files created with the JFCMonitor
  • added de.ugoe.cs.eventbench.jfc.JFCLogParser for parsing of log files created with the JFCMonitor (unfinished)
  • added de.ugoe.cs.eventbench.jfc.data.JFCEvent for working with JFC events
  • Property svn:mime-type set to text/plain
File size: 4.0 KB
Line 
1package de.ugoe.cs.eventbensch.jfc;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.IOException;
7import java.io.InputStreamReader;
8import java.io.UnsupportedEncodingException;
9import java.security.InvalidParameterException;
10import java.util.Collection;
11import java.util.List;
12
13import javax.xml.parsers.ParserConfigurationException;
14import javax.xml.parsers.SAXParser;
15import javax.xml.parsers.SAXParserFactory;
16
17import org.xml.sax.Attributes;
18import org.xml.sax.InputSource;
19import org.xml.sax.SAXException;
20import org.xml.sax.SAXParseException;
21import org.xml.sax.helpers.DefaultHandler;
22
23import de.ugoe.cs.eventbensch.jfc.data.JFCEvent;
24import 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 */
36public 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}
Note: See TracBrowser for help on using the repository browser.