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.commands;
|
---|
16 |
|
---|
17 | import java.util.Collection;
|
---|
18 | import java.util.HashMap;
|
---|
19 | import java.util.LinkedList;
|
---|
20 | import java.util.List;
|
---|
21 | import java.util.Map;
|
---|
22 | import java.util.regex.Matcher;
|
---|
23 | import java.util.regex.Pattern;
|
---|
24 |
|
---|
25 | import de.ugoe.cs.autoquest.CommandHelpers;
|
---|
26 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
27 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel;
|
---|
28 | import de.ugoe.cs.autoquest.plugin.html.HTMLLogParser;
|
---|
29 | import de.ugoe.cs.util.console.Command;
|
---|
30 | import de.ugoe.cs.util.console.Console;
|
---|
31 | import de.ugoe.cs.util.console.GlobalDataContainer;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * <p>
|
---|
35 | * Command to parse a file with sessions monitored by AutoQUESTs HTML monitor.
|
---|
36 | * </p>
|
---|
37 | *
|
---|
38 | * @author Patrick Harms
|
---|
39 | * @version 1.0
|
---|
40 | */
|
---|
41 | public class CMDparseHTML implements Command {
|
---|
42 |
|
---|
43 | /*
|
---|
44 | * (non-Javadoc)
|
---|
45 | *
|
---|
46 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
47 | */
|
---|
48 | @Override
|
---|
49 | public void run(List<Object> parameters) {
|
---|
50 | String filename = null;
|
---|
51 | String sequencesName = null;
|
---|
52 | Map<String, List<String>> parseParams = new HashMap<String, List<String>>();
|
---|
53 |
|
---|
54 | try {
|
---|
55 | for (int i = 0; i < parameters.size(); i++) {
|
---|
56 | String param = (String) parameters.get(i);
|
---|
57 | if (!param.startsWith("-")) {
|
---|
58 | if (filename == null) {
|
---|
59 | filename = param;
|
---|
60 | }
|
---|
61 | else if (sequencesName == null) {
|
---|
62 | sequencesName = param;
|
---|
63 | }
|
---|
64 | }
|
---|
65 | else {
|
---|
66 | Pattern parseParamPattern = Pattern.compile("-(\\w*)=([\\w=\\[\\]\\(\\)/]*)");
|
---|
67 | Matcher matcher = parseParamPattern.matcher(param);
|
---|
68 |
|
---|
69 | if (matcher.matches()) {
|
---|
70 | String key = matcher.group(1);
|
---|
71 | List<String> values = parseParams.get(key);
|
---|
72 |
|
---|
73 | if (values == null) {
|
---|
74 | values = new LinkedList<String>();
|
---|
75 | parseParams.put(key, values);
|
---|
76 | }
|
---|
77 |
|
---|
78 | values.add(matcher.group(2));
|
---|
79 | }
|
---|
80 | else {
|
---|
81 | String message = "parse parameter does not follow format: -<key>=<value>";
|
---|
82 | Console.printerrln(message);
|
---|
83 | throw new IllegalArgumentException(message);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 | catch (Exception e) {
|
---|
89 | throw new IllegalArgumentException("illegal parameters provided: " + e);
|
---|
90 | }
|
---|
91 |
|
---|
92 | if (sequencesName == null) {
|
---|
93 | sequencesName = "sequences";
|
---|
94 | }
|
---|
95 |
|
---|
96 | HTMLLogParser parser = new HTMLLogParser(parseParams);
|
---|
97 |
|
---|
98 | try {
|
---|
99 | parser.parseFile(filename);
|
---|
100 | }
|
---|
101 | catch (Exception e) {
|
---|
102 | Console.printerrln("Could not parse " + filename + ": " + e.getMessage());
|
---|
103 | return;
|
---|
104 | }
|
---|
105 |
|
---|
106 | Collection<List<Event>> sequences = parser.getSequences();
|
---|
107 |
|
---|
108 | GUIModel targets = parser.getGuiModel();
|
---|
109 |
|
---|
110 | if (GlobalDataContainer.getInstance().addData(sequencesName, sequences)) {
|
---|
111 | CommandHelpers.dataOverwritten(sequencesName);
|
---|
112 | }
|
---|
113 |
|
---|
114 | if (GlobalDataContainer.getInstance().addData(sequencesName + "_targets", targets)) {
|
---|
115 | CommandHelpers.dataOverwritten(sequencesName + "_targets");
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | /*
|
---|
120 | * (non-Javadoc)
|
---|
121 | *
|
---|
122 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
123 | */
|
---|
124 | @Override
|
---|
125 | public String help() {
|
---|
126 | return "parseHTML <filename> {<sequencesName>}";
|
---|
127 | }
|
---|
128 |
|
---|
129 | }
|
---|