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

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