[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 | |
---|
| 15 | package de.ugoe.cs.autoquest.plugin.html.guimodel; |
---|
| 16 | |
---|
[1490] | 17 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement; |
---|
[1876] | 18 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIView; |
---|
[1490] | 19 | |
---|
[961] | 20 | /** |
---|
| 21 | * <p> |
---|
[1276] | 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. |
---|
[961] | 25 | * </p> |
---|
| 26 | * |
---|
| 27 | * @author Patrick Harms |
---|
| 28 | */ |
---|
[989] | 29 | public class HTMLPageElement extends HTMLGUIElement { |
---|
[961] | 30 | |
---|
| 31 | /** |
---|
| 32 | * <p> |
---|
[1276] | 33 | * default serial version UID |
---|
[961] | 34 | * </p> |
---|
| 35 | */ |
---|
[1276] | 36 | private static final long serialVersionUID = 1L; |
---|
[961] | 37 | |
---|
| 38 | /** |
---|
| 39 | * <p> |
---|
[1276] | 40 | * instantiates a page element with it parent (either a document or another page element) |
---|
[961] | 41 | * </p> |
---|
| 42 | * |
---|
[1276] | 43 | * @param specification the specification of the page element |
---|
| 44 | * @param parent the parent GUI element |
---|
[961] | 45 | */ |
---|
[1276] | 46 | public HTMLPageElement(HTMLPageElementSpec specification, HTMLGUIElement parent) { |
---|
[961] | 47 | super(specification, parent); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * <p> |
---|
[1276] | 52 | * returns the name of the tag represented by this specification |
---|
[961] | 53 | * </p> |
---|
| 54 | * |
---|
[1276] | 55 | * @return the name of the tag represented by this specification |
---|
[961] | 56 | */ |
---|
[1264] | 57 | public String getTagName() { |
---|
| 58 | return ((HTMLPageElementSpec) super.getSpecification()).getTagName(); |
---|
[961] | 59 | } |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | * <p> |
---|
[1276] | 63 | * returns the id of the tag represented by this specification |
---|
[961] | 64 | * </p> |
---|
| 65 | * |
---|
[1276] | 66 | * @return the id of the tag represented by this specification |
---|
[961] | 67 | */ |
---|
[1264] | 68 | public String getHtmlId() { |
---|
| 69 | return ((HTMLPageElementSpec) super.getSpecification()).getHtmlId(); |
---|
[961] | 70 | } |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | * <p> |
---|
[1276] | 74 | * returns the index of the tag |
---|
[961] | 75 | * </p> |
---|
| 76 | * |
---|
[1276] | 77 | * @return the index of the tag |
---|
[961] | 78 | */ |
---|
[1264] | 79 | public int getIndex() { |
---|
| 80 | return ((HTMLPageElementSpec) super.getSpecification()).getIndex(); |
---|
[961] | 81 | } |
---|
| 82 | |
---|
[1490] | 83 | /* (non-Javadoc) |
---|
[1876] | 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) |
---|
[1490] | 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) { |
---|
[1496] | 101 | if (equals(otherElement)) { |
---|
[1490] | 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 | |
---|
[1264] | 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 | |
---|
[961] | 174 | } |
---|