package de.ugoe.cs.eventbench.jfc; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.security.InvalidParameterException; import java.util.Collection; import java.util.LinkedList; import java.util.List; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.helpers.DefaultHandler; import de.ugoe.cs.eventbench.jfc.data.JFCEvent; import de.ugoe.cs.util.console.Console; /** *

* This class provides functionality to parse XML log files generated by the * JFCMonitor of EventBench. The result of parsing a file is a collection of * event sequences. *

* * @author Steffen Herbold * @version 1.0 */ public class JFCLogParser extends DefaultHandler { /** *

* Collection of event sequences that is contained in the log file, which is * parsed. *

*/ private Collection> sequences; /** *

* Internal handle to the event that is currently being parsed. *

*/ private JFCEvent currentEvent; /** *

* Internal handle to the event sequence that is currently being parsed. *

*/ private List currentSequence = null; /** *

* Enumeration to differentiate if a parameter belongs to an event, a source * or the parent of a source. *

* * @author Steffen Herbold * @version 1.0 */ private enum ParamSource { EVENT, SOURCE, PARENT }; /** *

* Specifies whether the parameters that are currently being read belong the * the event, the source or the parent. *

*/ ParamSource paramSource = null; /** *

* Constructor. Creates a new JFCLogParser. *

*/ public JFCLogParser() { sequences = new LinkedList>(); } /** *

* Returns the collection of event sequences that is obtained from parsing * log files. *

* * @return collection of event sequences */ public Collection> getSequences() { return sequences; } /* * (non-Javadoc) * * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, * java.lang.String, java.lang.String, org.xml.sax.Attributes) */ public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { if (qName.equals("newsession")) { Console.traceln("start of session"); if (currentSequence != null && currentSequence.size() != 0) { sequences.add(currentSequence); currentSequence = new LinkedList(); } } else if (qName.equals("event")) { currentEvent = new JFCEvent(atts.getValue("id")); paramSource = ParamSource.EVENT; } else if (qName.equals("param")) { if (paramSource == ParamSource.EVENT) { currentEvent.addParameter(atts.getValue("name"), atts.getValue("value")); } else if (paramSource == ParamSource.SOURCE) { currentEvent.addSourceInformation(atts.getValue("name"), atts.getValue("value")); } else if (paramSource == ParamSource.PARENT) { currentEvent.addParentInformation(atts.getValue("name"), atts.getValue("value")); } } else if (qName.equals("source")) { paramSource = ParamSource.SOURCE; } else if (qName.equals("parent")) { paramSource = ParamSource.PARENT; } } /* * (non-Javadoc) * * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, * java.lang.String, java.lang.String) */ @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (qName.equals("event")) { currentSequence.add(currentEvent); } else if (qName.equals("source")) { paramSource = ParamSource.EVENT; } else if (qName.equals("parent")) { paramSource = ParamSource.SOURCE; } } /** *

* Parses a log file written by the JFCMonitor and creates a collection of * event sequences. *

* * @param filename * name and path of the log file */ public void parseFile(String filename) { if (filename == null) { throw new InvalidParameterException("filename must not be null"); } SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setValidating(true); SAXParser saxParser = null; InputSource inputSource = null; try { saxParser = spf.newSAXParser(); inputSource = new InputSource(new InputStreamReader( new FileInputStream(filename), "UTF-16")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } if (inputSource != null) { inputSource.setSystemId("file://" + new File(filename).getAbsolutePath()); try { if (saxParser == null) { throw new RuntimeException("SAXParser creation failed"); } saxParser.parse(inputSource, this); } catch (SAXParseException e) { Console.printerrln("Failure parsing file in line " + e.getLineNumber() + ", column " + e.getColumnNumber() + "."); e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } }