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

Last change on this file since 389 was 389, checked in by sherbold, 12 years ago
  • the commands parseDirJFC and parseJFC now pre-compute the equality of JFC target strings after loading data.
  • focus events now filtered from models
  • Property svn:mime-type set to text/plain
File size: 7.1 KB
Line 
1package de.ugoe.cs.eventbench.jfc;
2
3import java.awt.event.FocusEvent;
4import java.awt.event.MouseEvent;
5import java.io.File;
6import java.io.FileInputStream;
7import java.io.FileNotFoundException;
8import java.io.IOException;
9import java.io.InputStreamReader;
10import java.io.UnsupportedEncodingException;
11import java.security.InvalidParameterException;
12import java.util.Collection;
13import java.util.HashSet;
14import java.util.LinkedList;
15import java.util.List;
16
17import javax.xml.parsers.ParserConfigurationException;
18import javax.xml.parsers.SAXParser;
19import javax.xml.parsers.SAXParserFactory;
20
21import org.xml.sax.Attributes;
22import org.xml.sax.InputSource;
23import org.xml.sax.SAXException;
24import org.xml.sax.SAXParseException;
25import org.xml.sax.helpers.DefaultHandler;
26
27import de.ugoe.cs.eventbench.jfc.data.JFCEvent;
28import de.ugoe.cs.util.console.Console;
29
30/**
31 * <p>
32 * This class provides functionality to parse XML log files generated by the
33 * JFCMonitor of EventBench. The result of parsing a file is a collection of
34 * event sequences.
35 * </p>
36 *
37 * @author Steffen Herbold
38 * @version 1.0
39 */
40public class JFCLogParser extends DefaultHandler {
41
42        /**
43         * <p>
44         * Collection of event sequences that is contained in the log file, which is
45         * parsed.
46         * </p>
47         */
48        private Collection<List<JFCEvent>> sequences;
49
50        /**
51         * <p>
52         * Internal handle to the event that is currently being parsed.
53         * </p>
54         */
55        private JFCEvent currentEvent;
56
57        /**
58         * <p>
59         * Internal handle to the event sequence that is currently being parsed.
60         * </p>
61         */
62        private List<JFCEvent> currentSequence = null;
63
64        /**
65         * <p>
66         * Internally used string to build a string representing a component-node.
67         * </p>
68         */
69        private String componentString;
70
71        /**
72         * <p>
73         * Enumeration to differentiate if a parameter belongs to an event, a source
74         * or the parent of a source.
75         * </p>
76         *
77         * @author Steffen Herbold
78         * @version 1.0
79         */
80        private enum ParamSource {
81                EVENT, SOURCE, PARENT, COMPONENT
82        };
83
84        /**
85         * <p>
86         * Specifies whether the parameters that are currently being read belong the
87         * the event, the source or the parent.
88         * </p>
89         */
90        ParamSource paramSource = null;
91
92        private Collection<Integer> eventFilter;
93
94        /**
95         * <p>
96         * Constructor. Creates a new JFCLogParser.
97         * </p>
98         */
99        public JFCLogParser() {
100                sequences = new LinkedList<List<JFCEvent>>();
101                currentSequence = new LinkedList<JFCEvent>();
102                setupEventFilter();
103        }
104
105        private void setupEventFilter() {
106                eventFilter = new HashSet<Integer>();
107                eventFilter.add(MouseEvent.MOUSE_PRESSED);
108                eventFilter.add(MouseEvent.MOUSE_RELEASED);
109                eventFilter.add(FocusEvent.FOCUS_GAINED);
110        }
111
112        /**
113         * <p>
114         * Returns the collection of event sequences that is obtained from parsing
115         * log files.
116         * </p>
117         *
118         * @return collection of event sequences
119         */
120        public Collection<List<JFCEvent>> getSequences() {
121                return sequences;
122        }
123
124        /*
125         * (non-Javadoc)
126         *
127         * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String,
128         * java.lang.String, java.lang.String, org.xml.sax.Attributes)
129         */
130        public void startElement(String uri, String localName, String qName,
131                        Attributes atts) throws SAXException {
132                if (qName.equals("sessions")) {
133                        currentSequence = new LinkedList<JFCEvent>();
134                }
135                if (qName.equals("newsession")) {
136                        Console.traceln("start of session");
137                        if (currentSequence != null && !currentSequence.isEmpty()) {
138                                sequences.add(currentSequence);
139                        }
140                        currentSequence = new LinkedList<JFCEvent>();
141                } else if (qName.equals("event")) {
142                        int eventId = Integer.parseInt(atts.getValue("id"));
143                        if (!eventFilter.contains(eventId)) {
144                                currentEvent = new JFCEvent(atts.getValue("id"));
145                                paramSource = ParamSource.EVENT;
146                        }
147                } else if (currentEvent != null) {
148                        if (qName.equals("param")) {
149                                if (paramSource == ParamSource.EVENT) {
150                                        currentEvent.addParameter(atts.getValue("name"),
151                                                        atts.getValue("value"));
152                                } else if (paramSource == ParamSource.SOURCE) {
153                                        currentEvent.addSourceInformation(atts.getValue("name"),
154                                                        atts.getValue("value"));
155                                } else if (paramSource == ParamSource.PARENT) {
156                                        currentEvent.addParentInformation(atts.getValue("name"),
157                                                        atts.getValue("value"));
158                                } else if (paramSource == ParamSource.COMPONENT) {
159                                        componentString += "'" + atts.getValue("value") + "',";
160                                }
161                        } else if (qName.equals("source")) {
162                                paramSource = ParamSource.SOURCE;
163                        } else if (qName.equals("parent")) {
164                                paramSource = ParamSource.PARENT;
165                        } else if (qName.equals("component")) {
166                                paramSource = ParamSource.COMPONENT;
167                                componentString = "[";
168                        }
169                }
170        }
171
172        /*
173         * (non-Javadoc)
174         *
175         * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String,
176         * java.lang.String, java.lang.String)
177         */
178        @Override
179        public void endElement(String uri, String localName, String qName)
180                        throws SAXException {
181                if (qName.equals("sessions")) {
182                        if (currentSequence != null && !currentSequence.isEmpty()) {
183                                sequences.add(currentSequence);
184                        }
185                        currentSequence = null;
186                } else if (currentEvent != null) {
187                        if (qName.equals("event")) {
188                                currentSequence.add(currentEvent);
189                                currentEvent = null;
190                        } else if (qName.equals("source")) {
191                                paramSource = ParamSource.EVENT;
192                        } else if (qName.equals("parent")) {
193                                paramSource = ParamSource.SOURCE;
194                        } else if (qName.equals("component")) {
195                                paramSource.equals(ParamSource.SOURCE);
196                                currentEvent.extendTarget(componentString.substring(0,
197                                                componentString.length() - 1) + "]");
198                        }
199                }
200        }
201
202        /**
203         * <p>
204         * Parses a log file written by the JFCMonitor and creates a collection of
205         * event sequences.
206         * </p>
207         *
208         * @param filename
209         *            name and path of the log file
210         */
211        public void parseFile(String filename) {
212                if (filename == null) {
213                        throw new InvalidParameterException("filename must not be null");
214                }
215
216                SAXParserFactory spf = SAXParserFactory.newInstance();
217                spf.setValidating(true);
218
219                SAXParser saxParser = null;
220                InputSource inputSource = null;
221                try {
222                        saxParser = spf.newSAXParser();
223                        inputSource = new InputSource(new InputStreamReader(
224                                        new FileInputStream(filename), "UTF-16"));
225                } catch (UnsupportedEncodingException e) {
226                        e.printStackTrace();
227                } catch (ParserConfigurationException e) {
228                        e.printStackTrace();
229                } catch (SAXException e) {
230                        e.printStackTrace();
231                } catch (FileNotFoundException e) {
232                        e.printStackTrace();
233                }
234                if (inputSource != null) {
235                        inputSource.setSystemId("file://"
236                                        + new File(filename).getAbsolutePath());
237                        try {
238                                if (saxParser == null) {
239                                        throw new RuntimeException("SAXParser creation failed");
240                                }
241                                saxParser.parse(inputSource, this);
242                        } catch (SAXParseException e) {
243                                Console.printerrln("Failure parsing file in line "
244                                                + e.getLineNumber() + ", column " + e.getColumnNumber()
245                                                + ".");
246                                e.printStackTrace();
247                        } catch (SAXException e) {
248                                e.printStackTrace();
249                        } catch (IOException e) {
250                                e.printStackTrace();
251                        }
252                }
253        }
254
255}
Note: See TracBrowser for help on using the repository browser.