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

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