source: trunk/autoquest-plugin-html/src/main/java/de/ugoe/cs/autoquest/plugin/html/guimodel/HTMLPageElementSpec.java @ 1876

Last change on this file since 1876 was 1436, checked in by pharms, 10 years ago
  • corrected a bug in creating hashcodes for GUI elements (they changed after storing a GUI model to a file and then reloading it)
File size: 5.8 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.plugin.html.guimodel;
16
17import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec;
18
19/**
20 * <p>
21 * This is a GUI element specification for tags in HTML documents. Each tag belongs to a certain
22 * document. However, for similarity comparison, two page elements are similar even if they do
23 * not belong to the same document. Each page element has a tag name and either an id or at least
24 * an index in the list of siblings of the same type.
25 * </p>
26 *
27 * @author Patrick Harms
28 */
29public class HTMLPageElementSpec extends HTMLGUIElementSpec implements IGUIElementSpec {
30
31    /**
32     * <p>
33     * default serial version UID
34     * </p>
35     */
36    private static final long serialVersionUID = 1L;
37   
38    /**
39     * <p>
40     * the page to which the represented tag belongs
41     * </p>
42     */
43    private HTMLDocumentSpec page;
44   
45    /**
46     * <p>
47     * the name of the tag represented by this specification
48     * </p>
49     */
50    private String tagName;
51   
52    /**
53     * <p>
54     * the id of the tag represented by this specification, i.e., the value of the id attribute
55     * of the tag. May be null in the case the id attribute of the tag is not set.
56     * </p>
57     */
58    private String htmlId;
59   
60    /**
61     * <p>
62     * the index of the tag (0 based) in the list of siblings in the same parent being of the
63     * same type. If, e.g., a parent has three li tags as children, the first will have index 0,
64     * the second index 1 and the third index2. The indexes are ignored, if the tag have an
65     * assigned id.
66     * </p>
67     */
68    private int index;
69
70    /**
71     * <p>
72     * initializes the specification with the page to which the represented tag belongs, the tags
73     * name, its id or its index.
74     * </p>
75     *
76     * @param page    the page to which the represented tag belongs
77     * @param tagName the name of the tag represented by this specification
78     * @param htmlId  the id of the tag represented by this specification
79     * @param index   the index of the tag
80     *
81     * @throws IllegalArgumentException if page and name are null or if neither an id nor an index
82     *                                  are provided correctly
83     */
84    public HTMLPageElementSpec(HTMLDocumentSpec page, String tagName, String htmlId, int index) {
85        super(tagName, (page != null ? page.hashCode() : 0) +
86              (tagName != null ? tagName.hashCode() : 0) +
87              (htmlId != null ? htmlId.hashCode() : 0) + (index >= 0 ? index : 0));
88       
89        if (page == null) {
90            throw new IllegalArgumentException("page must not be null");
91        }
92        else if (tagName == null) {
93            throw new IllegalArgumentException("tag must not be null");
94        }
95        else if ((htmlId == null) && (index < 0)) {
96            throw new IllegalArgumentException
97                ("either id must not be null or the index must be greater or equal to 0");
98        }
99       
100        this.page = page;
101        this.tagName = tagName;
102        this.htmlId = htmlId;
103        this.index = index;
104    }
105
106    /* (non-Javadoc)
107     * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#getSimilarity(IGUIElementSpec)
108     */
109    @Override
110    public boolean getSimilarity(IGUIElementSpec other) {
111        if (other instanceof HTMLPageElementSpec) {
112            HTMLPageElementSpec otherSpec = (HTMLPageElementSpec) other;
113           
114            if (!super.getSimilarity(otherSpec)) {
115                return false;
116            }
117            /*else if (!page.getSimilarity(otherSpec.page)) {
118                return false;
119            }*/
120            else if (!tagName.equals(otherSpec.tagName)) {
121                return false;
122            }
123           
124            if (htmlId != null) {
125                return htmlId.equals(otherSpec.htmlId);
126            }
127            else if (index >= 0) {
128                return index == otherSpec.index;
129            }
130        }
131       
132        return false;
133    }
134
135    /* (non-Javadoc)
136     * @see java.lang.Object#toString()
137     */
138    @Override
139    public String toString() {
140        String str = getTagName();
141       
142        if ((getHtmlId() != null) && (!"".equals(getHtmlId()))) {
143            str += "(id=\"" + getHtmlId() + "\")";
144        }
145        else {
146            str += "[" + getIndex() + "]";
147        }
148       
149        return str;
150    }
151
152    /**
153     * <p>
154     * returns the page to which the represented tag belongs
155     * </p>
156     *
157     * @return the page to which the represented tag belongs
158     */
159    HTMLDocumentSpec getPage() {
160        return page;
161    }
162
163    /**
164     * <p>
165     * returns the name of the tag represented by this specification
166     * </p>
167     *
168     * @return the name of the tag represented by this specification
169     */
170    String getTagName() {
171        return tagName;
172    }
173
174    /**
175     * <p>
176     * returns the id of the tag represented by this specification
177     * </p>
178     *
179     * @return the id of the tag represented by this specification
180     */
181    String getHtmlId() {
182        return htmlId;
183    }
184
185    /**
186     * <p>
187     * returns the index of the tag
188     * </p>
189     *
190     * @return the index of the tag
191     */
192    int getIndex() {
193        return index;
194    }
195
196}
Note: See TracBrowser for help on using the repository browser.