1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.tonicsystems.jarjar;
18
19 import com.tonicsystems.jarjar.util.*;
20 import java.io.*;
21 import org.objectweb.asm.*;
22
23 class StringDumper
24 {
25 public StringDumper() {
26 }
27
28 public void run(String classPath, PrintWriter pw) throws IOException {
29 StringReader stringReader = new DumpStringReader(pw);
30 ClassPathIterator cp = new ClassPathIterator(classPath);
31 try {
32 while (cp.hasNext()) {
33 ClassPathEntry entry = cp.next();
34 InputStream in = entry.openStream();
35 try {
36 new ClassReader(in).accept(stringReader, 0);
37 } catch (Exception e) {
38 System.err.println("Error reading " + entry.getName() + ": " + e.getMessage());
39 } finally {
40 in.close();
41 }
42 pw.flush();
43 }
44 } catch (RuntimeIOException e) {
45 throw (IOException)e.getCause();
46 } finally {
47 cp.close();
48 }
49 }
50
51 private static class DumpStringReader extends StringReader
52 {
53 private final PrintWriter pw;
54 private String className;
55
56 public DumpStringReader(PrintWriter pw) {
57 this.pw = pw;
58 }
59
60 public void visitString(String className, String value, int line) {
61 if (value.length() > 0) {
62 if (!className.equals(this.className)) {
63 this.className = className;
64 pw.println(className.replace('/', '.'));
65 }
66 pw.print("\t");
67 if (line >= 0)
68 pw.print(line + ": ");
69 pw.print(escapeStringLiteral(value));
70 pw.println();
71 }
72 }
73 };
74
75 private static String escapeStringLiteral(String value) {
76 StringBuilder sb = new StringBuilder();
77 sb.append("\"");
78 char[] chars = value.toCharArray();
79 for (int i = 0, size = chars.length; i < size; i++) {
80 char ch = chars[i];
81 switch (ch) {
82 case '\n': sb.append("\\n"); break;
83 case '\r': sb.append("\\r"); break;
84 case '\b': sb.append("\\b"); break;
85 case '\f': sb.append("\\f"); break;
86 case '\t': sb.append("\\t"); break;
87 case '\"': sb.append("\\\""); break;
88 case '\\': sb.append("\\\\"); break;
89 default:
90 sb.append(ch);
91 }
92 }
93 sb.append("\"");
94 return sb.toString();
95 }
96 }