[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 | |
---|
| 17 | import 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 | */ |
---|
| 29 | public 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) { |
---|
| 85 | super(tagName); |
---|
[961] | 86 | |
---|
| 87 | if (page == null) { |
---|
| 88 | throw new IllegalArgumentException("page must not be null"); |
---|
| 89 | } |
---|
[1069] | 90 | else if (tagName == null) { |
---|
[961] | 91 | throw new IllegalArgumentException("tag must not be null"); |
---|
| 92 | } |
---|
[1069] | 93 | else if ((htmlId == null) && (index < 0)) { |
---|
[961] | 94 | throw new IllegalArgumentException |
---|
| 95 | ("either id must not be null or the index must be greater or equal to 0"); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | this.page = page; |
---|
[1069] | 99 | this.tagName = tagName; |
---|
| 100 | this.htmlId = htmlId; |
---|
[961] | 101 | this.index = index; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | /* (non-Javadoc) |
---|
| 105 | * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#getSimilarity(IGUIElementSpec) |
---|
| 106 | */ |
---|
| 107 | @Override |
---|
| 108 | public boolean getSimilarity(IGUIElementSpec other) { |
---|
| 109 | if (other instanceof HTMLPageElementSpec) { |
---|
| 110 | HTMLPageElementSpec otherSpec = (HTMLPageElementSpec) other; |
---|
| 111 | |
---|
| 112 | if (!super.getSimilarity(otherSpec)) { |
---|
| 113 | return false; |
---|
| 114 | } |
---|
[1264] | 115 | /*else if (!page.getSimilarity(otherSpec.page)) { |
---|
[961] | 116 | return false; |
---|
[1264] | 117 | }*/ |
---|
[1069] | 118 | else if (!tagName.equals(otherSpec.tagName)) { |
---|
[961] | 119 | return false; |
---|
| 120 | } |
---|
| 121 | |
---|
[1069] | 122 | if (htmlId != null) { |
---|
| 123 | return htmlId.equals(otherSpec.htmlId); |
---|
[961] | 124 | } |
---|
| 125 | else if (index >= 0) { |
---|
| 126 | return index == otherSpec.index; |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | return false; |
---|
| 131 | } |
---|
| 132 | |
---|
[1264] | 133 | /* (non-Javadoc) |
---|
| 134 | * @see java.lang.Object#toString() |
---|
| 135 | */ |
---|
| 136 | @Override |
---|
| 137 | public String toString() { |
---|
| 138 | String str = getTagName(); |
---|
| 139 | |
---|
| 140 | if ((getHtmlId() != null) && (!"".equals(getHtmlId()))) { |
---|
| 141 | str += "(id=\"" + getHtmlId() + "\")"; |
---|
| 142 | } |
---|
| 143 | else { |
---|
| 144 | str += "[" + getIndex() + "]"; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | return str; |
---|
| 148 | } |
---|
| 149 | |
---|
[961] | 150 | /** |
---|
| 151 | * <p> |
---|
[1276] | 152 | * returns the page to which the represented tag belongs |
---|
[961] | 153 | * </p> |
---|
| 154 | * |
---|
[1276] | 155 | * @return the page to which the represented tag belongs |
---|
[961] | 156 | */ |
---|
[1069] | 157 | HTMLDocumentSpec getPage() { |
---|
[961] | 158 | return page; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | /** |
---|
| 162 | * <p> |
---|
[1276] | 163 | * returns the name of the tag represented by this specification |
---|
[961] | 164 | * </p> |
---|
| 165 | * |
---|
[1276] | 166 | * @return the name of the tag represented by this specification |
---|
[961] | 167 | */ |
---|
[1069] | 168 | String getTagName() { |
---|
| 169 | return tagName; |
---|
[961] | 170 | } |
---|
| 171 | |
---|
| 172 | /** |
---|
| 173 | * <p> |
---|
[1276] | 174 | * returns the id of the tag represented by this specification |
---|
[961] | 175 | * </p> |
---|
| 176 | * |
---|
[1276] | 177 | * @return the id of the tag represented by this specification |
---|
[961] | 178 | */ |
---|
[1069] | 179 | String getHtmlId() { |
---|
| 180 | return htmlId; |
---|
[961] | 181 | } |
---|
| 182 | |
---|
| 183 | /** |
---|
| 184 | * <p> |
---|
[1276] | 185 | * returns the index of the tag |
---|
[961] | 186 | * </p> |
---|
| 187 | * |
---|
[1276] | 188 | * @return the index of the tag |
---|
[961] | 189 | */ |
---|
[1069] | 190 | int getIndex() { |
---|
[961] | 191 | return index; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | } |
---|