source: trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/jfc/JFCLogParser.java @ 305

Last change on this file since 305 was 305, checked in by sherbold, 13 years ago
  • fixed typo in package names
  • Property svn:mime-type set to text/plain
File size: 5.6 KB
Line 
1package de.ugoe.cs.eventbench.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.LinkedList;
12import java.util.List;
13
14import javax.xml.parsers.ParserConfigurationException;
15import javax.xml.parsers.SAXParser;
16import javax.xml.parsers.SAXParserFactory;
17
18import org.xml.sax.Attributes;
19import org.xml.sax.InputSource;
20import org.xml.sax.SAXException;
21import org.xml.sax.SAXParseException;
22import org.xml.sax.helpers.DefaultHandler;
23
24import de.ugoe.cs.eventbench.jfc.data.JFCEvent;
25import 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 */
37public 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        }
90
91        /**
92         * <p>
93         * Returns the collection of event sequences that is obtained from parsing
94         * log files.
95         * </p>
96         *
97         * @return collection of event sequences
98         */
99        public Collection<List<JFCEvent>> getSequences() {
100                return sequences;
101        }
102
103        /*
104         * (non-Javadoc)
105         *
106         * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String,
107         * java.lang.String, java.lang.String, org.xml.sax.Attributes)
108         */
109        public void startElement(String uri, String localName, String qName,
110                        Attributes atts) throws SAXException {
111                if (qName.equals("newsession")) {
112                        Console.traceln("start of session");
113                        if (currentSequence != null && currentSequence.size() != 0) {
114                                sequences.add(currentSequence);
115                                currentSequence = new LinkedList<JFCEvent>();
116                        }
117                } else if (qName.equals("event")) {
118                        currentEvent = new JFCEvent(atts.getValue("id"));
119                        paramSource = ParamSource.EVENT;
120                } else if (qName.equals("param")) {
121                        if (paramSource == ParamSource.EVENT) {
122                                currentEvent.addParameter(atts.getValue("name"),
123                                                atts.getValue("value"));
124                        } else if (paramSource == ParamSource.SOURCE) {
125                                currentEvent.addSourceInformation(atts.getValue("name"),
126                                                atts.getValue("value"));
127                        } else if (paramSource == ParamSource.PARENT) {
128                                currentEvent.addParentInformation(atts.getValue("name"),
129                                                atts.getValue("value"));
130                        }
131                } else if (qName.equals("source")) {
132                        paramSource = ParamSource.SOURCE;
133                } else if (qName.equals("parent")) {
134                        paramSource = ParamSource.PARENT;
135                }
136        }
137
138        /*
139         * (non-Javadoc)
140         *
141         * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String,
142         * java.lang.String, java.lang.String)
143         */
144        @Override
145        public void endElement(String uri, String localName, String qName)
146                        throws SAXException {
147                if (qName.equals("event")) {
148                        currentSequence.add(currentEvent);
149                } else if (qName.equals("source")) {
150                        paramSource = ParamSource.EVENT;
151                } else if (qName.equals("parent")) {
152                        paramSource = ParamSource.SOURCE;
153                }
154        }
155
156        /**
157         * <p>
158         * Parses a log file written by the JFCMonitor and creates a collection of
159         * event sequences.
160         * </p>
161         *
162         * @param filename
163         *            name and path of the log file
164         */
165        public void parseFile(String filename) {
166                if (filename == null) {
167                        throw new InvalidParameterException("filename must not be null");
168                }
169
170                SAXParserFactory spf = SAXParserFactory.newInstance();
171                spf.setValidating(true);
172
173                SAXParser saxParser = null;
174                InputSource inputSource = null;
175                try {
176                        saxParser = spf.newSAXParser();
177                        inputSource = new InputSource(new InputStreamReader(
178                                        new FileInputStream(filename), "UTF-16"));
179                } catch (UnsupportedEncodingException e) {
180                        e.printStackTrace();
181                } catch (ParserConfigurationException e) {
182                        e.printStackTrace();
183                } catch (SAXException e) {
184                        e.printStackTrace();
185                } catch (FileNotFoundException e) {
186                        e.printStackTrace();
187                }
188                if (inputSource != null) {
189                        inputSource.setSystemId("file://"
190                                        + new File(filename).getAbsolutePath());
191                        try {
192                                if (saxParser == null) {
193                                        throw new RuntimeException("SAXParser creation failed");
194                                }
195                                saxParser.parse(inputSource, this);
196                        } catch (SAXParseException e) {
197                                Console.printerrln("Failure parsing file in line "
198                                                + e.getLineNumber() + ", column " + e.getColumnNumber()
199                                                + ".");
200                                e.printStackTrace();
201                        } catch (SAXException e) {
202                                e.printStackTrace();
203                        } catch (IOException e) {
204                                e.printStackTrace();
205                        }
206                }
207        }
208
209}
Note: See TracBrowser for help on using the repository browser.