Index: maven-commandline-plugin/src/java/org/codelutin/option/generate/util/AbstractGeneratorContext.java diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/option/generate/util/AbstractGeneratorContext.java:1.1 --- /dev/null Wed Mar 19 20:21:47 2008 +++ maven-commandline-plugin/src/java/org/codelutin/option/generate/util/AbstractGeneratorContext.java Wed Mar 19 20:21:42 2008 @@ -0,0 +1,144 @@ +/** + * ##% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. This program is + * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * ##% + */ +package org.codelutin.option.generate.util; + +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.codelutin.option.generate.java.AbstractJavaGenerator; + +import java.io.File; + +/** + * Main generator context with all needed data + * + * @author chemit + */ +public abstract class AbstractGeneratorContext { + + /** + * @description Dépendance du projet. + * @parameter expression="${commandline.prefix}" + * @required + */ + protected String prefix; + + /** + * @description Package where to generate. + * @parameter expression="${commandline.package}" + * @required + */ + protected String packageName; + + /** + * @description Répertoire de sortie. + * @parameter expression="${commandline.out}" + * @required + */ + protected File out; + + /** + * @description prefix i18n pour la génération des clefs . + * @parameter expression="${commandline.i18nPrefix}" + */ + protected String i18nPrefix; + + /** + * @description flag pour afficher verbeusement ou non les logs + * @parameter expression="${commandline.verbose}" default-value="${maven.verbose}" + */ + protected boolean verbose; + + /** + * @description encoding pour les fichiers a generer + * @parameter expression="${commandline.encoding}" default-value="${maven.compile.encoding}" + */ + protected String encoding; + + public String getPrefix() { + return prefix; + } + + public void setPrefix(String prefix) { + this.prefix = prefix; + } + + public String getI18nPrefix() { + return i18nPrefix; + } + + public void setI18nPrefix(String i18nPrefix) { + this.i18nPrefix = i18nPrefix; + } + + public boolean isVerbose() { + return verbose; + } + + public void setVerbose(boolean verbose) { + this.verbose = verbose; + } + + public String getEncoding() { + return encoding; + } + + public void setEncoding(String encoding) { + this.encoding = encoding; + } + + public File getOut() { + return out; + } + + public void setOut(File out) { + this.out = out; + } + + public String getPackageName() { + return packageName; + } + + public void setPackageName(String packageName) { + this.packageName = packageName; + } + + public void init() { + + if (i18nPrefix == null) { + i18nPrefix = prefix.toLowerCase(); + } + + Logger logger = Logger.getLogger("org.codelutin"); + if (verbose) { + // set logger to debug level + if (logger != null) { + logger.setLevel(Level.DEBUG); + } + } else { + if (logger != null) { + logger.setLevel(Level.INFO); + } + } + if (encoding != null) { + // set encoding once for all + AbstractJavaGenerator.encoding = encoding; + } + + // make sure out exists + if (!out.exists()) { + out.mkdirs(); + } + } +} Index: maven-commandline-plugin/src/java/org/codelutin/option/generate/util/AbstractGeneratorGoal.java diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/option/generate/util/AbstractGeneratorGoal.java:1.1 --- /dev/null Wed Mar 19 20:21:47 2008 +++ maven-commandline-plugin/src/java/org/codelutin/option/generate/util/AbstractGeneratorGoal.java Wed Mar 19 20:21:42 2008 @@ -0,0 +1,106 @@ +/* +* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Code Lutin, +* Tony Chemit +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* ##% */ +package org.codelutin.option.generate.util; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.logging.Log; +import org.apache.maven.project.MavenProject; +import org.codelutin.i18n.I18n; +import org.codelutin.log.LutinLogFactory; +import org.codelutin.util.StringUtil; + +/** + * Classe de base pour une goal de génération + * + * @author tony + */ +public abstract class AbstractGeneratorGoal extends AbstractMojo { + + protected Log log = getLog(); + + /** + * @description Dépendance du projet. + * @parameter default-value="${project}" + * @readonly + */ + protected MavenProject project; + + /** le contexte du generateur */ + protected C context; + + /** timestamp */ + protected final long timestamp; + + protected AbstractGeneratorGoal(Class contextClass) { + timestamp = System.nanoTime(); + try { + context = contextClass.newInstance(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + protected abstract void generate() throws Exception; + + public void execute() throws MojoExecutionException, MojoFailureException { + + + try { + // init generator + init(); + + // do implented action + generate(); + + } catch (Exception e) { + throw new MojoExecutionException(e.getMessage(), e); + } finally { + getLog().info("total time " + StringUtil.convertTime(System.nanoTime(), timestamp)); + } + } + + protected void init() throws Exception { + + System.setProperty("org.apache.commons.logging.LogFactory", LutinLogFactory.class.getName()); + + // init i18n + I18n.initISO88591(); + + // init context + getContext().init(); + } + + public C getContext() { + return context; + } + + protected void checkInstanceOf(String givenClass, Class expectedClass) { + try { + Class clazz = Class.forName(givenClass); + //Class clazz = Class.forName(givenClass, true, loader); + if (!expectedClass.isAssignableFrom(clazz)) { + throw new IllegalArgumentException("required a " + expectedClass + " class but found a " + clazz); + } + } catch (ClassNotFoundException e) { + throw new IllegalArgumentException(e); + } + } +}