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 private boolean hadErrors = false;
84 public TranslationCollector(File base, File conf) {
86 taint = new LinkedList<>();
87 for (String s : new FileIterable(conf)) {
88 taint.add(TaintSource.parseTaint(s));
92 public void run(File out) throws IOException {
96 System.err.println("Total Translatable Strings: " + translations.size());
97 TreeSet<TranslationEntry> trs = new TreeSet<>(translations.values());
98 writePOFile(out, trs);
101 public void add(String text, String line) {
102 if (text.contains("\r") || text.contains("\n")) {
103 throw new Error("Malformed translation in " + line);
105 TranslationEntry i = translations.get(text);
107 translations.put(text, new TranslationEntry(text, line));
113 private void scanCode(LinkedList<TaintSource> taint) throws Error {
114 PrintWriter out = new PrintWriter(System.err);
115 Main m = new Main(out, out, false, null, null);
116 File[] fs = recurse(new File(new File(new File(base, "src"), "org"), "cacert"), new LinkedList<File>(), ".java").toArray(new File[0]);
117 String[] t = new String[fs.length + 3];
119 t[1] = new File(base, "bin").getAbsolutePath();
121 for (int i = 0; i < fs.length; i++) {
122 t[i + 3] = fs[i].getAbsolutePath();
125 FileSystem environment = m.getLibraryAccess();
126 CompilerOptions compilerOptions = new CompilerOptions(m.options);
127 compilerOptions.performMethodsFullRecovery = false;
128 compilerOptions.performStatementsRecovery = false;
130 compilerOptions.sourceLevel = ClassFileConstants.JDK1_7;
131 compilerOptions.complianceLevel = ClassFileConstants.JDK1_7;
132 compilerOptions.originalComplianceLevel = ClassFileConstants.JDK1_7;
134 ProblemReporter pr = new ProblemReporter(m.getHandlingPolicy(), compilerOptions, m.getProblemFactory());
135 ITypeRequestor tr = new ITypeRequestor() {
138 public void accept(ISourceType[] sourceType, PackageBinding packageBinding, AccessRestriction accessRestriction) {
139 throw new IllegalStateException("source type not implemented");
143 public void accept(IBinaryType binaryType, PackageBinding packageBinding, AccessRestriction accessRestriction) {
144 le.createBinaryTypeFrom(binaryType, packageBinding, accessRestriction);
148 public void accept(ICompilationUnit unit, AccessRestriction accessRestriction) {
149 throw new IllegalStateException("compilation unit not implemented");
152 le = new LookupEnvironment(tr, compilerOptions, pr, environment);
153 Parser parser = new Parser(pr, compilerOptions.parseLiteralExpressionsAsConstants);
154 CompilationUnit[] sourceUnits = m.getCompilationUnits();
155 CompilationUnitDeclaration[] parsedUnits = new CompilationUnitDeclaration[sourceUnits.length];
156 for (int i = 0; i < parsedUnits.length; i++) {
158 CompilationResult unitResult = new CompilationResult(sourceUnits[i], i, parsedUnits.length, compilerOptions.maxProblemsPerUnit);
159 CompilationUnitDeclaration parsedUnit = parser.parse(sourceUnits[i], unitResult);
160 le.buildTypeBindings(parsedUnit, null /* no access restriction */);
161 parsedUnits[i] = parsedUnit;
163 le.completeTypeBindings();
164 for (int i = 0; i < parsedUnits.length; i++) {
165 CompilationUnitDeclaration parsedUnit = parsedUnits[i];
167 parser.getMethodBodies(parsedUnit);
168 parsedUnit.scope.faultInTypes();
169 parsedUnit.scope.verifyMethods(le.methodVerifier());
170 parsedUnit.resolve();
172 for (int i = 0; i < parsedUnits.length; i++) {
173 CompilationUnitDeclaration parsedUnit = parsedUnits[i];
174 if (parsedUnit.compilationResult.problems != null) {
176 for (int c = 0; c < parsedUnit.compilationResult.problemCount; c++) {
177 CategorizedProblem problem = parsedUnit.compilationResult.problems[c];
178 if (problem.isError()) {
181 if (OUTPUT_WARNINGS || problem.isError()) {
182 System.err.println(problem);
183 StringBuilder prob = new StringBuilder();
184 prob.append(parsedUnit.compilationResult.fileName);
186 prob.append(problem.getSourceLineNumber());
187 System.err.println(prob.toString());
195 if (parsedUnit.types == null) {
196 System.err.println("No types");
199 TranslationCollectingVisitor v = new TranslationCollectingVisitor(parsedUnit, taint.toArray(new TaintSource[taint.size()]), this);
200 for (TypeDeclaration td : parsedUnit.types) {
201 td.traverse(v, td.scope);
203 hadErrors |= v.hadErrors();
205 parsedUnits[i] = parsedUnit;
209 private void scanTemplates() throws UnsupportedEncodingException, FileNotFoundException {
210 File[] ts = recurse(new File(new File(new File(base, "src"), "org"), "cacert"), new LinkedList<File>(), ".templ").toArray(new File[0]);
211 for (File file : ts) {
212 Template t = new Template(new InputStreamReader(new FileInputStream(file), "UTF-8"));
213 LinkedList<String> i = new LinkedList<String>();
214 t.addTranslations(i);
215 for (String string : i) {
216 add(string, file.getAbsolutePath().substring(base.getAbsolutePath().length() + 1) + ":1");
221 static LookupEnvironment le;
223 private static final boolean OUTPUT_WARNINGS = false;
225 private LinkedList<TaintSource> taint;
227 public static void main(String[] args) throws IOException {
228 TranslationCollector tc = new TranslationCollector(new File(args[1]), new File(args[0]));
229 tc.run(new File(args[2]));
237 public static void writePOFile(File target, Collection<TranslationEntry> strings) throws IOException {
238 PrintWriter out = new PrintWriter(target);
239 for (TranslationEntry s : strings) {
241 for (String st : s.getOccur()) {
245 out.println("msgid \"" + s.text.replace("\\", "\\\\").replace("\"", "\\\"") + "\"");
246 out.println("msgstr \"\"");
252 private static List<File> recurse(File file, List<File> toAdd, String pt) {
253 if (file.isDirectory()) {
254 for (File f : file.listFiles()) {
255 recurse(f, toAdd, pt);
258 if (file.getName().endsWith(pt)) {