Author: echatellier Date: 2011-05-24 15:49:23 +0200 (Tue, 24 May 2011) New Revision: 466 Url: http://nuiton.org/repositories/revision/sandbox/466 Log: Initial import. Added: i18nastparsingtest/pom.xml i18nastparsingtest/src/ i18nastparsingtest/src/main/ i18nastparsingtest/src/main/java/ i18nastparsingtest/src/main/java/org/ i18nastparsingtest/src/main/java/org/test/ i18nastparsingtest/src/main/java/org/test/parsing/ i18nastparsingtest/src/main/java/org/test/parsing/I18nASTParsingTest.java i18nastparsingtest/src/main/resources/ Modified: i18nastparsingtest/ Property changes on: i18nastparsingtest ___________________________________________________________________ Added: svn:ignore + .settings .classpath .project target Added: i18nastparsingtest/pom.xml =================================================================== --- i18nastparsingtest/pom.xml (rev 0) +++ i18nastparsingtest/pom.xml 2011-05-24 13:49:23 UTC (rev 466) @@ -0,0 +1,20 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>AntlrTest</groupId> + <artifactId>AntlrTest</artifactId> + <version>0.0.1-SNAPSHOT</version> + + <dependencies> + <dependency> + <groupId>org.nuiton</groupId> + <artifactId>nuiton-utils</artifactId> + <version>2.2</version> + </dependency> + + <dependency> + <groupId>com.google.code.javaparser</groupId> + <artifactId>javaparser</artifactId> + <version>1.0.1</version> + </dependency> + </dependencies> +</project> \ No newline at end of file Added: i18nastparsingtest/src/main/java/org/test/parsing/I18nASTParsingTest.java =================================================================== --- i18nastparsingtest/src/main/java/org/test/parsing/I18nASTParsingTest.java (rev 0) +++ i18nastparsingtest/src/main/java/org/test/parsing/I18nASTParsingTest.java 2011-05-24 13:49:23 UTC (rev 466) @@ -0,0 +1,134 @@ +/* + * #%L + * + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 Codelutin, Chatellier Eric + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.test.parsing; + +import static org.nuiton.i18n.I18n._; +import static org.nuiton.i18n.I18n.n_; +import japa.parser.JavaParser; +import japa.parser.ast.CompilationUnit; +import japa.parser.ast.expr.MethodCallExpr; +import japa.parser.ast.visitor.VoidVisitorAdapter; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; + +import org.nuiton.i18n.I18n; +import org.nuiton.i18n.init.ClassPathI18nInitializer; + +/** + * Test. + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public class I18nASTParsingTest { + + /** + * Ce commentaire est le commentaire du main, il ne doit pas être détecté. + * + * _("commentaire java doc"); + * n_("commentaire java doc"); + * + * @param args + */ + public static void main(String... args) { + + // ca non plus _("ca non plus"); + + I18n.init(new ClassPathI18nInitializer(), null); + + // ca par contre oui + I18n._("ca par contre oui"); + + // ca aussi, multiligne + String mastringtellementlonguequecavapasseralaligne = _( + "ca par contre oui"); + + // voir ca : c'est plus dur + String trans = _("toto" + "tutu"); + + // ca non + n_("ca aussi"); + _(mastringtellementlonguequecavapasseralaligne); + + // parsing + parseMeJavaParser(); + } + + protected static void parseMeJavaParser() { + + String meResource = I18nASTParsingTest.class.getName().replace(".", "/") + + ".java"; + + try { + //InputStream meAsStream = AntrlTest.class.getResourceAsStream("/" + meResource); + File file = new File("src/main/java/" + meResource); + InputStream meAsStream = new FileInputStream(file); + System.out.println("Reading file " + file); + + CompilationUnit cu; + try { + // parse the file + cu = JavaParser.parse(meAsStream); + } finally { + meAsStream.close(); + } + + // visit and print the methods names + new MethodVisitor().visit(cu, null); + + } catch (Exception e) { + e.printStackTrace(); + + // test dans un bloc + _("test dans n bloc"); + } + } + + /** + * Simple visitor implementation for visiting MethodDeclaration nodes. + */ + static class MethodVisitor extends VoidVisitorAdapter { + @Override + public void visit(MethodCallExpr n, Object arg) { + + if (n.getName().matches("[nl]?_")) { + if (!n.getArgs().isEmpty()) { + String firstArgs = n.getArgs().get(0).toString(); + if (firstArgs.matches("^\"[^\"]+\"$")) { + String key = firstArgs.substring(1).substring(0, firstArgs.length() - 2); + System.out.println("Found key : " + key); + } + + } + } + } + } +} Property changes on: i18nastparsingtest/src/main/java/org/test/parsing/I18nASTParsingTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL