Author: tchemit Date: 2010-03-09 18:51:09 +0100 (Tue, 09 Mar 2010) New Revision: 315 Log: - Anomalie #362: Opened resources are never closed - Evolution #363: Reformat code to respect the 80 caracters max per line Modified: trunk/ant-processor-task/src/main/java/org/nuiton/processor/ant/I18nExtractorTask.java trunk/ant-processor-task/src/main/java/org/nuiton/processor/ant/ProcessorTask.java trunk/maven-processor-plugin/src/main/java/org/nuiton/processor/plugin/ProcessorPlugin.java trunk/nuiton-processor/src/main/java/org/nuiton/processor/GeneratorTemplatesProcessor.java trunk/nuiton-processor/src/main/java/org/nuiton/processor/I18nExtractor.java trunk/nuiton-processor/src/main/java/org/nuiton/processor/LicenseProcessor.java trunk/nuiton-processor/src/main/java/org/nuiton/processor/LogsProcessor.java trunk/nuiton-processor/src/main/java/org/nuiton/processor/OptimisationProcessor.java trunk/nuiton-processor/src/main/java/org/nuiton/processor/Processor.java trunk/nuiton-processor/src/main/java/org/nuiton/processor/ProcessorUtil.java trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/ActiveLogsCodeFilter.java trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/DefaultFilter.java trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/Filter.java trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/GeneratorTemplatesFilter.java trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/GeneratorTemplatesFilterIn.java trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/I18nFilter.java trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/LicenseFilter.java trunk/nuiton-processor/src/test/java/org/nuiton/processor/filters/StringFilterTest.java Modified: trunk/ant-processor-task/src/main/java/org/nuiton/processor/ant/I18nExtractorTask.java =================================================================== --- trunk/ant-processor-task/src/main/java/org/nuiton/processor/ant/I18nExtractorTask.java 2010-02-21 10:27:35 UTC (rev 314) +++ trunk/ant-processor-task/src/main/java/org/nuiton/processor/ant/I18nExtractorTask.java 2010-03-09 17:51:09 UTC (rev 315) @@ -51,26 +51,35 @@ protected File destFile; - protected String[] includes = new String[] {}; + protected String[] includes = EMPTY_STRING_ARRAY; - protected String[] excludes = new String[] {}; + protected String[] excludes = EMPTY_STRING_ARRAY; protected String[] srcFileNames; + private static final String[] EMPTY_STRING_ARRAY = new String[] {}; + public I18nExtractorTask() { } protected void doExecute() throws BuildException { // creation du repertoire pour le fichier destination - destFile.getParentFile().mkdirs(); + File parentFile = destFile.getParentFile(); + if (!parentFile.exists()) { + boolean b = parentFile.mkdirs(); + if (!b) { + throw new BuildException( + "could not create directory " + parentFile); + } + } List<File> fileList = new ArrayList<File>(); - for (int i = 0; i < srcFileNames.length; i++) { - fileList.add(new File(srcFileNames[i])); + for (String srcFileName : srcFileNames) { + fileList.add(new File(srcFileName)); } try { - new I18nExtractor() - .extract(fileList.toArray(new File[0]), destFile); + new I18nExtractor().extract( + fileList.toArray(new File[fileList.size()]), destFile); } catch (IOException eee) { throw new BuildException(eee); } Modified: trunk/ant-processor-task/src/main/java/org/nuiton/processor/ant/ProcessorTask.java =================================================================== --- trunk/ant-processor-task/src/main/java/org/nuiton/processor/ant/ProcessorTask.java 2010-02-21 10:27:35 UTC (rev 314) +++ trunk/ant-processor-task/src/main/java/org/nuiton/processor/ant/ProcessorTask.java 2010-03-09 17:51:09 UTC (rev 315) @@ -24,8 +24,6 @@ import org.apache.tools.ant.BuildException; import org.apache.tools.ant.taskdefs.MatchingTask; import org.nuiton.processor.Processor; -import java.io.FileReader; -import java.io.FileWriter; import java.io.IOException; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.Project; @@ -55,17 +53,29 @@ public class ProcessorTask extends MatchingTask { // ProcessorTask public static final int MSG_VERBOSE = Project.MSG_VERBOSE; + + private static final String[] EMPTY_STRING_ARRAY = new String[] {}; + protected File srcDir; + protected File destDir; - protected String[] includes = new String[]{}; - protected String[] excludes = new String[]{}; + + protected String[] includes = EMPTY_STRING_ARRAY; + + protected String[] excludes = EMPTY_STRING_ARRAY; + protected String[] files; + protected String fileInPattern = ""; + protected String fileOutPattern = ""; + protected String filters = "org.nuiton.processor.filters.NoActionFilter"; + protected boolean overwrite = true; - protected boolean verbose = false; + protected boolean verbose; + public ProcessorTask() { } @@ -102,17 +112,20 @@ } protected void doExecute() throws BuildException { - Processor processor=null; + Processor processor; try { - processor = ProcessorUtil.newProcessor(this.filters, ","); + processor = ProcessorUtil.newProcessor(filters, ","); } catch (Exception ex) { - throw new BuildException("Could nto instanciate processor for reason : "+ex.getMessage(), ex); + throw new BuildException( + "Could nto instanciate processor for reason : " + + ex.getMessage(), ex); } // Processor processor = new Processor(getFilters()); int numberFiles; for (numberFiles = 0; numberFiles < files.length; numberFiles++) { String inputFileName = absoluteSourceName(files[numberFiles]); - String outputFileName = absoluteDestinationName(applyTransformationFilename(files[numberFiles])); + String outputFileName = absoluteDestinationName( + applyTransformationFilename(files[numberFiles])); if (verbose) { log("Using " + inputFileName); } @@ -121,10 +134,20 @@ log("Generating " + outputFileName); } // creation du repertoire pour le fichier destination - new File(outputFileName).getParentFile().mkdirs(); + File parentFile = new File(outputFileName).getParentFile(); + if (!parentFile.exists()) { + boolean b = parentFile.mkdirs(); + if (!b) { + throw new BuildException( + "could not create directory " + parentFile); + } + } try { - processor.process(new FileReader(inputFileName), - new FileWriter(outputFileName)); + ProcessorUtil.doProcess( + processor, inputFileName, outputFileName + ); +// processor.process(new FileReader(inputFileName), +// new FileWriter(outputFileName)); } catch (IOException eee) { throw new BuildException(eee); } @@ -164,14 +187,17 @@ getLocation()); } if (!srcDir.exists()) { - throw new BuildException("srcdir \"" + srcDir.getPath() + "\" does not exist!", getLocation()); + throw new BuildException("srcdir \"" + srcDir.getPath() + + "\" does not exist!", getLocation()); } if (destDir == null) { destDir = srcDir; } if (!destDir.isDirectory()) { - throw new BuildException("destination directory \"" + destDir + "\" does not exist or is not a directory", getLocation()); + throw new BuildException( + "destination directory \"" + destDir + + "\" does not exist or is not a directory", getLocation()); } // Build the list of files to compute @@ -202,7 +228,8 @@ } protected boolean isNewer(String filein, String fileout) { - boolean result = new File(filein).lastModified() > new File(fileout).lastModified(); + boolean result = new File(filein).lastModified() > + new File(fileout).lastModified(); if (result) { log(filein + " is newer than " + fileout, MSG_VERBOSE); } Modified: trunk/maven-processor-plugin/src/main/java/org/nuiton/processor/plugin/ProcessorPlugin.java =================================================================== --- trunk/maven-processor-plugin/src/main/java/org/nuiton/processor/plugin/ProcessorPlugin.java 2010-02-21 10:27:35 UTC (rev 314) +++ trunk/maven-processor-plugin/src/main/java/org/nuiton/processor/plugin/ProcessorPlugin.java 2010-03-09 17:51:09 UTC (rev 315) @@ -28,8 +28,6 @@ import org.nuiton.processor.ProcessorUtil; import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; import java.util.Arrays; import java.util.Map; import java.util.Map.Entry; @@ -142,10 +140,6 @@ */ protected Map<String, String> filesToTreate; -// public ProcessorPlugin() { -// super("all files are up-to-date."); -// } - @Override public boolean isVerbose() { return verbose; @@ -168,12 +162,9 @@ @Override protected void init() throws Exception { -// protected boolean init() throws Exception { if (filters.isEmpty()) { return; -// getLog().warn("No filters to use, skip execution."); -// return false; } MirroredFileUpdater updater = overwrite ? null : new MirroredFileUpdater(fileInPattern, fileOutPattern, srcDir, destDir); @@ -185,8 +176,6 @@ String[] aExcludes = excludes == null ? null : excludes.split(","); filesToTreate = getFilesToTreate(aIncludes, aExcludes, srcDir, updater); - -// return true; } @Override @@ -205,11 +194,6 @@ @Override protected void doAction() throws Exception { -// if (filesToTreate.isEmpty()) { -// getLog().info("No file to process."); -// return; -// } - if (isVerbose()) { printConfig(); } @@ -229,18 +213,14 @@ } // creation du repertoire pour le fichier destination createDirectoryIfNecessary(dstFile.getParentFile()); -// if (!dstFile.getParentFile().exists()) { -// boolean b = dstFile.getParentFile().mkdirs(); -// if (!b) { -// throw new IOException("could not create directory " + dstFile); -// } -// } - processor.process(new FileReader(srcFile), new FileWriter(dstFile)); + ProcessorUtil.doProcess(processor, srcFile, dstFile); +// processor.process(new FileReader(srcFile), new FileWriter(dstFile)); } if (isVerbose()) { - getLog().info("done in " + PluginHelper.convertTime(System.nanoTime() - t0)); + long time = System.nanoTime() - t0; + getLog().info("done in " + PluginHelper.convertTime(time)); } // on indique que le repertoire entrant n'est plus dans le build // car sinon on va avoir des classes dupliquées @@ -263,7 +243,8 @@ getLog().info("config - srcDir " + srcDir); getLog().info("config - destDir " + destDir); getLog().info("config - includes " + includes); - getLog().info("config - filters " + Arrays.asList(PluginHelper.splitAndTrim(filters, ","))); + getLog().info("config - filters " + Arrays.asList( + PluginHelper.splitAndTrim(filters, ","))); if (excludes != null) { getLog().info("config - excludes " + excludes); } Modified: trunk/nuiton-processor/src/main/java/org/nuiton/processor/GeneratorTemplatesProcessor.java =================================================================== --- trunk/nuiton-processor/src/main/java/org/nuiton/processor/GeneratorTemplatesProcessor.java 2010-02-21 10:27:35 UTC (rev 314) +++ trunk/nuiton-processor/src/main/java/org/nuiton/processor/GeneratorTemplatesProcessor.java 2010-03-09 17:51:09 UTC (rev 315) @@ -34,8 +34,6 @@ package org.nuiton.processor; -import java.io.FileReader; -import java.io.FileWriter; import java.io.IOException; import org.nuiton.processor.filters.GeneratorTemplatesFilter; @@ -49,7 +47,8 @@ static public void process(String filein, String fileout) throws IOException{ Processor processor = new GeneratorTemplatesProcessor(); - processor.process(new FileReader(filein), new FileWriter(fileout)); + ProcessorUtil.doProcess(processor,filein,fileout); +// processor.process(new FileReader(filein), new FileWriter(fileout)); } public static void main(String [] args)throws IOException{ Modified: trunk/nuiton-processor/src/main/java/org/nuiton/processor/I18nExtractor.java =================================================================== --- trunk/nuiton-processor/src/main/java/org/nuiton/processor/I18nExtractor.java 2010-02-21 10:27:35 UTC (rev 314) +++ trunk/nuiton-processor/src/main/java/org/nuiton/processor/I18nExtractor.java 2010-03-09 17:51:09 UTC (rev 315) @@ -44,8 +44,10 @@ import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashMap; -import java.util.Iterator; import java.util.List; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; import org.nuiton.processor.filters.I18nFilter; @@ -56,31 +58,44 @@ protected I18nFilter filter = new I18nFilter(); - protected HashMap<String,List<String>> i18nStringsMap = new HashMap<String,List<String>>(); + protected Map<String,List<String>> i18nStringsMap = + new HashMap<String,List<String>>(); public void extract(File[] srcFiles, File outputFile) throws IOException { - PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"))); - for (int i = 0; i < srcFiles.length; i++) { - processFile(srcFiles[i]); + PrintWriter writer = new PrintWriter( + new BufferedWriter( + new OutputStreamWriter( + new FileOutputStream(outputFile), "UTF-8"))); + try { + for (File srcFile : srcFiles) { + processFile(srcFile); + } + + for (Object o : i18nStringsMap.keySet()) { + String i18nString = (String) o; + List<?> locations = i18nStringsMap.get(i18nString); + for (Object location1 : locations) { + String location = (String) location1; + writer.println("# " + location); + } + writer.println(i18nString + "="); + } + } finally { + writer.close(); } - for (Iterator<?> iter = i18nStringsMap.keySet().iterator(); iter.hasNext();) { - String i18nString = (String) iter.next(); - List<?> locations = (List<?>) i18nStringsMap.get(i18nString); - for (Iterator<?> iterator = locations.iterator(); iterator.hasNext();) { - String location = (String) iterator.next(); - writer.println("# "+location); - } - writer.println(i18nString+"="); - } - writer.close(); } protected void processFile(File srcFile) throws IOException { - LineNumberReader lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(srcFile), "UTF-8")); - while (lnr.ready()) { - String line = lnr.readLine(); - int lineNumber = lnr.getLineNumber(); - processLine(line, srcFile, lineNumber); + LineNumberReader lnr = new LineNumberReader( + new InputStreamReader(new FileInputStream(srcFile), "UTF-8")); + try { + while (lnr.ready()) { + String line = lnr.readLine(); + int lineNumber = lnr.getLineNumber(); + processLine(line, srcFile, lineNumber); + } + } finally { + lnr.close(); } } @@ -89,34 +104,35 @@ if (I18nFilter.EMPTY_STRING.equals(i18nStringsSet)) { // Found a set of i18n Strings, split it. String[] i18nStrings = i18nStringsSet.split("="); - for (int i = 0; i < i18nStrings.length; i++) { - String i18nString = i18nStrings[i]; - - List<String> locations = i18nStringsMap.get(i18nString); + for (String i18nString : i18nStrings) { + List<String> locations = i18nStringsMap.get(i18nString); if (locations == null) { locations = new ArrayList<String>(); i18nStringsMap.put(i18nString, locations); } - locations.add(srcFile.getPath()+"("+lineNumber+")"); + locations.add(srcFile.getPath() + "(" + lineNumber + ")"); } } } public static void main(String[] args) { if(args.length < 2) { - System.out.println("Please give at least sources and destination file"); + System.out.println( + "Please give at least sources and destination file"); System.exit(0); } else if (args.length > 2) { File[] sources = new File[args.length-1]; for (int i = 0; i < args.length-1; i++) { sources[i] =new File(args[i]); } - File destination = new File(args[args.length-1]); + File destination = new File(args[args.length - 1]); try { new I18nExtractor().extract(sources, destination); } catch (IOException eee) { - java.util.logging.Logger.getLogger("org.nuiton.processor.I18nExtractor.").severe("Error during i18n extraction : "+eee); - System.out.println("!!! "+eee); // TODO remove this + Logger logger = + Logger.getLogger("org.nuiton.processor.I18nExtractor."); + logger.log(Level.SEVERE, + "Error during i18n extraction : " + eee, eee); } } Modified: trunk/nuiton-processor/src/main/java/org/nuiton/processor/LicenseProcessor.java =================================================================== --- trunk/nuiton-processor/src/main/java/org/nuiton/processor/LicenseProcessor.java 2010-02-21 10:27:35 UTC (rev 314) +++ trunk/nuiton-processor/src/main/java/org/nuiton/processor/LicenseProcessor.java 2010-03-09 17:51:09 UTC (rev 315) @@ -23,15 +23,12 @@ import org.nuiton.processor.filters.LicenseFilter; import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; import java.io.IOException; -/** +/** * This class is a processor of source header. * * @author chemit - * * @see LicenseFilter */ public class LicenseProcessor extends Processor { @@ -48,7 +45,8 @@ } public void process(File filein, File fileout) throws IOException { - process(new FileReader(filein), new FileWriter(fileout)); + ProcessorUtil.doProcess(this, filein, fileout); +// process(new FileReader(filein), new FileWriter(fileout)); } } Modified: trunk/nuiton-processor/src/main/java/org/nuiton/processor/LogsProcessor.java =================================================================== --- trunk/nuiton-processor/src/main/java/org/nuiton/processor/LogsProcessor.java 2010-02-21 10:27:35 UTC (rev 314) +++ trunk/nuiton-processor/src/main/java/org/nuiton/processor/LogsProcessor.java 2010-03-09 17:51:09 UTC (rev 315) @@ -36,56 +36,107 @@ import org.nuiton.processor.filters.*; import java.io.*; +import java.util.logging.Logger; /* * This class is a processor for source logs */ public class LogsProcessor extends Processor { + private static final String REMOVE_ACTION = "remove"; - /** Type of actions */ - protected static class Action {} - /** Do nothing */ - public final static Action NoAction = new Action(); + private static final String ACTIVE_ACTION = "active"; - /** Active logs in output. This is the default action */ - public final static Action Logs = new Action(); - /** Remove logs in output */ - public final static Action NoLogsCode = new Action(); + public enum Action { + NoAction, + Logs, + NoLogsCode + } +// /** Type of actions */ +// protected static class Action {} +// +// /** Do nothing */ +// public final static Action NoAction = new Action(); +// +// /** Active logs in output. This is the default action */ +// public final static Action Logs = new Action(); +// +// /** Remove logs in output */ +// public final static Action NoLogsCode = new Action(); + public LogsProcessor (Action action) { - if (action == NoAction) { - // No action is the default filter in Processor - } else if (action == NoLogsCode) { - setInputFilter(new RemoveLogsCodeFilter()); - } else { - // Go for logs - setInputFilter(new ActiveLogsCodeFilter()); + switch (action) { + + case NoAction: + // No action is the default filter in Processor + break; + + case Logs: + // Go for logs + setInputFilter(new ActiveLogsCodeFilter()); + break; + + case NoLogsCode: + setInputFilter(new RemoveLogsCodeFilter()); + break; } +// if (NoAction.equals(action)) { +// // No action is the default filter in Processor +// } else if (NoLogsCode.equals(action)) { +// setInputFilter(new RemoveLogsCodeFilter()); +// } else { +// // Go for logs +// setInputFilter(new ActiveLogsCodeFilter()); +// } } public static void main(String [] args) throws Exception { - LogsProcessor processor; - if(args.length < 2) { + if (args.length < 2) { System.out.println("Give source and destination file, then action"); System.out.println("Action may be remove or active (default is no action)"); - System.exit(0); - } else if (args.length > 2) { - if (args[2].equals("remove")) { - processor = new LogsProcessor(NoLogsCode); + return; +// System.exit(0); + } + Action action = Action.NoAction; + if (args.length > 2) { + if (args[2].equals(REMOVE_ACTION)) { + action = Action.NoLogsCode; System.out.println("Removing logs code"); - } else if (args[2].equals("active")) { - processor = new LogsProcessor(Logs); + } else if (args[2].equals(ACTIVE_ACTION)) { + action = Action.Logs; System.out.println("Setting logs active"); - } else { - processor = new LogsProcessor(NoAction); - System.out.println("No action taken"); } - try { - processor.process(new FileReader(args[0]), - new FileWriter(args[1])); - } catch (IOException eee) { - java.util.logging.Logger.getLogger("org.nuiton.processor.LogsProcessor.").severe("Error during log processing: "+eee); - } } + + if (action == Action.NoAction) { + System.out.println("No action taken"); + } + + LogsProcessor processor = new LogsProcessor(action); + ProcessorUtil.doProcess(processor, args[0], args[1]); +// if (args.length > 2) { +// if (args[2].equals(REMOVE_ACTION)) { +// processor = new LogsProcessor(Action.NoLogsCode); +// System.out.println("Removing logs code"); +// } else if (args[2].equals(ACTIVE_ACTION)) { +// processor = new LogsProcessor(Action.Logs); +// System.out.println("Setting logs active"); +// } else { +// processor = new LogsProcessor(Action.NoAction); +// System.out.println("No action taken"); +// } +// FileReader input = new FileReader(args[0]); +// FileWriter output = new FileWriter(args[1]); +// try { +// processor.process(input, output); +// } catch (IOException eee) { +// Logger logger = +// Logger.getLogger("org.nuiton.processor.LogsProcessor."); +// logger.severe("Error during log processing: "+eee); +// } finally { +// input.close(); +// output.close(); +// } +// } } } Modified: trunk/nuiton-processor/src/main/java/org/nuiton/processor/OptimisationProcessor.java =================================================================== --- trunk/nuiton-processor/src/main/java/org/nuiton/processor/OptimisationProcessor.java 2010-02-21 10:27:35 UTC (rev 314) +++ trunk/nuiton-processor/src/main/java/org/nuiton/processor/OptimisationProcessor.java 2010-03-09 17:51:09 UTC (rev 315) @@ -34,7 +34,8 @@ package org.nuiton.processor; -import java.io.*; +import java.io.IOException; + import org.nuiton.processor.filters.OptimisationFilter; public class OptimisationProcessor extends Processor { // OptimisationProcessor @@ -46,7 +47,8 @@ static public void process(String filein, String fileout) throws IOException{ Processor processor = new OptimisationProcessor(); - processor.process(new FileReader(filein), new FileWriter(fileout)); + ProcessorUtil.doProcess(processor, filein, fileout); +// processor.process(new FileReader(filein), new FileWriter(fileout)); } public static void main(String [] args)throws IOException{ Modified: trunk/nuiton-processor/src/main/java/org/nuiton/processor/Processor.java =================================================================== --- trunk/nuiton-processor/src/main/java/org/nuiton/processor/Processor.java 2010-02-21 10:27:35 UTC (rev 314) +++ trunk/nuiton-processor/src/main/java/org/nuiton/processor/Processor.java 2010-03-09 17:51:09 UTC (rev 315) @@ -34,7 +34,6 @@ package org.nuiton.processor; import org.nuiton.processor.filters.*; -import org.nuiton.processor.filters.Filter; import java.io.*; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -48,7 +47,9 @@ /** to use log facility, just put in your code: log.info(\"...\"); */ static private final Log log = LogFactory.getLog(Processor.class); + protected Filter[] inputFilter = new Filter[]{new NoActionFilter()}; + protected Filter outputFilter = new NoActionFilter(); public Processor() { @@ -72,8 +73,8 @@ protected BufferedReader getReader(Reader externalInput) { BufferedReader result = new BufferedReader(externalInput); - for (int i = 0; i < inputFilter.length; i++) { - result = new ProcessorReader(result, inputFilter[i]); + for (Filter anInputFilter : inputFilter) { + result = new ProcessorReader(result, anInputFilter); } return result; } @@ -85,27 +86,31 @@ * @param externalOutput the writer * @throws IOException if any pb */ - public void process(Reader externalInput, Writer externalOutput) throws IOException { + public void process(Reader externalInput, + Writer externalOutput) throws IOException { if (log.isTraceEnabled()) { log.trace("Debug du process"); } BufferedReader input = getReader(externalInput); - ProcessorWriter output = - new ProcessorWriter(new BufferedWriter(externalOutput), outputFilter); - - if (log.isTraceEnabled()) { - log.trace("input: " + input); - log.trace("output: " + output); - } - - String line = input.readLine(); - while (line != null) { + try { + ProcessorWriter output = new ProcessorWriter( + new BufferedWriter(externalOutput), outputFilter); if (log.isTraceEnabled()) { - log.trace("Ligne lu: " + line); + log.trace("input: " + input); + log.trace("output: " + output); } - output.writeLine(line); - line = input.readLine(); + + String line = input.readLine(); + while (line != null) { + if (log.isTraceEnabled()) { + log.trace("Ligne lu: " + line); + } + output.writeLine(line); + line = input.readLine(); + } + output.flush(); + } finally { + input.close(); } - output.flush(); } } Modified: trunk/nuiton-processor/src/main/java/org/nuiton/processor/ProcessorUtil.java =================================================================== --- trunk/nuiton-processor/src/main/java/org/nuiton/processor/ProcessorUtil.java 2010-02-21 10:27:35 UTC (rev 314) +++ trunk/nuiton-processor/src/main/java/org/nuiton/processor/ProcessorUtil.java 2010-03-09 17:51:09 UTC (rev 315) @@ -20,8 +20,15 @@ */ package org.nuiton.processor; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.nuiton.processor.filters.Filter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + /** * Utilities methods. * @@ -30,6 +37,9 @@ */ public class ProcessorUtil { + /** Logger */ + private static final Log log = LogFactory.getLog(ProcessorUtil.class); + /** * Instanciate a new {@link Processor} given an array of filters given by * their FQN separated by the given separator. @@ -37,9 +47,10 @@ * @param filters the string representionf of the filters * @param separator the separator of filter * @return the instanciated processor - * @throws java.lang.Exception if any pb + * @throws Exception if any pb */ - public static Processor newProcessor(String filters, String separator) throws Exception { + public static Processor newProcessor(String filters, + String separator) throws Exception { Filter[] result = getFilters(filters, separator); Processor processor = new Processor(result); return processor; @@ -53,9 +64,10 @@ * @param separator filter separator * @return the array of instanciated filters. * - * @throws java.lang.Exception if any pb + * @throws Exception if any pb */ - public static Filter[] getFilters(String filters, String separator) throws Exception { + public static Filter[] getFilters(String filters, + String separator) throws Exception { String[] filterList = filters.split(separator); Filter[] result = new Filter[filterList.length]; for (int i = 0; i < filterList.length; i++) { @@ -63,11 +75,60 @@ try { // Class.forName semble fonctionner maintenant // avant il fallait utiliser getClass().forName - result[i] = (Filter) Class.forName(filterList[i].trim()).newInstance(); + result[i] = (Filter) + Class.forName(filterList[i].trim()).newInstance(); } catch (Exception eee) { - throw new IllegalArgumentException("Error during looking for '" + filterList[i].trim() + "' class", eee); + throw new IllegalArgumentException( + "Error during looking for '" + filterList[i].trim() + + "' class", eee); } } return result; } + + /** + * Launch the process of the given {@code processor} to the given files. + * + * @param processor the processor to launch + * @param in the input file + * @param out the output file + * @throws IOException if any problems while processing file + * @since 1.0.3 + */ + public static void doProcess(Processor processor, + String in, String out) throws IOException { + doProcess(processor, new File(in), new File(out)); + } + + + /** + * Launch the process of the given {@code processor} to the given files. + * + * @param processor the processor to launch + * @param in the input file + * @param out the output file + * @throws IOException if any problems while processing file + * @since 1.0.3 + */ + public static void doProcess(Processor processor, + File in, File out) throws IOException { + FileReader input = new FileReader(in); + try { + FileWriter output = new FileWriter(out); + try { + processor.process(input, output); + } catch (IOException eee) { + if (log.isErrorEnabled()) { + log.error( + "Error while processing file " + in + " to " + out + + " with processor " + processor, eee); + } + throw eee; + } finally { + output.close(); + } + } finally { + input.close(); + } + } } Modified: trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/ActiveLogsCodeFilter.java =================================================================== --- trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/ActiveLogsCodeFilter.java 2010-02-21 10:27:35 UTC (rev 314) +++ trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/ActiveLogsCodeFilter.java 2010-03-09 17:51:09 UTC (rev 315) @@ -49,9 +49,12 @@ protected String performInFilter(String ch){ // Logs must be performed within a try{...}catch{} bloc return - "try {" + + "try {\n" + ch + - "} catch (Exception logsE) { System.err.println(\"Error in Logging instructions\"); logsE.printStackTrace(); }"; + "} catch (Exception logsE) {\n" + + " System.err.println(\"Error in Logging instructions\");\n" + + " logsE.printStackTrace();\n" + + "}"; } /** Modified: trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/DefaultFilter.java =================================================================== --- trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/DefaultFilter.java 2010-02-21 10:27:35 UTC (rev 314) +++ trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/DefaultFilter.java 2010-03-09 17:51:09 UTC (rev 315) @@ -50,8 +50,10 @@ public abstract class DefaultFilter implements Filter { // DefaultFilter public final static int NOT_FOUND = -1; + /** to use log facility, just put in your code: log.info(\"...\"); */ static private final Log log = LogFactory.getLog(DefaultFilter.class); + /** * le buffer interne pour conserve ce qui n'a pas encore été écrit */ @@ -63,8 +65,9 @@ /** Looking-for-header state */ SEARCH_HEADER, /** Looking-for-footer state */ - SEARCH_FOOTER; + SEARCH_FOOTER } + /** * l'état interne du filtre */ @@ -106,10 +109,13 @@ // Looking for a header matchingIndex = getMatchIndexFor(content, getHeader()); if (matchingIndex != NOT_FOUND) { - // Header found, return "out" part of it + recursive process of remaining chars + // Header found, return "out" part of it + recursive process + // of remaining chars changeState(State.SEARCH_FOOTER); StringBuilder buffer = new StringBuilder(); - buffer.append(performOutFilter(content.substring(0, matchingIndex))); + String outFilter = + performOutFilter(content.substring(0, matchingIndex)); + buffer.append(outFilter); buffer.append(parse(content.substring(matchingIndex))); return buffer.toString(); } @@ -121,11 +127,16 @@ // Looking for a footer matchingIndex = getMatchIndexFor(content, getFooter()); if (matchingIndex != NOT_FOUND) { - // Footer found, return "in" part of it + recursive process of remaining chars + // Footer found, return "in" part of it + recursive process + // of remaining chars changeState(State.SEARCH_HEADER); - int matchingEndIndex = matchingIndex + getMatchLengthFor(getFooter()); + int matchingEndIndex = matchingIndex + + getMatchLengthFor(getFooter()); StringBuilder buffer = new StringBuilder(); - buffer.append(performInFilter(performHeaderFooterFilter(content.substring(0, matchingEndIndex)))); + String headerFootfilter = performHeaderFooterFilter( + content.substring(0, matchingEndIndex)); + String inFilter = performInFilter(headerFootfilter); + buffer.append(inFilter); buffer.append(parse(content.substring(matchingEndIndex))); return buffer.toString(); } @@ -136,7 +147,8 @@ } public String performHeaderFooterFilter(String ch) { - return ch.substring(getHeader().length(), ch.length() - getFooter().length()); + return ch.substring(getHeader().length(), ch.length() - + getFooter().length()); } public int getMatchIndexFor(String input, String sequence) { @@ -149,7 +161,7 @@ @Override public boolean hasCachedData() { - return (cachedContent.length() > 0); + return cachedContent.length() > 0; } @Override @@ -166,10 +178,11 @@ protected void changeState(State newState) { if (log.isDebugEnabled()) { - log.debug("change state : <old:" + this.state + ", new:" + newState + ">"); + log.debug("change state : <old:" + state + ", new:" + + newState + ">"); } - this.state = newState; + state = newState; // le changement d'état réinitialise toujours le buffer interne cachedContent.setLength(0); } Modified: trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/Filter.java =================================================================== --- trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/Filter.java 2010-02-21 10:27:35 UTC (rev 314) +++ trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/Filter.java 2010-03-09 17:51:09 UTC (rev 315) @@ -40,7 +40,7 @@ */ public interface Filter { - final static String EMPTY_STRING = ""; + String EMPTY_STRING = ""; /** * Modified: trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/GeneratorTemplatesFilter.java =================================================================== --- trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/GeneratorTemplatesFilter.java 2010-02-21 10:27:35 UTC (rev 314) +++ trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/GeneratorTemplatesFilter.java 2010-03-09 17:51:09 UTC (rev 315) @@ -68,18 +68,19 @@ */ public class GeneratorTemplatesFilter extends DefaultFilter { - private static final Log log = LogFactory.getLog(GeneratorTemplatesFilter.class); + private static final Log log = + LogFactory.getLog(GeneratorTemplatesFilter.class); protected boolean passEmptyLine; protected boolean writeParentheses; protected String writeString; protected GeneratorTemplatesFilterIn inFilter; - public GeneratorTemplatesFilter() { - this.passEmptyLine = false; - this.writeParentheses = true; - this.writeString = "output.write"; - this.inFilter = new GeneratorTemplatesFilterIn(this); + public GeneratorTemplatesFilter() { + passEmptyLine = false; + writeParentheses = true; + writeString = "output.write"; + inFilter = new GeneratorTemplatesFilterIn(this); } public String getWriteString(){ @@ -170,6 +171,6 @@ res = filter.flush(); System.out.println(res); } - - + + } Modified: trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/GeneratorTemplatesFilterIn.java =================================================================== --- trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/GeneratorTemplatesFilterIn.java 2010-02-21 10:27:35 UTC (rev 314) +++ trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/GeneratorTemplatesFilterIn.java 2010-03-09 17:51:09 UTC (rev 315) @@ -43,9 +43,10 @@ */ public class GeneratorTemplatesFilterIn extends DefaultFilter { // GeneratorTemplatesFilterIn - private static final Log log = LogFactory.getLog(GeneratorTemplatesFilterIn.class); + private static final Log log = + LogFactory.getLog(GeneratorTemplatesFilterIn.class); - GeneratorTemplatesFilter parent = null; + GeneratorTemplatesFilter parent; protected String beginParenthese = ""; protected String endParenthese = ""; @@ -65,17 +66,18 @@ */ @Override protected String performInFilter(String ch) { - if (this.parent.writeParentheses) { - this.beginParenthese = "("; - this.endParenthese = ");"; + if (parent.writeParentheses) { + beginParenthese = "("; + endParenthese = ");"; } else { - this.beginParenthese = ""; - this.endParenthese = ""; + beginParenthese = ""; + endParenthese = ""; } if (ch.startsWith("=")) { return "\"+" + ch.substring(1) + "+\""; } - return "\"" + endParenthese + ch + parent.getWriteString() + beginParenthese + "\""; + return "\"" + endParenthese + ch + parent.getWriteString() + + beginParenthese + "\""; } /** @@ -98,16 +100,17 @@ */ @Override protected String performOutFilter(String ch) { - if (this.parent.writeParentheses) { - this.beginParenthese = "("; - this.endParenthese = ");"; + if (parent.writeParentheses) { + beginParenthese = "("; + endParenthese = ");"; } else { - this.beginParenthese = ""; - this.endParenthese = ""; + beginParenthese = ""; + endParenthese = ""; } String result = convertEndComment(ch).replaceAll("\"", "\\\\\"") .replaceAll("(\r\n|\n|\r)", - "\\\\n\"" + endParenthese + "\n" + parent.getWriteString() + beginParenthese + "\""); + "\\\\n\"" + endParenthese + "\n" + + parent.getWriteString() + beginParenthese + "\""); // it's important that \r\n is first in regexp. return result; Modified: trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/I18nFilter.java =================================================================== --- trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/I18nFilter.java 2010-02-21 10:27:35 UTC (rev 314) +++ trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/I18nFilter.java 2010-03-09 17:51:09 UTC (rev 315) @@ -43,8 +43,11 @@ private String footer = "\"\\s*(\\)|,|\\+|$)"; private Pattern headerPattern = Pattern.compile(getHeader()); + private Pattern footerPattern = Pattern.compile(getFooter()); - + + private Matcher matcher; + protected void setFooter(String footer) { this.footer = footer; } @@ -63,7 +66,6 @@ return footer; } - private Matcher matcher = null; @Override public int getMatchIndexFor(String input, String sequence) { int index = NOT_FOUND; Modified: trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/LicenseFilter.java =================================================================== --- trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/LicenseFilter.java 2010-02-21 10:27:35 UTC (rev 314) +++ trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/LicenseFilter.java 2010-03-09 17:51:09 UTC (rev 315) @@ -26,7 +26,8 @@ /** * Un filtre pour remplacer la license d'un fichier source java. * <p/> - * fixme : il faut ne pas autoriser le process de fichier java qui ne sont pas valide selon le header - footer... + * fixme : il faut ne pas autoriser le process de fichier java qui ne sont pas + * valide selon le header - footer... * * @author chemit */ @@ -34,15 +35,22 @@ /** to use log facility, just put in your code: log.info(\"...\"); */ static private final Log log = LogFactory.getLog(LicenseFilter.class); + public static final String HEADER = "*" + "#" + "#" + "%"; + public static final String FOOTER = "#" + "#" + "%" + "*"; + /** - * la licence a insere dans le header du fichier source. Ce header est formatte en commentaire (chaque ligne - * commence par un ' * ', sauf pour la premiere et derniere ligne). + * la licence a insere dans le header du fichier source. Ce header est + * formatte en commentaire (chaque ligne commence par un ' * ', sauf pour + * la premiere et derniere ligne). */ protected String licenseHeader; - /** flag pour indiquer si la licence a ete trouvee entre le header et le footer */ + + /** flag pour indiquer si la licence a ete trouvee entre le header et + * le footer */ protected boolean touched; + /** flag pour indiquer si on a rencontree le header du filtre */ protected boolean detectHeader; @@ -57,7 +65,8 @@ } if (touched) { // on autorise pas deux process de la licence dans un fichier java - throw new IllegalStateException("Can only have one license processor open tag " + LicenseFilter.HEADER); + throw new IllegalStateException("Can only have one license " + + "processor open tag " + HEADER); } if (getMatchIndexFor(ch, HEADER) == NOT_FOUND) { // on est bien dans la license, on peut effectuer le changement Modified: trunk/nuiton-processor/src/test/java/org/nuiton/processor/filters/StringFilterTest.java =================================================================== --- trunk/nuiton-processor/src/test/java/org/nuiton/processor/filters/StringFilterTest.java 2010-02-21 10:27:35 UTC (rev 314) +++ trunk/nuiton-processor/src/test/java/org/nuiton/processor/filters/StringFilterTest.java 2010-03-09 17:51:09 UTC (rev 315) @@ -7,8 +7,6 @@ import org.nuiton.processor.*; import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; import java.io.IOException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -35,8 +33,7 @@ File out = getFile(testdir, fileName); Processor processor = new Processor(); processor.setInputFilter(new GeneratorTemplatesFilter()); - processor.process(new FileReader(in), new FileWriter(out)); - String content = readAsString(out, "utf-8"); + String content = process(processor, in, out); if (log.isDebugEnabled()) { log.debug("output : " + out); log.debug(content); @@ -45,12 +42,19 @@ fileName = "StringFilterTest_1.java2"; in = StringFilterTest.class.getResource(fileName).getFile(); out = getFile(testdir, fileName); - processor.process(new FileReader(in), new FileWriter(out)); - content = readAsString(out, "utf-8"); + content = process(processor, in, out); if (log.isDebugEnabled()) { log.debug("output : " + out); log.debug(content); } //checkPattern("+\" int i = 0;\n\"", true, out); } + + protected String process(Processor processor, + String in, + File out) throws IOException { + ProcessorUtil.doProcess(processor,new File(in),out); + String content = readAsString(out, "utf-8"); + return content; + } }