Changeset 1049


Ignore:
Timestamp:
01/31/13 18:12:55 (11 years ago)
Author:
fglaser
Message:
  • JFCSimplifiedLogParser now handles the case where events reference components that have not been parsed yet
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/autoquest-plugin-jfc/src/main/java/de/ugoe/cs/autoquest/plugin/jfc/JFCSimplifiedLogParser.java

    r1027 r1049  
     1 
    12package de.ugoe.cs.autoquest.plugin.jfc; 
    23 
     
    5859    /** 
    5960     * <p> 
     61     * Map that holds events that had no registered target GUI element during parsing. Keys are the 
     62     * IDs of the unregistered targets. 
     63     * </p> 
     64     */ 
     65    private Map<Long, List<Event>> eventsWithoutTargets; 
     66 
     67    /** 
     68     * <p> 
    6069     * Collection of event sequences that is contained in the log file, which is parsed. 
    6170     * </p> 
     
    8190     *  
    8291     * <p> 
    83      * Internal handle to the hashcode of the parent of the GUI element, that is currently parsed.  
     92     * Internal handle to the hashcode of the parent of the GUI element, that is currently parsed. 
    8493     * </p> 
    8594     */ 
     
    129138    /** 
    130139     * <p> 
    131      * internal handle to the GUI element of the previous event to be potentially reused for the 
    132      * current 
    133      * </p> 
    134      */ 
    135     private IGUIElement lastGUIElement; 
    136  
    137     /** 
    138      * <p> 
    139140     * internal handle to the class ancestors 
    140      * </p>  
     141     * </p> 
    141142     */ 
    142143    private List<String> currentTypeHierarchy; 
     
    165166        sequences = new LinkedList<List<Event>>(); 
    166167        currentSequence = null; 
    167         //setupDefaultEventFilter(); 
     168        eventsWithoutTargets = new HashMap<Long, List<Event>>(); 
     169        // setupDefaultEventFilter(); 
    168170    } 
    169171 
     
    183185        sequences = new LinkedList<List<Event>>(); 
    184186        currentSequence = null; 
     187        eventsWithoutTargets = new HashMap<Long, List<Event>>(); 
    185188        eventFilter = ignoredEvents; 
    186189    } 
     
    223226            saxParser = spf.newSAXParser(); 
    224227            inputSource = 
    225                     new InputSource(new InputStreamReader(new FileInputStream(file), "UTF-8")); 
     228                new InputSource(new InputStreamReader(new FileInputStream(file), "UTF-8")); 
    226229        } 
    227230        catch (UnsupportedEncodingException e) { 
     
    255258            catch (SAXParseException e) { 
    256259                Console.printerrln("Failure parsing file in line " + e.getLineNumber() + 
    257                                    ", column " + e.getColumnNumber() + "."); 
     260                    ", column " + e.getColumnNumber() + "."); 
    258261                Console.logException(e); 
    259262                return; 
     
    270273            } 
    271274        } 
     275        if (!eventsWithoutTargets.isEmpty()) { 
     276            Console.printerr("Some events reference GUI elments that are not part of logfile. +" 
     277                + "These events have been ignored."); 
     278        } 
    272279    } 
    273280 
     
    301308     */ 
    302309    public void startElement(String uri, String localName, String qName, Attributes atts) 
    303             throws SAXException{ 
     310        throws SAXException 
     311    { 
    304312        if (qName.equals("sessions")) { 
    305313            currentSequence = new LinkedList<Event>(); 
     
    336344        } 
    337345        else if (qName.equals("param")) { 
    338             if (currentEventId != null){ 
    339                 if ("source".equals(atts.getValue("name"))){ 
     346            if (currentEventId != null) { 
     347                if ("source".equals(atts.getValue("name"))) { 
    340348                    currentEventSource = Long.parseLong(atts.getValue("value"), 16); 
    341349                } 
    342                 if ("timestamp".equals(atts.getValue("name"))){ 
     350                if ("timestamp".equals(atts.getValue("name"))) { 
    343351                    currentEventTimestamp = Long.parseLong(atts.getValue("value")); 
    344352                } 
    345353                currentEventParameters.put(atts.getValue("name"), atts.getValue("value")); 
    346             } else if(currentGUIElementHash != null){ 
     354            } 
     355            else if (currentGUIElementHash != null) { 
    347356                if ("title".equals(atts.getValue("name"))) { 
    348357                    currentGuiElementSpec.setName(atts.getValue("value")); 
     
    362371            } 
    363372        } 
    364         else if (qName.equals("ancestor")){ 
     373        else if (qName.equals("ancestor")) { 
    365374            currentTypeHierarchy.add(atts.getValue("name")); 
    366375        } 
    367         else if (qName.equals("ancestors")){ 
     376        else if (qName.equals("ancestors")) { 
    368377            currentTypeHierarchy = new LinkedList<String>(); 
    369378        } 
     
    384393            currentSequence = null; 
    385394        } 
    386         else if (qName.equals("ancestors")){ 
     395        else if (qName.equals("ancestors")) { 
    387396            currentGuiElementSpec.setTypeHierarchy(currentTypeHierarchy); 
    388397        } 
    389398        else if (qName.equals("component") && currentGUIElementHash != null) { 
    390             currentGUIElementTree.add(currentGUIElementHash, currentParentHash, currentGuiElementSpec); 
    391  
     399            currentGUIElementTree.add(currentGUIElementHash, currentParentHash, 
     400                                      currentGuiElementSpec); 
     401            List<Event> unhandledEvents = eventsWithoutTargets.get(currentGUIElementHash); 
     402            if (unhandledEvents != null) { 
     403                JFCGUIElement guiElement = 
     404                    (JFCGUIElement) currentGUIElementTree.find(currentGUIElementHash); 
     405                for (Event event : unhandledEvents) { 
     406                    event.setTarget(guiElement); 
     407                    guiElement.markAsUsed(); 
     408                    currentSequence.add(event); 
     409                } 
     410                eventsWithoutTargets.remove(currentGUIElementHash); 
     411            } 
    392412            currentGUIElementHash = null; 
    393413            currentParentHash = null; 
     
    400420                currentGUIElement = currentGUIElementTree.find(currentEventSource); 
    401421 
    402                 Event event = new Event 
    403                         (instantiateInteraction(currentEventId, currentEventParameters), 
    404                          (currentGUIElement == null ? lastGUIElement : currentGUIElement)); 
    405                 event.setTimestamp(currentEventTimestamp); 
    406                 JFCGUIElement currentEventTarget = (JFCGUIElement) event.getTarget(); 
    407                 currentEventTarget.markAsUsed(); 
    408  
    409                 currentSequence.add(event); 
     422                // in some rare cases the target GUI element of the event is not 
     423                // known yet 
     424                if (currentGUIElement == null) { 
     425                    Event event = 
     426                        new Event(instantiateInteraction(currentEventId, currentEventParameters)); 
     427                    event.setTimestamp(currentEventTimestamp); 
     428 
     429                    List<Event> eventList = eventsWithoutTargets.get(currentEventSource); 
     430                    if (eventList == null) { 
     431                        eventList = new ArrayList<Event>(); 
     432                        eventsWithoutTargets.put(currentEventSource, eventList); 
     433                    } 
     434                    eventList.add(event); 
     435                } 
     436                else { 
     437                    Event event = 
     438                        new Event(instantiateInteraction(currentEventId, currentEventParameters), 
     439                                  currentGUIElement); 
     440                    event.setTimestamp(currentEventTimestamp); 
     441                    JFCGUIElement currentEventTarget = (JFCGUIElement) event.getTarget(); 
     442                    currentEventTarget.markAsUsed(); 
     443 
     444                    currentSequence.add(event); 
     445                } 
    410446 
    411447                currentEventParameters = null; 
    412448                currentEventId = null; 
    413449                currentEventTimestamp = -1l; 
    414  
    415                 if (currentGUIElement != null) { 
    416                     lastGUIElement = currentGUIElement; 
    417                 } 
    418  
    419                 currentGUIElement = null; 
    420450            } 
    421451        } 
     
    427457     * interaction, that took place, i.e. the event type 
    428458     * </p> 
    429      * 
     459     *  
    430460     * @param eventId 
    431461     *            the id of the event 
    432462     * @param eventParameters 
    433463     *            the parameters provided for the event 
    434      *             
     464     *  
    435465     * @return as described 
    436466     *  
    437      * @throws SAXException thrown if the provided event id is unknown 
    438      */ 
    439     private IInteraction instantiateInteraction(JFCEventId          eventId, 
     467     * @throws SAXException 
     468     *             thrown if the provided event id is unknown 
     469     */ 
     470    private IInteraction instantiateInteraction(JFCEventId eventId, 
    440471                                                Map<String, String> eventParameters) 
    441                                                         throws SAXException 
    442                                                         { 
     472        throws SAXException 
     473    { 
    443474        switch (eventId) 
    444475        { 
     
    464495                throw new SAXException("unhandled event id " + eventId); 
    465496        } 
    466                                                         } 
     497    } 
    467498 
    468499    /** 
     
    471502     * which mouse button is pressed, released or clicked. 
    472503     * </p> 
    473      * 
     504     *  
    474505     * @param eventId 
    475506     *            the id of the event 
    476507     * @param eventParameters 
    477508     *            the parameters provided for the event 
    478      *             
     509     *  
    479510     * @return as described 
    480511     *  
    481      * @throws SAXException thrown if the provided event id or button index is unknown 
     512     * @throws SAXException 
     513     *             thrown if the provided event id or button index is unknown 
    482514     */ 
    483515    private IInteraction handleMouseAction(JFCEventId eventId, Map<String, String> eventParameters) 
    484             throws SAXException 
    485             { 
     516        throws SAXException 
     517    { 
    486518        MouseButtonInteraction.Button button = null; 
    487519 
    488         if (eventParameters.get("Button") != null) 
    489         { 
     520        if (eventParameters.get("Button") != null) { 
    490521            int buttonId = Integer.parseInt(eventParameters.get("Button")); 
    491             if (buttonId == MouseEvent.BUTTON1) 
    492             { 
     522            if (buttonId == MouseEvent.BUTTON1) { 
    493523                button = MouseButtonInteraction.Button.LEFT; 
    494524            } 
    495             else if (buttonId == MouseEvent.BUTTON2) 
    496             { 
     525            else if (buttonId == MouseEvent.BUTTON2) { 
    497526                button = MouseButtonInteraction.Button.MIDDLE; 
    498527            } 
    499             else if (buttonId == MouseEvent.BUTTON3) 
    500             { 
     528            else if (buttonId == MouseEvent.BUTTON3) { 
    501529                button = MouseButtonInteraction.Button.RIGHT; 
    502530            } 
    503             else 
    504             { 
     531            else { 
    505532                throw new SAXException("unknown mouse button index " + buttonId); 
    506533            } 
    507534        } 
    508535 
    509         if (JFCEventId.MOUSE_CLICKED == eventId) 
    510         { 
     536        if (JFCEventId.MOUSE_CLICKED == eventId) { 
    511537            int x = Integer.parseInt(eventParameters.get("X")); 
    512538            int y = Integer.parseInt(eventParameters.get("Y")); 
    513539            return new MouseClick(button, x, y); 
    514540        } 
    515         else if (JFCEventId.MOUSE_PRESSED == eventId) 
    516         { 
     541        else if (JFCEventId.MOUSE_PRESSED == eventId) { 
    517542            int x = Integer.parseInt(eventParameters.get("X")); 
    518543            int y = Integer.parseInt(eventParameters.get("Y")); 
    519544            return new MouseButtonDown(button, x, y); 
    520545        } 
    521         else if (JFCEventId.MOUSE_RELEASED == eventId) 
    522         { 
     546        else if (JFCEventId.MOUSE_RELEASED == eventId) { 
    523547            int x = Integer.parseInt(eventParameters.get("X")); 
    524548            int y = Integer.parseInt(eventParameters.get("Y")); 
    525549            return new MouseButtonUp(button, x, y); 
    526550        } 
    527         else 
    528         { 
     551        else { 
    529552            throw new SAXException("unknown event id " + eventId); 
    530553        } 
    531             } 
     554    } 
    532555 
    533556    /** 
    534557     * <p> 
    535558     * handles a keyboard interaction. The method determines based on the event id and the 
    536      * parameters which key on the keyboard is pressed or released. It further checks, if for  
    537      * every released key there is also a pressed event 
    538      * </p> 
    539      * 
     559     * parameters which key on the keyboard is pressed or released. It further checks, if for every 
     560     * released key there is also a pressed event 
     561     * </p> 
     562     *  
    540563     * @param eventId 
    541564     *            the id of the event 
    542565     * @param eventParameters 
    543566     *            the parameters provided for the event 
    544      *             
     567     *  
    545568     * @return as described 
    546569     *  
    547      * @throws SAXException thrown if the provided event id is unknown or if there is a key 
    548      *                      release without a preceding press of the same key 
     570     * @throws SAXException 
     571     *             thrown if the provided event id is unknown or if there is a key release without a 
     572     *             preceding press of the same key 
    549573     */ 
    550574    private IInteraction handleKeyAction(JFCEventId eventId, Map<String, String> eventParameters) 
    551             throws SAXException 
    552             { 
     575        throws SAXException 
     576    { 
    553577        // TODO handle shortcuts 
    554         if (JFCEventId.KEY_PRESSED == eventId) 
    555         { 
     578        if (JFCEventId.KEY_PRESSED == eventId) { 
    556579            VirtualKey key = VirtualKey.parseVirtualKey(eventParameters.get("KeyCode")); 
    557580            mPressedKeys.add(key); 
     
    559582            return new KeyPressed(key); 
    560583        } 
    561         else if (JFCEventId.KEY_RELEASED == eventId) 
    562         { 
     584        else if (JFCEventId.KEY_RELEASED == eventId) { 
    563585            VirtualKey key = VirtualKey.parseVirtualKey(eventParameters.get("KeyCode")); 
    564             if (mPressedKeys.contains(key)) 
    565             { 
     586            if (mPressedKeys.contains(key)) { 
    566587                mPressedKeys.remove(key); 
    567588            } 
    568             else 
    569             { 
    570                 Console.traceln(Level.SEVERE, "log file has an error, as it contains a key up event on key " + 
    571                         key + " for which there is no preceeding key down event"); 
     589            else { 
     590                Console.traceln(Level.SEVERE, 
     591                                "log file has an error, as it contains a key up event on key " + 
     592                                    key + " for which there is no preceeding key down event"); 
    572593            } 
    573594 
     
    576597 
    577598        throw new SAXException("unknown event id " + eventId); 
    578             } 
     599    } 
    579600 
    580601    /** 
     
    582603     * handles explicit keyboard focus changes. 
    583604     * </p> 
    584      * 
     605     *  
    585606     * @param eventId 
    586607     *            the id of the event 
    587608     * @param eventParameters 
    588609     *            the parameters provided for the event 
    589      *             
     610     *  
    590611     * @return as described 
    591612     *  
    592      * @throws SAXException thrown if the provided event id is unknown 
     613     * @throws SAXException 
     614     *             thrown if the provided event id is unknown 
    593615     */ 
    594616    private IInteraction handleNewFocus(JFCEventId eventId, Map<String, String> eventParameters) 
    595             throws SAXException 
    596             { 
    597         if (JFCEventId.FOCUS_GAINED == eventId) 
    598         { 
     617        throws SAXException 
     618    { 
     619        if (JFCEventId.FOCUS_GAINED == eventId) { 
    599620            return new KeyboardFocusChange(); 
    600621        } 
    601         else 
    602         { 
     622        else { 
    603623            throw new SAXException("unknown event id " + eventId); 
    604624        } 
    605             } 
     625    } 
    606626 
    607627    /** 
     
    611631     * </p> 
    612632     */ 
    613     /*private void setupDefaultEventFilter() { 
    614         eventFilter = new HashSet<JFCEventId>(); 
    615         eventFilter.add(JFCEventId.MOUSE_PRESSED); 
    616         eventFilter.add(JFCEventId.MOUSE_RELEASED); 
    617         eventFilter.add(JFCEventId.FOCUS_GAINED); 
    618     }*/ 
     633    /* 
     634     * private void setupDefaultEventFilter() { eventFilter = new HashSet<JFCEventId>(); 
     635     * eventFilter.add(JFCEventId.MOUSE_PRESSED); eventFilter.add(JFCEventId.MOUSE_RELEASED); 
     636     * eventFilter.add(JFCEventId.FOCUS_GAINED); } 
     637     */ 
    619638} 
Note: See TracChangeset for help on using the changeset viewer.