package de.ugoe.cs.autoquest.plugin.html; 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.util.Collection; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; 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.autoquest.eventcore.Event; import de.ugoe.cs.autoquest.eventcore.guimodel.GUIElementTree; import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel; import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement; import de.ugoe.cs.autoquest.plugin.html.eventcore.HTMLEventTypeFactory; import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLGUIElement; import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLGUIElementSpec; import de.ugoe.cs.util.console.Console; /** *
* This class provides the functionality to parse XML log files generated by the HTMLMonitor of * autoquest. The result of parsing a file is a collection of event sequences. *
* * @author Fabian Glaser * @version 1.0 * */ public class NewHTMLLogParser extends DefaultHandler { /** ** Constructor. Creates a new HTMLLogParser. *
*/ public NewHTMLLogParser() { sequences = new LinkedList* Collection of event sequences that is contained in the parsed log file. *
*/ private Collection* Internal handle to the parsed GUI structure, stored in a GUIElementTree *
*/ private GUIElementTree* Path of the GUI element currently being parsed. *
*/ private String currentGUIElementPath; /** ** Path of the parent of the GUI element currently being parsed. *
*/ private String currentParentPath; /** ** Source of the GUI element currently being parsed. *
*/ private String currentEventSource; /** ** Timestamp of the event currently being parsed. *
*/ private Long currentEventTimestamp; /** ** Internal handle to the parameters of the event currently being parsed. *
*/ private Map* Internal handle to the sequence currently being parsed. *
*/ private List* Internal handle to type of the event currently being parsed. *
*/ private String currentEventType; /** ** Class of the GUI element currently being parsed. *
*/ private String currentGUIElementClass; /** ** Index of the GUI element currently being parsed. *
*/ private String currentGUIElementIndex; /** * Internal handle to the specification of the GUI element currently being parsed. */ private HTMLGUIElementSpec currentGUIElementSpec; /** ** internal handle to the GUI element of the previous event to be potentially reused for the * current *
*/ private IGUIElement lastGUIElement; /** ** Parses a log file written by the HTMLMonitor 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 IllegalArgumentException("filename must not be null"); } parseFile(new File(filename)); } /** ** Parses a log file written by the HTMLMonitor and creates a collection of event sequences. *
* @param file * file to be parsed */ public void parseFile(File file) { if (file == null){ throw new IllegalArgumentException("file 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(file), "UTF-8")); } catch (UnsupportedEncodingException e){ Console.printerr("Error parsing file " + file.getName()); Console.logException(e); return; } catch (ParserConfigurationException e){ Console.printerr("Error parsing file " + file.getName()); Console.logException(e); return; } catch (SAXException e){ Console.printerr("Error parsing file " + file.getName()); Console.logException(e); } catch (FileNotFoundException e){ Console.printerr("Error parsing file " + file.getName()); Console.logException(e); } if (inputSource != null){ inputSource.setSystemId("file://" + file.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() + "."); Console.logException(e); } catch (SAXException e){ Console.printerr("Error parsing file " + file.getName()); Console.logException(e); return; } catch (IOException e){ Console.printerr("Error parsing file " + file.getName()); Console.logException(e); return; } } } @Override public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { if (qName.equals("session")) { currentSequence = new LinkedList* Returns a collection of event sequences that was obtained from parsing log files. *
* @return */ public Collection* Returns the GUI model that is obtained from parsing log files. *
* * @return GUIModel */ public GUIModel getGuiModel(){ return currentGUIElementTree.getGUIModel(); } }