1
3 package jminusminus;
4
5
8
9 class Util {
10
11
19
20 public static String escapeSpecialXMLChars(String s) {
21 String escapedString = s.replaceAll("&", "&");
22 escapedString = escapedString.replaceAll("<", "<");
23 escapedString = escapedString.replaceAll(">", ">");
24 escapedString = escapedString.replaceAll("\"", """);
25 escapedString = escapedString.replaceAll("'", "'");
26 return escapedString;
27 }
28
29
37
38 public static String unescape(String s) {
39 StringBuffer b = new StringBuffer();
40 for (int i = 0; i < s.length(); i++) {
41 char c = s.charAt(i);
42 if (c == '\\') {
43 i++;
44 if (i >= s.length()) {
45 break;
46 }
47 c = s.charAt(i);
48 switch (c) {
49 case 'b':
50 b.append('\b');
51 break;
52 case 't':
53 b.append('\t');
54 break;
55 case 'n':
56 b.append('\n');
57 break;
58 case 'f':
59 b.append('\f');
60 break;
61 case 'r':
62 b.append('\r');
63 break;
64 case '"':
65 b.append('"');
66 break;
67 case '\'':
68 b.append('\'');
69 break;
70 case '\\':
71 b.append('\\');
72 break;
73 }
74 } else {
75 b.append(c);
76 }
77 }
78 return b.toString();
79 }
80
81 }
82
83
86
87 class PrettyPrinter {
88
89
90 private int indentWidth;
91
92
93 private int indent;
94
95
98
99 public PrettyPrinter() {
100 this(2);
101 }
102
103
109
110 public PrettyPrinter(int indentWidth) {
111 this.indentWidth = indentWidth;
112 indent = 0;
113 }
114
115
118
119 public void indentRight() {
120 indent += indentWidth;
121 }
122
123
126
127 public void indentLeft() {
128 if (indent > 0) {
129 indent -= indentWidth;
130 }
131 }
132
133
136
137 public void println() {
138 doIndent();
139 System.out.println();
140 }
141
142
148
149 public void println(String s) {
150 doIndent();
151 System.out.println(s);
152 }
153
154
160
161 public void print(String s) {
162 doIndent();
163 System.out.print(s);
164 }
165
166
174
175 public void printf(String format, Object... args) {
176 doIndent();
177 System.out.printf(format, args);
178 }
179
180
183
184 private void doIndent() {
185 for (int i = 0; i < indent; i++) {
186 System.out.print(" ");
187 }
188 }
189
190 }
191