source: trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/windows/data/WindowsEvent.java @ 413

Last change on this file since 413 was 413, checked in by sherbold, 12 years ago
  • changed equals method of de.ugoe.cs.eventbench.windows.data.WindowsEvent? to be able to better identify equal widgets throughout multiple sessions. To this aim, the class de.ugoe.cs.eventbench.windows.data.MFCTargetComparator has been introduced to compare the target strings of WindowsEvents?.
  • changed parseXML to set up the MFCTargetComparator
  • Property svn:mime-type set to text/plain
File size: 2.2 KB
Line 
1package de.ugoe.cs.eventbench.windows.data;
2
3import java.io.ByteArrayInputStream;
4
5import javax.xml.parsers.DocumentBuilder;
6import javax.xml.parsers.DocumentBuilderFactory;
7
8import org.w3c.dom.Document;
9import org.w3c.dom.Element;
10import org.w3c.dom.NodeList;
11
12import de.ugoe.cs.eventbench.data.ReplayableEvent;
13
14/**
15 * <p>
16 * Convenience class for working with Windows MFC events.
17 * </p>
18 *
19 * @author Steffen Herbold
20 * @version 1.0
21 */
22public class WindowsEvent extends ReplayableEvent<WindowsMessage> {
23
24        /**
25         * <p>
26         * Id for object serialization.
27         * </p>
28         */
29        private static final long serialVersionUID = 1L;
30
31        /**
32         * <p>
33         * Constructor. Creates a new WindowEvent.
34         * </p>
35         *
36         * @see de.ugoe.cs.eventbench.data.Event#Event(String)
37         * @param type
38         *            type of the event.
39         */
40        public WindowsEvent(String type) {
41                super(type);
42        }
43
44        @Override
45        protected boolean targetEquals(String otherTarget) {
46                return MFCTargetComparator.compare(target, otherTarget);
47        }
48       
49        int targetHash = 0;
50       
51        @Override
52        protected int targetHashCode() {
53                if( targetHash==0 ) {
54                        int multiplier = 17;
55                        if (target != null) {
56                                Document doc;
57                                try {
58                                        DocumentBuilder documentBuilder = DocumentBuilderFactory
59                                                        .newInstance().newDocumentBuilder();
60                                        doc = documentBuilder.parse(new ByteArrayInputStream(
61                                                        ("<dummy>" + target + "</dummy>").getBytes("UTF-8")));
62                                } catch (Exception e) {
63                                        e.printStackTrace();
64                                        return 0;
65                                }
66                                doc.getDocumentElement().normalize();
67                                NodeList widgets = doc.getElementsByTagName("window");
68
69                                for (int i = 0; i < widgets.getLength(); i++) {
70                                        Element currentWidget = (Element) widgets.item(i);
71                                        targetHash = targetHash* multiplier + widgetHashCode(currentWidget);
72                                }
73                        }
74                }
75                return targetHash;
76        }
77       
78        private int widgetHashCode(Element currentWidget) {
79                int hashCode = 0;
80                int multiplier = 41;
81                hashCode = hashCode * multiplier + currentWidget.getAttribute("class").hashCode();
82                hashCode = hashCode * multiplier + currentWidget.getAttribute("resourceId").hashCode();
83                hashCode = hashCode * multiplier + currentWidget.getAttribute("isModal").hashCode();
84                return hashCode;
85        }
86}
Note: See TracBrowser for help on using the repository browser.