[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 | |
---|
[2146] | 17 | import de.ugoe.cs.autoquest.eventcore.IEventTargetSpec; |
---|
[961] | 18 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec; |
---|
| 19 | |
---|
| 20 | /** |
---|
| 21 | * <p> |
---|
[1276] | 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. |
---|
[961] | 26 | * </p> |
---|
| 27 | * |
---|
| 28 | * @author Patrick Harms |
---|
| 29 | */ |
---|
| 30 | public class HTMLPageElementSpec extends HTMLGUIElementSpec implements IGUIElementSpec { |
---|
| 31 | |
---|
[1276] | 32 | /** |
---|
| 33 | * <p> |
---|
| 34 | * default serial version UID |
---|
| 35 | * </p> |
---|
| 36 | */ |
---|
[961] | 37 | private static final long serialVersionUID = 1L; |
---|
| 38 | |
---|
[1276] | 39 | /** |
---|
| 40 | * <p> |
---|
| 41 | * the page to which the represented tag belongs |
---|
| 42 | * </p> |
---|
| 43 | */ |
---|
[1069] | 44 | private HTMLDocumentSpec page; |
---|
[961] | 45 | |
---|
[1276] | 46 | /** |
---|
| 47 | * <p> |
---|
| 48 | * the name of the tag represented by this specification |
---|
| 49 | * </p> |
---|
| 50 | */ |
---|
[1069] | 51 | private String tagName; |
---|
[961] | 52 | |
---|
[1276] | 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 | */ |
---|
[1069] | 59 | private String htmlId; |
---|
[961] | 60 | |
---|
[1276] | 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 | */ |
---|
[961] | 69 | private int index; |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | * <p> |
---|
[1276] | 73 | * initializes the specification with the page to which the represented tag belongs, the tags |
---|
| 74 | * name, its id or its index. |
---|
[961] | 75 | * </p> |
---|
| 76 | * |
---|
[1276] | 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 |
---|
[961] | 84 | */ |
---|
[1069] | 85 | public HTMLPageElementSpec(HTMLDocumentSpec page, String tagName, String htmlId, int index) { |
---|
[1436] | 86 | super(tagName, (page != null ? page.hashCode() : 0) + |
---|
| 87 | (tagName != null ? tagName.hashCode() : 0) + |
---|
| 88 | (htmlId != null ? htmlId.hashCode() : 0) + (index >= 0 ? index : 0)); |
---|
[961] | 89 | |
---|
| 90 | if (page == null) { |
---|
| 91 | throw new IllegalArgumentException("page must not be null"); |
---|
| 92 | } |
---|
[1069] | 93 | else if (tagName == null) { |
---|
[961] | 94 | throw new IllegalArgumentException("tag must not be null"); |
---|
| 95 | } |
---|
[1069] | 96 | else if ((htmlId == null) && (index < 0)) { |
---|
[961] | 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; |
---|
[1069] | 102 | this.tagName = tagName; |
---|
| 103 | this.htmlId = htmlId; |
---|
[961] | 104 | this.index = index; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | /* (non-Javadoc) |
---|
| 108 | * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#getSimilarity(IGUIElementSpec) |
---|
| 109 | */ |
---|
| 110 | @Override |
---|
[2146] | 111 | public boolean getSimilarity(IEventTargetSpec other) { |
---|
[961] | 112 | if (other instanceof HTMLPageElementSpec) { |
---|
| 113 | HTMLPageElementSpec otherSpec = (HTMLPageElementSpec) other; |
---|
| 114 | |
---|
| 115 | if (!super.getSimilarity(otherSpec)) { |
---|
| 116 | return false; |
---|
| 117 | } |
---|
[1264] | 118 | /*else if (!page.getSimilarity(otherSpec.page)) { |
---|
[961] | 119 | return false; |
---|
[1264] | 120 | }*/ |
---|
[1069] | 121 | else if (!tagName.equals(otherSpec.tagName)) { |
---|
[961] | 122 | return false; |
---|
| 123 | } |
---|
| 124 | |
---|
[1069] | 125 | if (htmlId != null) { |
---|
| 126 | return htmlId.equals(otherSpec.htmlId); |
---|
[961] | 127 | } |
---|
| 128 | else if (index >= 0) { |
---|
| 129 | return index == otherSpec.index; |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | return false; |
---|
| 134 | } |
---|
| 135 | |
---|
[1264] | 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 | |
---|
[961] | 153 | /** |
---|
| 154 | * <p> |
---|
[1276] | 155 | * returns the page to which the represented tag belongs |
---|
[961] | 156 | * </p> |
---|
| 157 | * |
---|
[1276] | 158 | * @return the page to which the represented tag belongs |
---|
[961] | 159 | */ |
---|
[1069] | 160 | HTMLDocumentSpec getPage() { |
---|
[961] | 161 | return page; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | /** |
---|
| 165 | * <p> |
---|
[1276] | 166 | * returns the name of the tag represented by this specification |
---|
[961] | 167 | * </p> |
---|
| 168 | * |
---|
[1276] | 169 | * @return the name of the tag represented by this specification |
---|
[961] | 170 | */ |
---|
[1069] | 171 | String getTagName() { |
---|
| 172 | return tagName; |
---|
[961] | 173 | } |
---|
| 174 | |
---|
| 175 | /** |
---|
| 176 | * <p> |
---|
[1276] | 177 | * returns the id of the tag represented by this specification |
---|
[961] | 178 | * </p> |
---|
| 179 | * |
---|
[1276] | 180 | * @return the id of the tag represented by this specification |
---|
[961] | 181 | */ |
---|
[1069] | 182 | String getHtmlId() { |
---|
| 183 | return htmlId; |
---|
[961] | 184 | } |
---|
| 185 | |
---|
| 186 | /** |
---|
| 187 | * <p> |
---|
[1276] | 188 | * returns the index of the tag |
---|
[961] | 189 | * </p> |
---|
| 190 | * |
---|
[1276] | 191 | * @return the index of the tag |
---|
[961] | 192 | */ |
---|
[1069] | 193 | int getIndex() { |
---|
[961] | 194 | return index; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | } |
---|