package de.ugoe.cs.eventbench.jfc; import java.awt.event.MouseEvent; 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.HashSet; 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* 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* Internally used string to build a string representing a component-node. *
*/ private String componentString; /** ** 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, COMPONENT }; /** ** Specifies whether the parameters that are currently being read belong the * the event, the source or the parent. *
*/ ParamSource paramSource = null; private Collection* 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* 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(); } } } }