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

Last change on this file since 1876 was 1876, checked in by pharms, 9 years ago
  • added support for views in 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.guimodel.IGUIElement;
18import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIView;
19
20/**
21 * <p>
22 * This is a GUI element representing tags in HTML documents. Each tag belongs to a certain
23 * 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 HTMLPageElement extends HTMLGUIElement {
30
31    /**
32     * <p>
33     * default serial version UID
34     * </p>
35     */
36    private static final long serialVersionUID = 1L;
37
38    /**
39     * <p>
40     * instantiates a page element with it parent (either a document or another page element)
41     * </p>
42     *
43     * @param specification the specification of the page element
44     * @param parent        the parent GUI element
45     */
46    public HTMLPageElement(HTMLPageElementSpec specification, HTMLGUIElement parent) {
47        super(specification, parent);
48    }
49
50    /**
51     * <p>
52     * returns the name of the tag represented by this specification
53     * </p>
54     *
55     * @return the name of the tag represented by this specification
56     */
57    public String getTagName() {
58        return ((HTMLPageElementSpec) super.getSpecification()).getTagName();
59    }
60
61    /**
62     * <p>
63     * returns the id of the tag represented by this specification
64     * </p>
65     *
66     * @return the id of the tag represented by this specification
67     */
68    public String getHtmlId() {
69        return ((HTMLPageElementSpec) super.getSpecification()).getHtmlId();
70    }
71
72    /**
73     * <p>
74     * returns the index of the tag
75     * </p>
76     *
77     * @return the index of the tag
78     */
79    public int getIndex() {
80        return ((HTMLPageElementSpec) super.getSpecification()).getIndex();
81    }
82
83    /* (non-Javadoc)
84     * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement#getView()
85     */
86    @Override
87    public IGUIView getView() {
88        return ((HTMLPageElementSpec) super.getSpecification()).getPage();
89    }
90
91    /* (non-Javadoc)
92     * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement#getDistanceTo(IGUIElement)
93     */
94    @Override
95    public double getDistanceTo(IGUIElement otherElement) {
96        if ((otherElement instanceof HTMLServer) || (otherElement instanceof HTMLDocument)) {
97            // use the implementation provided by the server or document
98            return otherElement.getDistanceTo(this);
99        }
100        else if (otherElement instanceof HTMLPageElement) {
101            if (equals(otherElement)) {
102                return DISTANCE_NONE;
103            }
104           
105            HTMLDocumentSpec documentSpec1 =
106                ((HTMLPageElementSpec) this.getSpecification()).getPage();
107            HTMLDocumentSpec documentSpec2 =
108                ((HTMLPageElementSpec) otherElement.getSpecification()).getPage();
109           
110            if (!documentSpec1.getSimilarity(documentSpec2)) {
111                if (!documentSpec1.getServer().getSimilarity(documentSpec2.getServer())) {
112                    return DISTANCE_DISTINCT_SERVER;
113                }
114                else {
115                    return DISTANCE_SAME_SERVER;
116                }
117            }
118            else {
119                // check if they have the same parent div element. If so, they are very close.
120                // If not, they may be structured completely differently
121                IGUIElement parentDiv1 = this;
122               
123                while (parentDiv1 != null) {
124                    if ((parentDiv1 instanceof HTMLPageElement) &&
125                        ("div".equals(((HTMLPageElement) parentDiv1).getTagName())))
126                    {
127                        break;
128                    }
129                    else {
130                        parentDiv1 = parentDiv1.getParent();
131                    }
132                }
133               
134                IGUIElement parentDiv2 = (HTMLPageElement) otherElement;
135               
136                while (parentDiv2 != null) {
137                    if ((parentDiv2 instanceof HTMLPageElement) &&
138                        ("div".equals(((HTMLPageElement) parentDiv2).getTagName())))
139                    {
140                        break;
141                    }
142                    else {
143                        parentDiv2 = parentDiv2.getParent();
144                    }
145                }
146               
147                // a check for the identity of the objects is sufficient. That they resist on the
148                // same document was checked beforehand. So even a condense of the GUI model should
149                // not cause an invalid result here.
150                if ((parentDiv1 == parentDiv2) ||
151                    ((parentDiv1 != null) && (parentDiv1.equals(parentDiv2))))
152                {
153                    return DISTANCE_SAME_DIV;
154                }
155                else {
156                    return DISTANCE_SAME_DOCUMENT;
157                }
158            }
159        }
160       
161        return DISTANCE_DISTINCT_SERVER;
162    }
163   
164    /*
165     * (non-Javadoc)
166     *
167     * @see de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLGUIElement#getElementDescriptor()
168     */
169    @Override
170    protected String getElementDescriptor() {
171        return getTagName();
172    }
173
174}
Note: See TracBrowser for help on using the repository browser.