[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.io.File; |
---|
| 18 | import java.io.FileInputStream; |
---|
| 19 | import java.io.FileNotFoundException; |
---|
| 20 | import java.io.IOException; |
---|
| 21 | import java.io.InputStreamReader; |
---|
| 22 | import java.io.UnsupportedEncodingException; |
---|
| 23 | import java.util.Collection; |
---|
| 24 | import java.util.HashMap; |
---|
| 25 | import java.util.LinkedList; |
---|
| 26 | import java.util.List; |
---|
| 27 | import java.util.Map; |
---|
| 28 | |
---|
| 29 | import javax.xml.parsers.ParserConfigurationException; |
---|
| 30 | import javax.xml.parsers.SAXParser; |
---|
| 31 | import javax.xml.parsers.SAXParserFactory; |
---|
| 32 | |
---|
| 33 | import org.xml.sax.Attributes; |
---|
| 34 | import org.xml.sax.InputSource; |
---|
| 35 | import org.xml.sax.SAXException; |
---|
| 36 | import org.xml.sax.SAXParseException; |
---|
| 37 | import org.xml.sax.helpers.DefaultHandler; |
---|
| 38 | |
---|
| 39 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
[1059] | 40 | import de.ugoe.cs.autoquest.eventcore.IEventType; |
---|
[1047] | 41 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIElementTree; |
---|
| 42 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel; |
---|
| 43 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement; |
---|
| 44 | import de.ugoe.cs.autoquest.plugin.html.eventcore.HTMLEventTypeFactory; |
---|
| 45 | import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLGUIElement; |
---|
| 46 | import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLGUIElementSpec; |
---|
[1059] | 47 | import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLPageElementSpec; |
---|
| 48 | import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLPageSpec; |
---|
| 49 | import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLServerSpec; |
---|
[1047] | 50 | import de.ugoe.cs.util.console.Console; |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * <p> |
---|
| 54 | * This class provides the functionality to parse XML log files generated by the HTMLMonitor of |
---|
| 55 | * autoquest. The result of parsing a file is a collection of event sequences. |
---|
| 56 | * </p> |
---|
| 57 | * |
---|
| 58 | * @author Fabian Glaser |
---|
| 59 | * @version 1.0 |
---|
| 60 | * |
---|
| 61 | */ |
---|
| 62 | public class NewHTMLLogParser extends DefaultHandler { |
---|
| 63 | /** |
---|
| 64 | * <p> |
---|
| 65 | * Constructor. Creates a new HTMLLogParser. |
---|
| 66 | * </p> |
---|
| 67 | */ |
---|
| 68 | public NewHTMLLogParser() { |
---|
| 69 | sequences = new LinkedList<List<Event>>(); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | * <p> |
---|
| 74 | * Collection of event sequences that is contained in the parsed log file. |
---|
| 75 | * </p> |
---|
| 76 | */ |
---|
| 77 | private Collection<List<Event>> sequences; |
---|
[1059] | 78 | |
---|
[1047] | 79 | /** |
---|
| 80 | * <p> |
---|
| 81 | * Internal handle to the parsed GUI structure, stored in a GUIElementTree |
---|
| 82 | * </p> |
---|
| 83 | */ |
---|
| 84 | private GUIElementTree<String> currentGUIElementTree; |
---|
[1059] | 85 | |
---|
[1047] | 86 | /** |
---|
| 87 | * <p> |
---|
| 88 | * Path of the GUI element currently being parsed. |
---|
| 89 | * </p> |
---|
| 90 | */ |
---|
| 91 | private String currentGUIElementPath; |
---|
[1059] | 92 | |
---|
[1047] | 93 | /** |
---|
| 94 | * <p> |
---|
| 95 | * Path of the parent of the GUI element currently being parsed. |
---|
| 96 | * </p> |
---|
| 97 | */ |
---|
| 98 | private String currentParentPath; |
---|
[1059] | 99 | |
---|
[1047] | 100 | /** |
---|
| 101 | * <p> |
---|
| 102 | * Source of the GUI element currently being parsed. |
---|
| 103 | * </p> |
---|
| 104 | */ |
---|
| 105 | private String currentEventSource; |
---|
[1059] | 106 | |
---|
[1047] | 107 | /** |
---|
| 108 | * <p> |
---|
| 109 | * Timestamp of the event currently being parsed. |
---|
| 110 | * </p> |
---|
| 111 | */ |
---|
| 112 | private Long currentEventTimestamp; |
---|
[1059] | 113 | |
---|
[1047] | 114 | /** |
---|
| 115 | * <p> |
---|
| 116 | * Internal handle to the parameters of the event currently being parsed. |
---|
| 117 | * </p> |
---|
| 118 | */ |
---|
| 119 | private Map<String, String> currentEventParameters; |
---|
[1059] | 120 | |
---|
[1047] | 121 | /** |
---|
| 122 | * <p> |
---|
[1059] | 123 | * Internal handle to the parameters of the GUI element currently being parsed. |
---|
| 124 | * </p> |
---|
| 125 | */ |
---|
| 126 | private Map<String, String> currentGUIElementParameters; |
---|
| 127 | /** |
---|
| 128 | * <p> |
---|
[1047] | 129 | * Internal handle to the sequence currently being parsed. |
---|
| 130 | * </p> |
---|
| 131 | */ |
---|
| 132 | private List<Event> currentSequence; |
---|
[1059] | 133 | |
---|
[1047] | 134 | /** |
---|
| 135 | * <p> |
---|
| 136 | * Internal handle to type of the event currently being parsed. |
---|
| 137 | * </p> |
---|
| 138 | */ |
---|
| 139 | private String currentEventType; |
---|
[1059] | 140 | |
---|
[1047] | 141 | /** |
---|
| 142 | * <p> |
---|
| 143 | * Class of the GUI element currently being parsed. |
---|
| 144 | * </p> |
---|
| 145 | */ |
---|
| 146 | private String currentGUIElementClass; |
---|
[1059] | 147 | |
---|
[1047] | 148 | /** |
---|
| 149 | * <p> |
---|
| 150 | * Index of the GUI element currently being parsed. |
---|
| 151 | * </p> |
---|
| 152 | */ |
---|
| 153 | private String currentGUIElementIndex; |
---|
[1059] | 154 | |
---|
[1047] | 155 | /** |
---|
| 156 | * <p> |
---|
| 157 | * internal handle to the GUI element of the previous event to be potentially reused for the |
---|
| 158 | * current |
---|
| 159 | * </p> |
---|
| 160 | */ |
---|
| 161 | private IGUIElement lastGUIElement; |
---|
[1059] | 162 | |
---|
[1047] | 163 | /** |
---|
| 164 | * <p> |
---|
[1059] | 165 | * internal handle to the server specification currently being used. |
---|
| 166 | * </p> |
---|
| 167 | */ |
---|
| 168 | private HTMLServerSpec currentServerSpec; |
---|
| 169 | |
---|
| 170 | /** |
---|
| 171 | * <p> |
---|
[1047] | 172 | * Parses a log file written by the HTMLMonitor and creates a collection of event sequences. |
---|
| 173 | * </p> |
---|
| 174 | * |
---|
| 175 | * @param filename |
---|
[1059] | 176 | * name and path of the log file |
---|
[1047] | 177 | */ |
---|
| 178 | public void parseFile(String filename) { |
---|
[1059] | 179 | if (filename == null) { |
---|
[1047] | 180 | throw new IllegalArgumentException("filename must not be null"); |
---|
| 181 | } |
---|
[1059] | 182 | |
---|
[1047] | 183 | parseFile(new File(filename)); |
---|
| 184 | } |
---|
[1059] | 185 | |
---|
[1047] | 186 | /** |
---|
| 187 | * <p> |
---|
| 188 | * Parses a log file written by the HTMLMonitor and creates a collection of event sequences. |
---|
| 189 | * </p> |
---|
[1059] | 190 | * |
---|
[1047] | 191 | * @param file |
---|
[1059] | 192 | * file to be parsed |
---|
[1047] | 193 | */ |
---|
| 194 | public void parseFile(File file) { |
---|
[1059] | 195 | if (file == null) { |
---|
[1047] | 196 | throw new IllegalArgumentException("file must not be null"); |
---|
| 197 | } |
---|
| 198 | SAXParserFactory spf = SAXParserFactory.newInstance(); |
---|
| 199 | spf.setValidating(true); |
---|
| 200 | SAXParser saxParser = null; |
---|
| 201 | InputSource inputSource = null; |
---|
[1059] | 202 | try { |
---|
[1047] | 203 | saxParser = spf.newSAXParser(); |
---|
| 204 | inputSource = |
---|
[1059] | 205 | new InputSource(new InputStreamReader(new FileInputStream(file), "UTF-8")); |
---|
[1047] | 206 | } |
---|
[1059] | 207 | catch (UnsupportedEncodingException e) { |
---|
[1047] | 208 | Console.printerr("Error parsing file " + file.getName()); |
---|
| 209 | Console.logException(e); |
---|
| 210 | return; |
---|
| 211 | } |
---|
[1059] | 212 | catch (ParserConfigurationException e) { |
---|
[1047] | 213 | Console.printerr("Error parsing file " + file.getName()); |
---|
| 214 | Console.logException(e); |
---|
| 215 | return; |
---|
| 216 | } |
---|
[1059] | 217 | catch (SAXException e) { |
---|
[1047] | 218 | Console.printerr("Error parsing file " + file.getName()); |
---|
| 219 | Console.logException(e); |
---|
| 220 | } |
---|
[1059] | 221 | catch (FileNotFoundException e) { |
---|
[1047] | 222 | Console.printerr("Error parsing file " + file.getName()); |
---|
| 223 | Console.logException(e); |
---|
| 224 | } |
---|
[1059] | 225 | if (inputSource != null) { |
---|
| 226 | inputSource.setSystemId("file://" + file.getAbsolutePath()); |
---|
| 227 | try { |
---|
| 228 | if (saxParser == null) { |
---|
| 229 | throw new RuntimeException("SaxParser creation failed"); |
---|
| 230 | } |
---|
| 231 | saxParser.parse(inputSource, this); |
---|
| 232 | } |
---|
| 233 | catch (SAXParseException e) { |
---|
| 234 | Console.printerrln("Failure parsing file in line " + e.getLineNumber() + |
---|
| 235 | ", column " + e.getColumnNumber() + "."); |
---|
| 236 | Console.logException(e); |
---|
| 237 | } |
---|
| 238 | catch (SAXException e) { |
---|
| 239 | Console.printerr("Error parsing file " + file.getName()); |
---|
| 240 | Console.logException(e); |
---|
| 241 | return; |
---|
| 242 | } |
---|
| 243 | catch (IOException e) { |
---|
| 244 | Console.printerr("Error parsing file " + file.getName()); |
---|
| 245 | Console.logException(e); |
---|
| 246 | return; |
---|
| 247 | } |
---|
| 248 | } |
---|
[1047] | 249 | } |
---|
| 250 | |
---|
[1059] | 251 | /* |
---|
| 252 | * (non-Javadoc) |
---|
| 253 | * |
---|
| 254 | * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, |
---|
| 255 | * java.lang.String, org.xml.sax.Attributes) |
---|
[1051] | 256 | */ |
---|
[1047] | 257 | @Override |
---|
| 258 | public void startElement(String uri, String localName, String qName, Attributes atts) |
---|
| 259 | throws SAXException |
---|
| 260 | { |
---|
| 261 | if (qName.equals("session")) { |
---|
[1059] | 262 | currentSequence = new LinkedList<Event>(); |
---|
| 263 | if (currentGUIElementTree == null) |
---|
| 264 | currentGUIElementTree = new GUIElementTree<String>(); |
---|
[1047] | 265 | } |
---|
| 266 | else if (qName.equals("component")) { |
---|
| 267 | currentGUIElementPath = atts.getValue("path"); |
---|
[1059] | 268 | currentGUIElementParameters = new HashMap<String, String>(); |
---|
[1047] | 269 | } |
---|
| 270 | else if (qName.equals("event")) { |
---|
| 271 | currentEventType = atts.getValue("type"); |
---|
| 272 | currentEventParameters = new HashMap<String, String>(); |
---|
| 273 | } |
---|
[1059] | 274 | else if (qName.equals("param")) { |
---|
[1047] | 275 | String paramName = atts.getValue("name"); |
---|
[1059] | 276 | if (currentGUIElementPath != null) { |
---|
| 277 | if ("parent".equals(paramName)) { |
---|
[1047] | 278 | currentParentPath = atts.getValue("value"); |
---|
| 279 | } |
---|
[1059] | 280 | if ("class".equals(paramName)) { |
---|
[1047] | 281 | currentGUIElementClass = atts.getValue("value"); |
---|
| 282 | } |
---|
[1059] | 283 | if ("index".equals(paramName)) { |
---|
[1047] | 284 | currentGUIElementIndex = atts.getValue("value"); |
---|
| 285 | } |
---|
[1059] | 286 | currentGUIElementParameters.put(paramName, atts.getValue("value")); |
---|
[1047] | 287 | } |
---|
[1059] | 288 | else if (currentEventType != null) { |
---|
| 289 | if ("target".equals(paramName)) { |
---|
[1047] | 290 | currentEventSource = atts.getValue("value"); |
---|
| 291 | } |
---|
[1059] | 292 | if ("timestamp".equals(paramName)) { |
---|
[1047] | 293 | currentEventTimestamp = Long.parseLong(atts.getValue("value")); |
---|
| 294 | } |
---|
| 295 | currentEventParameters.put(paramName, atts.getValue("value")); |
---|
| 296 | } |
---|
[1059] | 297 | else { |
---|
[1047] | 298 | throw new SAXException("param tag found where it should not be."); |
---|
| 299 | } |
---|
| 300 | } |
---|
[1059] | 301 | else { |
---|
[1047] | 302 | throw new SAXException("unknown tag found: " + qName); |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | } |
---|
| 306 | |
---|
[1059] | 307 | /* |
---|
| 308 | * (non-Javadoc) |
---|
| 309 | * |
---|
| 310 | * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, |
---|
| 311 | * java.lang.String) |
---|
[1051] | 312 | */ |
---|
[1047] | 313 | @Override |
---|
| 314 | public void endElement(String uri, String localName, String qName) throws SAXException { |
---|
| 315 | if (qName.equals("session")) { |
---|
[1059] | 316 | if (currentSequence != null && !currentSequence.isEmpty()) { |
---|
[1047] | 317 | sequences.add(currentSequence); |
---|
| 318 | } |
---|
| 319 | currentSequence = null; |
---|
| 320 | } |
---|
| 321 | else if (qName.equals("component") && currentGUIElementPath != null) { |
---|
[1059] | 322 | HTMLGUIElementSpec guiElementSpec = |
---|
| 323 | getGUIElementSpec(currentGUIElementClass, currentGUIElementParameters); |
---|
[1047] | 324 | currentGUIElementTree.add(currentGUIElementPath, currentParentPath, guiElementSpec); |
---|
[1059] | 325 | |
---|
[1047] | 326 | currentParentPath = null; |
---|
| 327 | currentGUIElementPath = null; |
---|
[1059] | 328 | currentGUIElementParameters = null; |
---|
[1047] | 329 | } |
---|
| 330 | else if (qName.equals("event")) { |
---|
| 331 | IGUIElement currentGUIElement; |
---|
| 332 | currentGUIElement = currentGUIElementTree.find(currentEventSource); |
---|
[1059] | 333 | |
---|
| 334 | IEventType eventType = |
---|
| 335 | HTMLEventTypeFactory.getInstance().getEventType(currentEventType, |
---|
| 336 | currentEventParameters, |
---|
| 337 | currentGUIElement); |
---|
| 338 | Event event = |
---|
| 339 | new Event(eventType, (currentGUIElement == null ? lastGUIElement |
---|
| 340 | : currentGUIElement)); |
---|
| 341 | |
---|
[1047] | 342 | event.setTimestamp(currentEventTimestamp); |
---|
| 343 | HTMLGUIElement currentEventTarget = (HTMLGUIElement) event.getTarget(); |
---|
[1051] | 344 | currentEventTarget.markUsed(); |
---|
[1047] | 345 | currentSequence.add(event); |
---|
[1059] | 346 | |
---|
[1047] | 347 | currentEventSource = null; |
---|
| 348 | currentEventTimestamp = -1l; |
---|
| 349 | currentEventParameters = null; |
---|
| 350 | currentEventType = null; |
---|
[1059] | 351 | |
---|
| 352 | if (currentGUIElement != null) { |
---|
[1047] | 353 | lastGUIElement = currentGUIElement; |
---|
| 354 | } |
---|
[1059] | 355 | |
---|
[1047] | 356 | currentGUIElement = null; |
---|
| 357 | } |
---|
| 358 | } |
---|
[1059] | 359 | |
---|
[1047] | 360 | /** |
---|
| 361 | * <p> |
---|
| 362 | * Returns a collection of event sequences that was obtained from parsing log files. |
---|
| 363 | * </p> |
---|
[1059] | 364 | * |
---|
[1047] | 365 | * @return |
---|
| 366 | */ |
---|
[1059] | 367 | public Collection<List<Event>> getSequences() { |
---|
[1047] | 368 | return sequences; |
---|
| 369 | } |
---|
[1059] | 370 | |
---|
[1047] | 371 | /** |
---|
| 372 | * <p> |
---|
| 373 | * Returns the GUI model that is obtained from parsing log files. |
---|
| 374 | * </p> |
---|
| 375 | * |
---|
| 376 | * @return GUIModel |
---|
| 377 | */ |
---|
[1059] | 378 | public GUIModel getGuiModel() { |
---|
[1047] | 379 | return currentGUIElementTree.getGUIModel(); |
---|
| 380 | } |
---|
| 381 | |
---|
[1059] | 382 | /** |
---|
| 383 | * Returns the HTMLGUIElementSpecification for a GUI Element described |
---|
| 384 | * by its class name and its parameters. |
---|
| 385 | * @param guiElementClass |
---|
| 386 | * @param guiElementParameters |
---|
| 387 | * @return |
---|
| 388 | */ |
---|
| 389 | private HTMLGUIElementSpec getGUIElementSpec(String guiElementClass, |
---|
| 390 | Map<String, String> guiElementParameters) |
---|
| 391 | { |
---|
| 392 | HTMLGUIElementSpec specification = null; |
---|
| 393 | if ("server".equals(guiElementClass)) { |
---|
| 394 | // TODO: add correct port handling |
---|
| 395 | specification = new HTMLServerSpec(guiElementParameters.get("htmlId"), 0); |
---|
| 396 | currentServerSpec = (HTMLServerSpec) specification; |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | else { |
---|
| 400 | String id = guiElementParameters.get("htmlId"); |
---|
| 401 | if (id == null) { |
---|
| 402 | HTMLPageElementSpec parentSpec = |
---|
| 403 | (HTMLPageElementSpec) currentGUIElementTree.find(currentParentPath) |
---|
| 404 | .getSpecification(); |
---|
| 405 | id = parentSpec.getPage().getPagePath(); |
---|
| 406 | } |
---|
| 407 | |
---|
| 408 | int index = -1; |
---|
| 409 | String indexStr = guiElementParameters.get("index"); |
---|
| 410 | |
---|
| 411 | if ((indexStr != null) && (!"".equals(indexStr))) { |
---|
| 412 | index = Integer.parseInt(indexStr); |
---|
| 413 | } |
---|
| 414 | String title = guiElementParameters.get("title"); |
---|
| 415 | HTMLPageSpec page = new HTMLPageSpec(currentServerSpec, id, title); |
---|
| 416 | specification = new HTMLPageElementSpec(page, guiElementClass, id, index); |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | return specification; |
---|
| 420 | } |
---|
[1047] | 421 | } |
---|