source: trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/windows/data/WindowsMessage.java @ 171

Last change on this file since 171 was 171, checked in by sherbold, 13 years ago
  • code documentation and formatting
File size: 11.3 KB
Line 
1package de.ugoe.cs.eventbench.windows.data;
2
3import java.security.InvalidParameterException;
4import java.util.HashMap;
5import java.util.Map;
6
7import de.ugoe.cs.eventbench.data.IReplayable;
8import de.ugoe.cs.util.StringTools;
9
10/**
11 * <p>
12 * Contains all informations about a windows message, i.e., all parameters that
13 * are read when a windows message is parsed as well as its target, hwnd, etc.
14 * </p>
15 *
16 * @author Steffen Herbold
17 * @version 1.0
18 *
19 */
20public class WindowsMessage implements IReplayable {
21
22        /**
23         * <p>
24         * Id for object serialization.
25         * </p>
26         */
27        private static final long serialVersionUID = 1L;
28
29        /**
30         * <p>
31         * Type of the message.
32         * </p>
33         */
34        final int type;
35
36        /**
37         * <p>
38         * Window class of the message target. Default: ""
39         * </p>
40         */
41        private String windowClass = "";
42
43        /**
44         * <p>
45         * Resource Id of the message target. Default: 0
46         * </p>
47         */
48        private int resourceId = 0;
49
50        /**
51         * <p>
52         * XML representation of the message target.
53         * </p>
54         */
55        private String xmlWindowDescription = "";
56
57        /**
58         * <p>
59         * String that contains the names of all parent widgets and itself, separated by dots,
60         * e.g., "GrandParent.Parent.self".
61         * </p>
62         */
63        private String parentNames = null;
64
65        /**
66         * <p>
67         * String that contains the window class of the parent widget.
68         * </p>
69         */
70        private String parentClass = null;
71
72        /**
73         * <p>
74         * LPARAM of the message. Default: 0
75         * </p>
76         */
77        private long LPARAM = 0;
78
79        /**
80         * <p>
81         * WPARAM of the message. Default: 0
82         * </p>
83         */
84        private long WPARAM = 0;
85
86        /**
87         * <p>
88         * If the LPARAM contains a HWND, this string stores the target of the HWND.
89         * </p>
90         */
91        private String LPARAMasWindowDesc = null;
92
93        /**
94         * <p>
95         * If the WPARAM contains a HWND, this string stores the target of the HWND.
96         * </p>
97         */
98        private String WPARAMasWindowDesc = null;
99
100        /**
101         * <p>
102         * Delay after sending the messages during a replay. Default: 0
103         * </p>
104         */
105        private int delay = 0;
106
107        /**
108         * <p>
109         * A map of all parameters, associated with the message, created during the
110         * parsing of messages from the logs {@code param}-nodes.
111         * </p>
112         */
113        private Map<String, String> params = new HashMap<String, String>();
114
115        /**
116         * <p>
117         * Constructor. Creates a new message with a given message type.
118         * </p>
119         *
120         * @param type
121         *            type of the message
122         */
123        public WindowsMessage(int type) {
124                this.type = type;
125        }
126
127        /**
128         * <p>
129         * Adds a parameter to the message.
130         * </p>
131         *
132         * @param type
133         *            type descriptor of the parameter
134         * @param value
135         *            value of the parameter
136         */
137        public void addParameter(String type, String value) {
138                params.put(type, value);
139                if (type.equals("LPARAM")) {
140                        LPARAM = Long.parseLong(value);
141                } else if (type.equals("WPARAM")) {
142                        WPARAM = Long.parseLong(value);
143                }
144        }
145
146        /**
147         * <p>
148         * Returns the type of the message.
149         * </p>
150         *
151         * @return type of the message
152         */
153        public int getType() {
154                return type;
155        }
156
157        /**
158         * <p>
159         * Returns the value of a parameter, given its type. If the parameter is not
160         * found, {@code null} is returned.
161         * </p>
162         *
163         * @param type
164         *            type of the parameter
165         * @return value of the parameter
166         */
167        public String getParameter(String type) {
168                return params.get(type);
169        }
170
171        /**
172         * <p>
173         * Returns the window class of the message target.
174         * </p>
175         *
176         * @return window class of the message target
177         */
178        public String getWindowClass() {
179                return windowClass;
180        }
181
182        /**
183         * <p>
184         * Returns the HWND the message is addressed to.
185         * </p>
186         *
187         * @return HWND the message is addressed to
188         */
189        public int getHwnd() {
190                int hwnd = -1;
191                String hwndString = getParameter("window.hwnd"); // possible, as
192                                                                                                                        // "window.hwnd" is
193                                                                                                                        // mandatory
194                if (hwndString != null) {
195                        hwnd = Integer.parseInt(hwndString);
196                }
197                return hwnd;
198        }
199
200        /**
201         * <p>
202         * Returns the resource Id of the message target.
203         * </p>
204         *
205         * @return resource Id of the message target
206         */
207        public int getWindowResourceId() {
208                return resourceId;
209        }
210
211        /**
212         * <p>
213         * Two {@link WindowsMessage} are equal, if their {@link #type},
214         * {@link #xmlWindowDescription}, and {@link #params} are equal.
215         * </p>
216         *
217         * @see java.lang.Object#equals(java.lang.Object)
218         */
219        @Override
220        public boolean equals(Object other) {
221                if (other == this) {
222                        return true;
223                }
224                boolean isEqual = false;
225                if (other instanceof WindowsMessage) {
226                        isEqual = ((WindowsMessage) other).type == this.type
227                                        && ((WindowsMessage) other).xmlWindowDescription
228                                                        .equals(this.xmlWindowDescription)
229                                        && ((WindowsMessage) other).params.equals(this.params);
230                }
231                return isEqual;
232        }
233
234        /*
235         * (non-Javadoc)
236         *
237         * @see java.lang.Object#hashCode()
238         */
239        @Override
240        public int hashCode() {
241                int multiplier = 17;
242                int hash = 42;
243
244                hash = multiplier * hash + type;
245                hash = multiplier * hash + xmlWindowDescription.hashCode();
246                hash = multiplier * hash + params.hashCode();
247
248                return hash;
249        }
250
251        /**
252         * <p>
253         * Returns a string representation of the message of the form
254         * "msg[target=HWND;type=TYPE]".
255         * </p>
256         *
257         * @see java.lang.Object#toString()
258         */
259        @Override
260        public String toString() {
261                return "msg[target=" + getParameter("window.hwnd") + ";type=" + type
262                                + "]";
263        }
264
265        /**
266         * <p>
267         * Retrieves the target string of a message from a given {@link WindowTree}
268         * through looking up the HWND the message is addressed to in the window
269         * tree.
270         * </p>
271         *
272         * @param windowTree
273         *            {@link WindowTree} from which the target is extracted
274         * @throws InvalidParameterException
275         *             thrown if HWND is not contained in windowTree
276         */
277        public void setTarget(WindowTree windowTree)
278                        throws InvalidParameterException {
279                int hwnd = Integer.parseInt(getParameter("window.hwnd"));
280                WindowTreeNode node = windowTree.find(hwnd);
281                if (node == null) {
282                        throw new InvalidParameterException("No window with HWND " + hwnd
283                                        + " found in window tree!");
284                } else {
285                        windowClass = node.getClassName();
286                        resourceId = node.getResourceId();
287                        xmlWindowDescription = node.xmlRepresentation();
288                        parentNames = node.getParentNames();
289                        WindowTreeNode parent = node.getParent();
290                        if (parent == null) {
291                                parentClass = "";
292                        } else {
293                                parentClass = parent.getClassName();
294                        }
295                }
296        }
297
298        /**
299         * <p>
300         * Sets the LPARAM of a message.
301         * </p>
302         *
303         * @param paramValue
304         *            value of the LPARAM
305         */
306        public void setLPARAM(long paramValue) {
307                LPARAM = paramValue;
308        }
309
310        /**
311         * <p>
312         * Sets the WPARAM of a message.
313         * </p>
314         *
315         * @param paramValue
316         *            value of the WPARAM
317         */
318        public void setWPARAM(long paramValue) {
319                WPARAM = paramValue;
320        }
321
322        /**
323         * <p>
324         * Returns the LPARAM of a message.
325         * </p>
326         *
327         * @return LPARAM of the message
328         */
329        public long getLPARAM() {
330                return LPARAM;
331        }
332
333        /**
334         * <p>
335         * Returns the WPARAM of a message.
336         * </p>
337         *
338         * @return WPARAM of the message
339         */
340        public long getWPARAM() {
341                return WPARAM;
342        }
343
344        /**
345         * <p>
346         * If the LPARAM contains a HWND, this function can be used to set a target
347         * string to identify the HWND at run-time.
348         * </p>
349         *
350         * @param windowDesc
351         *            target string
352         */
353        public void setLPARAMasWindowDesc(String windowDesc) {
354                LPARAMasWindowDesc = windowDesc;
355        }
356
357        /**
358         * <p>
359         * If the WPARAM contains a HWND, this function can be used to set a target
360         * string to identify the HWND at run-time.
361         * </p>
362         *
363         * @param windowDesc
364         *            target string
365         */
366        public void setWPARAMasWindowDesc(String windowDesc) {
367                WPARAMasWindowDesc = windowDesc;
368        }
369
370        /**
371         * <p>
372         * If the LPARAM contains a HWND and the target string for the HWND is set,
373         * this function returns the target string. Otherwise, {@code null} is
374         * returned.
375         * </p>
376         *
377         * @return target string if available; {@code null} otherwise
378         */
379        public String getLPARAMasWindowDesc() {
380                return LPARAMasWindowDesc;
381        }
382
383        /**
384         * <p>
385         * If the WPARAM contains a HWND and the target string for the HWND is set,
386         * this function returns the target string. Otherwise, {@code null} is
387         * returned.
388         * </p>
389         *
390         * @return target string if available; {@code null} otherwise
391         */
392        public String getWPARAMasWindowDesc() {
393                return WPARAMasWindowDesc;
394        }
395
396        /**
397         * <p>
398         * Returns the target string of the message.
399         * </p>
400         *
401         * @return target string of the message
402         */
403        public String getXmlWindowDescription() {
404                return xmlWindowDescription;
405        }
406
407        /**
408         * <p>
409         * Sets the target string manually.
410         * </p>
411         *
412         * @param xmlWindowDescription
413         *            target string
414         */
415        public void setXmlWindowDescription(String xmlWindowDescription) {
416                this.xmlWindowDescription = xmlWindowDescription;
417        }
418
419        /**
420         * <p>
421         * Returns the delay after this message during replays.
422         * </p>
423         *
424         * @return delay after this message
425         */
426        public int getDelay() {
427                return delay;
428        }
429
430        /**
431         * <p>
432         * Sets the delay after this message during replays.
433         * </p>
434         *
435         * @param delay
436         *            delay after this message
437         */
438        public void setDelay(int delay) {
439                this.delay = delay;
440        }
441
442        /**
443         * <p>
444         * Returns the parent names separated by dots, e.g., "GrandParent.Parent".
445         * </p>
446         *
447         * @return names of the parents
448         */
449        public String getParentNames() {
450                return parentNames;
451        }
452
453        /**
454         * <p>
455         * Returns the window class of the parent.
456         * </p>
457         *
458         * @return window classes of the parents
459         */
460        public String getParentClass() {
461                return parentClass;
462        }
463
464        /**
465         * <p>
466         * Returns the number of parameters stored together with this message.
467         * </p>
468         *
469         * @return
470         */
471        public int getNumParams() {
472                return params.size();
473        }
474
475        /*
476         * (non-Javadoc)
477         *
478         * @see de.ugoe.cs.eventbench.data.IReplayable#getReplay()
479         */
480        @Override
481        public String getReplay() {
482                StringBuilder currentMsgStr = new StringBuilder(400);
483                currentMsgStr.append("  <msg type=\"" + type + "\" ");
484                currentMsgStr.append("LPARAM=\"" + LPARAM + "\" ");
485                currentMsgStr.append("WPARAM=\"" + WPARAM + "\" ");
486                currentMsgStr.append("delay=\"" + delay + "\">");
487                if (LPARAMasWindowDesc != null) {
488                        currentMsgStr.append(StringTools.ENDLINE);
489                        currentMsgStr.append("   <LPARAM>");
490                        currentMsgStr.append(StringTools.ENDLINE);
491                        currentMsgStr.append(LPARAMasWindowDesc);
492                        currentMsgStr.append(StringTools.ENDLINE);
493                        currentMsgStr.append("</LPARAM>");
494                }
495                if (WPARAMasWindowDesc != null) {
496                        currentMsgStr.append(StringTools.ENDLINE);
497                        currentMsgStr.append("   <WPARAM>");
498                        currentMsgStr.append(StringTools.ENDLINE);
499                        currentMsgStr.append(WPARAMasWindowDesc);
500                        currentMsgStr.append(StringTools.ENDLINE);
501                        currentMsgStr.append("   </WPARAM>");
502                }
503                currentMsgStr.append(StringTools.ENDLINE);
504                currentMsgStr.append(xmlWindowDescription);
505                currentMsgStr.append(StringTools.ENDLINE);
506                currentMsgStr.append("  </msg>");
507                currentMsgStr.append(StringTools.ENDLINE);
508                return currentMsgStr.toString();
509        }
510
511        /*
512         * (non-Javadoc)
513         *
514         * @see de.ugoe.cs.eventbench.data.IReplayable#getTarget()
515         */
516        @Override
517        public String getTarget() {
518                return xmlWindowDescription;
519        }
520}
Note: See TracBrowser for help on using the repository browser.