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

Last change on this file since 1436 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
RevLine 
[961]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>
[1276]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.
[961]25 * </p>
26 *
27 * @author Patrick Harms
28 */
29public class HTMLPageElementSpec extends HTMLGUIElementSpec implements IGUIElementSpec {
30
[1276]31    /**
32     * <p>
33     * default serial version UID
34     * </p>
35     */
[961]36    private static final long serialVersionUID = 1L;
37   
[1276]38    /**
39     * <p>
40     * the page to which the represented tag belongs
41     * </p>
42     */
[1069]43    private HTMLDocumentSpec page;
[961]44   
[1276]45    /**
46     * <p>
47     * the name of the tag represented by this specification
48     * </p>
49     */
[1069]50    private String tagName;
[961]51   
[1276]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     */
[1069]58    private String htmlId;
[961]59   
[1276]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     */
[961]68    private int index;
69
70    /**
71     * <p>
[1276]72     * initializes the specification with the page to which the represented tag belongs, the tags
73     * name, its id or its index.
[961]74     * </p>
75     *
[1276]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
[961]83     */
[1069]84    public HTMLPageElementSpec(HTMLDocumentSpec page, String tagName, String htmlId, int index) {
[1436]85        super(tagName, (page != null ? page.hashCode() : 0) +
86              (tagName != null ? tagName.hashCode() : 0) +
87              (htmlId != null ? htmlId.hashCode() : 0) + (index >= 0 ? index : 0));
[961]88       
89        if (page == null) {
90            throw new IllegalArgumentException("page must not be null");
91        }
[1069]92        else if (tagName == null) {
[961]93            throw new IllegalArgumentException("tag must not be null");
94        }
[1069]95        else if ((htmlId == null) && (index < 0)) {
[961]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;
[1069]101        this.tagName = tagName;
102        this.htmlId = htmlId;
[961]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            }
[1264]117            /*else if (!page.getSimilarity(otherSpec.page)) {
[961]118                return false;
[1264]119            }*/
[1069]120            else if (!tagName.equals(otherSpec.tagName)) {
[961]121                return false;
122            }
123           
[1069]124            if (htmlId != null) {
125                return htmlId.equals(otherSpec.htmlId);
[961]126            }
127            else if (index >= 0) {
128                return index == otherSpec.index;
129            }
130        }
131       
132        return false;
133    }
134
[1264]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
[961]152    /**
153     * <p>
[1276]154     * returns the page to which the represented tag belongs
[961]155     * </p>
156     *
[1276]157     * @return the page to which the represented tag belongs
[961]158     */
[1069]159    HTMLDocumentSpec getPage() {
[961]160        return page;
161    }
162
163    /**
164     * <p>
[1276]165     * returns the name of the tag represented by this specification
[961]166     * </p>
167     *
[1276]168     * @return the name of the tag represented by this specification
[961]169     */
[1069]170    String getTagName() {
171        return tagName;
[961]172    }
173
174    /**
175     * <p>
[1276]176     * returns the id of the tag represented by this specification
[961]177     * </p>
178     *
[1276]179     * @return the id of the tag represented by this specification
[961]180     */
[1069]181    String getHtmlId() {
182        return htmlId;
[961]183    }
184
185    /**
186     * <p>
[1276]187     * returns the index of the tag
[961]188     * </p>
189     *
[1276]190     * @return the index of the tag
[961]191     */
[1069]192    int getIndex() {
[961]193        return index;
194    }
195
196}
Note: See TracBrowser for help on using the repository browser.