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.io.File;
|
---|
18 | import java.util.Arrays;
|
---|
19 | import java.util.Collection;
|
---|
20 | import java.util.HashMap;
|
---|
21 | import java.util.LinkedList;
|
---|
22 | import java.util.List;
|
---|
23 | import java.util.Map;
|
---|
24 | import java.util.logging.Level;
|
---|
25 | import java.util.regex.Matcher;
|
---|
26 | import java.util.regex.Pattern;
|
---|
27 |
|
---|
28 | import de.ugoe.cs.autoquest.CommandHelpers;
|
---|
29 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
30 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel;
|
---|
31 | import de.ugoe.cs.autoquest.plugin.html.HTMLLogParser;
|
---|
32 | import de.ugoe.cs.util.console.Command;
|
---|
33 | import de.ugoe.cs.util.console.Console;
|
---|
34 | import de.ugoe.cs.util.console.GlobalDataContainer;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * <p>
|
---|
38 | * Command that tries to parse all files in a folder as if they were log files generated by the
|
---|
39 | * HTMLMonitor. The result is one set of sequences for all files (not one set of sequences for each
|
---|
40 | * file!).
|
---|
41 | * </p>
|
---|
42 | *
|
---|
43 | * @author Patrick Harms
|
---|
44 | * @version 1.0
|
---|
45 | */
|
---|
46 | public class CMDparseDirHTML implements Command {
|
---|
47 |
|
---|
48 | /*
|
---|
49 | * (non-Javadoc)
|
---|
50 | *
|
---|
51 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
52 | */
|
---|
53 | @Override
|
---|
54 | public void run(List<Object> parameters) {
|
---|
55 | String path = null;
|
---|
56 | String sequencesName = null;
|
---|
57 | Map<String, List<String>> parseParams = new HashMap<String, List<String>>();
|
---|
58 |
|
---|
59 | try {
|
---|
60 | for (int i = 0; i < parameters.size(); i++) {
|
---|
61 | String param = (String) parameters.get(i);
|
---|
62 | if (!param.startsWith("-")) {
|
---|
63 | if (path == null) {
|
---|
64 | path = param;
|
---|
65 | }
|
---|
66 | else if (sequencesName == null) {
|
---|
67 | sequencesName = param;
|
---|
68 | }
|
---|
69 | }
|
---|
70 | else {
|
---|
71 | Pattern parseParamPattern = Pattern.compile("-(\\w*)=([\\w=\\[\\]\\(\\)/]*)");
|
---|
72 | Matcher matcher = parseParamPattern.matcher(param);
|
---|
73 |
|
---|
74 | if (matcher.matches()) {
|
---|
75 | String key = matcher.group(1);
|
---|
76 | List<String> values = parseParams.get(key);
|
---|
77 |
|
---|
78 | if (values == null) {
|
---|
79 | values = new LinkedList<String>();
|
---|
80 | parseParams.put(key, values);
|
---|
81 | }
|
---|
82 |
|
---|
83 | values.add(matcher.group(2));
|
---|
84 | }
|
---|
85 | else {
|
---|
86 | String message = "parse parameter does not follow format: -<key>=<value>";
|
---|
87 | Console.printerrln(message);
|
---|
88 | throw new IllegalArgumentException(message);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 | catch (Exception e) {
|
---|
94 | throw new IllegalArgumentException("illegal parameters provided: " + e);
|
---|
95 | }
|
---|
96 |
|
---|
97 | if (sequencesName == null) {
|
---|
98 | sequencesName = "sequences";
|
---|
99 | }
|
---|
100 |
|
---|
101 | File folder = new File(path);
|
---|
102 | if (!folder.isDirectory()) {
|
---|
103 | Console.printerrln(path + " is not a directory");
|
---|
104 | return;
|
---|
105 | }
|
---|
106 |
|
---|
107 | HTMLLogParser parser = new HTMLLogParser(parseParams);
|
---|
108 |
|
---|
109 | parseFile(folder, parser);
|
---|
110 |
|
---|
111 | Collection<List<Event>> sequences = parser.getSequences();
|
---|
112 |
|
---|
113 | GUIModel targets = parser.getGuiModel();
|
---|
114 |
|
---|
115 | if (GlobalDataContainer.getInstance().addData(sequencesName, sequences)) {
|
---|
116 | CommandHelpers.dataOverwritten(sequencesName);
|
---|
117 | }
|
---|
118 |
|
---|
119 | if (GlobalDataContainer.getInstance().addData(sequencesName + "_targets", targets)) {
|
---|
120 | CommandHelpers.dataOverwritten(sequencesName + "_targets");
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * <p>
|
---|
126 | * recursive method for parsing a directory structures
|
---|
127 | * </p>
|
---|
128 | *
|
---|
129 | * @param file the file object to be parsed. If the file is a folder, the method calls itself
|
---|
130 | * for all children
|
---|
131 | * @param parser the parser to use for parsing the files.
|
---|
132 | */
|
---|
133 | private void parseFile(File file, HTMLLogParser parser) {
|
---|
134 | if (file.isDirectory()) {
|
---|
135 | String[] children = file.list();
|
---|
136 | Arrays.sort(children);
|
---|
137 |
|
---|
138 | for (String child : children) {
|
---|
139 | File childFile = new File(file, child);
|
---|
140 | parseFile(childFile, parser);
|
---|
141 | }
|
---|
142 | }
|
---|
143 | else if (file.isFile()) {
|
---|
144 | String source = file.getAbsolutePath();
|
---|
145 | Console.traceln(Level.INFO, "Processing file: " + source);
|
---|
146 |
|
---|
147 | try {
|
---|
148 | parser.parseFile(file);
|
---|
149 | }
|
---|
150 | catch (Exception e) {
|
---|
151 | Console.printerrln("Could not parse " + source + ": " + e.getMessage());
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | /*
|
---|
157 | * (non-Javadoc)
|
---|
158 | *
|
---|
159 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
160 | */
|
---|
161 | @Override
|
---|
162 | public String help() {
|
---|
163 | return "parseDirHTML <directory> {<sequencesName>} {<clearId>} {<clearIndex>}";
|
---|
164 | }
|
---|
165 |
|
---|
166 | }
|
---|