forked from DrJavaAtRice/drjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTabPrintWriter.java
More file actions
111 lines (98 loc) · 3.19 KB
/
Copy pathTabPrintWriter.java
File metadata and controls
111 lines (98 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package edu.rice.cs.drjava.model.repl.types;
/**
* An extension of PrintWriter to support indenting levels.
*/
public class TabPrintWriter extends java.io.PrintWriter {
private final int _tabSize;
private int _numSpaces;
public TabPrintWriter(java.io.Writer writer, int tabSize) {
super(writer);
_tabSize = tabSize;
_numSpaces = 0;
}
/** ups indent for any future new lines. */
public void indent() { _numSpaces += _tabSize; }
public void unindent() { _numSpaces -= _tabSize; }
public void startLine(java.lang.Object s) {
startLine();
print(s);
}
public void startLine() {
println();
for (int i = 0; i < _numSpaces; i++) { print(' '); }
}
public void printEscaped(java.lang.Object o) { printEscaped(o.toString()); }
/** Print a string in Java source-compatible escaped form. All control characters
* (including line breaks) and quoting punctuation are escaped with a backslash.
*/
public void printEscaped(java.lang.String s) {
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
switch (c) {
case '\b': print("\\b"); break;
case '\t': print("\\t"); break;
case '\n': print("\\n"); break;
case '\f': print("\\f"); break;
case '\r': print("\\r"); break;
case '\"': print("\\\""); break;
case '\'': print("\\\'"); break;
case '\\': print("\\\\"); break;
default:
if (c < ' ' || c == '\u007f') {
print('\\');
// must use 3 digits so that unescaping doesn't consume too many chars ("\12" vs. "\0012")
java.lang.String num = java.lang.Integer.toOctalString(c);
while (num.length() < 3) num = "0" + num;
print(num);
}
else { print(c); }
break;
}
}
}
/** Conditionally print the serialzed form of the given object. */
public void printPossiblyEscaped(java.lang.String s, boolean lossless) {
if (lossless) {
print("\"");
printEscaped(s);
print("\"");
} else {
print(s);
}
}
/** Print the serialized form of the given object as a hexadecimal number.
* @throws RuntimeException If the object is not serializable.
*/
public void printSerialized(java.lang.Object o) {
java.io.ByteArrayOutputStream bs = new java.io.ByteArrayOutputStream();
try {
java.io.ObjectOutputStream objOut = new java.io.ObjectOutputStream(bs);
try { objOut.writeObject(o); }
finally { objOut.close(); }
}
catch (java.io.IOException e) { throw new java.lang.RuntimeException(e); }
printBytes(bs.toByteArray());
}
/** Conditionally print the serialzed form of the given object. */
public void printPossiblySerialized(java.lang.Object o, boolean lossless) {
if (lossless) {
printSerialized(o);
print(" ");
printEscaped(o);
} else {
print(o);
}
}
private void printBytes(byte[] bs) {
for (byte b : bs) {
int unsigned = ((int)b) & 0xff;
java.lang.String num = java.lang.Integer.toHexString(unsigned);
if (num.length() == 1) print("0");
print(num);
}
}
public void startObject(String name) {
print(name);
indent();
}
}