1 package rydeen;
2
3 import java.io.IOException;
4 import java.net.URL;
5
6 import jp.sourceforge.talisman.xmlcli.CommandLinePlus;
7 import jp.sourceforge.talisman.xmlcli.OptionsBuilder;
8 import jp.sourceforge.talisman.xmlcli.XmlCliConfigurationException;
9 import jp.sourceforge.talisman.xmlcli.builder.OptionsBuilderFactory;
10
11 import org.apache.commons.cli.CommandLineParser;
12 import org.apache.commons.cli.HelpFormatter;
13 import org.apache.commons.cli.Options;
14 import org.apache.commons.cli.PosixParser;
15 import org.w3c.dom.DOMException;
16
17 import rydeen.spi.ProcessorService;
18 import rydeen.utils.Author;
19 import rydeen.utils.Organization;
20 import rydeen.utils.Provider;
21
22
23
24
25
26
27 public class Main{
28 public Main(String[] args){
29 try{
30 Options options = buildOptions();
31 CommandLineParser parser = new PosixParser();
32 CommandLinePlus commandLine = new CommandLinePlus(parser.parse(options, args, false));
33 Environment env = new Environment();
34 System.out.println("help option: " + commandLine.hasOption("help"));
35 System.out.println("processors: " + commandLine.getOptionValue("processors"));
36 System.out.println("args length: " + commandLine.getArgs().length);
37 if(commandLine.hasOption("help")
38 || commandLine.getOptionValue("processors") == null
39 || commandLine.getArgs().length == 0){
40 showHelp(env, commandLine, options);
41 return;
42 }
43 Context context = new Context(env);
44 updateContext(context, commandLine);
45
46 Rydeen rydeen = new Rydeen(context);
47 rydeen.execute();
48
49 } catch(Exception e){
50 e.printStackTrace();
51 }
52 }
53
54 private void updateContext(Context context, CommandLinePlus cl){
55 for(String arg: cl.getArgs()){
56 context.addTarget(arg);
57 }
58 String processorNames = cl.getOptionValue("processors");
59 for(String processorName: processorNames.split(",")){
60 context.addProcessorName(processorName);
61 }
62
63 context.setDestination(cl.getOptionValue("destination"));
64 }
65
66 private void showHelp(Environment env, CommandLinePlus commandLine, Options options){
67 String processorName = commandLine.getOptionValue("processors");
68 if(processorName == null){
69 showSimpleHelp(commandLine, env, options);
70 }
71 else{
72 boolean flag = false;
73 for(String pName: processorName.split(",[ \t]*")){
74 flag = flag | showProcessorHelp(pName, env, options);
75 }
76 if(flag){
77 showProcessorList(env);
78 }
79 }
80 }
81
82 private void showUnknownProcessor(String processorName){
83 System.out.printf("%s: unknown%n", processorName);
84 }
85
86 private boolean showProcessorHelp(String processorName, Environment env, Options options){
87 boolean flag = false;
88 ProcessorService processorService = env.getService(processorName);
89 if(processorService == null){
90 showUnknownProcessor(processorName);
91 flag = true;
92 }
93 else{
94 Provider provider = processorService.getProvider();
95
96 System.out.printf("Processor Name: %s%n", processorName);
97 System.out.printf("Description: %s%n", processorService.getDescription());
98 Author[] authors = provider.getAuthors();
99 if(authors.length == 1){
100 System.out.printf("Author: %s%n", authors[0]);
101 }
102 else if(authors.length > 1){
103 System.out.println("Authors:");
104 for(Author author: authors){
105 System.out.printf(" %s%n", author);
106 }
107 }
108 Organization org = provider.getOrganization();
109 if(org != null){
110 System.out.printf("Organization: %s ", org.getName());
111 if(org.getUrl() != null){
112 System.out.printf("<%s>", org.getUrl());
113 }
114 System.out.println();
115 }
116 Arguments arguments = processorService.getDefaultArguments();
117 if(arguments.getArgumentCount() > 0){
118 System.out.println();
119 System.out.println("Parameters");
120 for(Argument arg: arguments){
121 System.out.printf(
122 " %s: %s%n %s%n",
123 arg.getName(), arg.getValue(), arg.getDescription()
124 );
125 }
126 }
127 }
128 return flag;
129 }
130
131 private void showSimpleHelp(CommandLinePlus commandLine, Environment env, Options options){
132 HelpFormatter formatter = new HelpFormatter();
133 Package packageObject = getClass().getPackage();
134 if(packageObject != null){
135 formatter.printHelp(
136 String.format(
137 "java rydeen-%s.jar [OPTIONS] <targets...>",
138 packageObject.getImplementationVersion()
139 ),
140 options
141 );
142 }
143 else{
144 formatter.printHelp("java rydeen.jar [OPTIONS] <targets...>", options);
145 }
146
147 showProcessorList(env);
148 }
149
150 private void showProcessorList(Environment env){
151 if(env.getServiceCount() > 0){
152 System.out.println();
153 System.out.println("Available Processors");
154 for(ProcessorService service: env){
155 System.out.printf(" %s: %s%n", service.getProcessorName(), service.getDescription());
156 }
157 }
158 }
159
160 private Options buildOptions(){
161 try{
162 OptionsBuilderFactory factory = OptionsBuilderFactory.getInstance();
163 URL location = getClass().getResource("/resources/options.xml");
164 OptionsBuilder builder = factory.createBuilder(location);
165 Options options = builder.buildOptions();
166
167 return options;
168 }catch(XmlCliConfigurationException ex){
169 ex.printStackTrace();
170 }catch(DOMException ex){
171 ex.printStackTrace();
172 }catch(IOException ex){
173 ex.printStackTrace();
174 }
175 return null;
176 }
177
178 public static void main(String[] args){
179 new Main(args);
180 }
181 }