1
2
3
4
5
6
7 package yawn.ui.cli;
8
9 import java.io.FileNotFoundException;
10 import java.io.FileOutputStream;
11 import java.io.OutputStreamWriter;
12
13 import org.apache.commons.cli.CommandLine;
14 import org.apache.commons.cli.GnuParser;
15 import org.apache.commons.cli.HelpFormatter;
16 import org.apache.commons.cli.MissingArgumentException;
17 import org.apache.commons.cli.MissingOptionException;
18 import org.apache.commons.cli.Option;
19 import org.apache.commons.cli.Options;
20 import org.apache.commons.cli.ParseException;
21 import org.apache.commons.cli.UnrecognizedOptionException;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import yawn.io.Experiment;
26 import yawn.io.serialization.SerializationException;
27 import yawn.io.serialization.xml.CastorXmlSerializer;
28
29 /***
30 * @author marti
31 */
32 public class CreateSampleExperiment {
33
34 private static final Log log = LogFactory
35 .getLog(CreateSampleExperiment.class);
36
37 protected static void printHelpMessage(String usageMessage, Options options) {
38 HelpFormatter hf = new HelpFormatter();
39 hf.printHelp(usageMessage, options, true);
40 }
41
42 protected static void printErrorMessage(String error) {
43 log.fatal("Application error message: " + error);
44 }
45
46 private static Option EXPERIMENT_OPTION = new Option("x", "experiment",
47 true, "Experiment setup xml file to create.");
48
49 private static Option HELP_OPTION = new Option("h", "help", false,
50 "View this help message.");
51
52 private static String USAGE_MESG = "java yawn.ui.cli.CreateSampleExperiment";
53
54 private static Option MODEL_OPTION = new Option("m", "model-config", true,
55 "Model configuration class name to use.");
56
57 private static Option ENVIRONMENT_OPTION = new Option("e", "environment", true,
58 "Environment class name to use.");
59
60 private static Option STORAGE_OPTION = new Option("s", "storage", true,
61 "Storage class name to use.");
62
63 private static String WELCOME_MESG = "\nWelcome to CreateSampleExperiment: A tool for creating samples of experiment configuration files.\n"
64 + "Distributed under the GNU license (see http://www.gnu.org/licenses/gpl.html).\n"
65 + "Check http://yawn2.sourceforge.net for updates.\n";
66
67 public static void main(String args[]) {
68 CommandLine cl = null;
69 try {
70 String a = "ASERE";
71
72 for (int i = 0; i < a.length(); i++){
73 System.out.println(Integer.toHexString(a.charAt(i)));
74 }
75 cl = parseCommandLine(args);
76 String experimentFileName = cl.getOptionValue(EXPERIMENT_OPTION.getOpt());
77 String configClassName = cl.getOptionValue(MODEL_OPTION.getOpt());
78 String environmentClassName = cl.getOptionValue(ENVIRONMENT_OPTION.getOpt());
79 String storageClassName = cl.getOptionValue(STORAGE_OPTION.getOpt());
80
81 Experiment exp = new Experiment();
82
83 CastorXmlSerializer serializer = new CastorXmlSerializer();
84
85 FileOutputStream fos = new FileOutputStream(experimentFileName);
86 OutputStreamWriter writer = new OutputStreamWriter(fos);
87
88 serializer.serialize(exp,writer);
89 } catch (CommandLineHaltRequestedException e) {
90 System.exit(1);
91 } catch (FileNotFoundException e) {
92
93 e.printStackTrace();
94 } catch (SerializationException e) {
95
96 e.printStackTrace();
97 }
98
99 }
100
101 private static CommandLine parseCommandLine(String[] args)
102 throws CommandLineHaltRequestedException {
103 EXPERIMENT_OPTION.setRequired(true);
104 EXPERIMENT_OPTION.setArgs(1);
105 EXPERIMENT_OPTION.setArgName("experiment-file.xml");
106
107 MODEL_OPTION.setRequired(true);
108 MODEL_OPTION.setArgs(1);
109 MODEL_OPTION.setArgName("yawn.one.ModelConfig");
110
111 ENVIRONMENT_OPTION.setRequired(true);
112 ENVIRONMENT_OPTION.setArgs(1);
113 ENVIRONMENT_OPTION.setArgName("yawn.one.Environment");
114
115 STORAGE_OPTION.setRequired(true);
116 STORAGE_OPTION.setArgs(1);
117 STORAGE_OPTION.setArgName("yawn.one.Storage");
118
119 Options options = new Options();
120 options.addOption(EXPERIMENT_OPTION);
121 options.addOption(MODEL_OPTION);
122 options.addOption(ENVIRONMENT_OPTION);
123 options.addOption(STORAGE_OPTION);
124
125
126 for (int i = 0; i < args.length; i++) {
127 if (args[i].equals("-" + HELP_OPTION.getOpt())
128 || args[i].equals("--" + HELP_OPTION.getLongOpt())) {
129 printHelpMessage(USAGE_MESG, options);
130 throw new CommandLineHaltRequestedException();
131 }
132 }
133
134 CommandLine cl = null;
135
136 try {
137 return (new GnuParser()).parse(options, args);
138 } catch (ParseException e) {
139 if (e instanceof MissingOptionException) {
140 printErrorMessage("Missing required option(s).");
141 } else if (e instanceof MissingArgumentException) {
142 printErrorMessage("Missing required argument.");
143 } else if (e instanceof UnrecognizedOptionException) {
144 printErrorMessage("Unrecognized option `" + e.getMessage()
145 + "'.");
146 } else {
147 printErrorMessage(e.getMessage());
148 }
149 printHelpMessage(USAGE_MESG, options);
150 throw new CommandLineHaltRequestedException();
151 }
152 }
153 }