[950] | 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;
|
---|
[1012] | 18 | import java.util.Arrays;
|
---|
[950] | 19 | import java.util.Collection;
|
---|
| 20 | import java.util.List;
|
---|
| 21 | import java.util.logging.Level;
|
---|
[1339] | 22 | import java.util.regex.Matcher;
|
---|
| 23 | import java.util.regex.Pattern;
|
---|
[950] | 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) {
|
---|
[1339] | 52 | String path = null;
|
---|
| 53 | String sequencesName = null;
|
---|
[1496] | 54 | String parseParamFile = null;
|
---|
[950] | 55 |
|
---|
| 56 | try {
|
---|
[1339] | 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 {
|
---|
[1496] | 68 | Pattern parseParamPattern = Pattern.compile("-(\\w*)=([\\w/\\.-]*)");
|
---|
[1339] | 69 | Matcher matcher = parseParamPattern.matcher(param);
|
---|
| 70 |
|
---|
| 71 | if (matcher.matches()) {
|
---|
| 72 | String key = matcher.group(1);
|
---|
[1496] | 73 | if (!"parseParamFile".equals(key)) {
|
---|
| 74 | String message = "unknown parameter: " + key;
|
---|
| 75 | Console.printerrln(message);
|
---|
| 76 | throw new IllegalArgumentException(message);
|
---|
[1339] | 77 | }
|
---|
| 78 |
|
---|
[1496] | 79 | parseParamFile = matcher.group(2);
|
---|
[1339] | 80 | }
|
---|
| 81 | else {
|
---|
[1496] | 82 | String message = "parameter does not follow format: -<key>=<value>";
|
---|
[1339] | 83 | Console.printerrln(message);
|
---|
| 84 | throw new IllegalArgumentException(message);
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
[950] | 87 | }
|
---|
| 88 | }
|
---|
| 89 | catch (Exception e) {
|
---|
[1087] | 90 | throw new IllegalArgumentException("illegal parameters provided: " + e);
|
---|
[950] | 91 | }
|
---|
[1339] | 92 |
|
---|
| 93 | if (sequencesName == null) {
|
---|
| 94 | sequencesName = "sequences";
|
---|
| 95 | }
|
---|
[950] | 96 |
|
---|
| 97 | File folder = new File(path);
|
---|
| 98 | if (!folder.isDirectory()) {
|
---|
| 99 | Console.printerrln(path + " is not a directory");
|
---|
| 100 | return;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[1496] | 103 | HTMLLogParser parser = new HTMLLogParser(parseParamFile);
|
---|
[950] | 104 |
|
---|
[1220] | 105 | parseFile(folder, parser);
|
---|
[950] | 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 |
|
---|
[1220] | 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 |
|
---|
[2232] | 133 | if (children != null) {
|
---|
| 134 | Arrays.sort(children);
|
---|
| 135 |
|
---|
| 136 | for (String child : children) {
|
---|
| 137 | File childFile = new File(file, child);
|
---|
| 138 | parseFile(childFile, parser);
|
---|
| 139 | }
|
---|
[1220] | 140 | }
|
---|
| 141 | }
|
---|
| 142 | else if (file.isFile()) {
|
---|
| 143 | String source = file.getAbsolutePath();
|
---|
| 144 | Console.traceln(Level.INFO, "Processing file: " + source);
|
---|
| 145 |
|
---|
| 146 | try {
|
---|
| 147 | parser.parseFile(file);
|
---|
| 148 | }
|
---|
| 149 | catch (Exception e) {
|
---|
| 150 | Console.printerrln("Could not parse " + source + ": " + e.getMessage());
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[950] | 155 | /*
|
---|
| 156 | * (non-Javadoc)
|
---|
| 157 | *
|
---|
| 158 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
| 159 | */
|
---|
| 160 | @Override
|
---|
| 161 | public String help() {
|
---|
[1496] | 162 | return "parseDirHTML <directory> [<sequencesName>] {-parseParams=path/to/parseParamsFile}";
|
---|
[950] | 163 | }
|
---|
| 164 |
|
---|
| 165 | }
|
---|