diff --git a/codegen/src/main/java/org/jclouds/codegen/util/CodeGenerator.java b/codegen/src/main/java/org/jclouds/codegen/util/CodeGenerator.java
index 83ff899e05..91bb689e52 100644
--- a/codegen/src/main/java/org/jclouds/codegen/util/CodeGenerator.java
+++ b/codegen/src/main/java/org/jclouds/codegen/util/CodeGenerator.java
@@ -45,156 +45,141 @@ import freemarker.template.Template;
import freemarker.template.TemplateException;
/**
- * Converts object models representing AWS API beans into Java
- * classes.
+ * Converts object models representing AWS API beans into Java classes.
*
* This implementation is designed to perform the following steps:
*
- * - Parse the JSON object representation produced by the parse_ec2.pl
- * perl script
+ * - Parse the JSON object representation produced by the parse_ec2.pl perl script
* - Convert the JSON into Java object models (@see org.jclouds.aws.codegen.models)
* - Iterate through the Command objects generated above and with each one:
- *
- * - Load the FreeMarkertemplate file for the given
- * object
- * - Create a package directory to receive the Java file, if necessary
- * - Process the object through the template, producing a Java implementation of the object
- *
+ *
+ * - Load the FreeMarkertemplate file for the given object
+ * - Create a package directory to receive the Java file, if necessary
+ * - Process the object through the template, producing a Java implementation of the object
+ *
*
* @author James Murty
*/
public class CodeGenerator {
- public final static String COMMAND_TEMPLATE_FILENAME = "Command.ftl";
- public final static String BEAN_TEMPLATE_FILENAME = "Bean.ftl";
-
- private final File templateDirectory;
- private final File targetDirectory;
- private final String rootPackageName;
- private final Configuration config;
-
- public CodeGenerator(String rootPackageName, File templateDirectory, File targetDirectory)
- throws IOException
- {
- this.rootPackageName = rootPackageName;
- this.templateDirectory = templateDirectory;
- this.targetDirectory = targetDirectory;
-
- config = new Configuration();
- config.setDirectoryForTemplateLoading(this.templateDirectory);
- }
-
- /**
- * Parse a JSON object model file (as generated by
- * aws/src/main/bin/parse_ec2.pl) and convert
- * it into a Java object graph.
- *
- * @param objectModelFile
- * @return
- * @throws JsonParseException
- * @throws FileNotFoundException
- */
- public Model parseModelFromJSON(File objectModelFile)
- throws JsonParseException, FileNotFoundException
- {
- Gson gson = new Gson();
- return gson.fromJson(new FileReader(objectModelFile), Model.class);
- }
-
- /**
- * Parse a JSON object model file and iterate over the resulting
- * objects generating Java code files.
- *
- * @param objectModelFile
- * @throws JsonParseException
- * @throws IOException
- * @throws TemplateException
- */
- public void generateCode(File objectModelFile)
- throws JsonParseException, IOException, TemplateException
- {
- Model model = parseModelFromJSON(objectModelFile);
-
- for (Package pkg: model.getPackages()) {
- for (Command command: pkg.getCommands()) {
- generateClassFile(command, COMMAND_TEMPLATE_FILENAME);
-
- if (command.getHandler() != null) {
- generateClassFile(command.getHandler(), BEAN_TEMPLATE_FILENAME);
- }
- if (command.getOptions() != null) {
- generateClassFile(command.getOptions(), BEAN_TEMPLATE_FILENAME);
- }
- if (command.getResponse() != null) {
- generateClassFile(command.getResponse(), BEAN_TEMPLATE_FILENAME);
- }
- }
- }
- }
-
- /**
- * Generate a Java code file for the given object model bean, using the
- * given template name as the basis for the Java class.
- *
- * @param bean
- * @param templateFileName
- * @throws IOException
- * @throws TemplateException
- */
- public void generateClassFile(BaseBean bean, String templateFileName)
- throws IOException, TemplateException
- {
- String shortClassName = bean.getClassName().substring(
- bean.getClassName().lastIndexOf('.') + 1);
-
- Map objectMap = new HashMap();
- objectMap.put("bean", bean);
- objectMap.put("rootPackageName", this.rootPackageName);
- objectMap.put("shortClassName", shortClassName);
+ public final static String COMMAND_TEMPLATE_FILENAME = "Command.ftl";
+ public final static String BEAN_TEMPLATE_FILENAME = "Bean.ftl";
- File packageDir = new File(
- this.targetDirectory, bean.getPackageName().replace('.', '/'));
-
- if (packageDir.exists() || packageDir.mkdirs()) {
- File javaFile = new File(packageDir, shortClassName + ".java");
- FileWriter fw = new FileWriter(javaFile);
- System.out.println("Generated " + javaFile);
- applyTemplate(templateFileName, objectMap, fw);
- } else {
- throw new IllegalStateException(
- "Unable to create target package directory: " + packageDir);
- }
- }
-
- /**
- * Apply the given template to the given object, and output the results into the given writer.
- *
- * @param templateFileName
- * @param objectMap
- * @param writer
- * @throws IOException
- * @throws TemplateException
- */
- public void applyTemplate(String templateFileName, Map objectMap, Writer writer)
- throws IOException, TemplateException
- {
- Template template = this.config.getTemplate(templateFileName, "UTF-8");
- template.process(objectMap, writer);
- }
+ private final File targetDirectory;
+ private final String rootPackageName;
+ private final Configuration config;
+
+ public CodeGenerator(String rootPackageName, File targetDirectory) throws IOException {
+ this.rootPackageName = rootPackageName;
+ this.targetDirectory = targetDirectory;
+
+ config = new Configuration();
+ config.setClassForTemplateLoading(this.getClass(), "/templates");
+ }
+
+ /**
+ * Parse a JSON object model file (as generated by aws/src/main/bin/parse_ec2.pl) and
+ * convert it into a Java object graph.
+ *
+ * @param objectModelFile
+ * @return
+ * @throws JsonParseException
+ * @throws FileNotFoundException
+ */
+ public Model parseModelFromJSON(File objectModelFile) throws JsonParseException,
+ FileNotFoundException {
+ Gson gson = new Gson();
+ return gson.fromJson(new FileReader(objectModelFile), Model.class);
+ }
+
+ /**
+ * Parse a JSON object model file and iterate over the resulting objects generating Java code
+ * files.
+ *
+ * @param objectModelFile
+ * @throws JsonParseException
+ * @throws IOException
+ * @throws TemplateException
+ */
+ public void generateCode(File objectModelFile) throws JsonParseException, IOException,
+ TemplateException {
+ Model model = parseModelFromJSON(objectModelFile);
+
+ for (Package pkg : model.getPackages()) {
+ for (Command command : pkg.getCommands()) {
+ generateClassFile(command, COMMAND_TEMPLATE_FILENAME);
+
+ if (command.getHandler() != null) {
+ generateClassFile(command.getHandler(), BEAN_TEMPLATE_FILENAME);
+ }
+ if (command.getOptions() != null) {
+ generateClassFile(command.getOptions(), BEAN_TEMPLATE_FILENAME);
+ }
+ if (command.getResponse() != null) {
+ generateClassFile(command.getResponse(), BEAN_TEMPLATE_FILENAME);
+ }
+ }
+ }
+ }
+
+ /**
+ * Generate a Java code file for the given object model bean, using the given template name as
+ * the basis for the Java class.
+ *
+ * @param bean
+ * @param templateFileName
+ * @throws IOException
+ * @throws TemplateException
+ */
+ public void generateClassFile(BaseBean bean, String templateFileName) throws IOException,
+ TemplateException {
+ String shortClassName = bean.getClassName().substring(
+ bean.getClassName().lastIndexOf('.') + 1);
+
+ Map objectMap = new HashMap();
+ objectMap.put("bean", bean);
+ objectMap.put("rootPackageName", this.rootPackageName);
+ objectMap.put("shortClassName", shortClassName);
+
+ File packageDir = new File(this.targetDirectory, bean.getPackageName().replace('.', '/'));
+
+ if (packageDir.exists() || packageDir.mkdirs()) {
+ File javaFile = new File(packageDir, shortClassName + ".java");
+ FileWriter fw = new FileWriter(javaFile);
+ System.out.println("Generated " + javaFile);
+ applyTemplate(templateFileName, objectMap, fw);
+ } else {
+ throw new IllegalStateException("Unable to create target package directory: " + packageDir);
+ }
+ }
+
+ /**
+ * Apply the given template to the given object, and output the results into the given writer.
+ *
+ * @param templateFileName
+ * @param objectMap
+ * @param writer
+ * @throws IOException
+ * @throws TemplateException
+ */
+ public void applyTemplate(String templateFileName, Map objectMap, Writer writer)
+ throws IOException, TemplateException {
+ Template template = this.config.getTemplate(templateFileName, "UTF-8");
+ template.process(objectMap, writer);
+ }
+
+ public static void main(String[] args) throws Exception {
+ if (args.length != 3) {
+ System.out.println("Usage: CodeGenerator objectModelFile rootPackage targetDir");
+ System.out.println(" E.g: CodeGenerator ec2.json org.jclouds.aws.ec2 aws/ec2/api/src");
+ System.exit(1);
+ }
+ File objectModelFile = new File(args[0]);
+
+ String rootPackage = args[1];
+
+ File targetDir = new File(args[2]);
+
+ new CodeGenerator(rootPackage, targetDir).generateCode(objectModelFile);
+ }
-
- public static void main(String[] args) throws Exception {
- if (args.length != 4) {
- System.out.println(
- "Usage: CodeGenerator objectModelFile rootPackage templatesDir targetDir");
- System.out.println(
- " E.g: CodeGenerator ec2.json org.jclouds.aws.ec2 aws/codegen/templates aws/ec2/api/src");
- System.exit(1);
- }
-
- CodeGenerator codeGenerator = new CodeGenerator(
- args[1], new File(args[2]), new File(args[3]));
- codeGenerator.generateCode(new File(args[0]));
- }
-
}