001 package jp.osdl.jbento2.analyzer;
002
003 import java.io.BufferedReader;
004 import java.io.File;
005 import java.io.FileNotFoundException;
006 import java.io.FileReader;
007 import java.io.IOException;
008 import java.util.ArrayList;
009 import java.util.HashMap;
010 import java.util.Iterator;
011 import java.util.List;
012 import java.util.Map;
013 import java.util.Set;
014
015 import org.apache.commons.cli.*;
016 import org.apache.commons.io.FilenameUtils;
017
018 public class MultiResultCollectorAllCli {
019
020 private final String JBENTO_FILE = "jbento.conf";
021 private String delimiter = ",";
022 private Map map = null;
023 private String targetRow = null;
024 private String targetColumn = null;
025 private String seriesName = null;
026 private String xLabelName = null;
027
028 public static void main(String[] args) throws Exception {
029 Options options = new Options();
030
031 options.addOption(
032 OptionBuilder.
033 withArgName("source dir").
034 hasArg().
035 withDescription("source directory.").
036 withLongOpt("source").
037 create("s"));
038
039 options.addOption(
040 OptionBuilder.
041 withArgName("config file").
042 hasArg().
043 withDescription("config file").
044 withLongOpt("conf").
045 create("f"));
046
047 if (args.length == 0) {
048 HelpFormatter formatter = new HelpFormatter();
049 formatter.printHelp("MultiResultCollectorAllCli", options, true);
050 return;
051 }
052
053 CommandLineParser parser = new PosixParser();
054 CommandLine line = null;
055 try {
056 line = parser.parse(options, args);
057 } catch (ParseException ex) {
058 throw ex;
059 }
060
061 if (line.hasOption("h") ||
062 (line.getOptions().length == 0 && line.getArgs().length == 0)) {
063 HelpFormatter formatter = new HelpFormatter();
064 formatter.printHelp("MultiResultCollectorAllCli", options, true);
065 return;
066 }
067
068 String configFile = line.getOptionValue("f");
069 String srcDir = line.getOptionValue("s");
070
071 new MultiResultCollectorAllCli().execute(configFile, srcDir);
072 }
073
074 public void execute(String configFileName, String srcDirName)
075 throws IOException, ParseException {
076 File configFile = new File(configFileName);
077 List config = parseConfig(configFile);
078
079 File srcDir = new File(srcDirName);
080
081 if (!srcDir.isDirectory()) {
082 throw new IllegalArgumentException(srcDir.getName()
083 + "is not a directory");
084 }
085
086 Iterator ite = config.iterator();
087 while (ite.hasNext()) {
088 ResultCollectConfig conf = (ResultCollectConfig) ite.next();
089
090 Map constName = new HashMap();// ResultCollector.confの--constNameに設定された値を入れる
091 List param = conf.getParams();
092 for (int j = 0; j < param.size(); j++) {
093 if (param.get(j).equals("--constName")) {
094 constName.put(param.get(j + 1), param.get(j + 2));
095 } else if (param.get(j).equals("--xLabelName")) {
096 xLabelName = (String) param.get(j + 1);
097 } else if (param.get(j).equals("--seriesName")) {
098 seriesName = (String) param.get(j + 1);
099 } else if (param.get(j).equals("--target")) {
100 targetRow = (String) param.get(j + 1);
101 targetColumn = (String) param.get(j + 2);
102 }
103 }
104
105 map = new HashMap();
106
107 traverse(srcDir, conf, constName);
108
109 if (conf.getFileName().indexOf(".") == -1) {
110 return;
111 }
112
113 MultiResultCollector collector = new MultiResultCollector();
114 collector.setTarget(targetRow);
115 collector.setMap(map);
116
117 String destDirName =
118 srcDir.getAbsolutePath() + File.separator + "result_collect";
119 File destDir = new File(destDirName);
120 destDir.mkdirs();
121 String destFileName =
122 FilenameUtils.getBaseName(conf.getFileName()) + ".csv";
123 collector.setDestName(destDirName + File.separator + destFileName);
124
125 collector.analyzeNumeric();
126 }
127 }
128
129 void traverse(File src, ResultCollectConfig config, Map constName)
130 throws IOException, ParseException {
131 Map confMap = new HashMap();
132 String fileName = null;// 検索対象ファイル
133
134 File[] files = src.listFiles();
135 fileName = config.getFileName();
136
137 for (int i = 0; i < files.length; i++) {
138 File file = files[i];
139 if (file.isDirectory()) {
140 traverse(file, config, constName);
141 } else {
142
143 if (file.getName().equals(JBENTO_FILE)) {
144 try {
145 boolean isMatch = false;
146 confMap = readConfFile(file);
147
148 Set const_keyset = constName.keySet();
149 Iterator ite_const = const_keyset.iterator();
150
151 // constName指定なし
152 if (const_keyset.size() == 0
153 && confMap.containsKey(seriesName)
154 && confMap.containsKey(xLabelName)) {
155 isMatch = true;
156 }
157 while (ite_const.hasNext()) {
158 String const_key = (String) ite_const.next();
159 if (confMap.get(const_key).equals(
160 constName.get(const_key))) {
161 if (confMap.containsKey(seriesName)
162 && confMap.containsKey(xLabelName)) {
163 isMatch = true;
164 }
165 } else {
166 isMatch = false;
167 break;
168 }
169 }
170 List xyset = new ArrayList();
171 // matchしたら、対象ファイルを読み込む
172 if (isMatch) {
173 xyset.add(confMap.get(seriesName));
174 xyset.add(confMap.get(xLabelName));
175 findFile(file, xyset, fileName, targetRow);
176 // continue;
177 } else {
178 // noop
179 // continue;
180 }
181
182 } catch (IndexOutOfBoundsException e) {
183 e.printStackTrace();
184 }
185 }
186 }
187 }
188 }
189
190 protected Map readConfFile(File src) throws IOException {
191
192 BufferedReader reader = null;
193 Map confMap = new HashMap();
194 try {
195 reader = new BufferedReader(new FileReader(src));
196
197 while (true) {
198 String line = reader.readLine();
199 if (line == null) {
200 break;
201 }
202 String[] data = line.split(" ");
203 confMap.put(data[0], data[1]);
204 }
205
206 } finally {
207 if (reader != null) {
208 try {
209 reader.close();
210 } catch (IOException ignore) {
211 // noop.
212 }
213 }
214 }
215 return confMap;
216 }
217
218 private File determineTargetDir(File src) {
219 String path = src.getAbsolutePath();
220 if (path.lastIndexOf("conf" + File.separator) == -1) {
221 return null;
222 }
223 return new File(
224 path.substring(0, path.lastIndexOf("conf" + File.separator)) +
225 "report");
226 }
227
228 protected void findFile(File src, List keyList, String fileName,
229 String target) throws IOException {
230 File targetDir = determineTargetDir(src);
231 if (targetDir == null) {
232 return;
233 }
234
235 String targetFile = null;
236 // sarの結果の場合は「kuma01」等のディレクトリ名が入るので場合分けする
237 if (fileName.indexOf("average") != -1) {
238 targetFile =
239 targetDir.getAbsolutePath()
240 + File.separator
241 + fileName.substring(0, fileName.indexOf("."))
242 + File.separator + fileName;
243
244 } else {
245 targetFile =
246 targetDir.getAbsolutePath() + File.separator + fileName;
247 }
248
249 BufferedReader reader = null;
250
251 try {
252 reader = new BufferedReader(new FileReader(targetFile));
253
254 String firstLine = reader.readLine();
255 String[] columns = firstLine.split(delimiter);
256 int columns_index = -1;
257
258 for (int i = 0; i < columns.length; i++) {
259
260 columns[i] = columns[i].trim();
261 if (columns[i].equals(target)) {
262 columns_index = i;
263 }
264 }
265
266 while (true) {
267 String line = reader.readLine();
268 if (line == null) {
269 break;
270 }
271 String[] data = line.split(delimiter);
272
273 for (int i = 0; i < data.length; i++) {
274 data[i] = data[i].trim();
275 }
276
277 if (!data[0].equals(targetColumn)) {
278 continue;
279 } else {
280 map.put(keyList, data[columns_index]);
281 }
282 }
283 } catch (FileNotFoundException e) {
284 // noop
285
286 } finally {
287 if (reader != null) {
288 try {
289 reader.close();
290 } catch (IOException ignore) {
291 // noop.
292 }
293 }
294 }
295 }
296
297 List parseConfig(File confFile) throws IOException {
298 List result = new ArrayList();
299
300 BufferedReader reader = new BufferedReader(new FileReader(confFile));
301
302 String line = null;
303 ResultCollectConfig config = null;
304 while (true) {
305 line = reader.readLine();
306 if (line == null) {
307 break;
308 } else if (line.startsWith("#")) {
309 continue;
310 } else if (line.length() == 0) {
311 continue;
312 } else if (line.startsWith(" ") || line.startsWith("\t")) {
313 if (config == null) {
314 throw new IllegalStateException("parse error around "
315 + line);
316 }
317 List list = config.getParams();
318 line = line.trim();
319 String[] data = parseLine(line);
320 for (int i = 0; i < data.length; i++) {
321 list.add(data[i]);
322 }
323 } else {
324 config = new ResultCollectConfig(line);
325 result.add(config);
326 }
327
328 }
329 return result;
330 }
331
332 String[] parseLine(String str) {
333 ArrayList result = new ArrayList();
334 char[] chars = str.toCharArray();
335 StringBuffer buffer = new StringBuffer();
336 boolean isInternalQuotes = false;
337 for (int i = 0; i < chars.length; i++) {
338 char c = chars[i];
339 if (c == ' ') {
340 if (isInternalQuotes) {
341 buffer.append(c);
342 } else {
343 result.add(buffer.toString());
344 buffer = new StringBuffer();
345 }
346 } else if (c == '"') {
347 isInternalQuotes = !isInternalQuotes;
348 } else {
349 buffer.append(c);
350 }
351 }
352 if (buffer.length() > 0) {
353 result.add(buffer.toString());
354 }
355 return (String[]) result.toArray(new String[0]);
356 }
357
358 private class ResultCollectConfig {
359 private String fileName = null;
360
361 private List params = new ArrayList();
362
363 public ResultCollectConfig(String fileName) {
364 this.fileName = fileName;
365 }
366
367 public List getParams() {
368 return params;
369 }
370
371 public String getFileName() {
372 return fileName;
373 }
374 }
375 }