Index: maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/XmlParser.java diff -u maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/XmlParser.java:1.2 maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/XmlParser.java:1.3 --- maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/XmlParser.java:1.2 Fri Oct 26 15:39:22 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/XmlParser.java Tue Oct 30 11:55:14 2007 @@ -1,15 +1,10 @@ package org.codelutin.i18n.plugin.extension; -import java.io.BufferedReader; 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.io.StringWriter; -import java.net.URISyntaxException; -import java.net.URL; import java.util.Arrays; import java.util.List; import java.util.Properties; @@ -24,9 +19,8 @@ import org.apache.maven.plugin.MojoFailureException; import org.codehaus.plexus.util.DirectoryScanner; import org.codelutin.i18n.plugin.core.AbstractI18nPlugin; -import org.codelutin.util.FileUtil; -import org.codelutin.util.Resource; -import org.codelutin.util.StringUtil; +import org.codelutin.i18n.plugin.core.Parser; +import org.codelutin.i18n.plugin.core.ParserException; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; @@ -34,94 +28,118 @@ * Récupération des chaîne à traduire depuis les fichiers xml. * * @author julien - * - * @goal xmlparser - * @phase compile */ -public class XmlParser extends AbstractI18nPlugin { +public abstract class XmlParser extends AbstractI18nPlugin implements Parser { - /** - * @description Source xml. - * @parameter expression="${i18n.srcXml}" default-value="${basedir}/src/uimodel" - * @readonly - */ + /** Taille du buffer pour les lectures/écritures */ + protected static final int BUFFER_SIZE = 8 * 1024; + protected File srcXml; - - /** - * @description Patern de recherche des fichiers xml. - * @parameter expression="${i18n.paternSrcXml}" default-value="**\/*.xml" - */ - String paternSrcXml; - - /** - * @description Règles xml. - * @parameter expression="${i18n.rules}" default-value="xmlparser.rules" - */ - protected String rules; - + protected String outGetter; + protected String patternXml; + protected String fileRules; + protected List rules; + protected Properties properties; protected XPathFactory factory; protected XPath xpath; - protected XPathExpression expression; - protected List rulesXpath; - protected Properties properties; /* * (non-Javadoc) * @see org.apache.maven.plugin.AbstractMojo#execute() */ public void execute() throws MojoExecutionException, MojoFailureException { - DirectoryScanner ds = new DirectoryScanner(); - ds.setBasedir(srcXml); - ds.setIncludes(new String[] {paternSrcXml}); - ds.scan(); - String[] files = ds.getIncludedFiles(); - + init(); + this.factory = XPathFactory.newInstance(); + this.rules = getRules(fileRules); + this.xpath = factory.newXPath(); + this.properties = new Properties(); + try { - properties = new Properties(); - rulesXpath = getRules(); - - for (int i = 0; i < files.length; i++) { - parseFile(srcXml.getAbsolutePath() + File.separator + files[i]); - } - } catch (Exception e) { + parse(); + } catch (ParserException e) { log.error("Error XML parsing ", e); throw new MojoFailureException("Error XML parsing"); } - + out.mkdirs(); try { - OutputStream xmlparserOut = new FileOutputStream(out.getAbsolutePath() + File.separatorChar + "xmlparser.getter"); + OutputStream xmlparserOut = new FileOutputStream(out.getAbsolutePath() + File.separatorChar + outGetter); properties.store(xmlparserOut, null); } catch (Exception e) { log.error("Generate Error I/O ", e); throw new MojoFailureException("Generate Error I/O "); } + 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]; + log.info("Parse xml : " + fileName); + parseFile(new File(fileName)); + } + } - log.info("Xmlparser success : xmlparser.getter"); + /* + * (non-Javadoc) + * @see org.codelutin.i18n.plugin.core.Parser#parseFile(java.io.File) + */ + public void parseFile(File file) { + for (String rule : rules) { + parseLine(file, rule); + } } - public void parseFile(String fileName) throws IOException, XPathExpressionException { - factory = XPathFactory.newInstance(); - xpath = factory.newXPath(); + /* + * (non-Javadoc) + * @see org.codelutin.i18n.plugin.core.Parser#parseLine(java.io.File, java.lang.String) + */ + public void parseLine(File file, String line) { + NodeList list = null; + try { + XPathExpression expression = xpath.compile(line); + InputSource inputSource = new InputSource(file.getAbsolutePath()); // TODO: A déplacer pour les performances + list = (NodeList) expression.evaluate(inputSource, XPathConstants.NODESET); + } catch (XPathExpressionException e) { + throw new ParserException(e); + } - InputSource file = new InputSource(fileName); - log.info("Parse xml : " + fileName); - for (String rule : rulesXpath) { - parseXpath(file, rule); + for (int index = 0; index < list.getLength(); index++) { + String value = list.item(index).getTextContent(); + value = extract(value); + if(value != null) { + properties.put(value, value); + } } } - private List getRules() throws IOException { + private List getRules(String fileRules) { // Lecture - InputStream inputStream = getClass().getClassLoader().getResourceAsStream(rules); - String readInputStream = readInputStream(inputStream); + ClassLoader classLoader = getClass().getClassLoader(); + InputStream inputStream = classLoader.getResourceAsStream(fileRules); + String readInputStream = ""; + try { + readInputStream = readInputStream(inputStream); + } catch (IOException e) { + throw new ParserException(e); + } // Suppression readInputStream = readInputStream.replaceAll("#.*\n", ""); // commentaire readInputStream = readInputStream.replaceAll("\n", " "); // saut de ligne readInputStream = readInputStream.replaceAll(" +", " "); // 1 seul espace readInputStream = readInputStream.trim(); - + // Découpage String[] split = readInputStream.split(" ", 0); List asList = Arrays.asList(split); @@ -139,14 +157,4 @@ return result; } - private void parseXpath(InputSource file, String path) throws XPathExpressionException { - expression = xpath.compile(path); - NodeList list = (NodeList) expression.evaluate(file, XPathConstants.NODESET); - - for (int index = 0; index < list.getLength(); index++) { - String value = list.item(index).getTextContent(); - properties.put(value, value); - } - } - } Index: maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/CodeParser.java diff -u /dev/null maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/CodeParser.java:1.1 --- /dev/null Tue Oct 30 11:55:19 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/CodeParser.java Tue Oct 30 11:55:14 2007 @@ -0,0 +1,129 @@ +/* *##% + * Copyright (C) 2002, 2003, 2004 + * Code Lutin, Cédric Pineau, Benjamin Poussin + * + * 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. + *##%*/ + +/* * + * I18nExtractor.java + * + * Created: Aug 16, 2004 + * + * @author Cédric Pineau + * @version $Revision: 1.1 $ + * + * Last update : $Date: 2007-10-30 11:55:14 $ + * by : $Author: ruchaud $ + */ + +package org.codelutin.i18n.plugin.extension; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStreamReader; +import java.io.LineNumberReader; + +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.ParserException; +import org.codelutin.processor.filters.I18nFilter; + +/** + * Récupération des chaîne à traduire depuis les fichiers java. + * + * @author julien + * + * @goal parserCode + * @phase compile + */ +public class CodeParser extends AbstractI18nPlugin implements Parser { + + protected I18nFilter filter = new I18nFilter(); + + /** + * @description Source java. + * @parameter expression="${i18n.srcCode}" default-value="${basedir}/src/java" + */ + protected File srcCode; + + /** + * @description Patern de recherche des fichiers java. + * @parameter expression="${i18n.paternCode}" default-value="**\/*.java" + */ + String paternCode; + + public void execute() throws MojoExecutionException, MojoFailureException { + init(); + + try { + parse(); + } catch (ParserException e) { + log.error("Error code parsing ", e); + throw new MojoFailureException("Error code parsing"); + } + } + + public void parse() { + DirectoryScanner ds = new DirectoryScanner(); + ds.setBasedir(srcCode); + ds.setIncludes(new String[] {paternCode}); + ds.scan(); + String[] files = ds.getIncludedFiles(); + + for (int i = 0; i < files.length; i++) { + String fileName = srcCode.getAbsolutePath() + File.separator + files[i]; +// log.info("Parse code : " + fileName); + parseFile(new File(fileName)); + } + } + + public void parseFile(File srcFile) { + try { + LineNumberReader lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(srcFile), "UTF-8")); + while (lnr.ready()) { + String line = lnr.readLine(); + parseLine(srcFile, line); + } + } catch (Exception e) { + throw new ParserException(e); + } + } + + public void parseLine(File srcFile, String line) { + String i18nStringsSet = filter.parse(line); + if (i18nStringsSet != I18nFilter.EMPTY_STRING) { + // Found a set of i18n Strings, split it. + String[] i18nStrings = i18nStringsSet.split("="); + for (int i = 0; i < i18nStrings.length; i++) { + String i18nString = i18nStrings[i]; + + log.info("i18nString : " + i18nString); + } + } + } + + public String extract(String i18nString) { + return i18nString; + } + + public void init() { + + } + +} Index: maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/XmlParserJaxx.java diff -u /dev/null maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/XmlParserJaxx.java:1.1 --- /dev/null Tue Oct 30 11:55:19 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/XmlParserJaxx.java Tue Oct 30 11:55:14 2007 @@ -0,0 +1,59 @@ +package org.codelutin.i18n.plugin.extension; + +import java.io.File; + +/** + * Récupération des chaîne à traduire depuis les fichiers xml. + * + * @author julien + * + * @goal parserJaxx + * @phase compile + */ +public class XmlParserJaxx extends XmlParser { + + /** + * @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" + */ + String patternJaax; + + /** + * @description Règles xml. + * @parameter expression="${i18n.rulesJaax}" default-value="jaxx.rules" + */ + protected String rulesJaax; + + /* + * (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"; + } + + /* + * (non-Javadoc) + * @see org.codelutin.i18n.plugin.extension.XmlParser#extract(java.lang.String) + */ + public String extract(String i18nString) { + int i18nStart = i18nString.indexOf("{_(\""); + int i18nEnd = i18nString.lastIndexOf("\")}"); + + 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/XmlParserSwixat.java diff -u /dev/null maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/XmlParserSwixat.java:1.1 --- /dev/null Tue Oct 30 11:55:19 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/XmlParserSwixat.java Tue Oct 30 11:55:14 2007 @@ -0,0 +1,52 @@ +package org.codelutin.i18n.plugin.extension; + +import java.io.File; + +/** + * Récupération des chaîne à traduire depuis les fichiers xml. + * + * @author julien + * + * @goal parserSwixat + * @phase compile + */ +public class XmlParserSwixat extends XmlParser { + + /** + * @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" + */ + String patternSwixat; + + /** + * @description Règles xml. + * @parameter expression="${i18n.rulesSwixat}" default-value="swixat.rules" + */ + protected String rulesSwixat; + + /* + * (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"; + } + + /* + * (non-Javadoc) + * @see org.codelutin.i18n.plugin.extension.XmlParser#extract(java.lang.String) + */ + public String extract(String i18nString) { + return i18nString; + } +}