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.1 maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserSwixat.java:1.2 --- maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserSwixat.java:1.1 Wed Oct 31 16:17:52 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserSwixat.java Fri Nov 2 10:28:08 2007 @@ -2,6 +2,8 @@ import java.io.File; +import org.codelutin.i18n.plugin.ui.KeysModifier; + /** * Récupération des chaîne à traduire depuis les fichiers xml. * @@ -36,6 +38,7 @@ * @see org.codelutin.i18n.plugin.extension.XmlParser#init() */ public void init() { + this.events.add(new KeysModifier("=\\s*[\"\']", "[\"\']")); this.srcXml = srcSwixat; this.patternXml = patternSwixat; this.fileRules = rulesSwixat; 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.1 maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserJaxx.java:1.2 --- maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserJaxx.java:1.1 Wed Oct 31 16:17:52 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserJaxx.java Fri Nov 2 10:28:08 2007 @@ -2,6 +2,8 @@ import java.io.File; +import org.codelutin.i18n.plugin.ui.KeysModifier; + /** * Récupération des chaîne à traduire depuis les fichiers xml. * @@ -36,6 +38,7 @@ * @see org.codelutin.i18n.plugin.extension.XmlParser#init() */ public void init() { + this.events.add(new KeysModifier("{_(\"", "\")}")); this.srcXml = srcJaax; this.patternXml = patternJaax; this.fileRules = rulesJaax; 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.1 maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserXml.java:1.2 --- maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserXml.java:1.1 Wed Oct 31 16:17:52 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserXml.java Fri Nov 2 10:28:08 2007 @@ -5,13 +5,9 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.util.Arrays; -import java.util.LinkedHashMap; +import java.util.ArrayList; 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; @@ -25,8 +21,6 @@ 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; @@ -45,13 +39,13 @@ protected String outGetter; protected String patternXml; protected String fileRules; - protected List rules; - protected Properties result; - protected Map resultFile; + + protected String rules; protected XPathFactory factory; protected XPath xpath; + protected Properties result; - protected ParserEvent event = new KeysModifier(); + protected List events = new ArrayList(); /** * Initialisation des paramètres du parser @@ -75,6 +69,7 @@ this.rules = getRules(fileRules); this.xpath = factory.newXPath(); this.result = new Properties(); + out.mkdirs(); try { parse(); @@ -83,14 +78,6 @@ 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); } @@ -108,10 +95,15 @@ 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); + for (ParserEvent event : events) { + event.eventChangeFile(file); + } parseFile(file); + for (ParserEvent event : events) { + event.eventNextFile(file); + } } } @@ -120,33 +112,24 @@ * @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); + NodeList list = null; + InputSource inputSource = new InputSource(file.getAbsolutePath()); // TODO: A déplacer pour les performances - String content = ""; - int region = 0; - - try { - content = FileUtil.readAsString(file); - } catch (IOException e) { - throw new ParserException(e); - } + try { + // Recherche des clés à partir d'un xpath + XPathExpression expression = xpath.compile(rules); + list = (NodeList) expression.evaluate(inputSource, XPathConstants.NODESET); - 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"); + for (int index = 0; index < list.getLength(); index++) { + Node node = list.item(index); + parseLine(file, node.getTextContent()); } - log.info(content); - - result.putAll(resultFile); + // Sauvegarde dans le fichier de sortie + OutputStream xmlparserOut = new FileOutputStream(out.getAbsolutePath() + File.separatorChar + outGetter); + result.store(xmlparserOut, null); + } catch (Exception e) { + throw new ParserException(e); } } @@ -154,32 +137,26 @@ * (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); - } + public void parseLine(File file, String key) { + key = extract(key); + if(key != null) { + String keyModified = key; + for (ParserEvent event : events) { + event.eventChangeKey(key); + keyModified = event.eventGetRealKey(); } - } catch (Exception e) { - throw new ParserException(e); + result.put(keyModified, key); } - } - private List getRules(String fileRules) { + /** + * Récupère le xpath à partir d'un fichier + * @param fileRules + * @return + */ + private String getRules(String fileRules) { + String result; + // Lecture ClassLoader classLoader = getClass().getClassLoader(); InputStream inputStream = classLoader.getResourceAsStream(fileRules); @@ -189,19 +166,22 @@ } 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; + 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 + */ private String readInputStream(InputStream in) throws IOException { String result = ""; byte[] buffer = new byte[BUFFER_SIZE]; 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.1 maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserJava.java:1.2 --- maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserJava.java:1.1 Wed Oct 31 16:17:52 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/extension/ParserJava.java Fri Nov 2 10:28:08 2007 @@ -23,9 +23,9 @@ * Created: Aug 16, 2004 * * @author Cédric Pineau - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ * - * Last update : $Date: 2007-10-31 16:17:52 $ + * Last update : $Date: 2007-11-02 10:28:08 $ * by : $Author: ruchaud $ */ @@ -37,6 +37,8 @@ import java.io.InputStreamReader; import java.io.LineNumberReader; import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; import java.util.Properties; import org.apache.maven.plugin.MojoExecutionException; @@ -44,6 +46,7 @@ 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.processor.filters.I18nFilter; @@ -70,15 +73,18 @@ String paternJava; protected I18nFilter filter; - protected Properties properties; + protected Properties result; + protected List events = new ArrayList(); + /* * (non-Javadoc) * @see org.apache.maven.plugin.AbstractMojo#execute() */ public void execute() throws MojoExecutionException, MojoFailureException { - this.properties = new Properties(); + this.result = new Properties(); this.filter = new I18nFilter(); + out.mkdirs(); try { parse(); @@ -87,14 +93,6 @@ 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"); } @@ -111,8 +109,16 @@ for (int i = 0; i < files.length; i++) { String fileName = srcJava.getAbsolutePath() + File.separator + files[i]; + File file = new File(fileName); + log.info("Parse code : " + fileName); - parseFile(new File(fileName)); + for (ParserEvent event : events) { + event.eventChangeFile(file); + } + parseFile(file); + for (ParserEvent event : events) { + event.eventNextFile(file); + } } } @@ -127,6 +133,9 @@ String line = lnr.readLine(); parseLine(srcFile, line); } + + OutputStream xmlparserOut = new FileOutputStream(out.getAbsolutePath() + File.separatorChar + "java.getter"); + result.store(xmlparserOut, null); } catch (Exception e) { throw new ParserException(e); } @@ -143,8 +152,12 @@ String[] keys = keysSet.split("="); for (int i = 0; i < keys.length; i++) { String key = keys[i]; - properties.put(key, key); - log.info(" > " + key); + String keyModified = key; + for (ParserEvent event : events) { + event.eventChangeKey(key); + keyModified = event.eventGetRealKey(); + } + result.put(keyModified, key); } } }