source: trunk/autoquest-htmlmonitor/src/main/java/de/ugoe/cs/autoquest/htmlmonitor/HtmlPageElement.java @ 1174

Last change on this file since 1174 was 1089, checked in by pharms, 11 years ago
  • prevent logging of the same GUI elements several times
  • improved efficiency of hash code calculation
  • improved efficiency of reuse of GUI elements
File size: 6.1 KB
Line 
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
15package 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 */
24class 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     * the hash code of this document
54     */
55    private int hashCode;
56   
57    /**
58     * <p>
59     * instantiates a new element representing a tag in an HTML page
60     * </p>
61     *
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
69     */
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;
93        this.tagName = tagName;
94        this.htmlId = htmlId;
95        this.index = index;
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);
101    }
102
103    /**
104     * <p>
105     * returns the document to which the represented tag belongs
106     * </p>
107     *
108     * @return the document
109     */
110    HtmlDocument getDocument() {
111        return document;
112    }
113
114    /**
115     * <p>
116     * returns the name of the tag represented by this page element.
117     * </p>
118     *
119     * @return the tagName
120     */
121    String getTagName() {
122        return tagName;
123    }
124
125    /**
126     * <p>
127     * returns the id of the page element.
128     * </p>
129     *
130     * @return the id
131     */
132    String getHtmlId() {
133        return htmlId;
134    }
135
136    /**
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     *
142     * @return the index
143     */
144    Integer getIndex() {
145        return index;
146    }
147
148    /**
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.
153     * </p>
154     *
155     * @return as described
156     */
157    String getDOMPath() {
158        StringBuffer result = new StringBuffer();
159        if (parent != null) {
160            result.append(parent.getDOMPath());
161        }
162
163        result.append("/");
164        result.append(tagName);
165       
166        if ((htmlId != null) && (!"".equals(htmlId))) {
167            result.append("(htmlId=");
168            result.append(htmlId);
169            result.append(")");
170        }
171        else {
172            result.append("[");
173            result.append(index);
174            result.append("]");
175        }
176       
177        return result.toString();
178    }
179
180    /* (non-Javadoc)
181     * @see java.lang.Object#equals(java.lang.Object)
182     */
183    @Override
184    public boolean equals(Object obj) {
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() {
215        return hashCode;
216    }
217
218}
Note: See TracBrowser for help on using the repository browser.