package de.ugoe.cs.eventbench.windows.data; import java.security.InvalidParameterException; import java.util.HashMap; import java.util.Map; import de.ugoe.cs.eventbench.data.IReplayable; import de.ugoe.cs.util.StringTools; /** *
* Contains all informations about a windows message, i.e., all parameters that * are read when a windows message is parsed as well as its target, hwnd, etc. *
* * @author Steffen Herbold * @version 1.0 * */ public class WindowsMessage implements IReplayable { /** ** Id for object serialization. *
*/ private static final long serialVersionUID = 1L; /** ** Type of the message. *
*/ final int type; /** ** Window class of the message target. Default: "" *
*/ private String windowClass = ""; /** ** Resource Id of the message target. Default: 0 *
*/ private int resourceId = 0; /** ** XML representation of the message target. *
*/ private String xmlWindowDescription = ""; /** ** String that contains the names of all parent widgets and itself, separated by dots, * e.g., "GrandParent.Parent.self". *
*/ private String parentNames = null; /** ** String that contains the window class of the parent widget. *
*/ private String parentClass = null; /** ** LPARAM of the message. Default: 0 *
*/ private long LPARAM = 0; /** ** WPARAM of the message. Default: 0 *
*/ private long WPARAM = 0; /** ** If the LPARAM contains a HWND, this string stores the target of the HWND. *
*/ private String LPARAMasWindowDesc = null; /** ** If the WPARAM contains a HWND, this string stores the target of the HWND. *
*/ private String WPARAMasWindowDesc = null; /** ** Delay after sending the messages during a replay. Default: 0 *
*/ private int delay = 0; /** ** A map of all parameters, associated with the message, created during the * parsing of messages from the logs {@code param}-nodes. *
*/ private Map* Constructor. Creates a new message with a given message type. *
* * @param type * type of the message */ public WindowsMessage(int type) { this.type = type; } /** ** Adds a parameter to the message. *
* * @param type * type descriptor of the parameter * @param value * value of the parameter */ public void addParameter(String type, String value) { params.put(type, value); if (type.equals("LPARAM")) { LPARAM = Long.parseLong(value); } else if (type.equals("WPARAM")) { WPARAM = Long.parseLong(value); } } /** ** Returns the type of the message. *
* * @return type of the message */ public int getType() { return type; } /** ** Returns the value of a parameter, given its type. If the parameter is not * found, {@code null} is returned. *
* * @param type * type of the parameter * @return value of the parameter */ public String getParameter(String type) { return params.get(type); } /** ** Returns the window class of the message target. *
* * @return window class of the message target */ public String getWindowClass() { return windowClass; } /** ** Returns the HWND the message is addressed to. *
* * @return HWND the message is addressed to */ public int getHwnd() { int hwnd = -1; String hwndString = getParameter("window.hwnd"); // possible, as // "window.hwnd" is // mandatory if (hwndString != null) { hwnd = Integer.parseInt(hwndString); } return hwnd; } /** ** Returns the resource Id of the message target. *
* * @return resource Id of the message target */ public int getWindowResourceId() { return resourceId; } /** ** Two {@link WindowsMessage} are equal, if their {@link #type}, * {@link #xmlWindowDescription}, and {@link #params} are equal. *
* * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object other) { if (other == this) { return true; } boolean isEqual = false; if (other instanceof WindowsMessage) { isEqual = ((WindowsMessage) other).type == this.type && ((WindowsMessage) other).xmlWindowDescription .equals(this.xmlWindowDescription) && ((WindowsMessage) other).params.equals(this.params); } return isEqual; } /* * (non-Javadoc) * * @see java.lang.Object#hashCode() */ @Override public int hashCode() { int multiplier = 17; int hash = 42; hash = multiplier * hash + type; hash = multiplier * hash + xmlWindowDescription.hashCode(); hash = multiplier * hash + params.hashCode(); return hash; } /** ** Returns a string representation of the message of the form * "msg[target=HWND;type=TYPE]". *
* * @see java.lang.Object#toString() */ @Override public String toString() { return "msg[target=" + getParameter("window.hwnd") + ";type=" + type + "]"; } /** ** Retrieves the target string of a message from a given {@link WindowTree} * through looking up the HWND the message is addressed to in the window * tree. *
* * @param windowTree * {@link WindowTree} from which the target is extracted * @throws InvalidParameterException * thrown if HWND is not contained in windowTree */ public void setTarget(WindowTree windowTree) throws InvalidParameterException { int hwnd = Integer.parseInt(getParameter("window.hwnd")); WindowTreeNode node = windowTree.find(hwnd); if (node == null) { throw new InvalidParameterException("No window with HWND " + hwnd + " found in window tree!"); } else { windowClass = node.getClassName(); resourceId = node.getResourceId(); xmlWindowDescription = node.xmlRepresentation(); parentNames = node.getParentNames(); WindowTreeNode parent = node.getParent(); if (parent == null) { parentClass = ""; } else { parentClass = parent.getClassName(); } } } /** ** Sets the LPARAM of a message. *
* * @param paramValue * value of the LPARAM */ public void setLPARAM(long paramValue) { LPARAM = paramValue; } /** ** Sets the WPARAM of a message. *
* * @param paramValue * value of the WPARAM */ public void setWPARAM(long paramValue) { WPARAM = paramValue; } /** ** Returns the LPARAM of a message. *
* * @return LPARAM of the message */ public long getLPARAM() { return LPARAM; } /** ** Returns the WPARAM of a message. *
* * @return WPARAM of the message */ public long getWPARAM() { return WPARAM; } /** ** If the LPARAM contains a HWND, this function can be used to set a target * string to identify the HWND at run-time. *
* * @param windowDesc * target string */ public void setLPARAMasWindowDesc(String windowDesc) { LPARAMasWindowDesc = windowDesc; } /** ** If the WPARAM contains a HWND, this function can be used to set a target * string to identify the HWND at run-time. *
* * @param windowDesc * target string */ public void setWPARAMasWindowDesc(String windowDesc) { WPARAMasWindowDesc = windowDesc; } /** ** If the LPARAM contains a HWND and the target string for the HWND is set, * this function returns the target string. Otherwise, {@code null} is * returned. *
* * @return target string if available; {@code null} otherwise */ public String getLPARAMasWindowDesc() { return LPARAMasWindowDesc; } /** ** If the WPARAM contains a HWND and the target string for the HWND is set, * this function returns the target string. Otherwise, {@code null} is * returned. *
* * @return target string if available; {@code null} otherwise */ public String getWPARAMasWindowDesc() { return WPARAMasWindowDesc; } /** ** Returns the target string of the message. *
* * @return target string of the message */ public String getXmlWindowDescription() { return xmlWindowDescription; } /** ** Sets the target string manually. *
* * @param xmlWindowDescription * target string */ public void setXmlWindowDescription(String xmlWindowDescription) { this.xmlWindowDescription = xmlWindowDescription; } /** ** Returns the delay after this message during replays. *
* * @return delay after this message */ public int getDelay() { return delay; } /** ** Sets the delay after this message during replays. *
* * @param delay * delay after this message */ public void setDelay(int delay) { this.delay = delay; } /** ** Returns the parent names separated by dots, e.g., "GrandParent.Parent". *
* * @return names of the parents */ public String getParentNames() { return parentNames; } /** ** Returns the window class of the parent. *
* * @return window classes of the parents */ public String getParentClass() { return parentClass; } /** ** Returns the number of parameters stored together with this message. *
* * @return number of parameters stored with this message */ public int getNumParams() { return params.size(); } /* * (non-Javadoc) * * @see de.ugoe.cs.eventbench.data.IReplayable#getReplay() */ @Override public String getReplay() { StringBuilder currentMsgStr = new StringBuilder(400); currentMsgStr.append("