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