Index: maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserSwixat.java diff -u /dev/null maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserSwixat.java:1.1 --- /dev/null Wed Oct 31 16:17:57 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserSwixat.java Wed Oct 31 16:17:52 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 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" + */ + 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; + } +} Index: maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserJaxx.java diff -u /dev/null maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserJaxx.java:1.1 --- /dev/null Wed Oct 31 16:17:57 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserJaxx.java Wed Oct 31 16:17:52 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 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" + */ + 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/ParserXml.java diff -u /dev/null maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserXml.java:1.1 --- /dev/null Wed Oct 31 16:17:57 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserXml.java Wed Oct 31 16:17:52 2007 @@ -0,0 +1,215 @@ +package org.codelutin.i18n.plugin.extension; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +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.i18n.plugin.ui.KeysModifier; +import org.codelutin.util.FileUtil; +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 { + + /** 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; + protected List rules; + protected Properties result; + protected Map resultFile; + protected XPathFactory factory; + protected XPath xpath; + + protected ParserEvent event = new KeysModifier(); + + /** + * Initialisation des paramètres du parser + */ + public abstract void init(); + + /** + * Fonction d'extraitraction de la chaine + * @param i18nString + * @return + */ + public abstract String extract(String i18nString); + + /* + * (non-Javadoc) + * @see org.apache.maven.plugin.AbstractMojo#execute() + */ + public void execute() throws MojoExecutionException, MojoFailureException { + init(); + this.factory = XPathFactory.newInstance(); + this.rules = getRules(fileRules); + this.xpath = factory.newXPath(); + this.result = new Properties(); + + try { + 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 + outGetter); + result.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]; + File file = new File(fileName); + + log.info("Parse xml : " + fileName); + event.eventChangeFile(file); + parseFile(file); + } + } + + /* + * (non-Javadoc) + * @see org.codelutin.i18n.plugin.core.Parser#parseFile(java.io.File) + */ + public void parseFile(File file) { + for (String rule : rules) { + resultFile = new LinkedHashMap(); + + parseLine(file, rule); + + String content = ""; + int region = 0; + + try { + content = FileUtil.readAsString(file); + } catch (IOException e) { + throw new ParserException(e); + } + + for (Object key : resultFile.keySet()) { + log.info("" + key); + Pattern pattern = Pattern.compile("(=\\s*[\"\'])(" + Pattern.quote("" + key) + ")([\"\'])"); + Matcher matcher = pattern.matcher(content); + matcher.region(region, content.length()); + matcher.find(); + region = matcher.start(); + content = matcher.replaceFirst("$1" + resultFile.get(key) + "$3"); + } + + log.info(content); + + result.putAll(resultFile); + } + } + + /* + * (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; + InputSource inputSource = new InputSource(file.getAbsolutePath()); // TODO: A déplacer pour les performances + + try { + XPathExpression expression = xpath.compile(line); + list = (NodeList) expression.evaluate(inputSource, XPathConstants.NODESET); + + for (int index = 0; index < list.getLength(); index++) { + Node node = list.item(index); + String key = node.getTextContent(); + key = extract(key); + if(key != null) { + event.eventChangeKey(key); + String keyModified = event.eventGetRealKey(); + resultFile.put(key, keyModified); + log.info(" > " + keyModified); + } + } + } catch (Exception e) { + throw new ParserException(e); + } + + } + + private List getRules(String fileRules) { + // Lecture + 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); + return asList; + } + + private String readInputStream(InputStream in) throws IOException { + String result = ""; + byte[] buffer = new byte[BUFFER_SIZE]; + while (in.read(buffer, 0, BUFFER_SIZE) != -1) { + String tmp = new String(buffer); + result += tmp; + } + in.close(); + return result; + } +} Index: maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserJava.java diff -u /dev/null maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserJava.java:1.1 --- /dev/null Wed Oct 31 16:17:57 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserJava.java Wed Oct 31 16:17:52 2007 @@ -0,0 +1,151 @@ +/* *##% + * 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-31 16:17:52 $ + * by : $Author: ruchaud $ + */ + +package org.codelutin.i18n.plugin.extension; + +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.ParserException; +import org.codelutin.processor.filters.I18nFilter; + +/** + * Récupération des chaîne à traduire depuis les fichiers java. + * + * @author julien + * + * @goal parserJava + * @phase compile + */ +public class ParserJava extends AbstractI18nPlugin implements Parser { + + /** + * @description Source java. + * @parameter expression="${i18n.srcJava}" default-value="${basedir}/src/java" + */ + protected File srcJava; + + /** + * @description Patern de recherche des fichiers java. + * @parameter expression="${i18n.paternJava}" default-value="**\/*.java" + */ + String paternJava; + + protected I18nFilter filter; + protected Properties properties; + + /* + * (non-Javadoc) + * @see org.apache.maven.plugin.AbstractMojo#execute() + */ + public void execute() throws MojoExecutionException, MojoFailureException { + this.properties = new Properties(); + this.filter = new I18nFilter(); + + try { + parse(); + } catch (ParserException e) { + log.error("Error code parsing ", e); + throw new MojoFailureException("Error code parsing"); + } + + out.mkdirs(); + try { + OutputStream xmlparserOut = new FileOutputStream(out.getAbsolutePath() + File.separatorChar + "java.getter"); + 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 : 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 (int i = 0; i < files.length; i++) { + String fileName = srcJava.getAbsolutePath() + File.separator + files[i]; + log.info("Parse code : " + fileName); + parseFile(new File(fileName)); + } + } + + /* + * (non-Javadoc) + * @see org.codelutin.i18n.plugin.core.Parser#parseFile(java.io.File) + */ + 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); + } + } + + /* + * (non-Javadoc) + * @see org.codelutin.i18n.plugin.core.Parser#parseLine(java.io.File, java.lang.String) + */ + public void parseLine(File srcFile, String line) { + String keysSet = filter.parse(line); + if (keysSet != I18nFilter.EMPTY_STRING) { + // Found a set of i18n Strings, split it. + String[] keys = keysSet.split("="); + for (int i = 0; i < keys.length; i++) { + String key = keys[i]; + properties.put(key, key); + log.info(" > " + key); + } + } + } +}