1 package org.cacert.gigi.localisation;
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.io.PrintWriter;
8 import java.io.UnsupportedEncodingException;
9 import java.util.ArrayList;
10 import java.util.Arrays;
11 import java.util.Collection;
12 import java.util.HashMap;
13 import java.util.LinkedList;
14 import java.util.List;
15 import java.util.TreeSet;
17 import org.cacert.gigi.output.template.Template;
18 import org.eclipse.jdt.core.compiler.CategorizedProblem;
19 import org.eclipse.jdt.internal.compiler.CompilationResult;
20 import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
21 import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
22 import org.eclipse.jdt.internal.compiler.batch.CompilationUnit;
23 import org.eclipse.jdt.internal.compiler.batch.FileSystem;
24 import org.eclipse.jdt.internal.compiler.batch.Main;
25 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
26 import org.eclipse.jdt.internal.compiler.env.AccessRestriction;
27 import org.eclipse.jdt.internal.compiler.env.IBinaryType;
28 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
29 import org.eclipse.jdt.internal.compiler.env.ISourceType;
30 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
31 import org.eclipse.jdt.internal.compiler.impl.ITypeRequestor;
32 import org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment;
33 import org.eclipse.jdt.internal.compiler.lookup.PackageBinding;
34 import org.eclipse.jdt.internal.compiler.parser.Parser;
35 import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
37 public class TranslationCollector {
38 static class TranslationEntry implements Comparable<TranslationEntry> {
42 public TranslationEntry(String text, String occur) {
46 public List<String> getOccur() {
48 return Arrays.asList(occur1);
52 public void add(String t) {
54 occur = new ArrayList<>(Arrays.asList(occur1));
59 public int compareTo(TranslationEntry o) {
60 int i = occur1.compareTo(o.occur1);
65 return text.compareTo(o.text);
69 private HashMap<String, TranslationEntry> translations = new HashMap<>();
71 public final File base;
73 public TranslationCollector(File base, File conf) {
75 taint = new LinkedList<>();
76 for (String s : new FileIterable(conf)) {
77 taint.add(TaintSource.parseTaint(s));
80 public void run(File out) throws IOException {
85 .println("Total Translatable Strings: " + translations.size());
86 TreeSet<TranslationEntry> trs = new TreeSet<>(translations.values());
87 writePOFile(out, trs);
91 public void add(String text, String line) {
92 TranslationEntry i = translations.get(text);
94 translations.put(text, new TranslationEntry(text, line));
100 private void scanCode(LinkedList<TaintSource> taint) throws Error {
101 PrintWriter out = new PrintWriter(System.out);
102 Main m = new Main(out, out, false, null, null);
104 new File(new File(new File(base, "src"), "org"), "cacert"),
105 new LinkedList<File>(), ".java").toArray(new File[0]);
106 String[] t = new String[fs.length + 3];
108 t[1] = new File(base, "bin").getAbsolutePath();
110 for (int i = 0; i < fs.length; i++) {
111 t[i + 3] = fs[i].getAbsolutePath();
114 FileSystem environment = m.getLibraryAccess();
115 CompilerOptions compilerOptions = new CompilerOptions(m.options);//new HashMap<>());//m.options);
116 compilerOptions.performMethodsFullRecovery = false;
117 compilerOptions.performStatementsRecovery = false;
119 compilerOptions.sourceLevel = ClassFileConstants.JDK1_7;
120 compilerOptions.complianceLevel = ClassFileConstants.JDK1_7;
121 compilerOptions.originalComplianceLevel = ClassFileConstants.JDK1_7;
123 ProblemReporter pr = new ProblemReporter(m.getHandlingPolicy(),
124 compilerOptions, m.getProblemFactory());
125 ITypeRequestor tr = new ITypeRequestor() {
128 public void accept(ISourceType[] sourceType,
129 PackageBinding packageBinding,
130 AccessRestriction accessRestriction) {
131 throw new IllegalStateException("source type not implemented");
135 public void accept(IBinaryType binaryType,
136 PackageBinding packageBinding,
137 AccessRestriction accessRestriction) {
138 le.createBinaryTypeFrom(binaryType, packageBinding,
143 public void accept(ICompilationUnit unit,
144 AccessRestriction accessRestriction) {
145 throw new IllegalStateException(
146 "compilation unit not implemented");
149 le = new LookupEnvironment(tr, compilerOptions, pr, environment);
150 Parser parser = new Parser(pr,
151 compilerOptions.parseLiteralExpressionsAsConstants);
152 CompilationUnit[] sourceUnits = m.getCompilationUnits();
153 CompilationUnitDeclaration[] parsedUnits = new CompilationUnitDeclaration[sourceUnits.length];
154 for (int i = 0; i < parsedUnits.length; i++) {
156 CompilationResult unitResult = new CompilationResult(
157 sourceUnits[i], i, parsedUnits.length,
158 compilerOptions.maxProblemsPerUnit);
159 CompilationUnitDeclaration parsedUnit = parser.parse(
160 sourceUnits[i], unitResult);
161 le.buildTypeBindings(parsedUnit, null /*no access restriction*/);
162 parsedUnits[i] = parsedUnit;
164 le.completeTypeBindings();
165 for (int i = 0; i < parsedUnits.length; i++) {
166 CompilationUnitDeclaration parsedUnit = parsedUnits[i];
168 parser.getMethodBodies(parsedUnit);
169 parsedUnit.scope.faultInTypes();
170 parsedUnit.scope.verifyMethods(le.methodVerifier());
171 parsedUnit.resolve();
173 for (int i = 0; i < parsedUnits.length; i++) {
174 CompilationUnitDeclaration parsedUnit = parsedUnits[i];
175 if (parsedUnit.compilationResult.problems != null) {
177 for (int c = 0; c < parsedUnit.compilationResult.problemCount; c++) {
178 CategorizedProblem problem = parsedUnit.compilationResult.problems[c];
179 if (problem.isError()) {
182 if (OUTPUT_WARNINGS || problem.isError()) {
183 System.out.println(problem);
184 StringBuilder prob = new StringBuilder();
185 prob.append(parsedUnit.compilationResult.fileName);
187 prob.append(problem.getSourceLineNumber());
188 System.out.println(prob.toString());
196 if (parsedUnit.types == null) {
197 System.out.println("No types");
200 TranslationCollectingVisitor v = new TranslationCollectingVisitor(
202 taint.toArray(new TaintSource[taint.size()]), this);
203 for (TypeDeclaration td : parsedUnit.types) {
204 td.traverse(v, td.scope);
207 parsedUnits[i] = parsedUnit;
210 private void scanTemplates() throws UnsupportedEncodingException,
211 FileNotFoundException {
213 new File(new File(new File(base, "src"), "org"), "cacert"),
214 new LinkedList<File>(), ".templ").toArray(new File[0]);
215 for (File file : ts) {
216 Template t = new Template(new InputStreamReader(
217 new FileInputStream(file), "UTF-8"));
218 LinkedList<String> i = new LinkedList<String>();
219 t.addTranslations(i);
220 for (String string : i) {
222 file.getAbsolutePath().substring(
223 base.getAbsolutePath().length() + 1)
229 static LookupEnvironment le;
230 private static final boolean OUTPUT_WARNINGS = false;
232 private LinkedList<TaintSource> taint;
233 public static void main(String[] args) throws IOException {
234 new TranslationCollector(new File(args[1]), new File(args[0]))
235 .run(new File(args[2]));
238 public static void writePOFile(File target,
239 Collection<TranslationEntry> strings) throws IOException {
240 PrintWriter out = new PrintWriter(target);
241 for (TranslationEntry s : strings) {
243 for (String st : s.getOccur()) {
247 out.println("msgid \""
248 + s.text.replace("\\", "\\\\").replace("\"", "\\\"") + "\"");
249 out.println("msgstr \"\"");
255 private static List<File> recurse(File file, List<File> toAdd, String pt) {
256 if (file.isDirectory()) {
257 for (File f : file.listFiles()) {
258 recurse(f, toAdd, pt);
261 if (file.getName().endsWith(pt)) {