View Javadoc

1   /**
2    * Copyright 2007 Google Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  }