Index: maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserJava.java diff -u maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserJava.java:1.10 maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserJava.java:1.11 --- maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserJava.java:1.10 Wed Jan 2 16:52:51 2008 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserJava.java Sat Jan 5 13:10:56 2008 @@ -19,123 +19,67 @@ package org.codelutin.i18n.plugin.extension; +import org.codelutin.i18n.plugin.core.AbstractI18nParser; +import org.codelutin.i18n.plugin.core.ParserEvent; +import org.codelutin.i18n.plugin.core.ParserException; +import org.codelutin.processor.filters.I18nFilter; + import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.io.OutputStream; -import java.util.Properties; - -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.codehaus.plexus.util.DirectoryScanner; -import org.codelutin.i18n.plugin.core.AbstractI18nPlugin; -import org.codelutin.i18n.plugin.core.Parser; -import org.codelutin.i18n.plugin.core.ParserEvent; -import org.codelutin.i18n.plugin.core.ParserException; -import org.codelutin.i18n.plugin.ui.KeysModifier; -import org.codelutin.processor.filters.I18nFilter; -import org.codelutin.util.FileUtil; /** * Récupération des chaîne à traduire depuis les fichiers java. - * + * * @author julien - * * @goal parserJava * @phase compile */ -public class ParserJava extends AbstractI18nPlugin implements Parser { +public class ParserJava extends AbstractI18nParser { /** - * @description Source java. - * @parameter expression="${i18n.srcJava}" default-value="${basedir}/src/java" + * @description Source entries (src+includes+excludes) . + * @parameter expression="${i18n.defaultIncludes}" default-value="**\\/*.java" */ - protected File srcJava; + protected String defaultIncludes; /** - * @description Patern de recherche des fichiers java. - * @parameter expression="${i18n.paternJava}" default-value="**\/*.java" + * @description default src for an entry. + * @parameter expression="${i18n.defaultBasedir}" default-value="${basedir}/src/java" */ - String paternJava; + protected File defaultBasedir; - protected I18nFilter filter; - protected Properties result; - protected Properties oldParser; - protected Properties oldLanguage; - - /* - * (non-Javadoc) - * @see org.apache.maven.plugin.AbstractMojo#execute() - */ - public void execute() throws MojoExecutionException, MojoFailureException { - this.result = new Properties(); - this.filter = new I18nFilter(); - this.oldParser = new Properties(); - this.oldLanguage = new Properties(); - - // Événements - if(keysModifier) { - addParserEvent(KeysModifier.getInstance("_\\(\\s*\"", "\"\\s*(\\)|,|\\+|$)")); - } + protected String[] getDefaultIncludes() { + return new String[]{defaultIncludes}; + } - out.mkdirs(); + protected File getDefaultBasedir() { + return defaultBasedir; + } - try { - // Reprise sur un ancien parsing - File oldParserFile = new File(out.getAbsolutePath() + File.separatorChar + "java.getter"); - oldParserFile.createNewFile(); - oldParser.load(new FileInputStream(oldParserFile)); - - File saveFile = new File(out.getAbsolutePath() + File.separatorChar + "java.getter~"); - FileUtil.copy(oldParserFile, saveFile); - - // Anciennes clés disponnibles - File oldLanguageFile = new File(src.getAbsolutePath() + File.separatorChar + "language-" + bundles[0] + ".properties"); - oldLanguageFile.createNewFile(); - oldLanguage.load(new FileInputStream(oldLanguageFile)); + protected String getKeyModifierStart() { + return "_\\(\\s*\""; + } - // Parsing - parse(); + protected String getKeyModifierEnd() { + return "\"\\s*(\\)|,|\\+|$)"; + } - // Suppression du fichier sauvegarder - saveFile.delete(); + protected String getOutGetter() { + return "java.getter"; + } - } catch (Exception e) { - log.error("Error code parsing ", e); - throw new MojoFailureException("Error code parsing"); - } + protected I18nFilter filter; - log.info("Parser success : java.getter"); - } - - /* - * (non-Javadoc) - * @see org.codelutin.i18n.plugin.core.Parser#parse() - */ - public void parse() { - DirectoryScanner ds = new DirectoryScanner(); - ds.setBasedir(srcJava); - ds.setIncludes(new String[] {paternJava}); - ds.scan(); - String[] files = ds.getIncludedFiles(); - - for (String file1 : files) { - String fileName = srcJava.getAbsolutePath() + File.separator + file1; - File file = new File(fileName); - - log.info("Parse code : " + fileName); - for (ParserEvent event : events) { - event.eventChangeFile(file); - } - parseFile(file); - for (ParserEvent event : events) { - event.eventNextFile(file); - } - } + @Override + public void init() { + super.init(); + filter = new I18nFilter(); } - + /* * (non-Javadoc) * @see org.codelutin.i18n.plugin.core.Parser#parseFile(java.io.File) @@ -143,7 +87,7 @@ public void parseFile(File srcFile) { try { int size = result.size(); - + LineNumberReader lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(srcFile))); while (lnr.ready()) { String line = lnr.readLine(); @@ -152,7 +96,7 @@ // Détection de nouvelles clés, sauvegarde du fichier pour pouvoir le restaurer en cas de plantage if(size != result.size()) { - OutputStream xmlparserOut = new FileOutputStream(out.getAbsolutePath() + File.separatorChar + "java.getter"); + OutputStream xmlparserOut = new FileOutputStream(out.getAbsolutePath() + File.separatorChar + getOutGetter()); result.store(xmlparserOut, null); } } catch (Exception e) { Index: maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserJaxx.java diff -u maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserJaxx.java:1.6 maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserJaxx.java:1.7 --- maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserJaxx.java:1.6 Wed Jan 2 16:52:51 2008 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserJaxx.java Sat Jan 5 13:10:56 2008 @@ -19,53 +19,45 @@ package org.codelutin.i18n.plugin.extension; -import java.io.File; - -import org.codelutin.i18n.plugin.ui.KeysModifier; - /** * Récupération des chaîne à traduire depuis les fichiers xml Jaxx. - * + * * @author julien - * * @goal parserJaxx * @phase compile */ public class ParserJaxx extends ParserXml { /** - * @description Source xml. - * @parameter expression="${i18n.srcJaax}" default-value="${basedir}/src/uimodel" - * @readonly - */ - protected File srcJaax; - - /** - * @description Patern de recherche des fichiers xml. - * @parameter expression="${i18n.patternJaax}" default-value="**\/*.jaxx" + * @description Source entries (src+includes+excludes) . + * @parameter expression="${i18n.defaultIncludes}" default-value="**\\/*.jaxx" */ - String patternJaax; - + protected String defaultIncludes; + /** * @description Règles xml. * @parameter expression="${i18n.rulesJaax}" default-value="jaxx.rules" */ - protected String rulesJaax; + protected String fileRules; - /* - * (non-Javadoc) - * @see org.codelutin.i18n.plugin.extension.XmlParser#init() - */ - public void init() { - this.srcXml = srcJaax; - this.patternXml = patternJaax; - this.fileRules = rulesJaax; - this.outGetter = "jaxx.getter"; - - // Événements - if(keysModifier) { - addParserEvent(KeysModifier.getInstance("\\{_\\(\"", "\"\\)\\}")); - } + protected String[] getDefaultIncludes() { + return new String[]{defaultIncludes}; + } + + protected String getOutGetter() { + return "jaxx.getter"; + } + + protected String getKeyModifierStart() { + return "\\{_\\(\""; + } + + protected String getKeyModifierEnd() { + return "\"\\)\\}"; + } + + protected String getFileRules() { + return fileRules; } /* @@ -75,11 +67,12 @@ public String extract(String i18nString) { int i18nStart = i18nString.indexOf("{_(\""); int i18nEnd = i18nString.lastIndexOf("\")}"); - - if(i18nStart != -1 && i18nEnd != -1) { + + if (i18nStart != -1 && i18nEnd != -1) { return i18nString.substring(i18nStart + 4, i18nEnd); } else { return null; } } + } Index: maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserSwixat.java diff -u maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserSwixat.java:1.5 maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserSwixat.java:1.6 --- maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserSwixat.java:1.5 Wed Jan 2 16:52:50 2008 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserSwixat.java Sat Jan 5 13:10:56 2008 @@ -19,53 +19,46 @@ package org.codelutin.i18n.plugin.extension; -import java.io.File; - -import org.codelutin.i18n.plugin.ui.KeysModifier; - /** * Récupération des chaîne à traduire depuis les fichiers xml Swixat. - * + * * @author julien - * * @goal parserSwixat * @phase compile */ public class ParserSwixat extends ParserXml { /** - * @description Source xml. - * @parameter expression="${i18n.srcSwixat}" default-value="${basedir}/src/uimodel" - * @readonly - */ - protected File srcSwixat; - - /** - * @description Patern de recherche des fichiers xml. - * @parameter expression="${i18n.patternSwixat}" default-value="**\/*.xml" + * @description Source entries (src+includes+excludes) . + * @parameter expression="${i18n.defaultIncludes}" default-value="**\\/*.xml" */ - String patternSwixat; + protected String defaultIncludes; /** * @description Règles xml. * @parameter expression="${i18n.rulesSwixat}" default-value="swixat.rules" */ - protected String rulesSwixat; + protected String fileRules; - /* - * (non-Javadoc) - * @see org.codelutin.i18n.plugin.extension.XmlParser#init() - */ - public void init() { - this.srcXml = srcSwixat; - this.patternXml = patternSwixat; - this.fileRules = rulesSwixat; - this.outGetter = "swixat.getter"; - - // Événements - if(keysModifier) { - addParserEvent(KeysModifier.getInstance("=\\s*[\"\']", "[\"\']")); - } + protected String[] getDefaultIncludes() { + return new String[]{defaultIncludes}; + } + + + protected String getKeyModifierStart() { + return "=\\s*[\"\']"; + } + + protected String getKeyModifierEnd() { + return "[\"\']"; + } + + protected String getOutGetter() { + return "swixat.getter"; + } + + protected String getFileRules() { + return fileRules; } /* Index: maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserXml.java diff -u maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserXml.java:1.8 maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserXml.java:1.9 --- maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserXml.java:1.8 Thu Nov 8 14:33:41 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserXml.java Sat Jan 5 13:10:56 2008 @@ -20,132 +20,60 @@ package org.codelutin.i18n.plugin.extension; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.util.Properties; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathFactory; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.codehaus.plexus.util.DirectoryScanner; -import org.codelutin.i18n.plugin.core.AbstractI18nPlugin; -import org.codelutin.i18n.plugin.core.Parser; import org.codelutin.i18n.plugin.core.ParserEvent; import org.codelutin.i18n.plugin.core.ParserException; -import org.codelutin.util.FileUtil; +import org.codelutin.i18n.plugin.core.AbstractI18nParser; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; /** * Récupération des chaîne à traduire depuis les fichiers xml. - * + * * @author julien */ -public abstract class ParserXml extends AbstractI18nPlugin implements Parser { +public abstract class ParserXml extends AbstractI18nParser { /** Taille du buffer pour les lectures/écritures */ protected static final int BUFFER_SIZE = 8 * 1024; - protected File srcXml; - protected String outGetter; - protected String patternXml; - protected String fileRules; + /** + * @description default src for an entry. + * @parameter expression="${i18n.defaultBasedir}" default-value="${basedir}/src/uimodel" + * @required + */ + protected File defaultBasedir; protected String rules; protected XPathFactory factory; protected XPath xpath; - protected Properties result; - - protected Properties oldParser; - protected Properties oldLanguage; - - /** - * Initialisation des paramètres du parser - */ - public abstract void init(); /** - * Fonction d'extraitraction de la chaine - * @param i18nString - * @return + * Fonction d'extraaction de la chaine + * @param i18nString le clef i18n + * @return la chaine */ public abstract String extract(String i18nString); - /* - * (non-Javadoc) - * @see org.apache.maven.plugin.AbstractMojo#execute() - */ - public void execute() throws MojoExecutionException, MojoFailureException { - init(); + /** @return le fichier des rules */ + protected abstract String getFileRules(); + + @Override + public void init() { + super.init(); this.factory = XPathFactory.newInstance(); - this.rules = getRules(fileRules); + this.rules = getRules(getFileRules()); this.xpath = factory.newXPath(); - this.result = new Properties(); - this.oldParser = new Properties(); - this.oldLanguage = new Properties(); - - out.mkdirs(); - - try { - // Reprise sur un ancien parsing - File oldParserFile = new File(out.getAbsolutePath() + File.separatorChar + outGetter); - oldParserFile.createNewFile(); - oldParser.load(new FileInputStream(oldParserFile)); - - File saveFile = new File(out.getAbsolutePath() + File.separatorChar + outGetter + "~"); - FileUtil.copy(oldParserFile, saveFile); - - // Anciennes clés disponnibles - File oldLanguageFile = new File(src.getAbsolutePath() + File.separatorChar + "language-" + bundles[0] + ".properties"); - oldLanguageFile.createNewFile(); - oldLanguage.load(new FileInputStream(oldLanguageFile)); - - // Parsing - parse(); - - // Suppression du fichier sauvegarder - saveFile.delete(); - - } catch (Exception e) { - log.error("Error XML parsing ", e); - throw new MojoFailureException("Error XML parsing"); - } - - log.info("Parser success : " + outGetter); - } - - /* - * (non-Javadoc) - * @see org.codelutin.i18n.plugin.core.Parser#parse() - */ - public void parse() { - DirectoryScanner ds = new DirectoryScanner(); - ds.setBasedir(srcXml); - ds.setIncludes(new String[] {patternXml}); - ds.scan(); - String[] files = ds.getIncludedFiles(); - - for (int i = 0; i < files.length; i++) { - String fileName = srcXml.getAbsolutePath() + File.separator + files[i]; - File file = new File(fileName); - - log.info("Parse xml : " + fileName); - for (ParserEvent event : events) { - event.eventChangeFile(file); - } - parseFile(file); - for (ParserEvent event : events) { - event.eventNextFile(file); - } - } } /* @@ -153,12 +81,12 @@ * @see org.codelutin.i18n.plugin.core.Parser#parseFile(java.io.File) */ public void parseFile(File file) { - NodeList list = null; + NodeList list; InputSource inputSource = new InputSource(file.getAbsolutePath()); // TODO: A déplacer pour les performances try { int size = result.size(); - + // Recherche des clés à partir d'un xpath XPathExpression expression = xpath.compile(rules); list = (NodeList) expression.evaluate(inputSource, XPathConstants.NODESET); @@ -170,7 +98,7 @@ // Détection de nouvelles clés, sauvegarde du fichier pour pouvoir le restaurer en cas de plantage if(size != result.size()) { - OutputStream xmlparserOut = new FileOutputStream(out.getAbsolutePath() + File.separatorChar + outGetter); + OutputStream xmlparserOut = new FileOutputStream(out.getAbsolutePath() + File.separatorChar + getOutGetter()); result.store(xmlparserOut, null); } } catch (Exception e) { @@ -190,7 +118,7 @@ event.eventChangeKey(key, !oldLanguage.containsKey(key)); keyModified = event.eventGetRealKey(); } - + if(oldParser.containsKey(key)) { result.put(keyModified, oldParser.get(key)); } else { @@ -199,38 +127,42 @@ } } + protected File getDefaultBasedir() { + return defaultBasedir; + } + /** * Récupère le xpath à partir d'un fichier - * @param fileRules - * @return + * @param fileRules le nom du fichier contant les règles + * @return le xpath à partir d'un fichier */ private String getRules(String fileRules) { String result; - + // Lecture ClassLoader classLoader = getClass().getClassLoader(); InputStream inputStream = classLoader.getResourceAsStream(fileRules); - String readInputStream = ""; + String readInputStream; try { readInputStream = readInputStream(inputStream); } catch (IOException e) { throw new ParserException(e); } - + // Suppression result = readInputStream.trim(); result = result.replaceAll("#.*\n", ""); // suppression des commentaires result = result.replaceAll("\\s+", " | "); // contruction du xpath avec des ou result = result.replaceAll("(^ \\| )|( \\| $)", ""); // suppression des ou de début ee fin - + return result; } /** * Permet la lecture d'un InputStream - * @param in - * @return - * @throws IOException + * @param in le flux entrant + * @return le contenu du flux + * @throws IOException si problème de lecture dans flux entrant */ private String readInputStream(InputStream in) throws IOException { String result = "";