Index: maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/XmlParser.java diff -u /dev/null maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/XmlParser.java:1.1 --- /dev/null Fri Oct 26 14:56:44 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/XmlParser.java Fri Oct 26 14:56:39 2007 @@ -0,0 +1,147 @@ +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; + +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathExpressionException; +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.util.FileUtil; +import org.codelutin.util.Resource; +import org.codelutin.util.StringUtil; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; + +/** + * + * @author julien + * + * @goal xmlparser + * @phase compile + */ +public class XmlParser extends AbstractI18nPlugin { + + /** + * @description Source xml. + * @parameter expression="${i18n.srcXml}" default-value="${basedir}/src/uimodel" + * @readonly + */ + 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 XPathFactory factory; + protected XPath xpath; + protected XPathExpression expression; + protected List rulesXpath; + protected Properties properties; + + public void execute() throws MojoExecutionException, MojoFailureException { + DirectoryScanner ds = new DirectoryScanner(); + ds.setBasedir(srcXml); + ds.setIncludes(new String[] {paternSrcXml}); + ds.scan(); + String[] files = ds.getIncludedFiles(); + + try { + properties = new Properties(); + rulesXpath = getRules(); + + for (int i = 0; i < files.length; i++) { + parseFile(srcXml.getAbsolutePath() + File.separator + files[i]); + } + } catch (Exception 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"); + properties.store(xmlparserOut, null); + } catch (Exception e) { + log.error("Generate Error I/O ", e); + throw new MojoFailureException("Generate Error I/O "); + } + + log.info("Xmlparser success : xmlparser.getter"); + } + + public void parseFile(String fileName) throws IOException, XPathExpressionException { + factory = XPathFactory.newInstance(); + xpath = factory.newXPath(); + + InputSource file = new InputSource(fileName); + log.info("Parse xml : " + fileName); + for (String rule : rulesXpath) { + parseXpath(file, rule); + } + } + + private List getRules() throws IOException { + // Lecture + InputStream inputStream = getClass().getClassLoader().getResourceAsStream(rules); + String readInputStream = readInputStream(inputStream); + + // 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; + } + + 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/Gettext.java diff -u /dev/null maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/Gettext.java:1.1 --- /dev/null Fri Oct 26 14:56:44 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/Gettext.java Fri Oct 26 14:56:39 2007 @@ -0,0 +1,95 @@ +package org.codelutin.i18n.plugin.extension; + +import java.io.File; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.codehaus.plexus.util.DirectoryScanner; +import org.codehaus.plexus.util.cli.CommandLineException; +import org.codehaus.plexus.util.cli.CommandLineUtils; +import org.codehaus.plexus.util.cli.Commandline; +import org.codehaus.plexus.util.cli.StreamConsumer; +import org.codelutin.i18n.plugin.core.AbstractI18nPlugin; +import org.codelutin.i18n.plugin.util.LoggerStreamConsumer; + +/** + * + * @author julien + * + * @goal gettext + * @phase compile + */ +public class Gettext extends AbstractI18nPlugin { + + /** + * @description Source Encoding. + * @parameter expression="${i18n.encoding}" default-value="utf-8" + */ + protected String encoding; + + /** + * @description Gettext keywords (see -k in help for details). + * @parameter expression="${i18n.keywords}" default-value="-k_ -k_n" + */ + protected String keywords; + + /** + * @description xgettext command. + * @parameter expression="${i18n.xgettextCmd}" default-value="xgettext" + */ + protected String xgettextCmd; + + /** + * @description Source java. + * @parameter expression="${i18n.srcJava}" default-value="${project.build.sourceDirectory}" + * @readonly + */ + protected File srcJava; + + /** + * @description Patern de recherche des fichiers java. + * @parameter expression="${i18n.paternSrcXml}" default-value="**\/*.java" + */ + String paternSrcJava; + + public void execute() throws MojoExecutionException, MojoFailureException { + // Initialisation du répertoire de travail + out.mkdirs(); + File xgettextOut = new File(out.getAbsolutePath() + File.separatorChar + "xgettext.getter"); + + // Récupération des clés + Commandline xgettext = new Commandline(); + xgettext.setExecutable(xgettextCmd); + xgettext.createArgument().setValue("--from-code=" + encoding); + xgettext.createArgument().setValue("--output=" + xgettextOut.getAbsolutePath()); + xgettext.createArgument().setValue("--language=Java"); + xgettext.createArgument().setValue("--no-location"); + xgettext.createArgument().setValue("--sort-output"); + xgettext.createArgument().setValue("--msgstr-prefix="); + xgettext.createArgument().setLine(keywords); + xgettext.createArgument().setValue("--properties-output"); + + DirectoryScanner ds = new DirectoryScanner(); + ds.setBasedir(srcJava); + ds.setIncludes(new String[] {paternSrcJava}); + ds.scan(); + String[] files = ds.getIncludedFiles(); + + for (int i = 0; i < files.length; i++) { + xgettext.createArgument().setValue(srcJava.getAbsolutePath() + File.separator + files[i]); + } + + log.info("Xgettext command : " + xgettext); + + StreamConsumer outLog = new LoggerStreamConsumer(log, LoggerStreamConsumer.INFO); + StreamConsumer errLog = new LoggerStreamConsumer(log, LoggerStreamConsumer.WARN); + try { + CommandLineUtils.executeCommandLine(xgettext, outLog, errLog); + } catch (CommandLineException e) { + log.error("Could not execute " + xgettextCmd + ".", e); + throw new MojoFailureException("Could not execute " + xgettextCmd + "."); + } + + log.info("Xgettext success : xgettext.getter"); + } +}