[1019] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
| 2 | // |
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 4 | // you may not use this file except in compliance with the License. |
---|
| 5 | // You may obtain a copy of the License at |
---|
| 6 | // |
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 8 | // |
---|
| 9 | // Unless required by applicable law or agreed to in writing, software |
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 12 | // See the License for the specific language governing permissions and |
---|
| 13 | // limitations under the License. |
---|
| 14 | |
---|
| 15 | package de.ugoe.cs.autoquest.htmlmonitor; |
---|
| 16 | |
---|
| 17 | /** |
---|
| 18 | * <p> |
---|
[1069] | 19 | * represents an element of an HTML GUI, i.e. a tag within a web page. |
---|
[1019] | 20 | * </p> |
---|
| 21 | * |
---|
| 22 | * @author Patrick Harms |
---|
| 23 | */ |
---|
[1069] | 24 | class HtmlPageElement extends HtmlGUIElement { |
---|
[1019] | 25 | |
---|
| 26 | /** |
---|
[1069] | 27 | * the document to which the represented tag belongs |
---|
[1019] | 28 | */ |
---|
[1069] | 29 | private HtmlDocument document; |
---|
[1019] | 30 | |
---|
| 31 | /** |
---|
[1069] | 32 | * the parent page element; if null, the document is considered the parent |
---|
[1019] | 33 | */ |
---|
[1069] | 34 | private HtmlPageElement parent; |
---|
[1019] | 35 | |
---|
| 36 | /** |
---|
[1069] | 37 | * the name of the tag represented by this page element. |
---|
[1019] | 38 | */ |
---|
[1069] | 39 | private String tagName; |
---|
[1019] | 40 | |
---|
| 41 | /** |
---|
[1069] | 42 | * the id of the page element inside the DOM. |
---|
[1019] | 43 | */ |
---|
[1069] | 44 | private String htmlId; |
---|
[1019] | 45 | |
---|
| 46 | /** |
---|
[1022] | 47 | * the index of this element regarding all elements with the same tag name in the list of |
---|
| 48 | * children of the parent element |
---|
[1019] | 49 | */ |
---|
| 50 | private Integer index; |
---|
| 51 | |
---|
| 52 | /** |
---|
[1089] | 53 | * the hash code of this document |
---|
| 54 | */ |
---|
| 55 | private int hashCode; |
---|
| 56 | |
---|
| 57 | /** |
---|
[1019] | 58 | * <p> |
---|
[1022] | 59 | * instantiates a new element representing a tag in an HTML page |
---|
[1019] | 60 | * </p> |
---|
| 61 | * |
---|
[1069] | 62 | * @param id the id of the page element |
---|
| 63 | * @param document the document to which the represented tag belongs |
---|
| 64 | * @param parent the parent page element; if null, the document is considered the parent |
---|
| 65 | * @param tagName the name of the represented tag |
---|
| 66 | * @param htmlId the id of the tag in the DOM |
---|
| 67 | * @param index the index of the represented tag regarding all tags with the same |
---|
| 68 | * tag name in the list of children of the parent tag |
---|
[1019] | 69 | */ |
---|
[1069] | 70 | HtmlPageElement(String id, |
---|
| 71 | HtmlDocument document, |
---|
| 72 | HtmlPageElement parent, |
---|
| 73 | String tagName, |
---|
| 74 | String htmlId, |
---|
| 75 | Integer index) |
---|
| 76 | { |
---|
| 77 | super(id, parent == null ? document : parent); |
---|
| 78 | |
---|
| 79 | if (document == null) { |
---|
| 80 | throw new IllegalArgumentException("document must not be null"); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | if (tagName == null) { |
---|
| 84 | throw new IllegalArgumentException("tagName must not be null"); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | if ((htmlId == null) && (index == null)) { |
---|
| 88 | throw new IllegalArgumentException("either one of htmlId and index must not be null"); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | this.document = document; |
---|
| 92 | this.parent = parent; |
---|
[1019] | 93 | this.tagName = tagName; |
---|
[1069] | 94 | this.htmlId = htmlId; |
---|
[1019] | 95 | this.index = index; |
---|
[1089] | 96 | |
---|
| 97 | this.hashCode = this.document.hashCode() + this.tagName.hashCode() + |
---|
| 98 | (this.parent != null ? this.parent.hashCode() : 0) + |
---|
| 99 | (this.htmlId != null ? this.htmlId.hashCode() : 0) + |
---|
| 100 | (this.index != null ? this.index : 0); |
---|
[1019] | 101 | } |
---|
| 102 | |
---|
| 103 | /** |
---|
| 104 | * <p> |
---|
[1069] | 105 | * returns the document to which the represented tag belongs |
---|
[1019] | 106 | * </p> |
---|
| 107 | * |
---|
[1069] | 108 | * @return the document |
---|
[1019] | 109 | */ |
---|
[1069] | 110 | HtmlDocument getDocument() { |
---|
| 111 | return document; |
---|
[1019] | 112 | } |
---|
| 113 | |
---|
| 114 | /** |
---|
[1022] | 115 | * <p> |
---|
[1069] | 116 | * returns the name of the tag represented by this page element. |
---|
[1022] | 117 | * </p> |
---|
| 118 | * |
---|
[1019] | 119 | * @return the tagName |
---|
| 120 | */ |
---|
| 121 | String getTagName() { |
---|
| 122 | return tagName; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | /** |
---|
[1022] | 126 | * <p> |
---|
[1069] | 127 | * returns the id of the page element. |
---|
[1022] | 128 | * </p> |
---|
| 129 | * |
---|
[1019] | 130 | * @return the id |
---|
| 131 | */ |
---|
[1069] | 132 | String getHtmlId() { |
---|
| 133 | return htmlId; |
---|
[1019] | 134 | } |
---|
| 135 | |
---|
| 136 | /** |
---|
[1022] | 137 | * <p> |
---|
| 138 | * returns the index of this element regarding all elements with the same tag name in the list |
---|
| 139 | * of children of the parent element |
---|
| 140 | * </p> |
---|
| 141 | * |
---|
[1019] | 142 | * @return the index |
---|
| 143 | */ |
---|
| 144 | Integer getIndex() { |
---|
| 145 | return index; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | /** |
---|
[1022] | 149 | * <p> |
---|
| 150 | * calculates and returns the path to the represented page element through the DOM. Includes |
---|
| 151 | * the parent path if any. The represented element is included in the path with its tag name |
---|
| 152 | * and its id if it has one, or its index. |
---|
[1019] | 153 | * </p> |
---|
| 154 | * |
---|
[1022] | 155 | * @return as described |
---|
[1019] | 156 | */ |
---|
[1069] | 157 | String getDOMPath() { |
---|
[1019] | 158 | StringBuffer result = new StringBuffer(); |
---|
[1069] | 159 | if (parent != null) { |
---|
| 160 | result.append(parent.getDOMPath()); |
---|
[1019] | 161 | } |
---|
| 162 | |
---|
| 163 | result.append("/"); |
---|
| 164 | result.append(tagName); |
---|
| 165 | |
---|
[1069] | 166 | if ((htmlId != null) && (!"".equals(htmlId))) { |
---|
| 167 | result.append("(htmlId="); |
---|
| 168 | result.append(htmlId); |
---|
[1019] | 169 | result.append(")"); |
---|
| 170 | } |
---|
| 171 | else { |
---|
| 172 | result.append("["); |
---|
| 173 | result.append(index); |
---|
| 174 | result.append("]"); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | return result.toString(); |
---|
| 178 | } |
---|
| 179 | |
---|
[1069] | 180 | /* (non-Javadoc) |
---|
[1075] | 181 | * @see java.lang.Object#equals(java.lang.Object) |
---|
[1069] | 182 | */ |
---|
| 183 | @Override |
---|
[1075] | 184 | public boolean equals(Object obj) { |
---|
[1069] | 185 | if (this == obj) { |
---|
| 186 | return true; |
---|
| 187 | } |
---|
| 188 | else if (obj instanceof HtmlPageElement) { |
---|
| 189 | return equals((HtmlPageElement) obj); |
---|
| 190 | } |
---|
| 191 | else { |
---|
| 192 | return false; |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | /* (non-Javadoc) |
---|
| 197 | * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlGUIElement#equals(de.ugoe.cs.autoquest.htmlmonitor.HtmlGUIElement) |
---|
| 198 | */ |
---|
| 199 | public boolean equals(HtmlPageElement other) { |
---|
| 200 | if (this == other) { |
---|
| 201 | return true; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | return (document.equals(other.document) && tagName.equals(other.tagName) && |
---|
| 205 | (parent != null ? parent.equals(other.parent) : other.parent == null) && |
---|
| 206 | (htmlId != null ? htmlId.equals(other.htmlId) : other.htmlId == null) && |
---|
| 207 | (index != null ? index.equals(other.index) : other.index == null)); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | /* (non-Javadoc) |
---|
| 211 | * @see java.lang.Object#hashCode() |
---|
| 212 | */ |
---|
| 213 | @Override |
---|
| 214 | public int hashCode() { |
---|
[1089] | 215 | return hashCode; |
---|
[1069] | 216 | } |
---|
| 217 | |
---|
[1019] | 218 | } |
---|