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.plugin.html.guimodel; |
---|
16 | |
---|
17 | import 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 | */ |
---|
29 | public 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); |
---|
86 | |
---|
87 | if (page == null) { |
---|
88 | throw new IllegalArgumentException("page must not be null"); |
---|
89 | } |
---|
90 | else if (tagName == null) { |
---|
91 | throw new IllegalArgumentException("tag must not be null"); |
---|
92 | } |
---|
93 | else if ((htmlId == null) && (index < 0)) { |
---|
94 | throw new IllegalArgumentException |
---|
95 | ("either id must not be null or the index must be greater or equal to 0"); |
---|
96 | } |
---|
97 | |
---|
98 | this.page = page; |
---|
99 | this.tagName = tagName; |
---|
100 | this.htmlId = htmlId; |
---|
101 | this.index = index; |
---|
102 | } |
---|
103 | |
---|
104 | /* (non-Javadoc) |
---|
105 | * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#getSimilarity(IGUIElementSpec) |
---|
106 | */ |
---|
107 | @Override |
---|
108 | public boolean getSimilarity(IGUIElementSpec other) { |
---|
109 | if (other instanceof HTMLPageElementSpec) { |
---|
110 | HTMLPageElementSpec otherSpec = (HTMLPageElementSpec) other; |
---|
111 | |
---|
112 | if (!super.getSimilarity(otherSpec)) { |
---|
113 | return false; |
---|
114 | } |
---|
115 | /*else if (!page.getSimilarity(otherSpec.page)) { |
---|
116 | return false; |
---|
117 | }*/ |
---|
118 | else if (!tagName.equals(otherSpec.tagName)) { |
---|
119 | return false; |
---|
120 | } |
---|
121 | |
---|
122 | if (htmlId != null) { |
---|
123 | return htmlId.equals(otherSpec.htmlId); |
---|
124 | } |
---|
125 | else if (index >= 0) { |
---|
126 | return index == otherSpec.index; |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | return false; |
---|
131 | } |
---|
132 | |
---|
133 | /* (non-Javadoc) |
---|
134 | * @see java.lang.Object#toString() |
---|
135 | */ |
---|
136 | @Override |
---|
137 | public String toString() { |
---|
138 | String str = getTagName(); |
---|
139 | |
---|
140 | if ((getHtmlId() != null) && (!"".equals(getHtmlId()))) { |
---|
141 | str += "(id=\"" + getHtmlId() + "\")"; |
---|
142 | } |
---|
143 | else { |
---|
144 | str += "[" + getIndex() + "]"; |
---|
145 | } |
---|
146 | |
---|
147 | return str; |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * <p> |
---|
152 | * returns the page to which the represented tag belongs |
---|
153 | * </p> |
---|
154 | * |
---|
155 | * @return the page to which the represented tag belongs |
---|
156 | */ |
---|
157 | HTMLDocumentSpec getPage() { |
---|
158 | return page; |
---|
159 | } |
---|
160 | |
---|
161 | /** |
---|
162 | * <p> |
---|
163 | * returns the name of the tag represented by this specification |
---|
164 | * </p> |
---|
165 | * |
---|
166 | * @return the name of the tag represented by this specification |
---|
167 | */ |
---|
168 | String getTagName() { |
---|
169 | return tagName; |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | * <p> |
---|
174 | * returns the id of the tag represented by this specification |
---|
175 | * </p> |
---|
176 | * |
---|
177 | * @return the id of the tag represented by this specification |
---|
178 | */ |
---|
179 | String getHtmlId() { |
---|
180 | return htmlId; |
---|
181 | } |
---|
182 | |
---|
183 | /** |
---|
184 | * <p> |
---|
185 | * returns the index of the tag |
---|
186 | * </p> |
---|
187 | * |
---|
188 | * @return the index of the tag |
---|
189 | */ |
---|
190 | int getIndex() { |
---|
191 | return index; |
---|
192 | } |
---|
193 | |
---|
194 | } |
---|