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> |
---|
19 | * represents an element of an HTML GUI, i.e. a tag within a web page. |
---|
20 | * </p> |
---|
21 | * |
---|
22 | * @author Patrick Harms |
---|
23 | */ |
---|
24 | class HtmlPageElement extends HtmlGUIElement { |
---|
25 | |
---|
26 | /** |
---|
27 | * the document to which the represented tag belongs |
---|
28 | */ |
---|
29 | private HtmlDocument document; |
---|
30 | |
---|
31 | /** |
---|
32 | * the parent page element; if null, the document is considered the parent |
---|
33 | */ |
---|
34 | private HtmlPageElement parent; |
---|
35 | |
---|
36 | /** |
---|
37 | * the name of the tag represented by this page element. |
---|
38 | */ |
---|
39 | private String tagName; |
---|
40 | |
---|
41 | /** |
---|
42 | * the id of the page element inside the DOM. |
---|
43 | */ |
---|
44 | private String htmlId; |
---|
45 | |
---|
46 | /** |
---|
47 | * the index of this element regarding all elements with the same tag name in the list of |
---|
48 | * children of the parent element |
---|
49 | */ |
---|
50 | private Integer index; |
---|
51 | |
---|
52 | /** |
---|
53 | * <p> |
---|
54 | * instantiates a new element representing a tag in an HTML page |
---|
55 | * </p> |
---|
56 | * |
---|
57 | * @param id the id of the page element |
---|
58 | * @param document the document to which the represented tag belongs |
---|
59 | * @param parent the parent page element; if null, the document is considered the parent |
---|
60 | * @param tagName the name of the represented tag |
---|
61 | * @param htmlId the id of the tag in the DOM |
---|
62 | * @param index the index of the represented tag regarding all tags with the same |
---|
63 | * tag name in the list of children of the parent tag |
---|
64 | */ |
---|
65 | HtmlPageElement(String id, |
---|
66 | HtmlDocument document, |
---|
67 | HtmlPageElement parent, |
---|
68 | String tagName, |
---|
69 | String htmlId, |
---|
70 | Integer index) |
---|
71 | { |
---|
72 | super(id, parent == null ? document : parent); |
---|
73 | |
---|
74 | if (document == null) { |
---|
75 | throw new IllegalArgumentException("document must not be null"); |
---|
76 | } |
---|
77 | |
---|
78 | if (tagName == null) { |
---|
79 | throw new IllegalArgumentException("tagName must not be null"); |
---|
80 | } |
---|
81 | |
---|
82 | if ((htmlId == null) && (index == null)) { |
---|
83 | throw new IllegalArgumentException("either one of htmlId and index must not be null"); |
---|
84 | } |
---|
85 | |
---|
86 | this.document = document; |
---|
87 | this.parent = parent; |
---|
88 | this.tagName = tagName; |
---|
89 | this.htmlId = htmlId; |
---|
90 | this.index = index; |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * <p> |
---|
95 | * returns the document to which the represented tag belongs |
---|
96 | * </p> |
---|
97 | * |
---|
98 | * @return the document |
---|
99 | */ |
---|
100 | HtmlDocument getDocument() { |
---|
101 | return document; |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * <p> |
---|
106 | * returns the name of the tag represented by this page element. |
---|
107 | * </p> |
---|
108 | * |
---|
109 | * @return the tagName |
---|
110 | */ |
---|
111 | String getTagName() { |
---|
112 | return tagName; |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * <p> |
---|
117 | * returns the id of the page element. |
---|
118 | * </p> |
---|
119 | * |
---|
120 | * @return the id |
---|
121 | */ |
---|
122 | String getHtmlId() { |
---|
123 | return htmlId; |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | * <p> |
---|
128 | * returns the index of this element regarding all elements with the same tag name in the list |
---|
129 | * of children of the parent element |
---|
130 | * </p> |
---|
131 | * |
---|
132 | * @return the index |
---|
133 | */ |
---|
134 | Integer getIndex() { |
---|
135 | return index; |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | * <p> |
---|
140 | * calculates and returns the path to the represented page element through the DOM. Includes |
---|
141 | * the parent path if any. The represented element is included in the path with its tag name |
---|
142 | * and its id if it has one, or its index. |
---|
143 | * </p> |
---|
144 | * |
---|
145 | * @return as described |
---|
146 | */ |
---|
147 | String getDOMPath() { |
---|
148 | StringBuffer result = new StringBuffer(); |
---|
149 | if (parent != null) { |
---|
150 | result.append(parent.getDOMPath()); |
---|
151 | } |
---|
152 | |
---|
153 | result.append("/"); |
---|
154 | result.append(tagName); |
---|
155 | |
---|
156 | if ((htmlId != null) && (!"".equals(htmlId))) { |
---|
157 | result.append("(htmlId="); |
---|
158 | result.append(htmlId); |
---|
159 | result.append(")"); |
---|
160 | } |
---|
161 | else { |
---|
162 | result.append("["); |
---|
163 | result.append(index); |
---|
164 | result.append("]"); |
---|
165 | } |
---|
166 | |
---|
167 | return result.toString(); |
---|
168 | } |
---|
169 | |
---|
170 | /* (non-Javadoc) |
---|
171 | * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlGUIElement#equals(de.ugoe.cs.autoquest.htmlmonitor.HtmlGUIElement) |
---|
172 | */ |
---|
173 | @Override |
---|
174 | public boolean equals(HtmlGUIElement obj) { |
---|
175 | if (this == obj) { |
---|
176 | return true; |
---|
177 | } |
---|
178 | else if (obj instanceof HtmlPageElement) { |
---|
179 | return equals((HtmlPageElement) obj); |
---|
180 | } |
---|
181 | else { |
---|
182 | return false; |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | /* (non-Javadoc) |
---|
187 | * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlGUIElement#equals(de.ugoe.cs.autoquest.htmlmonitor.HtmlGUIElement) |
---|
188 | */ |
---|
189 | public boolean equals(HtmlPageElement other) { |
---|
190 | if (this == other) { |
---|
191 | return true; |
---|
192 | } |
---|
193 | |
---|
194 | return (document.equals(other.document) && tagName.equals(other.tagName) && |
---|
195 | (parent != null ? parent.equals(other.parent) : other.parent == null) && |
---|
196 | (htmlId != null ? htmlId.equals(other.htmlId) : other.htmlId == null) && |
---|
197 | (index != null ? index.equals(other.index) : other.index == null)); |
---|
198 | } |
---|
199 | |
---|
200 | /* (non-Javadoc) |
---|
201 | * @see java.lang.Object#hashCode() |
---|
202 | */ |
---|
203 | @Override |
---|
204 | public int hashCode() { |
---|
205 | return document.hashCode() + tagName.hashCode() + (parent != null ? parent.hashCode() : 0) + |
---|
206 | (htmlId != null ? htmlId.hashCode() : 0) + (index != null ? index : 0); |
---|
207 | } |
---|
208 | |
---|
209 | } |
---|