public static JFMGenerator getInstance( final Class<? extends Object> clazz, final FreeMarkerGeneratorArguments arguments) throws Exception { // load the bean class Class<? extends Object> beanClass; if (arguments.getJavaClassFile() != null) { beanClass = ClassUtils.loadClassFromFile(arguments.getJavaClassFile()); } else if (arguments.getJavaClassName() != null) { beanClass = Class.forName(arguments.getJavaClassName()); } else { throw new IllegalStateException("either javaClassName or " + "javaClassFile is required"); } final JFMGenerator generator = loadGeneratorInstance(clazz); generator.setModel(createModel(beanClass, arguments)); // setup the template reader if (arguments.getTemplateFile() != null) { final File template = arguments.getTemplateFile(); generator.setTemplateReader(new FileReader(template)); } else if (arguments.getTemplateResourceName() != null) { final String resourceName = arguments.getTemplateResourceName(); final URL url = ClassLoader.getSystemResource(resourceName); final Reader reader = new InputStreamReader(url.openStream()); generator.setTemplateReader(reader); } else { throw new IllegalStateException( "either templateFile or " + "templateResourceName is required"); } generator.afterPropertiesSet(); // initializing bean return generator; }
public static void generateToOutputFileArgument( final JFMGenerator generator, final FreeMarkerGeneratorArguments arguments) throws IOException, TemplateException { final File output = arguments.getOutputFile(); output.getParentFile().mkdirs(); if (!output.exists() && !output.createNewFile()) { throw new IllegalStateException("could not create output file"); } final OutputStream outputStream = new FileOutputStream(output, false); try { generator.generate(outputStream); } finally { if (outputStream != null) { outputStream.close(); } if (generator.getTemplateReader() != null) { generator.getTemplateReader().close(); } } logger.info(output.getAbsolutePath() + " written"); }