Author: tchemit Date: 2010-05-08 18:51:38 +0200 (Sat, 08 May 2010) New Revision: 892 Url: http://nuiton.org/repositories/revision/eugene/892 Log: improve code Modified: trunk/eugene/src/main/java/org/nuiton/eugene/AbstractGenerator.java Modified: trunk/eugene/src/main/java/org/nuiton/eugene/AbstractGenerator.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/AbstractGenerator.java 2010-05-08 16:51:07 UTC (rev 891) +++ trunk/eugene/src/main/java/org/nuiton/eugene/AbstractGenerator.java 2010-05-08 16:51:38 UTC (rev 892) @@ -25,25 +25,23 @@ package org.nuiton.eugene; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.eugene.models.Model; + import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; -import org.nuiton.eugene.models.Model; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - /** * AbstractGenerator - * + * <p/> * Created: 28 oct. 2009 * + * @author fdesbois <fdesbois@codelutin.com> * @param <M> type of Model - * - * @author fdesbois <fdesbois@codelutin.com> */ public abstract class AbstractGenerator<M extends Model> extends Template<M> { @@ -109,7 +107,7 @@ protected boolean isNewerThanSource(File file) { if (log.isDebugEnabled()) { log.debug("source date: " + getLastModifiedSource() - + " file date: " + file.lastModified() + "(" + file + ")"); + + " file date: " + file.lastModified() + " (" + file + ")"); } return file.lastModified() > getLastModifiedSource(); } @@ -120,7 +118,7 @@ /** * Test if given package is allowed for generation. - * + * <p/> * An element can be generated if his package is in the * {@link #generatedPackages} list or if {@link #generatedPackages} is null * or empty. @@ -166,43 +164,57 @@ } protected void write(File outputFile, MonitorWriter out) { - File write0utputFile = outputFile; - if (out.isModified()) { - try { - write0utputFile = outputFile.getCanonicalFile(); - if (!write0utputFile.getParentFile().exists()) { - boolean b = write0utputFile.getParentFile().mkdirs(); - if (!b) { - throw new IllegalStateException( - "could not create directory " + - write0utputFile.getParentFile()); - } + if (!out.isModified()) { + if (log.isDebugEnabled()) { + log.debug("skip generation of " + outputFile + ", No content."); + } + return; + } + try { + outputFile = outputFile.getCanonicalFile(); + + if (!outputFile.getParentFile().exists()) { + boolean b = outputFile.getParentFile().mkdirs(); + if (!b) { + throw new IllegalStateException( + "could not create directory " + + outputFile.getParentFile()); } + } - Writer output; - - String encoding = getEncoding(); - FileOutputStream stream = new FileOutputStream(write0utputFile); - if (encoding != null) { - if (log.isDebugEnabled()) { - log.debug("Force encoding to " + encoding + " : " + this); - } - output = new OutputStreamWriter(stream, encoding); - } else { - output = new OutputStreamWriter(stream); + Writer output = getWriter(outputFile); + try { + if (log.isDebugEnabled()) { + log.debug("Will generate file " + outputFile); } - try { - output.write(out.getBuffer().toString()); - } finally { - output.close(); - } - } catch (IOException eee) { - if (log.isWarnEnabled()) { - log.warn("Unable to write file : " + write0utputFile.getAbsolutePath(), eee); - } - throw new RuntimeException(eee); + output.write(out.getBuffer().toString()); + } finally { + output.close(); } + } catch (IOException eee) { + if (log.isWarnEnabled()) { + log.warn("Unable to write file : " + + outputFile.getAbsolutePath(), eee); + } + throw new RuntimeException(eee); } } + + protected Writer getWriter(File outputFile) throws IOException { + + FileOutputStream stream = new FileOutputStream(outputFile); + Writer output; + String encoding = getEncoding(); + if (encoding != null) { + if (log.isDebugEnabled()) { + log.debug("Force encoding to " + encoding + " : " + this); + } + output = new OutputStreamWriter(stream, encoding); + } else { + output = new OutputStreamWriter(stream); + } + return output; + + } }
participants (1)
-
tchemit@users.nuiton.org