001    package jp.osdl.jbento2.chart;
002    
003    import java.io.BufferedReader;
004    import java.io.File;
005    import java.io.FileReader;
006    import java.io.IOException;
007    import java.util.ArrayList;
008    import java.util.Iterator;
009    import java.util.List;
010    import java.util.regex.Matcher;
011    import java.util.regex.Pattern;
012    
013    import org.apache.commons.cli.CommandLine;
014    import org.apache.commons.cli.CommandLineParser;
015    import org.apache.commons.cli.HelpFormatter;
016    import org.apache.commons.cli.OptionBuilder;
017    import org.apache.commons.cli.Options;
018    import org.apache.commons.cli.ParseException;
019    import org.apache.commons.cli.PosixParser;
020    
021    /**
022     * Generate graphs. graph file name and title is same source csv file.
023     * 
024     * 
025     */
026    public class ChartAllCli {
027    
028        public static void main(String[] args) throws Exception {
029            Options options = new Options();
030    
031            options.addOption(OptionBuilder.withArgName("source dir").hasArg()
032                    .withDescription("source directory.").withLongOpt("source")
033                    .create("s"));
034    
035            options.addOption(OptionBuilder.withArgName("config file").hasArg()
036                            .withDescription("config file").withLongOpt("conf")
037                            .create("f"));
038            
039            options.addOption(OptionBuilder.withArgName("renew charts")
040                    .withDescription("renew charts").withLongOpt("renew").create(
041                            "r"));
042    
043            if (args.length == 0) {
044                HelpFormatter formatter = new HelpFormatter();
045                formatter.printHelp("ChartAllCli", options, true);
046                return;
047            }
048    
049            CommandLineParser parser = new PosixParser();
050            CommandLine line = null;
051            try {
052                line = parser.parse(options, args);
053            } catch (ParseException ex) {
054                throw ex;
055            }
056    
057            if (line.hasOption("h")
058                    || (line.getOptions().length == 0 && line.getArgs().length == 0)) {
059                HelpFormatter formatter = new HelpFormatter();
060                formatter.printHelp("ChartAllCli", options, true);
061                return;
062            }
063            
064            boolean renew = false;
065            if (line.hasOption("r")) {
066                renew = true;
067            }
068    
069            String configFile = line.getOptionValue("f");
070            String srcDir = line.getOptionValue("s");
071    
072            new ChartAllCli().execute(configFile, srcDir, renew);
073        }
074    
075        public void execute(String configFileName, String srcDirName, boolean renew)
076                throws IOException, ParseException {
077            File configFile = new File(configFileName);
078            List config = parseConfig(configFile);
079    
080            File srcDir = new File(srcDirName);
081    
082            if (!srcDir.isDirectory()) {
083                throw new IllegalArgumentException(srcDir.getName()
084                        + "is not a directory");
085            }
086            traverse(srcDir, config, renew);
087        }
088    
089        void traverse(File dir, List configList, boolean renew) throws IOException,
090                ParseException {
091            File[] files = dir.listFiles();
092            for (int i = 0; i < files.length; i++) {
093                File file = files[i];
094                if (file.isDirectory()) {
095                    traverse(file, configList, renew);
096                } else {
097                    Iterator ite = configList.iterator();
098                    while (ite.hasNext()) {
099                        ChartConfig config = (ChartConfig) ite.next();
100                        String regexName = config.getRegexName();
101                        Pattern pattern = Pattern.compile((String) regexName);
102                        Matcher matcher = pattern.matcher(file.getName());
103                        if (matcher.matches()) {
104    //                        System.out.println("generate chart for "
105    //                                + file.getAbsolutePath());
106                            try {
107                                List src = config.getParams();
108                                List args = new ArrayList(src.size());
109                                for (int j = 0; j < src.size(); j++) {
110                                    args.add(src.get(j));
111                                }
112                                args.add("-s");
113                                args.add(file.getAbsolutePath());
114                                if (renew) {
115                                    args.add("--renew");
116                                }
117                                ChartCli.main((String[]) args
118                                        .toArray(new String[0]));
119                            } catch (IOException e) {
120                                e.printStackTrace(System.err);
121                            }
122                        }
123                    }
124                }
125            }
126        }
127    
128        List parseConfig(File confFile) throws IOException {
129            List result = new ArrayList();
130    
131            BufferedReader reader = new BufferedReader(new FileReader(confFile));
132    
133            String line = null;
134            ChartConfig config = null;
135            while (true) {
136                line = reader.readLine();
137                if (line == null) {
138                    break;
139                } else if (line.startsWith("#")) {
140                    continue;
141                } else if (line.length() == 0) {
142                    continue;
143                } else if (line.startsWith(" ") || line.startsWith("\t")) {
144                    if (config == null) {
145                        throw new IllegalStateException("parse error around "
146                                + line);
147                    }
148                    List list = config.getParams();
149                    line = line.trim();
150                    String[] datas = parseLine(line);
151                    for (int i = 0; i < datas.length; i++) {
152                        String data = datas[i];
153                        list.add(data);
154                    }
155                } else {
156                    config = new ChartConfig(line);
157                    result.add(config);
158                }
159    
160            }
161            return result;
162        }
163    
164        String[] parseLine(String str) {
165            ArrayList result = new ArrayList();
166            char[] chars = str.toCharArray();
167            StringBuffer buffer = new StringBuffer();
168            boolean isInternalQuotes = false;
169            for (int i = 0; i < chars.length; i++) {
170                char c = chars[i];
171                if (c == ' ') {
172                    if (isInternalQuotes) {
173                        buffer.append(c);
174                    } else {
175                        result.add(buffer.toString());
176                        buffer = new StringBuffer();
177                    }
178                } else if (c == '"') {
179                    isInternalQuotes = !isInternalQuotes;
180                } else {
181                    buffer.append(c);
182                }
183            }
184            if (buffer.length() > 0) {
185                result.add(buffer.toString());
186            }
187            return (String[]) result.toArray(new String[0]);
188        }
189    
190        private class ChartConfig {
191            private String regexName = null;
192    
193            private List params = new ArrayList();
194    
195            public ChartConfig(String regexName) {
196                this.regexName = regexName;
197            }
198    
199            public List getParams() {
200                return params;
201            }
202    
203            public String getRegexName() {
204                return regexName;
205            }
206        }
207    
208    }