001    package jp.osdl.jbento2.chart;
002    
003    import java.io.File;
004    import java.io.IOException;
005    import java.util.Iterator;
006    
007    import org.apache.commons.cli.CommandLine;
008    import org.apache.commons.cli.CommandLineParser;
009    import org.apache.commons.cli.HelpFormatter;
010    import org.apache.commons.cli.Option;
011    import org.apache.commons.cli.OptionBuilder;
012    import org.apache.commons.cli.Options;
013    import org.apache.commons.cli.ParseException;
014    import org.apache.commons.cli.PosixParser;
015    
016    public class ChartCli {
017    
018        public static void main(String[] args) throws ParseException, IOException {
019            Options options = new Options();
020    
021            options.addOption(OptionBuilder.withArgName("source file").hasArg()
022                    .isRequired().withDescription("source file.").withLongOpt(
023                            "source").create("s"));
024    
025            options.addOption(OptionBuilder.withArgName("dest file").hasArg()
026                    .withDescription("dest file.").withLongOpt("dest").create("d"));
027    
028            options.addOption(OptionBuilder.withArgName("dest file postfix")
029                    .hasArg().withDescription("dest file postfix.").withLongOpt(
030                            "postfix").create("p"));
031    
032            options.addOption(OptionBuilder.withArgName("title").hasArg()
033                    .withDescription("title.").withLongOpt("title").create("i"));
034    
035            options.addOption(OptionBuilder.withArgName("x Label").hasArg()
036                    .withDescription("x-axis Label.").withLongOpt("xLabel")
037                    .create());
038    
039            options.addOption(OptionBuilder.withArgName("y Label").hasArg()
040                    .withDescription("y-axis Label.").withLongOpt("yLabel")
041                    .create());
042    
043            options.addOption(OptionBuilder.withArgName("width").hasArg()
044                    .withDescription("width of graph.").withLongOpt("width")
045                    .create());
046    
047            options.addOption(OptionBuilder.withArgName("height").hasArg()
048                    .withDescription("height of graph.").withLongOpt("height")
049                    .create());
050    
051            options.addOption(OptionBuilder.withArgName("x Min").hasArg()
052                    .withDescription("minimum number of x-axis.").withLongOpt("xMin")
053                    .create());
054            
055            options.addOption(OptionBuilder.withArgName("x Max").hasArg()
056                    .withDescription("max number of x-axis.").withLongOpt("xMax")
057                    .create("X"));
058    
059            options.addOption(OptionBuilder.withArgName("y Min").hasArg()
060                    .withDescription("minimum number of y-axis.").withLongOpt("yMin")
061                    .create());
062    
063            options.addOption(OptionBuilder.withArgName("y Max").hasArg()
064                    .withDescription("max number of y-axis.").withLongOpt("yMax")
065                    .create("Y"));
066    
067            options.addOption(OptionBuilder.withArgName("no shape")
068                    .withDescription("remove shape").withLongOpt("noshape")
069                    .create());
070    
071            options.addOption(OptionBuilder.withArgName("categories").hasArgs()
072                    .withDescription("categories of graph").withLongOpt(
073                            "categories").create("c"));
074    
075            options.addOption(OptionBuilder.withArgName("end of data").hasArg()
076                    .withDescription("string about end of data in csv file")
077                    .withLongOpt("eod").create("e"));
078    
079            options.addOption(OptionBuilder.withArgName("chart type").hasArg()
080                    .isRequired().withDescription("chart type").withLongOpt("type")
081                    .create("t"));
082    
083            options.addOption(OptionBuilder.withArgName("dataset builder").hasArg()
084                    .isRequired().withDescription("dataset builder name")
085                    .withLongOpt("builder").create("b"));
086            
087            options.addOption(OptionBuilder.withArgName("renew chart")
088                    .withDescription("renew chart").withLongOpt("renew").create());
089    
090            if (args.length == 0) {
091                HelpFormatter formatter = new HelpFormatter();
092                formatter.printHelp("ChartCli", options, true);
093                return;
094            }
095    
096            CommandLineParser parser = new PosixParser();
097            CommandLine line = null;
098            try {
099                line = parser.parse(options, args);
100            } catch (ParseException ex) {
101                throw ex;
102            }
103    
104            if (line.hasOption("h")
105                    || (line.getOptions().length == 0 && line.getArgs().length == 0)) {
106                HelpFormatter formatter = new HelpFormatter();
107                formatter.printHelp("ChartCli", options, true);
108                return;
109            }
110    
111            ChartGenerator generator = new ChartGenerator();
112    
113            for (Iterator it = line.iterator(); it.hasNext();) {
114                Option o = (Option) it.next();
115                String longOpt = o.getLongOpt();
116                if (longOpt.equals("source")) {
117                    generator.setSrc(new File(o.getValue()));
118                } else if (longOpt.equals("dest")) {
119                    generator.setDest(new File(o.getValue()));
120                } else if (longOpt.equals("postfix")) {
121                    generator.setPostfix(o.getValue());
122                } else if (longOpt.equals("title")) {
123                    generator.setTitle(o.getValue());
124                } else if (longOpt.equals("xLabel")) {
125                    generator.setXlabel(o.getValue());
126                } else if (longOpt.equals("yLabel")) {
127                    generator.setYlabel(o.getValue());
128                } else if (longOpt.equals("width")) {
129                    generator.setWidth(Integer.parseInt(o.getValue()));
130                } else if (longOpt.equals("height")) {
131                    generator.setHeight(Integer.parseInt(o.getValue()));
132                } else if (longOpt.equals("xMin")) {
133                    generator.setXMin(Long.parseLong(o.getValue()));
134                } else if (longOpt.equals("xMax")) {
135                    generator.setXMax(Long.parseLong(o.getValue()));
136                } else if (longOpt.equals("yMin")) {
137                    generator.setYMin(Long.parseLong(o.getValue()));
138                } else if (longOpt.equals("yMax")) {
139                    generator.setYMax(Long.parseLong(o.getValue()));
140                } else if (longOpt.equals("noshape")) {
141                    generator.setShape(false);
142                } else if (longOpt.equals("categories")) {
143                    generator.setCategories(o.getValues());
144                } else if (longOpt.equals("eod")) {
145                    generator.setEndOfData(o.getValue());
146                } else if (longOpt.equals("type")) {
147                    generator.setChartType(ChartTypeFactory.create(o.getValue()));
148                } else if (longOpt.equals("builder")) {
149                    generator.setDsBuilder(DataSetBuilderFactory
150                            .getDataSetBuilder(o.getValue()));
151                } else if (longOpt.equals("renew")) {
152                    generator.setRenew(true);
153                }
154            }
155    
156            generator.execute();
157        }
158    }