package de.ugoe.cs.quest.plugin.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.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; 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.quest.eventcore.Event; import de.ugoe.cs.quest.eventcore.gui.IInteraction; import de.ugoe.cs.quest.eventcore.gui.InteractionEventList; import de.ugoe.cs.quest.eventcore.gui.KeyPressed; import de.ugoe.cs.quest.eventcore.gui.KeyReleased; import de.ugoe.cs.quest.eventcore.gui.KeyboardFocusChange; import de.ugoe.cs.quest.eventcore.gui.MouseButtonDown; import de.ugoe.cs.quest.eventcore.gui.MouseButtonInteraction; import de.ugoe.cs.quest.eventcore.gui.MouseButtonUp; import de.ugoe.cs.quest.eventcore.gui.MouseClick; import de.ugoe.cs.quest.eventcore.guimodel.GUIElementFactory; import de.ugoe.cs.quest.eventcore.guimodel.GUIModel; import de.ugoe.cs.quest.eventcore.guimodel.GUIModelException; import de.ugoe.cs.quest.eventcore.guimodel.IGUIElement; import de.ugoe.cs.quest.plugin.jfc.eventcore.JFCEvent; import de.ugoe.cs.quest.plugin.jfc.eventcore.JFCEventId; import de.ugoe.cs.quest.plugin.jfc.guimodel.JFCGUIElementSpec; import de.ugoe.cs.tasktree.keyboardmaps.VirtualKey; 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 id of the event that is currently being parsed. *

*/ private JFCEventId currentEventId; /** *

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

*/ private Map currentEventParameters; /** *

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

*/ private Map currentSourceParameters; /** *

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

*/ private Map currentParentParameters; /** *

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

*/ private InteractionEventList currentSequence; /** *

* internal handle to the parameters currently parsed for a component *

*/ private JFCGUIElementSpec currentGuiElementSpec; /** *

* internal handle to the last parsed component *

*/ private List currentGuiElementPath = new ArrayList(); /** *

* internal handle to the component of the previous event to be potentially reused for the * current *

*/ private IGUIElement lastGUIElement; /** *

* the model of the GUI elements, that were found during parsing *

*/ private GUIModel guiModel = new GUIModel(); /** *

* this is used to check, if for every pressed key, there is a release of it *

*/ private List mPressedKeys = new ArrayList(); /** *

* 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; /** *

* a specification for event ids to be omitted by the parser *

*/ private Collection eventFilter; /** *

* Constructor. Creates a new JFCLogParser with a default event filter. This ignores focus * events, mouse pressed, and mouse released events. *

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

* Constructor. Creates a new JFCLogParser with a specific event filter. The events in the * provided collection are ignored by the parser. As events, the constants of the different * event classes must be used. E.g. creating a collection and putting * MouseEvent.MOUSE_PRESSED will cause the parser to ignore all mouse pressed * events. If the provided collection is null, no event is ignored. *

* * @param ignoredEvents * the events to be ignored by the parser, can be null */ public JFCLogParser(Collection ignoredEvents) { sequences = new LinkedList>(); currentSequence = new InteractionEventList(); eventFilter = ignoredEvents; } /** *

* 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"); } parseFile(new File(filename)); } /** *

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

* * @param file * name and path of the log file */ public void parseFile(File file) { if (file == null) { throw new InvalidParameterException("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) { // TODO handle Exception e.printStackTrace(); } catch (ParserConfigurationException e) { // TODO handle Exception e.printStackTrace(); } catch (SAXException e) { // TODO handle Exception e.printStackTrace(); } catch (FileNotFoundException e) { // TODO handle Exception e.printStackTrace(); } 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() + "."); e.printStackTrace(); } catch (SAXException e) { // TODO handle Exception e.printStackTrace(); } catch (IOException e) { // TODO handle Exception e.printStackTrace(); } } } /** *

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

* * @return collection of event sequences */ public Collection> getSequences() { return sequences; } /** *

* Returns the gui model that is obtained from parsing log files. *

* * @return collection of event sequences */ public GUIModel getGuiModel() { return guiModel; } /* * (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("sessions")) { currentSequence = new InteractionEventList(); } if (qName.equals("newsession")) { Console.traceln("start of session"); if (currentSequence != null && !currentSequence.isEmpty()) { // create a copy of the list just to have a correctly typed one. sequences.add(currentSequence.subList(0, currentSequence.size() - 1)); } currentSequence = new InteractionEventList(); } else if (qName.equals("event")) { JFCEventId eventId = JFCEventId.parseEventId(atts.getValue("id")); if ((eventFilter == null) || (!eventFilter.contains(eventId))) { currentEventId = eventId; currentEventParameters = new HashMap(); currentSourceParameters = new HashMap(); currentParentParameters = new HashMap(); paramSource = ParamSource.EVENT; } } else if (currentEventId != null) { if (qName.equals("param")) { if (paramSource == ParamSource.EVENT) { currentEventParameters.put(atts.getValue("name"), atts.getValue("value")); } else if (paramSource == ParamSource.SOURCE) { currentSourceParameters.put(atts.getValue("name"), atts.getValue("value")); } else if (paramSource == ParamSource.PARENT) { currentParentParameters.put(atts.getValue("name"), atts.getValue("value")); } else if (paramSource == ParamSource.COMPONENT) { if ("title".equals(atts.getValue("name"))) { currentGuiElementSpec.setName(atts.getValue("value")); } else if ("class".equals(atts.getValue("name"))) { currentGuiElementSpec.setType(atts.getValue("value")); } else if ("icon".equals(atts.getValue("name"))) { currentGuiElementSpec.setIcon(atts.getValue("value")); } else if ("index".equals(atts.getValue("name"))) { currentGuiElementSpec.setIndex(Integer.parseInt(atts.getValue("value"))); } else if ("hash".equals(atts.getValue("name"))) { currentGuiElementSpec.setElementHash(atts.getValue("value")); } } } else if (qName.equals("source")) { paramSource = ParamSource.SOURCE; } else if (qName.equals("parent")) { paramSource = ParamSource.PARENT; } else if (qName.equals("component")) { paramSource = ParamSource.COMPONENT; currentGuiElementSpec = new JFCGUIElementSpec(); } } } /* * (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("sessions")) { if (currentSequence != null && !currentSequence.isEmpty()) { sequences.add(currentSequence); } currentSequence = null; } else if (currentEventId != null) { if (qName.equals("event")) { if (currentGuiElementPath.size() <= 0) { // no component specification available. Try to parse the GUI element from // the toString parameter currentGuiElementSpec = new JFCGUIElementSpec(); getGUIElementSpecFromToString(currentSourceParameters.get("toString")); currentGuiElementPath.add(currentGuiElementSpec); currentGuiElementSpec = null; } IGUIElement currentGUIElement; try { // TODO right now, there is null pointer exception possible, if the factory cannot create a the GUIElement. We need to devise where and how this should be handled best currentGUIElement = guiModel.integratePath (currentGuiElementPath, GUIElementFactory.getInstance()); } catch (GUIModelException e) { throw new SAXException("error in the GUI model provided in the log", e); } JFCEvent event = new JFCEvent (instantiateInteraction(currentEventId, currentEventParameters), (currentGUIElement == null ? lastGUIElement : currentGUIElement), currentEventParameters, currentSourceParameters, currentParentParameters); currentSequence.add(event); currentEventParameters = null; currentSourceParameters = null; currentParentParameters = null; currentGuiElementSpec = null; currentGuiElementPath.clear(); currentEventId = null; if (currentGUIElement != null) { lastGUIElement = currentGUIElement; } currentGUIElement = null; } else if (qName.equals("source")) { paramSource = ParamSource.EVENT; } else if (qName.equals("parent")) { paramSource = ParamSource.SOURCE; } else if (qName.equals("component")) { paramSource.equals(ParamSource.SOURCE); currentGuiElementPath.add(currentGuiElementSpec); currentGuiElementSpec = null; } } } /** *

* depending on the event id and the event parameters, this method instantiates the concrete * interaction, that took place, i.e. the event type *

* * @param eventId * the id of the event * @param eventParameters * the parameters provided for the event * * @return as described * * @throws SAXException thrown if the provided event id is unknown */ private IInteraction instantiateInteraction(JFCEventId eventId, Map eventParameters) throws SAXException { switch (eventId) { case FOCUS_GAINED: return handleNewFocus(eventId, eventParameters); case KEY_PRESSED: case KEY_RELEASED: case KEY_TYPED: return handleKeyAction(eventId, eventParameters); case MOUSE_CLICKED: case MOUSE_PRESSED: case MOUSE_RELEASED: case MOUSE_MOVED: case MOUSE_ENTERED: case MOUSE_EXITED: case MOUSE_DRAGGED: case MOUSE_WHEEL: return handleMouseAction(eventId, eventParameters); default: throw new SAXException("unhandled event id " + eventId); } } /** *

* handles a mouse interaction. The method determines based on the event id and the parameters * which mouse button is pressed, released or clicked. *

* * @param eventId * the id of the event * @param eventParameters * the parameters provided for the event * * @return as described * * @throws SAXException thrown if the provided event id or button index is unknown */ private IInteraction handleMouseAction(JFCEventId eventId, Map eventParameters) throws SAXException { MouseButtonInteraction.Button button = null; if (eventParameters.get("Button") != null) { int buttonId = Integer.parseInt(eventParameters.get("Button")); if (buttonId == MouseEvent.BUTTON1) { button = MouseButtonInteraction.Button.LEFT; } else if (buttonId == MouseEvent.BUTTON2) { button = MouseButtonInteraction.Button.MIDDLE; } else if (buttonId == MouseEvent.BUTTON3) { button = MouseButtonInteraction.Button.RIGHT; } else { throw new SAXException("unknown mouse button index " + buttonId); } } if (JFCEventId.MOUSE_CLICKED == eventId) { return new MouseClick(button); } else if (JFCEventId.MOUSE_PRESSED == eventId) { return new MouseButtonDown(button); } else if (JFCEventId.MOUSE_RELEASED == eventId) { return new MouseButtonUp(button); } else { throw new SAXException("unknown event id " + eventId); } } /** *

* handles a keyboard interaction. The method determines based on the event id and the * parameters which key on the keyboard is pressed or released. It further checks, if for * every released key there is also a pressed event *

* * @param eventId * the id of the event * @param eventParameters * the parameters provided for the event * * @return as described * * @throws SAXException thrown if the provided event id is unknown or if there is a key * release without a preceding press of the same key */ private IInteraction handleKeyAction(JFCEventId eventId, Map eventParameters) throws SAXException { // TODO handle shortcuts if (JFCEventId.KEY_PRESSED == eventId) { VirtualKey key = VirtualKey.parseVirtualKey(eventParameters.get("KeyCode")); mPressedKeys.add(key); return new KeyPressed(key); } else if (JFCEventId.KEY_RELEASED == eventId) { VirtualKey key = VirtualKey.parseVirtualKey(eventParameters.get("KeyCode")); if (mPressedKeys.contains(key)) { mPressedKeys.remove(key); } else { throw new SAXException ("log file has an error, as it contains a key up event on key " + key + " for which there is no preceeding key down event"); } return new KeyReleased(key); } throw new SAXException("unknown event id " + eventId); } /** *

* handles explicit keyboard focus changes. *

* * @param eventId * the id of the event * @param eventParameters * the parameters provided for the event * * @return as described * * @throws SAXException thrown if the provided event id is unknown */ private IInteraction handleNewFocus(JFCEventId eventId, Map eventParameters) throws SAXException { if (JFCEventId.FOCUS_GAINED == eventId) { return new KeyboardFocusChange(); } else { throw new SAXException("unknown event id " + eventId); } } /** *

* for some events in the log file, no component specification is provided. In this case the * GUI element on which the event is executed must be determined based on the * toString parameter of the event. This is achieved through this method. The * toString parameter does not always carry sufficient information for the GUI * elements. For example the title is not necessarily provided. Therefore some of this * information is generated. *

* * @param toStringValue * the toString parameter of the event to be parsed for the GUI element * * @return the appropriate GUI Element * * @throws SAXException thrown if the provided value of the toString parameter * can not be parsed */ private void getGUIElementSpecFromToString(String toStringValue) throws SAXException { try { String pattern = "([\\w$\\.]*)\\[.*\\]"; Matcher matcher1 = Pattern.compile(pattern).matcher(toStringValue); if (!matcher1.find()) { throw new IllegalArgumentException ("could not parse target from toString parameter"); } String type = matcher1.group(1); currentGuiElementSpec.setName("unknown"); currentGuiElementSpec.setType(type); currentGuiElementSpec.setIcon("unknown"); currentGuiElementSpec.setIndex(-1); currentGuiElementSpec.setElementHash("-1"); } catch (Exception e) { throw new SAXException("could not parse target", e); } } /** *

* creates a default event filter that ignores focus changes, mouse pressed and mouse released * events. *

*/ private void setupDefaultEventFilter() { eventFilter = new HashSet(); eventFilter.add(JFCEventId.MOUSE_PRESSED); eventFilter.add(JFCEventId.MOUSE_RELEASED); eventFilter.add(JFCEventId.FOCUS_GAINED); } }