[1051] | 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. |
---|
[1047] | 14 | |
---|
| 15 | package de.ugoe.cs.autoquest.plugin.html; |
---|
| 16 | |
---|
| 17 | import java.util.List; |
---|
| 18 | import java.util.Map; |
---|
[1069] | 19 | import java.util.regex.Matcher; |
---|
| 20 | import java.util.regex.Pattern; |
---|
[1047] | 21 | |
---|
| 22 | import org.xml.sax.SAXException; |
---|
| 23 | |
---|
| 24 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
[1059] | 25 | import de.ugoe.cs.autoquest.eventcore.IEventType; |
---|
[1047] | 26 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel; |
---|
[1084] | 27 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModelException; |
---|
[1047] | 28 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement; |
---|
| 29 | import de.ugoe.cs.autoquest.plugin.html.eventcore.HTMLEventTypeFactory; |
---|
[1069] | 30 | import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLDocumentSpec; |
---|
[1047] | 31 | import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLGUIElement; |
---|
| 32 | import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLGUIElementSpec; |
---|
[1059] | 33 | import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLPageElementSpec; |
---|
| 34 | import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLServerSpec; |
---|
[1047] | 35 | |
---|
| 36 | /** |
---|
| 37 | * <p> |
---|
| 38 | * This class provides the functionality to parse XML log files generated by the HTMLMonitor of |
---|
[1069] | 39 | * AutoQUEST. The result of parsing a file is a collection of event sequences and a GUI model |
---|
[1047] | 40 | * </p> |
---|
| 41 | * |
---|
[1069] | 42 | * @author Fabian Glaser, Patrick Harms |
---|
[1047] | 43 | * @version 1.0 |
---|
| 44 | * |
---|
| 45 | */ |
---|
[1069] | 46 | public class HTMLLogParser extends AbstractDefaultLogParser { |
---|
| 47 | |
---|
[1047] | 48 | /** |
---|
[1232] | 49 | * <p> |
---|
| 50 | * the pattern used for parsing HTML GUI element paths |
---|
| 51 | * </p> |
---|
[1047] | 52 | */ |
---|
[1069] | 53 | private Pattern htmlElementPattern = |
---|
| 54 | Pattern.compile("(\\w+)(\\[(\\d+)\\]|\\(htmlId=([\\w-]+)\\))"); |
---|
[1047] | 55 | |
---|
[1069] | 56 | /* (non-Javadoc) |
---|
[1232] | 57 | * @see de.ugoe.cs.autoquest.plugin.html.AbstractDefaultLogParser#handleGUIElement(String, Map) |
---|
[1047] | 58 | */ |
---|
[1069] | 59 | @Override |
---|
| 60 | protected boolean handleGUIElement(String id, Map<String, String> parameters) |
---|
| 61 | throws SAXException |
---|
| 62 | { |
---|
| 63 | HTMLGUIElementSpec specification = null; |
---|
| 64 | |
---|
| 65 | String parentId = parameters.get("parent"); |
---|
| 66 | IGUIElement parent = super.getGUIElementTree().find(parentId); |
---|
[1059] | 67 | |
---|
[1069] | 68 | if (parameters.containsKey("host")) { |
---|
| 69 | // this is a server specification |
---|
| 70 | int port = 80; |
---|
| 71 | String portStr = parameters.get(port); |
---|
| 72 | |
---|
| 73 | if (portStr != null) { |
---|
| 74 | port = Integer.parseInt(portStr); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | specification = new HTMLServerSpec(parameters.get("host"), port); |
---|
[1047] | 78 | } |
---|
[1069] | 79 | else if (parameters.containsKey("path")) { |
---|
| 80 | // this is a document specification |
---|
| 81 | |
---|
| 82 | if (parent != null) { |
---|
| 83 | if (!(parent.getSpecification() instanceof HTMLServerSpec)) { |
---|
| 84 | throw new SAXException |
---|
| 85 | ("invalid log: parent GUI element of a document is not of type server"); |
---|
[1059] | 86 | } |
---|
[1069] | 87 | |
---|
| 88 | specification = new HTMLDocumentSpec |
---|
| 89 | ((HTMLServerSpec) parent.getSpecification(), parameters.get("path"), |
---|
| 90 | parameters.get("query"), parameters.get("title")); |
---|
[1059] | 91 | } |
---|
[1069] | 92 | else if (parentId == null) { |
---|
| 93 | throw new SAXException("invalid log: a document has no parent id"); |
---|
[1059] | 94 | } |
---|
[1069] | 95 | } |
---|
| 96 | else if (parameters.containsKey("tagname")) { |
---|
| 97 | String tagName = parameters.get("tagname"); |
---|
| 98 | |
---|
| 99 | if (!tagNameMustBeConsidered(tagName)) { |
---|
| 100 | return true; |
---|
[1059] | 101 | } |
---|
[1047] | 102 | |
---|
[1069] | 103 | if (parent != null) { |
---|
| 104 | IGUIElement document = parent; |
---|
| 105 | |
---|
| 106 | while ((document != null) && |
---|
| 107 | (!(document.getSpecification() instanceof HTMLDocumentSpec))) |
---|
| 108 | { |
---|
| 109 | document = document.getParent(); |
---|
[1047] | 110 | } |
---|
[1069] | 111 | |
---|
| 112 | if (document == null) { |
---|
| 113 | throw new SAXException |
---|
| 114 | ("invalid log: parent hierarchy of a page element does not contain a " + |
---|
| 115 | "document"); |
---|
[1047] | 116 | } |
---|
[1069] | 117 | |
---|
| 118 | int index = -1; |
---|
| 119 | String indexStr = parameters.get("index"); |
---|
| 120 | |
---|
| 121 | if ((indexStr != null) && (!"".equals(indexStr))) { |
---|
| 122 | index = Integer.parseInt(indexStr); |
---|
[1047] | 123 | } |
---|
[1069] | 124 | |
---|
| 125 | specification = new HTMLPageElementSpec |
---|
| 126 | ((HTMLDocumentSpec) document.getSpecification(), tagName, |
---|
| 127 | parameters.get("htmlid"), index); |
---|
[1266] | 128 | |
---|
[1047] | 129 | } |
---|
[1069] | 130 | else if (parentId == null) { |
---|
| 131 | throw new SAXException("invalid log: a page element has no parent id"); |
---|
[1047] | 132 | } |
---|
| 133 | } |
---|
[1059] | 134 | else { |
---|
[1069] | 135 | throw new SAXException("invalid log: unknown GUI element"); |
---|
[1047] | 136 | } |
---|
| 137 | |
---|
[1069] | 138 | if (specification != null) { |
---|
[1084] | 139 | try { |
---|
| 140 | super.getGUIElementTree().add(id, parentId, specification); |
---|
| 141 | } |
---|
| 142 | catch (GUIModelException e) { |
---|
| 143 | throw new SAXException("could not handle GUI element with id " + |
---|
| 144 | id + ": " + e.getMessage(), e); |
---|
| 145 | } |
---|
[1069] | 146 | return true; |
---|
| 147 | } |
---|
| 148 | else { |
---|
| 149 | return false; |
---|
| 150 | } |
---|
[1047] | 151 | } |
---|
| 152 | |
---|
[1069] | 153 | /* (non-Javadoc) |
---|
[1232] | 154 | * @see de.ugoe.cs.autoquest.plugin.html.AbstractDefaultLogParser#handleEvent(String, Map) |
---|
[1051] | 155 | */ |
---|
[1047] | 156 | @Override |
---|
[1069] | 157 | protected boolean handleEvent(String type, Map<String, String> parameters) throws SAXException { |
---|
| 158 | String targetId = parameters.get("target"); |
---|
| 159 | |
---|
| 160 | if (targetId == null) { |
---|
| 161 | String targetDocument = parameters.get("targetDocument"); |
---|
| 162 | String targetDOMPath = parameters.get("targetDOMPath"); |
---|
| 163 | |
---|
| 164 | if ((targetDocument == null) || (targetDOMPath == null)) { |
---|
| 165 | throw new SAXException("event has no target defined"); |
---|
[1047] | 166 | } |
---|
[1069] | 167 | |
---|
| 168 | targetId = determineTargetId(targetDocument, targetDOMPath); |
---|
| 169 | |
---|
| 170 | if (targetId == null) { |
---|
| 171 | // the target id can not be determined yet |
---|
| 172 | return false; |
---|
| 173 | } |
---|
[1047] | 174 | } |
---|
[1069] | 175 | |
---|
| 176 | IGUIElement target = super.getGUIElementTree().find(targetId); |
---|
| 177 | |
---|
| 178 | if (target == null) { |
---|
[1093] | 179 | // event not processible yet |
---|
[1069] | 180 | return false; |
---|
[1047] | 181 | } |
---|
[1059] | 182 | |
---|
[1069] | 183 | IEventType eventType = |
---|
| 184 | HTMLEventTypeFactory.getInstance().getEventType(type, parameters, target); |
---|
| 185 | |
---|
[1084] | 186 | if (eventType != null) { |
---|
| 187 | Event event = new Event(eventType, target); |
---|
[1059] | 188 | |
---|
[1084] | 189 | String timestampStr = parameters.get("timestamp"); |
---|
[1069] | 190 | |
---|
[1084] | 191 | if (timestampStr != null) { |
---|
| 192 | event.setTimestamp(Long.parseLong(timestampStr)); |
---|
| 193 | } |
---|
[1059] | 194 | |
---|
[1084] | 195 | ((HTMLGUIElement) event.getTarget()).markUsed(); |
---|
[1069] | 196 | |
---|
[1084] | 197 | super.addToSequence(event); |
---|
| 198 | } |
---|
| 199 | // else ignore unknown event type |
---|
[1059] | 200 | |
---|
[1069] | 201 | return true; |
---|
[1047] | 202 | } |
---|
[1059] | 203 | |
---|
[1047] | 204 | /** |
---|
| 205 | * <p> |
---|
[1232] | 206 | * used to determine the id of a target denoted by an event. This is only required for older |
---|
| 207 | * document formats. The new formats use concrete ids. |
---|
[1047] | 208 | * </p> |
---|
| 209 | */ |
---|
[1069] | 210 | private String determineTargetId(String targetDocument, String targetDOMPath) |
---|
| 211 | throws SAXException |
---|
| 212 | { |
---|
| 213 | IGUIElement document = super.getGUIElementTree().find(targetDocument); |
---|
| 214 | |
---|
| 215 | if (document == null) { |
---|
| 216 | return null; |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | if (!(document.getSpecification() instanceof HTMLDocumentSpec)) { |
---|
| 220 | throw new SAXException("an id that should refer to an HTML document refers to" + |
---|
| 221 | "something else"); |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | GUIModel model = super.getGUIElementTree().getGUIModel(); |
---|
| 225 | IGUIElement child = document; |
---|
| 226 | String[] pathElements = targetDOMPath.split("/"); |
---|
| 227 | int pathIndex = 0; |
---|
| 228 | |
---|
| 229 | HTMLPageElementSpec compareSpec; |
---|
| 230 | String tagName; |
---|
| 231 | int index; |
---|
| 232 | String htmlId; |
---|
| 233 | |
---|
| 234 | while ((pathIndex < pathElements.length) && (child != null)) { |
---|
| 235 | if ((pathElements[pathIndex] != null) && (!"".equals(pathElements[pathIndex]))) { |
---|
| 236 | Matcher matcher = htmlElementPattern.matcher(pathElements[pathIndex]); |
---|
| 237 | if (!matcher.matches()) { |
---|
| 238 | throw new SAXException |
---|
| 239 | ("could not parse target DOM path element " + pathElements[pathIndex]); |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | tagName = matcher.group(1); |
---|
| 243 | String indexStr = matcher.group(3); |
---|
| 244 | htmlId = matcher.group(4); |
---|
| 245 | |
---|
| 246 | index = -1; |
---|
| 247 | if ((indexStr != null) && (!"".equals(indexStr))) { |
---|
| 248 | index = Integer.parseInt(indexStr); |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | compareSpec = new HTMLPageElementSpec |
---|
| 252 | ((HTMLDocumentSpec) document.getSpecification(), tagName, htmlId, index); |
---|
| 253 | |
---|
| 254 | List<IGUIElement> children = model.getChildren(child); |
---|
| 255 | child = null; |
---|
| 256 | |
---|
| 257 | for (IGUIElement candidate : children) { |
---|
| 258 | if (compareSpec.getSimilarity(candidate.getSpecification())) { |
---|
| 259 | child = candidate; |
---|
| 260 | break; |
---|
| 261 | } |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | pathIndex++; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | if (child != null) { |
---|
| 269 | return super.getGUIElementTree().find(child); |
---|
| 270 | } |
---|
| 271 | else { |
---|
| 272 | return null; |
---|
| 273 | } |
---|
[1047] | 274 | } |
---|
[1059] | 275 | |
---|
[1047] | 276 | /** |
---|
| 277 | * <p> |
---|
[1069] | 278 | * checks if tags with the provided name must be handled in the GUI model. As an example, |
---|
| 279 | * it is not necessary to handle "head" tags and anything included in them. |
---|
[1047] | 280 | * </p> |
---|
[1069] | 281 | * |
---|
[1232] | 282 | * @param tagName the tag name to check |
---|
| 283 | * |
---|
| 284 | * @return true, if the tag must be considered, false else |
---|
[1059] | 285 | */ |
---|
[1069] | 286 | private boolean tagNameMustBeConsidered(String tagName) { |
---|
[1266] | 287 | if (!tagName.startsWith("input_")) { |
---|
| 288 | for (int i = 0; i < tagName.length(); i++) { |
---|
| 289 | // all known HTML tags are either letters or digits, but nothing else. Any GUI model |
---|
| 290 | // containing something different is proprietary and, therefore, ignored. |
---|
| 291 | if (!Character.isLetterOrDigit(tagName.charAt(i))) { |
---|
| 292 | return false; |
---|
| 293 | } |
---|
| 294 | } |
---|
| 295 | } |
---|
| 296 | |
---|
[1069] | 297 | return |
---|
| 298 | !"head".equals(tagName) && !"title".equals(tagName) && !"script".equals(tagName) && |
---|
| 299 | !"style".equals(tagName) && !"link".equals(tagName) && !"meta".equals(tagName) && |
---|
| 300 | !"iframe".equals(tagName) && !"input_hidden".equals(tagName) && |
---|
| 301 | !"option".equals(tagName) && !"tt".equals(tagName) && !"br".equals(tagName) && |
---|
[1232] | 302 | !"colgroup".equals(tagName) && !"col".equals(tagName) && !"hr".equals(tagName) && |
---|
[1266] | 303 | !"param".equals(tagName) && !"sfmsg".equals(tagName); |
---|
[1059] | 304 | |
---|
[1069] | 305 | } |
---|
[1059] | 306 | |
---|
[1047] | 307 | } |
---|