OSDN Git Service

必要のないクラスを削除し,クラスの整理を行った.
[stigmata/stigmata.git] / src / main / java / jp / sourceforge / stigmata / Main.java
1 package jp.sourceforge.stigmata;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.net.MalformedURLException;
6 import java.net.URL;
7 import java.util.ArrayList;
8 import java.util.List;
9
10 import jp.sourceforge.stigmata.command.HelpCommand;
11 import jp.sourceforge.stigmata.command.StigmataCommandFactory;
12 import jp.sourceforge.stigmata.digger.ClasspathContext;
13 import jp.sourceforge.stigmata.hook.Phase;
14 import jp.sourceforge.stigmata.hook.StigmataHookManager;
15 import jp.sourceforge.stigmata.spi.BirthmarkService;
16 import jp.sourceforge.talisman.xmlcli.CommandLinePlus;
17 import jp.sourceforge.talisman.xmlcli.OptionsBuilder;
18 import jp.sourceforge.talisman.xmlcli.XmlCliConfigurationException;
19 import jp.sourceforge.talisman.xmlcli.builder.OptionsBuilderFactory;
20
21 import org.apache.commons.cli.CommandLineParser;
22 import org.apache.commons.cli.Options;
23 import org.apache.commons.cli.ParseException;
24 import org.apache.commons.cli.PosixParser;
25 import org.w3c.dom.DOMException;
26
27 /**
28  * Front end class.
29  * 
30  * @author Haruaki TAMADA
31  */
32 public final class Main{
33     /**
34      * main process.
35      * @throws org.apache.commons.cli.ParseException 
36      */
37     public Main(String[] args) throws ParseException{
38         Options options = buildOptions();
39         CommandLineParser parser = new PosixParser();
40         CommandLinePlus commandLine = new CommandLinePlus(parser.parse(options, args, false));
41
42         Stigmata stigmata = Stigmata.getInstance();
43         stigmata.configuration(commandLine.getOptionValue("config-file"), commandLine.hasOption("reset-config"));
44
45         String[] arguments = commandLine.getArgs();
46         String commandString = "gui";
47
48         if(arguments.length > 0){
49             commandString = arguments[0];
50         }
51
52         arguments = shiftArray(arguments);
53
54         BirthmarkContext context = stigmata.createContext();
55         updateContext(context, commandLine);
56
57         StigmataCommandFactory factory = StigmataCommandFactory.getInstance();
58         factory.registerCommand("help", new HelpCommand(options));
59
60         StigmataCommand command = factory.getCommand(commandString);
61         if(!command.isAvailableArguments(arguments)){
62             command = factory.getCommand("help");
63         }
64
65         StigmataHookManager.getInstance().runHook(Phase.SETUP, context.getEnvironment());
66
67         command.setUp(context.getEnvironment());
68         command.perform(stigmata, context, arguments);
69         command.tearDown(context.getEnvironment());
70     }
71
72     /**
73      * shift right given array.
74      * @param args
75      */
76     private String[] shiftArray(String[] args){
77         if(args.length > 0){
78             String[] arguments = new String[args.length - 1];
79             System.arraycopy(args, 1, arguments, 0, arguments.length);
80             args = arguments;
81         }
82         return args;
83     }
84
85     private void updateContext(BirthmarkContext context, CommandLinePlus cl){
86         BirthmarkEnvironment env = context.getEnvironment();
87
88         String[] birthmarks = getTargetBirthmarks(env, cl);
89         for(int i = 0; i < birthmarks.length; i++){
90             context.addBirthmarkType(birthmarks[i]);
91         }
92         if(cl.hasOption("filter")){
93             String[] filters = cl.getOptionValues("filter");
94             for(int i = 0; i < filters.length; i++){
95                 context.addFilterType(filters[i]);
96             }
97         }
98         if(cl.hasOption("store-target")){
99             String value = cl.getOptionValue("store-target");
100             BirthmarkStoreTarget bst = BirthmarkStoreTarget.valueOf(value);
101             if(bst == null){
102                 bst = BirthmarkStoreTarget.MEMORY;
103             }
104             context.setStoreTarget(bst);
105         }
106         if(cl.hasOption("extraction-unit")){
107             ExtractionUnit unit = ExtractionUnit.valueOf(cl.getOptionValue("extraction-unit"));
108             context.setExtractionUnit(unit);
109         }
110         if(cl.hasOption("format")){
111             context.setFormat(cl.getOptionValue("format"));
112         }
113         else{
114             context.setFormat("xml");
115         }
116
117         addClasspath(env.getClasspathContext(), cl);
118     }
119
120     private String[] getTargetBirthmarks(BirthmarkEnvironment env, CommandLinePlus cl){
121         String[] birthmarks = cl.getOptionValues("birthmark");
122         if(birthmarks == null || birthmarks.length == 0){
123             List<String> birthmarkList = new ArrayList<String>();
124             for(BirthmarkService service: env.getServices()){
125                 if(!service.isExperimental()){
126                     birthmarkList.add(service.getType());
127                 }
128             }
129             birthmarks = birthmarkList.toArray(new String[birthmarkList.size()]);
130         }
131         return birthmarks;
132     }
133
134     private void addClasspath(ClasspathContext context, CommandLinePlus commandLine){
135         String[] classpath = commandLine.getOptionValues("classpath");
136
137         if(classpath != null){
138             for(String cp: classpath){
139                 try{
140                     File f = new File(cp);
141                     if(f.exists()){
142                         context.addClasspath(f.toURI().toURL());
143                     }
144                 }catch(MalformedURLException ex){
145                 }
146             }
147         }
148     }
149
150     private Options buildOptions(){
151         try{
152             OptionsBuilderFactory factory = OptionsBuilderFactory.getInstance();
153             URL location = getClass().getResource("/resources/options.xml");
154             OptionsBuilder builder = factory.createBuilder(location);
155             Options options = builder.buildOptions();
156
157             return options;
158         }catch(XmlCliConfigurationException ex){
159             ex.printStackTrace();
160         }catch(DOMException ex){
161             ex.printStackTrace();
162         }catch(IOException ex){
163             ex.printStackTrace();
164         }
165         return null;
166     }
167
168     public static void main(String[] args) throws Exception{
169         new Main(args);
170     }
171 }