[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); |
---|
[1047] | 128 | } |
---|
[1069] | 129 | else if (parentId == null) { |
---|
| 130 | throw new SAXException("invalid log: a page element has no parent id"); |
---|
[1047] | 131 | } |
---|
| 132 | } |
---|
[1059] | 133 | else { |
---|
[1069] | 134 | throw new SAXException("invalid log: unknown GUI element"); |
---|
[1047] | 135 | } |
---|
| 136 | |
---|
[1069] | 137 | if (specification != null) { |
---|
[1084] | 138 | try { |
---|
| 139 | super.getGUIElementTree().add(id, parentId, specification); |
---|
| 140 | } |
---|
| 141 | catch (GUIModelException e) { |
---|
| 142 | throw new SAXException("could not handle GUI element with id " + |
---|
| 143 | id + ": " + e.getMessage(), e); |
---|
| 144 | } |
---|
[1069] | 145 | return true; |
---|
| 146 | } |
---|
| 147 | else { |
---|
| 148 | return false; |
---|
| 149 | } |
---|
[1047] | 150 | } |
---|
| 151 | |
---|
[1069] | 152 | /* (non-Javadoc) |
---|
[1232] | 153 | * @see de.ugoe.cs.autoquest.plugin.html.AbstractDefaultLogParser#handleEvent(String, Map) |
---|
[1051] | 154 | */ |
---|
[1047] | 155 | @Override |
---|
[1069] | 156 | protected boolean handleEvent(String type, Map<String, String> parameters) throws SAXException { |
---|
| 157 | String targetId = parameters.get("target"); |
---|
| 158 | |
---|
| 159 | if (targetId == null) { |
---|
| 160 | String targetDocument = parameters.get("targetDocument"); |
---|
| 161 | String targetDOMPath = parameters.get("targetDOMPath"); |
---|
| 162 | |
---|
| 163 | if ((targetDocument == null) || (targetDOMPath == null)) { |
---|
| 164 | throw new SAXException("event has no target defined"); |
---|
[1047] | 165 | } |
---|
[1069] | 166 | |
---|
| 167 | targetId = determineTargetId(targetDocument, targetDOMPath); |
---|
| 168 | |
---|
| 169 | if (targetId == null) { |
---|
| 170 | // the target id can not be determined yet |
---|
| 171 | return false; |
---|
| 172 | } |
---|
[1047] | 173 | } |
---|
[1069] | 174 | |
---|
| 175 | IGUIElement target = super.getGUIElementTree().find(targetId); |
---|
| 176 | |
---|
| 177 | if (target == null) { |
---|
[1093] | 178 | // event not processible yet |
---|
[1069] | 179 | return false; |
---|
[1047] | 180 | } |
---|
[1059] | 181 | |
---|
[1069] | 182 | IEventType eventType = |
---|
| 183 | HTMLEventTypeFactory.getInstance().getEventType(type, parameters, target); |
---|
| 184 | |
---|
[1084] | 185 | if (eventType != null) { |
---|
| 186 | Event event = new Event(eventType, target); |
---|
[1059] | 187 | |
---|
[1084] | 188 | String timestampStr = parameters.get("timestamp"); |
---|
[1069] | 189 | |
---|
[1084] | 190 | if (timestampStr != null) { |
---|
| 191 | event.setTimestamp(Long.parseLong(timestampStr)); |
---|
| 192 | } |
---|
[1059] | 193 | |
---|
[1084] | 194 | ((HTMLGUIElement) event.getTarget()).markUsed(); |
---|
[1069] | 195 | |
---|
[1084] | 196 | super.addToSequence(event); |
---|
| 197 | } |
---|
| 198 | // else ignore unknown event type |
---|
[1059] | 199 | |
---|
[1069] | 200 | return true; |
---|
[1047] | 201 | } |
---|
[1059] | 202 | |
---|
[1047] | 203 | /** |
---|
| 204 | * <p> |
---|
[1232] | 205 | * used to determine the id of a target denoted by an event. This is only required for older |
---|
| 206 | * document formats. The new formats use concrete ids. |
---|
[1047] | 207 | * </p> |
---|
| 208 | */ |
---|
[1069] | 209 | private String determineTargetId(String targetDocument, String targetDOMPath) |
---|
| 210 | throws SAXException |
---|
| 211 | { |
---|
| 212 | IGUIElement document = super.getGUIElementTree().find(targetDocument); |
---|
| 213 | |
---|
| 214 | if (document == null) { |
---|
| 215 | return null; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | if (!(document.getSpecification() instanceof HTMLDocumentSpec)) { |
---|
| 219 | throw new SAXException("an id that should refer to an HTML document refers to" + |
---|
| 220 | "something else"); |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | GUIModel model = super.getGUIElementTree().getGUIModel(); |
---|
| 224 | IGUIElement child = document; |
---|
| 225 | String[] pathElements = targetDOMPath.split("/"); |
---|
| 226 | int pathIndex = 0; |
---|
| 227 | |
---|
| 228 | HTMLPageElementSpec compareSpec; |
---|
| 229 | String tagName; |
---|
| 230 | int index; |
---|
| 231 | String htmlId; |
---|
| 232 | |
---|
| 233 | while ((pathIndex < pathElements.length) && (child != null)) { |
---|
| 234 | if ((pathElements[pathIndex] != null) && (!"".equals(pathElements[pathIndex]))) { |
---|
| 235 | Matcher matcher = htmlElementPattern.matcher(pathElements[pathIndex]); |
---|
| 236 | if (!matcher.matches()) { |
---|
| 237 | throw new SAXException |
---|
| 238 | ("could not parse target DOM path element " + pathElements[pathIndex]); |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | tagName = matcher.group(1); |
---|
| 242 | String indexStr = matcher.group(3); |
---|
| 243 | htmlId = matcher.group(4); |
---|
| 244 | |
---|
| 245 | index = -1; |
---|
| 246 | if ((indexStr != null) && (!"".equals(indexStr))) { |
---|
| 247 | index = Integer.parseInt(indexStr); |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | compareSpec = new HTMLPageElementSpec |
---|
| 251 | ((HTMLDocumentSpec) document.getSpecification(), tagName, htmlId, index); |
---|
| 252 | |
---|
| 253 | List<IGUIElement> children = model.getChildren(child); |
---|
| 254 | child = null; |
---|
| 255 | |
---|
| 256 | for (IGUIElement candidate : children) { |
---|
| 257 | if (compareSpec.getSimilarity(candidate.getSpecification())) { |
---|
| 258 | child = candidate; |
---|
| 259 | break; |
---|
| 260 | } |
---|
| 261 | } |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | pathIndex++; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | if (child != null) { |
---|
| 268 | return super.getGUIElementTree().find(child); |
---|
| 269 | } |
---|
| 270 | else { |
---|
| 271 | return null; |
---|
| 272 | } |
---|
[1047] | 273 | } |
---|
[1059] | 274 | |
---|
[1047] | 275 | /** |
---|
| 276 | * <p> |
---|
[1069] | 277 | * checks if tags with the provided name must be handled in the GUI model. As an example, |
---|
| 278 | * it is not necessary to handle "head" tags and anything included in them. |
---|
[1047] | 279 | * </p> |
---|
[1069] | 280 | * |
---|
[1232] | 281 | * @param tagName the tag name to check |
---|
| 282 | * |
---|
| 283 | * @return true, if the tag must be considered, false else |
---|
[1059] | 284 | */ |
---|
[1069] | 285 | private boolean tagNameMustBeConsidered(String tagName) { |
---|
| 286 | return |
---|
| 287 | !"head".equals(tagName) && !"title".equals(tagName) && !"script".equals(tagName) && |
---|
| 288 | !"style".equals(tagName) && !"link".equals(tagName) && !"meta".equals(tagName) && |
---|
| 289 | !"iframe".equals(tagName) && !"input_hidden".equals(tagName) && |
---|
| 290 | !"option".equals(tagName) && !"tt".equals(tagName) && !"br".equals(tagName) && |
---|
[1232] | 291 | !"colgroup".equals(tagName) && !"col".equals(tagName) && !"hr".equals(tagName) && |
---|
| 292 | !"param".equals(tagName); |
---|
[1059] | 293 | |
---|
[1069] | 294 | } |
---|
[1059] | 295 | |
---|
[1047] | 296 | } |
---|