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

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