[305] | 1 | package de.ugoe.cs.eventbench.jfc;
|
---|
[299] | 2 |
|
---|
[369] | 3 | import java.awt.event.MouseEvent;
|
---|
[299] | 4 | import java.io.File;
|
---|
| 5 | import java.io.FileInputStream;
|
---|
| 6 | import java.io.FileNotFoundException;
|
---|
| 7 | import java.io.IOException;
|
---|
| 8 | import java.io.InputStreamReader;
|
---|
| 9 | import java.io.UnsupportedEncodingException;
|
---|
| 10 | import java.security.InvalidParameterException;
|
---|
| 11 | import java.util.Collection;
|
---|
[369] | 12 | import java.util.HashSet;
|
---|
[300] | 13 | import java.util.LinkedList;
|
---|
[299] | 14 | import java.util.List;
|
---|
| 15 |
|
---|
| 16 | import javax.xml.parsers.ParserConfigurationException;
|
---|
| 17 | import javax.xml.parsers.SAXParser;
|
---|
| 18 | import javax.xml.parsers.SAXParserFactory;
|
---|
| 19 |
|
---|
| 20 | import org.xml.sax.Attributes;
|
---|
| 21 | import org.xml.sax.InputSource;
|
---|
| 22 | import org.xml.sax.SAXException;
|
---|
| 23 | import org.xml.sax.SAXParseException;
|
---|
| 24 | import org.xml.sax.helpers.DefaultHandler;
|
---|
| 25 |
|
---|
[305] | 26 | import de.ugoe.cs.eventbench.jfc.data.JFCEvent;
|
---|
[299] | 27 | import 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 | */
|
---|
| 39 | public 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 | */
|
---|
[300] | 47 | private Collection<List<JFCEvent>> sequences;
|
---|
[299] | 48 |
|
---|
| 49 | /**
|
---|
| 50 | * <p>
|
---|
[300] | 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;
|
---|
[369] | 62 |
|
---|
[359] | 63 | /**
|
---|
| 64 | * <p>
|
---|
| 65 | * Internally used string to build a string representing a component-node.
|
---|
| 66 | * </p>
|
---|
| 67 | */
|
---|
| 68 | private String componentString;
|
---|
[300] | 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 {
|
---|
[359] | 80 | EVENT, SOURCE, PARENT, COMPONENT
|
---|
[300] | 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 |
|
---|
[369] | 91 | private Collection<Integer> eventFilter;
|
---|
| 92 |
|
---|
[300] | 93 | /**
|
---|
| 94 | * <p>
|
---|
[301] | 95 | * Constructor. Creates a new JFCLogParser.
|
---|
| 96 | * </p>
|
---|
| 97 | */
|
---|
| 98 | public JFCLogParser() {
|
---|
| 99 | sequences = new LinkedList<List<JFCEvent>>();
|
---|
[317] | 100 | currentSequence = new LinkedList<JFCEvent>();
|
---|
[369] | 101 | setupEventFilter();
|
---|
[301] | 102 | }
|
---|
| 103 |
|
---|
[369] | 104 | private void setupEventFilter() {
|
---|
| 105 | eventFilter = new HashSet<Integer>();
|
---|
| 106 | eventFilter.add(MouseEvent.MOUSE_PRESSED);
|
---|
| 107 | eventFilter.add(MouseEvent.MOUSE_RELEASED);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[301] | 110 | /**
|
---|
| 111 | * <p>
|
---|
[299] | 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 {
|
---|
[369] | 130 | if (qName.equals("sessions")) {
|
---|
[353] | 131 | currentSequence = new LinkedList<JFCEvent>();
|
---|
| 132 | }
|
---|
[299] | 133 | if (qName.equals("newsession")) {
|
---|
| 134 | Console.traceln("start of session");
|
---|
[317] | 135 | if (currentSequence != null && !currentSequence.isEmpty()) {
|
---|
[300] | 136 | sequences.add(currentSequence);
|
---|
| 137 | }
|
---|
[353] | 138 | currentSequence = new LinkedList<JFCEvent>();
|
---|
[299] | 139 | } else if (qName.equals("event")) {
|
---|
[369] | 140 | int eventId = Integer.parseInt(atts.getValue("id"));
|
---|
| 141 | if (!eventFilter.contains(eventId)) {
|
---|
| 142 | currentEvent = new JFCEvent(atts.getValue("id"));
|
---|
| 143 | paramSource = ParamSource.EVENT;
|
---|
[300] | 144 | }
|
---|
[369] | 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 | }
|
---|
[299] | 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 {
|
---|
[369] | 179 | if (qName.equals("sessions")) {
|
---|
| 180 | if (currentSequence != null && !currentSequence.isEmpty()) {
|
---|
[317] | 181 | sequences.add(currentSequence);
|
---|
| 182 | }
|
---|
[353] | 183 | currentSequence = null;
|
---|
[369] | 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 | }
|
---|
[299] | 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 | }
|
---|