[52] | 1 | package de.ugoe.cs.eventbench.windows.data;
|
---|
[1] | 2 |
|
---|
| 3 | import java.security.InvalidParameterException;
|
---|
| 4 | import java.util.HashMap;
|
---|
| 5 | import java.util.Map;
|
---|
| 6 |
|
---|
| 7 | import de.ugoe.cs.eventbench.data.IReplayable;
|
---|
| 8 | import de.ugoe.cs.util.StringTools;
|
---|
| 9 |
|
---|
[171] | 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 | */
|
---|
[1] | 20 | public class WindowsMessage implements IReplayable {
|
---|
[171] | 21 |
|
---|
[87] | 22 | /**
|
---|
[171] | 23 | * <p>
|
---|
| 24 | * Id for object serialization.
|
---|
| 25 | * </p>
|
---|
[87] | 26 | */
|
---|
| 27 | private static final long serialVersionUID = 1L;
|
---|
[171] | 28 |
|
---|
| 29 | /**
|
---|
| 30 | * <p>
|
---|
| 31 | * Type of the message.
|
---|
| 32 | * </p>
|
---|
| 33 | */
|
---|
[1] | 34 | final int type;
|
---|
[171] | 35 |
|
---|
| 36 | /**
|
---|
| 37 | * <p>
|
---|
| 38 | * Window class of the message target. Default: ""
|
---|
| 39 | * </p>
|
---|
| 40 | */
|
---|
[1] | 41 | private String windowClass = "";
|
---|
[171] | 42 |
|
---|
| 43 | /**
|
---|
| 44 | * <p>
|
---|
| 45 | * Resource Id of the message target. Default: 0
|
---|
| 46 | * </p>
|
---|
| 47 | */
|
---|
[1] | 48 | private int resourceId = 0;
|
---|
[171] | 49 |
|
---|
| 50 | /**
|
---|
| 51 | * <p>
|
---|
| 52 | * XML representation of the message target.
|
---|
| 53 | * </p>
|
---|
| 54 | */
|
---|
[1] | 55 | private String xmlWindowDescription = "";
|
---|
[171] | 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 | */
|
---|
[1] | 63 | private String parentNames = null;
|
---|
[171] | 64 |
|
---|
| 65 | /**
|
---|
| 66 | * <p>
|
---|
| 67 | * String that contains the window class of the parent widget.
|
---|
| 68 | * </p>
|
---|
| 69 | */
|
---|
[141] | 70 | private String parentClass = null;
|
---|
[1] | 71 |
|
---|
[171] | 72 | /**
|
---|
| 73 | * <p>
|
---|
| 74 | * LPARAM of the message. Default: 0
|
---|
| 75 | * </p>
|
---|
| 76 | */
|
---|
[1] | 77 | private long LPARAM = 0;
|
---|
[171] | 78 |
|
---|
| 79 | /**
|
---|
| 80 | * <p>
|
---|
| 81 | * WPARAM of the message. Default: 0
|
---|
| 82 | * </p>
|
---|
| 83 | */
|
---|
[1] | 84 | private long WPARAM = 0;
|
---|
| 85 |
|
---|
[171] | 86 | /**
|
---|
| 87 | * <p>
|
---|
| 88 | * If the LPARAM contains a HWND, this string stores the target of the HWND.
|
---|
| 89 | * </p>
|
---|
| 90 | */
|
---|
[1] | 91 | private String LPARAMasWindowDesc = null;
|
---|
[171] | 92 |
|
---|
| 93 | /**
|
---|
| 94 | * <p>
|
---|
| 95 | * If the WPARAM contains a HWND, this string stores the target of the HWND.
|
---|
| 96 | * </p>
|
---|
| 97 | */
|
---|
[1] | 98 | private String WPARAMasWindowDesc = null;
|
---|
| 99 |
|
---|
[171] | 100 | /**
|
---|
| 101 | * <p>
|
---|
| 102 | * Delay after sending the messages during a replay. Default: 0
|
---|
| 103 | * </p>
|
---|
| 104 | */
|
---|
[1] | 105 | private int delay = 0;
|
---|
[171] | 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 | */
|
---|
[1] | 113 | private Map<String, String> params = new HashMap<String, String>();
|
---|
| 114 |
|
---|
[171] | 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 | */
|
---|
[1] | 123 | public WindowsMessage(int type) {
|
---|
| 124 | this.type = type;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[171] | 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 | */
|
---|
[1] | 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 |
|
---|
[171] | 146 | /**
|
---|
| 147 | * <p>
|
---|
| 148 | * Returns the type of the message.
|
---|
| 149 | * </p>
|
---|
| 150 | *
|
---|
| 151 | * @return type of the message
|
---|
| 152 | */
|
---|
[1] | 153 | public int getType() {
|
---|
| 154 | return type;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[171] | 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 | */
|
---|
[1] | 167 | public String getParameter(String type) {
|
---|
| 168 | return params.get(type);
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[171] | 171 | /**
|
---|
| 172 | * <p>
|
---|
| 173 | * Returns the window class of the message target.
|
---|
| 174 | * </p>
|
---|
| 175 | *
|
---|
| 176 | * @return window class of the message target
|
---|
| 177 | */
|
---|
[1] | 178 | public String getWindowClass() {
|
---|
| 179 | return windowClass;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[171] | 182 | /**
|
---|
| 183 | * <p>
|
---|
| 184 | * Returns the HWND the message is addressed to.
|
---|
| 185 | * </p>
|
---|
| 186 | *
|
---|
| 187 | * @return HWND the message is addressed to
|
---|
| 188 | */
|
---|
[1] | 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 |
|
---|
[171] | 200 | /**
|
---|
| 201 | * <p>
|
---|
| 202 | * Returns the resource Id of the message target.
|
---|
| 203 | * </p>
|
---|
| 204 | *
|
---|
| 205 | * @return resource Id of the message target
|
---|
| 206 | */
|
---|
[1] | 207 | public int getWindowResourceId() {
|
---|
| 208 | return resourceId;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
[171] | 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 | */
|
---|
[1] | 219 | @Override
|
---|
| 220 | public boolean equals(Object other) {
|
---|
[171] | 221 | if (other == this) {
|
---|
[1] | 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 |
|
---|
[171] | 234 | /*
|
---|
| 235 | * (non-Javadoc)
|
---|
| 236 | *
|
---|
| 237 | * @see java.lang.Object#hashCode()
|
---|
| 238 | */
|
---|
[1] | 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 |
|
---|
[171] | 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 | */
|
---|
[1] | 259 | @Override
|
---|
| 260 | public String toString() {
|
---|
| 261 | return "msg[target=" + getParameter("window.hwnd") + ";type=" + type
|
---|
| 262 | + "]";
|
---|
| 263 | }
|
---|
| 264 |
|
---|
[171] | 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 | */
|
---|
[1] | 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();
|
---|
[141] | 289 | WindowTreeNode parent = node.getParent();
|
---|
[171] | 290 | if (parent == null) {
|
---|
[141] | 291 | parentClass = "";
|
---|
| 292 | } else {
|
---|
| 293 | parentClass = parent.getClassName();
|
---|
| 294 | }
|
---|
[1] | 295 | }
|
---|
| 296 | }
|
---|
| 297 |
|
---|
[171] | 298 | /**
|
---|
| 299 | * <p>
|
---|
| 300 | * Sets the LPARAM of a message.
|
---|
| 301 | * </p>
|
---|
| 302 | *
|
---|
| 303 | * @param paramValue
|
---|
| 304 | * value of the LPARAM
|
---|
| 305 | */
|
---|
[1] | 306 | public void setLPARAM(long paramValue) {
|
---|
| 307 | LPARAM = paramValue;
|
---|
| 308 | }
|
---|
| 309 |
|
---|
[171] | 310 | /**
|
---|
| 311 | * <p>
|
---|
| 312 | * Sets the WPARAM of a message.
|
---|
| 313 | * </p>
|
---|
| 314 | *
|
---|
| 315 | * @param paramValue
|
---|
| 316 | * value of the WPARAM
|
---|
| 317 | */
|
---|
[1] | 318 | public void setWPARAM(long paramValue) {
|
---|
| 319 | WPARAM = paramValue;
|
---|
| 320 | }
|
---|
| 321 |
|
---|
[171] | 322 | /**
|
---|
| 323 | * <p>
|
---|
| 324 | * Returns the LPARAM of a message.
|
---|
| 325 | * </p>
|
---|
| 326 | *
|
---|
| 327 | * @return LPARAM of the message
|
---|
| 328 | */
|
---|
[1] | 329 | public long getLPARAM() {
|
---|
| 330 | return LPARAM;
|
---|
| 331 | }
|
---|
| 332 |
|
---|
[171] | 333 | /**
|
---|
| 334 | * <p>
|
---|
| 335 | * Returns the WPARAM of a message.
|
---|
| 336 | * </p>
|
---|
| 337 | *
|
---|
| 338 | * @return WPARAM of the message
|
---|
| 339 | */
|
---|
[1] | 340 | public long getWPARAM() {
|
---|
| 341 | return WPARAM;
|
---|
| 342 | }
|
---|
| 343 |
|
---|
[171] | 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 | */
|
---|
[1] | 353 | public void setLPARAMasWindowDesc(String windowDesc) {
|
---|
| 354 | LPARAMasWindowDesc = windowDesc;
|
---|
| 355 | }
|
---|
| 356 |
|
---|
[171] | 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 | */
|
---|
[1] | 366 | public void setWPARAMasWindowDesc(String windowDesc) {
|
---|
| 367 | WPARAMasWindowDesc = windowDesc;
|
---|
| 368 | }
|
---|
| 369 |
|
---|
[171] | 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 | */
|
---|
[1] | 379 | public String getLPARAMasWindowDesc() {
|
---|
| 380 | return LPARAMasWindowDesc;
|
---|
| 381 | }
|
---|
| 382 |
|
---|
[171] | 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 | */
|
---|
[1] | 392 | public String getWPARAMasWindowDesc() {
|
---|
| 393 | return WPARAMasWindowDesc;
|
---|
| 394 | }
|
---|
| 395 |
|
---|
[171] | 396 | /**
|
---|
| 397 | * <p>
|
---|
| 398 | * Returns the target string of the message.
|
---|
| 399 | * </p>
|
---|
| 400 | *
|
---|
| 401 | * @return target string of the message
|
---|
| 402 | */
|
---|
[1] | 403 | public String getXmlWindowDescription() {
|
---|
| 404 | return xmlWindowDescription;
|
---|
| 405 | }
|
---|
| 406 |
|
---|
[171] | 407 | /**
|
---|
| 408 | * <p>
|
---|
| 409 | * Sets the target string manually.
|
---|
| 410 | * </p>
|
---|
| 411 | *
|
---|
| 412 | * @param xmlWindowDescription
|
---|
| 413 | * target string
|
---|
| 414 | */
|
---|
[1] | 415 | public void setXmlWindowDescription(String xmlWindowDescription) {
|
---|
| 416 | this.xmlWindowDescription = xmlWindowDescription;
|
---|
| 417 | }
|
---|
| 418 |
|
---|
[171] | 419 | /**
|
---|
| 420 | * <p>
|
---|
| 421 | * Returns the delay after this message during replays.
|
---|
| 422 | * </p>
|
---|
| 423 | *
|
---|
| 424 | * @return delay after this message
|
---|
| 425 | */
|
---|
[1] | 426 | public int getDelay() {
|
---|
| 427 | return delay;
|
---|
| 428 | }
|
---|
| 429 |
|
---|
[171] | 430 | /**
|
---|
| 431 | * <p>
|
---|
| 432 | * Sets the delay after this message during replays.
|
---|
| 433 | * </p>
|
---|
| 434 | *
|
---|
| 435 | * @param delay
|
---|
| 436 | * delay after this message
|
---|
| 437 | */
|
---|
[1] | 438 | public void setDelay(int delay) {
|
---|
| 439 | this.delay = delay;
|
---|
| 440 | }
|
---|
| 441 |
|
---|
[171] | 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 | */
|
---|
[1] | 449 | public String getParentNames() {
|
---|
| 450 | return parentNames;
|
---|
| 451 | }
|
---|
[171] | 452 |
|
---|
| 453 | /**
|
---|
| 454 | * <p>
|
---|
| 455 | * Returns the window class of the parent.
|
---|
| 456 | * </p>
|
---|
| 457 | *
|
---|
| 458 | * @return window classes of the parents
|
---|
| 459 | */
|
---|
[141] | 460 | public String getParentClass() {
|
---|
| 461 | return parentClass;
|
---|
| 462 | }
|
---|
[1] | 463 |
|
---|
[171] | 464 | /**
|
---|
| 465 | * <p>
|
---|
| 466 | * Returns the number of parameters stored together with this message.
|
---|
| 467 | * </p>
|
---|
| 468 | *
|
---|
[223] | 469 | * @return number of parameters stored with this message
|
---|
[171] | 470 | */
|
---|
[1] | 471 | public int getNumParams() {
|
---|
| 472 | return params.size();
|
---|
| 473 | }
|
---|
[171] | 474 |
|
---|
| 475 | /*
|
---|
| 476 | * (non-Javadoc)
|
---|
| 477 | *
|
---|
| 478 | * @see de.ugoe.cs.eventbench.data.IReplayable#getReplay()
|
---|
| 479 | */
|
---|
| 480 | @Override
|
---|
[90] | 481 | public String getReplay() {
|
---|
[1] | 482 | StringBuilder currentMsgStr = new StringBuilder(400);
|
---|
[171] | 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) {
|
---|
[1] | 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>");
|
---|
[171] | 494 | }
|
---|
| 495 | if (WPARAMasWindowDesc != null) {
|
---|
[1] | 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 | }
|
---|
[171] | 510 |
|
---|
| 511 | /*
|
---|
| 512 | * (non-Javadoc)
|
---|
| 513 | *
|
---|
| 514 | * @see de.ugoe.cs.eventbench.data.IReplayable#getTarget()
|
---|
| 515 | */
|
---|
| 516 | @Override
|
---|
[1] | 517 | public String getTarget() {
|
---|
| 518 | return xmlWindowDescription;
|
---|
| 519 | }
|
---|
| 520 | }
|
---|