]> WPIA git - gigi.git/blob - util-testing/org/cacert/gigi/localisation/TranslationCollector.java
upd: remove old, commented-out code from translation collector
[gigi.git] / util-testing / org / cacert / gigi / localisation / TranslationCollector.java
1 package org.cacert.gigi.localisation;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.io.InputStreamReader;
8 import java.io.PrintWriter;
9 import java.io.UnsupportedEncodingException;
10 import java.util.ArrayList;
11 import java.util.Arrays;
12 import java.util.Collection;
13 import java.util.HashMap;
14 import java.util.LinkedList;
15 import java.util.List;
16 import java.util.TreeSet;
17
18 import org.cacert.gigi.output.template.Template;
19 import org.eclipse.jdt.core.compiler.CategorizedProblem;
20 import org.eclipse.jdt.internal.compiler.CompilationResult;
21 import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
22 import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
23 import org.eclipse.jdt.internal.compiler.batch.CompilationUnit;
24 import org.eclipse.jdt.internal.compiler.batch.FileSystem;
25 import org.eclipse.jdt.internal.compiler.batch.Main;
26 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
27 import org.eclipse.jdt.internal.compiler.env.AccessRestriction;
28 import org.eclipse.jdt.internal.compiler.env.IBinaryType;
29 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
30 import org.eclipse.jdt.internal.compiler.env.ISourceType;
31 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
32 import org.eclipse.jdt.internal.compiler.impl.ITypeRequestor;
33 import org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment;
34 import org.eclipse.jdt.internal.compiler.lookup.PackageBinding;
35 import org.eclipse.jdt.internal.compiler.parser.Parser;
36 import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
37
38 public class TranslationCollector {
39
40     static class TranslationEntry implements Comparable<TranslationEntry> {
41
42         String text;
43
44         String occur1;
45
46         List<String> occur;
47
48         public TranslationEntry(String text, String occur) {
49             this.text = text;
50             occur1 = occur;
51         }
52
53         public List<String> getOccur() {
54             if (occur == null) {
55                 return Arrays.asList(occur1);
56             }
57             return occur;
58         }
59
60         public void add(String t) {
61             if (occur == null) {
62                 occur = new ArrayList<>(Arrays.asList(occur1));
63             }
64             occur.add(t);
65         }
66
67         @Override
68         public int compareTo(TranslationEntry o) {
69             int i = occur1.compareTo(o.occur1);
70             if (i != 0) {
71                 return i;
72             }
73
74             return text.compareTo(o.text);
75         }
76     }
77
78     private HashMap<String, TranslationEntry> translations = new HashMap<>();
79
80     public final File base;
81
82     public TranslationCollector(File base, File conf) {
83         this.base = base;
84         taint = new LinkedList<>();
85         for (String s : new FileIterable(conf)) {
86             taint.add(TaintSource.parseTaint(s));
87         }
88     }
89
90     public void run(File out) throws IOException {
91         scanTemplates();
92         scanCode(taint);
93
94         System.out.println("Total Translatable Strings: " + translations.size());
95         TreeSet<TranslationEntry> trs = new TreeSet<>(translations.values());
96         writePOFile(out, trs);
97
98     }
99
100     public void add(String text, String line) {
101         if (text.contains("\r") || text.contains("\n")) {
102             throw new Error("Malformed translation in " + line);
103         }
104         TranslationEntry i = translations.get(text);
105         if (i == null) {
106             translations.put(text, new TranslationEntry(text, line));
107             return;
108         }
109         i.add(line);
110     }
111
112     private void scanCode(LinkedList<TaintSource> taint) throws Error {
113         PrintWriter out = new PrintWriter(System.out);
114         Main m = new Main(out, out, false, null, null);
115         File[] fs = recurse(new File(new File(new File(base, "src"), "org"), "cacert"), new LinkedList<File>(), ".java").toArray(new File[0]);
116         String[] t = new String[fs.length + 3];
117         t[0] = "-cp";
118         t[1] = new File(base, "bin").getAbsolutePath();
119         t[2] = "-7";
120         for (int i = 0; i < fs.length; i++) {
121             t[i + 3] = fs[i].getAbsolutePath();
122         }
123         m.configure(t);
124         FileSystem environment = m.getLibraryAccess();
125         CompilerOptions compilerOptions = new CompilerOptions(m.options);
126         compilerOptions.performMethodsFullRecovery = false;
127         compilerOptions.performStatementsRecovery = false;
128         // check
129         compilerOptions.sourceLevel = ClassFileConstants.JDK1_7;
130         compilerOptions.complianceLevel = ClassFileConstants.JDK1_7;
131         compilerOptions.originalComplianceLevel = ClassFileConstants.JDK1_7;
132
133         ProblemReporter pr = new ProblemReporter(m.getHandlingPolicy(), compilerOptions, m.getProblemFactory());
134         ITypeRequestor tr = new ITypeRequestor() {
135
136             @Override
137             public void accept(ISourceType[] sourceType, PackageBinding packageBinding, AccessRestriction accessRestriction) {
138                 throw new IllegalStateException("source type not implemented");
139             }
140
141             @Override
142             public void accept(IBinaryType binaryType, PackageBinding packageBinding, AccessRestriction accessRestriction) {
143                 le.createBinaryTypeFrom(binaryType, packageBinding, accessRestriction);
144             }
145
146             @Override
147             public void accept(ICompilationUnit unit, AccessRestriction accessRestriction) {
148                 throw new IllegalStateException("compilation unit not implemented");
149             }
150         };
151         le = new LookupEnvironment(tr, compilerOptions, pr, environment);
152         Parser parser = new Parser(pr, compilerOptions.parseLiteralExpressionsAsConstants);
153         CompilationUnit[] sourceUnits = m.getCompilationUnits();
154         CompilationUnitDeclaration[] parsedUnits = new CompilationUnitDeclaration[sourceUnits.length];
155         for (int i = 0; i < parsedUnits.length; i++) {
156
157             CompilationResult unitResult = new CompilationResult(sourceUnits[i], i, parsedUnits.length, compilerOptions.maxProblemsPerUnit);
158             CompilationUnitDeclaration parsedUnit = parser.parse(sourceUnits[i], unitResult);
159             le.buildTypeBindings(parsedUnit, null /* no access restriction */);
160             parsedUnits[i] = parsedUnit;
161         }
162         le.completeTypeBindings();
163         for (int i = 0; i < parsedUnits.length; i++) {
164             CompilationUnitDeclaration parsedUnit = parsedUnits[i];
165
166             parser.getMethodBodies(parsedUnit);
167             parsedUnit.scope.faultInTypes();
168             parsedUnit.scope.verifyMethods(le.methodVerifier());
169             parsedUnit.resolve();
170         }
171         for (int i = 0; i < parsedUnits.length; i++) {
172             CompilationUnitDeclaration parsedUnit = parsedUnits[i];
173             if (parsedUnit.compilationResult.problems != null) {
174                 int err = 0;
175                 for (int c = 0; c < parsedUnit.compilationResult.problemCount; c++) {
176                     CategorizedProblem problem = parsedUnit.compilationResult.problems[c];
177                     if (problem.isError()) {
178                         err++;
179                     }
180                     if (OUTPUT_WARNINGS || problem.isError()) {
181                         System.out.println(problem);
182                         StringBuilder prob = new StringBuilder();
183                         prob.append(parsedUnit.compilationResult.fileName);
184                         prob.append(":");
185                         prob.append(problem.getSourceLineNumber());
186                         System.out.println(prob.toString());
187                     }
188                 }
189                 if (err > 0) {
190                     throw new Error();
191                 }
192             }
193
194             if (parsedUnit.types == null) {
195                 System.out.println("No types");
196
197             } else {
198                 TranslationCollectingVisitor v = new TranslationCollectingVisitor(parsedUnit, taint.toArray(new TaintSource[taint.size()]), this);
199                 for (TypeDeclaration td : parsedUnit.types) {
200                     td.traverse(v, td.scope);
201                 }
202             }
203             parsedUnits[i] = parsedUnit;
204         }
205     }
206
207     private void scanTemplates() throws UnsupportedEncodingException, FileNotFoundException {
208         File[] ts = recurse(new File(new File(new File(base, "src"), "org"), "cacert"), new LinkedList<File>(), ".templ").toArray(new File[0]);
209         for (File file : ts) {
210             Template t = new Template(new InputStreamReader(new FileInputStream(file), "UTF-8"));
211             LinkedList<String> i = new LinkedList<String>();
212             t.addTranslations(i);
213             for (String string : i) {
214                 add(string, file.getAbsolutePath().substring(base.getAbsolutePath().length() + 1) + ":1");
215             }
216         }
217     }
218
219     static LookupEnvironment le;
220
221     private static final boolean OUTPUT_WARNINGS = false;
222
223     private LinkedList<TaintSource> taint;
224
225     public static void main(String[] args) throws IOException {
226         new TranslationCollector(new File(args[1]), new File(args[0])).run(new File(args[2]));
227     }
228
229     public static void writePOFile(File target, Collection<TranslationEntry> strings) throws IOException {
230         PrintWriter out = new PrintWriter(target);
231         for (TranslationEntry s : strings) {
232             out.print("#:");
233             for (String st : s.getOccur()) {
234                 out.print(" " + st);
235             }
236             out.println();
237             out.println("msgid \"" + s.text.replace("\\", "\\\\").replace("\"", "\\\"") + "\"");
238             out.println("msgstr \"\"");
239             out.println();
240         }
241         out.close();
242     }
243
244     private static List<File> recurse(File file, List<File> toAdd, String pt) {
245         if (file.isDirectory()) {
246             for (File f : file.listFiles()) {
247                 recurse(f, toAdd, pt);
248             }
249         } else {
250             if (file.getName().endsWith(pt)) {
251                 toAdd.add(file);
252             }
253         }
254         return toAdd;
255     }
256 }