mirror of https://github.com/apache/jclouds.git
Issue 36: changed to use classpath to load templates
git-svn-id: http://jclouds.googlecode.com/svn/trunk@870 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
parent
e102d1e64f
commit
60018ae8c8
|
@ -45,22 +45,18 @@ 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.
|
||||
* <p>
|
||||
* This implementation is designed to perform the following steps:
|
||||
* <ul>
|
||||
* <li>Parse the JSON object representation produced by the <tt>parse_ec2.pl</tt>
|
||||
* perl script</li>
|
||||
* <li>Parse the JSON object representation produced by the <tt>parse_ec2.pl</tt> perl script</li>
|
||||
* <li>Convert the JSON into Java object models (@see org.jclouds.aws.codegen.models)</li>
|
||||
* <li>Iterate through the Command objects generated above and with each one:
|
||||
* <ul>
|
||||
* <li>Load the <a href="http://freemarker.org/">FreeMarker</a>template file for the given
|
||||
* object</li>
|
||||
* <li>Load the <a href="http://freemarker.org/">FreeMarker</a>template file for the given object</li>
|
||||
* <li>Create a package directory to receive the Java file, if necessary</li>
|
||||
* <li>Process the object through the template, producing a Java implementation of the object</li>
|
||||
* <ul>
|
||||
* </li>
|
||||
* <ul></li>
|
||||
*
|
||||
* @author James Murty
|
||||
*/
|
||||
|
@ -68,55 +64,48 @@ 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
|
||||
{
|
||||
public CodeGenerator(String rootPackageName, File targetDirectory) throws IOException {
|
||||
this.rootPackageName = rootPackageName;
|
||||
this.templateDirectory = templateDirectory;
|
||||
this.targetDirectory = targetDirectory;
|
||||
|
||||
config = new Configuration();
|
||||
config.setDirectoryForTemplateLoading(this.templateDirectory);
|
||||
config.setClassForTemplateLoading(this.getClass(), "/templates");
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a JSON object model file (as generated by
|
||||
* <tt>aws/src/main/bin/parse_ec2.pl</tt>) and convert
|
||||
* it into a Java object graph.
|
||||
* Parse a JSON object model file (as generated by <tt>aws/src/main/bin/parse_ec2.pl</tt>) and
|
||||
* convert it into a Java object graph.
|
||||
*
|
||||
* @param objectModelFile
|
||||
* @return
|
||||
* @throws JsonParseException
|
||||
* @throws FileNotFoundException
|
||||
*/
|
||||
public Model parseModelFromJSON(File objectModelFile)
|
||||
throws JsonParseException, 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.
|
||||
* 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
|
||||
{
|
||||
public void generateCode(File objectModelFile) throws JsonParseException, IOException,
|
||||
TemplateException {
|
||||
Model model = parseModelFromJSON(objectModelFile);
|
||||
|
||||
for (Package pkg: model.getPackages()) {
|
||||
for (Command command: pkg.getCommands()) {
|
||||
for (Package pkg : model.getPackages()) {
|
||||
for (Command command : pkg.getCommands()) {
|
||||
generateClassFile(command, COMMAND_TEMPLATE_FILENAME);
|
||||
|
||||
if (command.getHandler() != null) {
|
||||
|
@ -133,17 +122,16 @@ public class CodeGenerator {
|
|||
}
|
||||
|
||||
/**
|
||||
* Generate a Java code file for the given object model bean, using the
|
||||
* given template name as the basis for the Java class.
|
||||
* 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
|
||||
{
|
||||
public void generateClassFile(BaseBean bean, String templateFileName) throws IOException,
|
||||
TemplateException {
|
||||
String shortClassName = bean.getClassName().substring(
|
||||
bean.getClassName().lastIndexOf('.') + 1);
|
||||
|
||||
|
@ -152,8 +140,7 @@ public class CodeGenerator {
|
|||
objectMap.put("rootPackageName", this.rootPackageName);
|
||||
objectMap.put("shortClassName", shortClassName);
|
||||
|
||||
File packageDir = new File(
|
||||
this.targetDirectory, bean.getPackageName().replace('.', '/'));
|
||||
File packageDir = new File(this.targetDirectory, bean.getPackageName().replace('.', '/'));
|
||||
|
||||
if (packageDir.exists() || packageDir.mkdirs()) {
|
||||
File javaFile = new File(packageDir, shortClassName + ".java");
|
||||
|
@ -161,8 +148,7 @@ public class CodeGenerator {
|
|||
System.out.println("Generated " + javaFile);
|
||||
applyTemplate(templateFileName, objectMap, fw);
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"Unable to create target package directory: " + packageDir);
|
||||
throw new IllegalStateException("Unable to create target package directory: " + packageDir);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -176,25 +162,24 @@ public class CodeGenerator {
|
|||
* @throws TemplateException
|
||||
*/
|
||||
public void applyTemplate(String templateFileName, Map<String, Object> objectMap, Writer writer)
|
||||
throws IOException, TemplateException
|
||||
{
|
||||
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 != 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");
|
||||
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]);
|
||||
|
||||
CodeGenerator codeGenerator = new CodeGenerator(
|
||||
args[1], new File(args[2]), new File(args[3]));
|
||||
codeGenerator.generateCode(new File(args[0]));
|
||||
String rootPackage = args[1];
|
||||
|
||||
File targetDir = new File(args[2]);
|
||||
|
||||
new CodeGenerator(rootPackage, targetDir).generateCode(objectModelFile);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue