fix: collect translation strings from anonymous class' constructors
authorFelix Dörre <felix@dogcraft.de>
Sun, 24 Jul 2016 11:57:13 +0000 (13:57 +0200)
committerFelix Dörre <felix@dogcraft.de>
Sun, 24 Jul 2016 21:44:26 +0000 (23:44 +0200)
In Eclipse-terms: from "QualifiedAllocationExpressions"

Change-Id: Ide5261e30799cb3220a0427cd72c882bd1a01efa

src/org/cacert/gigi/Gigi.java
src/org/cacert/gigi/output/SimpleMenuItem.java
src/org/cacert/gigi/output/SimpleUntranslatedMenuItem.java [new file with mode: 0644]
util-testing/org/cacert/gigi/localisation/TranslationCollectingVisitor.java
util-testing/org/cacert/gigi/localisation/conf.txt

index 41e504f..19b2f79 100644 (file)
@@ -31,6 +31,7 @@ import org.cacert.gigi.output.Menu;
 import org.cacert.gigi.output.MenuCollector;
 import org.cacert.gigi.output.PageMenuItem;
 import org.cacert.gigi.output.SimpleMenuItem;
+import org.cacert.gigi.output.SimpleUntranslatedMenuItem;
 import org.cacert.gigi.output.template.Form;
 import org.cacert.gigi.output.template.Form.CSRFException;
 import org.cacert.gigi.output.template.Outputable;
@@ -190,9 +191,7 @@ public final class Gigi extends HttpServlet {
             rootMenu = new MenuCollector();
 
             Menu languages = new Menu("Language");
-            for (Locale l : Language.getSupportedLocales()) {
-                languages.addItem(new SimpleMenuItem("?lang=" + l.toString(), l.getDisplayName(l)));
-            }
+            addLanguages(languages);
             categories.add(languages);
             for (Menu menu : categories) {
                 menu.prepare();
@@ -203,6 +202,12 @@ public final class Gigi extends HttpServlet {
             return rootMenu;
         }
 
+        private void addLanguages(Menu languages) {
+            for (Locale l : Language.getSupportedLocales()) {
+                languages.addItem(new SimpleUntranslatedMenuItem("?lang=" + l.toString(), l.getDisplayName(l)));
+            }
+        }
+
         public Map<String, Page> getPages() {
             return Collections.unmodifiableMap(pages);
         }
index 961a288..7def5ca 100644 (file)
@@ -1,34 +1,18 @@
 package org.cacert.gigi.output;
 
 import java.io.PrintWriter;
-import java.util.Map;
 
 import org.cacert.gigi.localisation.Language;
-import org.cacert.gigi.util.AuthorizationContext;
 
-public class SimpleMenuItem implements IMenuItem {
-
-    private final String href;
-
-    private final String name;
+public class SimpleMenuItem extends SimpleUntranslatedMenuItem {
 
     public SimpleMenuItem(String href, String name) {
-        this.href = href;
-        this.name = name;
+        super(href, name);
     }
 
     @Override
-    public void output(PrintWriter out, Language l, Map<String, Object> vars) {
-        out.print("<li><a href=\"");
-        out.print(href);
-        out.print("\">");
+    protected void printContent(PrintWriter out, Language l) {
         out.print(l.getTranslation(name));
-        out.print("</a></li>");
-    }
-
-    @Override
-    public boolean isPermitted(AuthorizationContext ac) {
-        return true;
     }
 
 }
diff --git a/src/org/cacert/gigi/output/SimpleUntranslatedMenuItem.java b/src/org/cacert/gigi/output/SimpleUntranslatedMenuItem.java
new file mode 100644 (file)
index 0000000..8206a29
--- /dev/null
@@ -0,0 +1,38 @@
+package org.cacert.gigi.output;
+
+import java.io.PrintWriter;
+import java.util.Map;
+
+import org.cacert.gigi.localisation.Language;
+import org.cacert.gigi.util.AuthorizationContext;
+
+public class SimpleUntranslatedMenuItem implements IMenuItem {
+
+    private final String href;
+
+    protected final String name;
+
+    public SimpleUntranslatedMenuItem(String href, String name) {
+        this.href = href;
+        this.name = name;
+    }
+
+    protected void printContent(PrintWriter out, Language l) {
+        out.print(name);
+    }
+
+    @Override
+    public void output(PrintWriter out, Language l, Map<String, Object> vars) {
+        out.print("<li><a href=\"");
+        out.print(href);
+        out.print("\">");
+        printContent(out, l);
+        out.print("</a></li>");
+    }
+
+    @Override
+    public boolean isPermitted(AuthorizationContext ac) {
+        return true;
+    }
+
+}
index 16f7c41..53e5c1c 100644 (file)
@@ -2,6 +2,7 @@ package org.cacert.gigi.localisation;
 import java.io.File;
 import java.io.IOException;
 import java.lang.reflect.Method;
+import java.util.Stack;
 
 import org.eclipse.jdt.internal.compiler.ASTVisitor;
 import org.eclipse.jdt.internal.compiler.ast.AllocationExpression;
@@ -13,6 +14,7 @@ import org.eclipse.jdt.internal.compiler.ast.Expression;
 import org.eclipse.jdt.internal.compiler.ast.MessageSend;
 import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
 import org.eclipse.jdt.internal.compiler.ast.NullLiteral;
+import org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression;
 import org.eclipse.jdt.internal.compiler.ast.StringLiteral;
 import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
 import org.eclipse.jdt.internal.compiler.lookup.ClassScope;
@@ -25,6 +27,8 @@ public final class TranslationCollectingVisitor extends ASTVisitor {
        TaintSource[] ts;
        private TranslationCollector translationCollector;
 
+    Stack<QualifiedAllocationExpression> anonymousConstructorCall = new Stack<>();
+
        public TranslationCollectingVisitor(CompilationUnitDeclaration unit,
                        TaintSource[] target, TranslationCollector c) {
                this.unit = unit;
@@ -67,6 +71,20 @@ public final class TranslationCollectingVisitor extends ASTVisitor {
                }
                return super.visit(allocationExpression, scope);
        }
+
+    @Override
+    public boolean visit(QualifiedAllocationExpression qualifiedAllocationExpression, BlockScope scope) {
+        anonymousConstructorCall.push(qualifiedAllocationExpression);
+        return super.visit(qualifiedAllocationExpression, scope);
+    }
+
+    @Override
+    public void endVisit(QualifiedAllocationExpression qualifiedAllocationExpression, BlockScope scope) {
+        if(anonymousConstructorCall.pop() != qualifiedAllocationExpression){
+            throw new Error("stack illegally manipulated");
+        }
+    }
+
        @Override
        public boolean visit(ExplicitConstructorCall explicitConstructor,
                        BlockScope scope) {
@@ -76,6 +94,9 @@ public final class TranslationCollectingVisitor extends ASTVisitor {
                for (TaintSource t0 : ts) {
                        if (t0.equals(t)) {
                                Expression[] ags = explicitConstructor.arguments;
+                if (anonymousConstructorCall.size() > 0) {
+                    ags = anonymousConstructorCall.peek().arguments;
+                }
                                if (ags == null) {
                                        System.out.println(explicitConstructor);
                                        return true;
@@ -183,7 +204,8 @@ public final class TranslationCollectingVisitor extends ASTVisitor {
                                + (call == null ? "constructor" : call.sourceStart) + " => "
                                + caller);
                System.out.println(e.getClass());
-               System.out.println("To ignore: " + b.toConfLine());
+               System.out.println(
+                               "To ignore: " + (b == null ? "don't know" : b.toConfLine()));
        }
        private void testEnum(Expression e, MethodBinding binding) {
                if (binding.parameters.length != 0) {
index e661604..0c2d1a9 100644 (file)
@@ -5,7 +5,7 @@ org.cacert.gigi Gigi.MenuBuilder.putPage(String, Page, String),2
 org.cacert.gigi Gigi.MenuBuilder.getMenu(String),0
 org.cacert.gigi GigiApiException.GigiApiException(String),0=>org.cacert.gigi GigiApiException.formatPlain(PrintWriter),0
 org.cacert.gigi.output Menu.Menu(String),0=>org.cacert.gigi.output Menu.output(PrintWriter, Language, Map),0
-org.cacert.gigi.output SimpleMenuItem.SimpleMenuItem(String,String),1=>org.cacert.gigi.output SimpleMenuItem.output(PrintWriter, Language, Map),0
+org.cacert.gigi.output SimpleMenuItem.SimpleMenuItem(String, String),1=>org.cacert.gigi.output SimpleMenuItem.output(PrintWriter, Language, Map),0
 org.cacert.gigi.dbObjects Digest.Digest(String),0
 org.cacert.gigi.dbObjects Certificate.CertificateStatus.CertificateStatus(String),0
 org.cacert.gigi.pages Page.Page(String),0