1 package org.cacert.gigi.localisation;
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;
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;
38 public class TranslationCollector {
40 static class TranslationEntry implements Comparable<TranslationEntry> {
48 public TranslationEntry(String text, String occur) {
53 public List<String> getOccur() {
55 return Arrays.asList(occur1);
60 public void add(String t) {
62 occur = new ArrayList<>(Arrays.asList(occur1));
68 public int compareTo(TranslationEntry o) {
69 int i = occur1.compareTo(o.occur1);
74 return text.compareTo(o.text);
78 private HashMap<String, TranslationEntry> translations = new HashMap<>();
80 public final File base;
82 public TranslationCollector(File base, File conf) {
84 taint = new LinkedList<>();
85 for (String s : new FileIterable(conf)) {
86 taint.add(TaintSource.parseTaint(s));
90 public void run(File out) throws IOException {
94 System.out.println("Total Translatable Strings: " + translations.size());
95 TreeSet<TranslationEntry> trs = new TreeSet<>(translations.values());
96 writePOFile(out, trs);
100 public void add(String text, String line) {
101 if (text.contains("\r") || text.contains("\n")) {
102 throw new Error("Malformed translation in " + line);
104 TranslationEntry i = translations.get(text);
106 translations.put(text, new TranslationEntry(text, line));
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];
118 t[1] = new File(base, "bin").getAbsolutePath();
120 for (int i = 0; i < fs.length; i++) {
121 t[i + 3] = fs[i].getAbsolutePath();
124 FileSystem environment = m.getLibraryAccess();
125 CompilerOptions compilerOptions = new CompilerOptions(m.options);
126 compilerOptions.performMethodsFullRecovery = false;
127 compilerOptions.performStatementsRecovery = false;
129 compilerOptions.sourceLevel = ClassFileConstants.JDK1_7;
130 compilerOptions.complianceLevel = ClassFileConstants.JDK1_7;
131 compilerOptions.originalComplianceLevel = ClassFileConstants.JDK1_7;
133 ProblemReporter pr = new ProblemReporter(m.getHandlingPolicy(), compilerOptions, m.getProblemFactory());
134 ITypeRequestor tr = new ITypeRequestor() {
137 public void accept(ISourceType[] sourceType, PackageBinding packageBinding, AccessRestriction accessRestriction) {
138 throw new IllegalStateException("source type not implemented");
142 public void accept(IBinaryType binaryType, PackageBinding packageBinding, AccessRestriction accessRestriction) {
143 le.createBinaryTypeFrom(binaryType, packageBinding, accessRestriction);
147 public void accept(ICompilationUnit unit, AccessRestriction accessRestriction) {
148 throw new IllegalStateException("compilation unit not implemented");
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++) {
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;
162 le.completeTypeBindings();
163 for (int i = 0; i < parsedUnits.length; i++) {
164 CompilationUnitDeclaration parsedUnit = parsedUnits[i];
166 parser.getMethodBodies(parsedUnit);
167 parsedUnit.scope.faultInTypes();
168 parsedUnit.scope.verifyMethods(le.methodVerifier());
169 parsedUnit.resolve();
171 for (int i = 0; i < parsedUnits.length; i++) {
172 CompilationUnitDeclaration parsedUnit = parsedUnits[i];
173 if (parsedUnit.compilationResult.problems != null) {
175 for (int c = 0; c < parsedUnit.compilationResult.problemCount; c++) {
176 CategorizedProblem problem = parsedUnit.compilationResult.problems[c];
177 if (problem.isError()) {
180 if (OUTPUT_WARNINGS || problem.isError()) {
181 System.out.println(problem);
182 StringBuilder prob = new StringBuilder();
183 prob.append(parsedUnit.compilationResult.fileName);
185 prob.append(problem.getSourceLineNumber());
186 System.out.println(prob.toString());
194 if (parsedUnit.types == null) {
195 System.out.println("No types");
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);
203 parsedUnits[i] = parsedUnit;
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");
219 static LookupEnvironment le;
221 private static final boolean OUTPUT_WARNINGS = false;
223 private LinkedList<TaintSource> taint;
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]));
229 public static void writePOFile(File target, Collection<TranslationEntry> strings) throws IOException {
230 PrintWriter out = new PrintWriter(target);
231 for (TranslationEntry s : strings) {
233 for (String st : s.getOccur()) {
237 out.println("msgid \"" + s.text.replace("\\", "\\\\").replace("\"", "\\\"") + "\"");
238 out.println("msgstr \"\"");
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);
250 if (file.getName().endsWith(pt)) {