Author: tchemit Date: 2009-10-11 15:10:37 +0200 (Sun, 11 Oct 2009) New Revision: 1573 Removed: branches/jaxx-2.X/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/TemplateGenerator.java Modified: branches/jaxx-2.X/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateHelpMojo.java Log: use VelocityTemplateGenerator from maven-helper-plugin Modified: branches/jaxx-2.X/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateHelpMojo.java =================================================================== --- branches/jaxx-2.X/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateHelpMojo.java 2009-10-10 16:48:11 UTC (rev 1572) +++ branches/jaxx-2.X/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateHelpMojo.java 2009-10-11 13:10:37 UTC (rev 1573) @@ -39,6 +39,7 @@ import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.StringUtils; import org.nuiton.i18n.I18n; +import org.nuiton.plugin.VelocityTemplateGenerator; import org.nuiton.util.FileUtil; import org.nuiton.util.SortedProperties; @@ -347,7 +348,7 @@ protected int generateContentFiles(File localizedTarget, Properties env, String localePath) throws Exception { int touchedFiles = 0; - TemplateGenerator gen = prepareGenerator(contentTemplate); + VelocityTemplateGenerator gen = prepareGenerator(contentTemplate); Enumeration<?> keys = helpIds.keys(); while (keys.hasMoreElements()) { String key = (String) keys.nextElement(); @@ -572,17 +573,17 @@ } protected void doGen(File template, File f, Properties env) throws Exception { - TemplateGenerator gen = prepareGenerator(template); + VelocityTemplateGenerator gen = prepareGenerator(template); gen.generate(env, f); } - protected TemplateGenerator prepareGenerator(File template) throws Exception { + protected VelocityTemplateGenerator prepareGenerator(File template) throws Exception { URL templateURL = getTemplate(template); if (verbose) { getLog().info("using template " + templateURL); } - TemplateGenerator gen = new TemplateGenerator(project, templateURL); + VelocityTemplateGenerator gen = new VelocityTemplateGenerator(project, templateURL); return gen; } } Deleted: branches/jaxx-2.X/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/TemplateGenerator.java =================================================================== --- branches/jaxx-2.X/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/TemplateGenerator.java 2009-10-10 16:48:11 UTC (rev 1572) +++ branches/jaxx-2.X/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/TemplateGenerator.java 2009-10-11 13:10:37 UTC (rev 1573) @@ -1,142 +0,0 @@ -/* - * Copyright 2001-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License" ); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.nuiton.jaxx.plugin; - -import java.io.File; -import java.io.FileWriter; -import java.net.URISyntaxException; -import java.net.URL; -import java.util.Iterator; -import java.util.Properties; - -import org.apache.maven.project.MavenProject; -import org.apache.velocity.Template; -import org.apache.velocity.VelocityContext; -import org.apache.velocity.app.VelocityEngine; - -/** - * Generator of template base on velocity. - * - * @author chemit - * @since 1.3 - * - */ -public class TemplateGenerator { - - protected VelocityEngine engine; - protected final MavenProject mavenProject; - protected Template velocityTemplate; - - protected TemplateGenerator(MavenProject mavenProject, URL template) throws URISyntaxException { - - if (mavenProject == null) { - throw new IllegalArgumentException("mavenProject must not be null"); - } - - if (template == null) { - throw new IllegalArgumentException("template must not be null"); - } - - this.mavenProject = mavenProject; - - Properties props = new Properties(); - - String templateName; - - if (template.toURI().isOpaque()) { - - // template is in a jar - - props = new Properties(); - props.setProperty("resource.loader", "jar"); - props.setProperty("jar.resource.loader.description", "Jar resource loader for default webstart templates"); - props.setProperty("jar.resource.loader.class", "org.apache.velocity.runtime.resource.loader.JarResourceLoader"); - - // obtain the jar url - - String url = template.toString(); - int i = url.indexOf("!"); - templateName = url.substring(i + 2); - - props.setProperty("jar.resource.loader.path", url.substring(0, i + 2)); - - } else { - - - templateName = new File(template.getFile()).getName(); - - //props.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.NullLogSystem"); - props.setProperty("file.resource.loader.path", template.getFile()); - - } - - try { - engine = new VelocityEngine(); - //engine.setProperty("runtime.log.logsystem", new NullLogSystem()); - engine.init(props); - } catch (Exception e) { - IllegalArgumentException iae = new IllegalArgumentException("Could not initialise Velocity"); - iae.initCause(e); - throw iae; - } - - try { - this.velocityTemplate = engine.getTemplate(templateName); - } catch (Exception e) { - IllegalArgumentException iae = - new IllegalArgumentException("Could not load the template file from '" + template + "'"); - iae.initCause(e); - throw iae; - } - } - - public void generate(Properties context, File outputFile) throws Exception { - - - VelocityContext vcontext = new VelocityContext(); - - // Note: properties that contain dots will not be properly parsed by Velocity. Should we replace dots with underscores ? - addPropertiesToContext(System.getProperties(), vcontext); - - addPropertiesToContext(mavenProject.getProperties(), vcontext); - addPropertiesToContext(context, vcontext); - - vcontext.put("project", mavenProject.getModel()); - - vcontext.put("outputFile", outputFile.getName()); - - FileWriter writer = new FileWriter(outputFile); - - try { - velocityTemplate.merge(vcontext, writer); - writer.flush(); - } catch (Exception e) { - throw new Exception("Could not generate the template " + velocityTemplate.getName() + ": " + e.getMessage(), e); - } finally { - writer.close(); - } - } - - protected void addPropertiesToContext(Properties properties, VelocityContext context) { - - for (Iterator<?> iter = properties.keySet().iterator(); iter.hasNext();) { - String key = (String) iter.next(); - Object value = properties.get(key); - context.put(key, value); - } - - } -}