/
HTMLogger.java
272 lines (243 loc) · 9.98 KB
/
HTMLogger.java
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package net.ME1312.Galaxi.Engine.Library.Log;
import org.fusesource.jansi.AnsiOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.util.LinkedList;
/**
* HTML Log Stream Class
*/
public class HTMLogger extends AnsiOutputStream {
private final String[] ANSI_COLOR_MAP = new String[]{"000000", "cd0000", "25bc24", "d7d700", "0000c3", "be00be", "00a5dc", "cccccc"};
private final String[] ANSI_BRIGHT_COLOR_MAP = new String[]{"808080", "ff0000", "31e722", "ffff00", "0000ff", "ff00ff", "00c8ff", "ffffff"};
private final byte[] BYTES_NBSP = " ".getBytes();
private final byte[] BYTES_QUOT = """.getBytes();
private final byte[] BYTES_AMP = "&".getBytes();
private final byte[] BYTES_LT = "<".getBytes();
private final byte[] BYTES_GT = ">".getBytes();
private LinkedList<String> closingAttributes = new LinkedList<String>();
protected boolean ansi = true;
private boolean underline = false;
private boolean strikethrough = false;
/**
* Parse data from an OutputStream
*
* @param os OutputStream
*/
public HTMLogger(OutputStream os) {
super(os);
}
private void write(String s) throws IOException {
super.out.write(s.getBytes());
}
private void writeAttribute(String s) throws IOException {
this.write("<" + s + ">");
this.closingAttributes.add(0, s);
}
private void closeAttribute(String s) throws IOException {
LinkedList<String> closedAttributes = new LinkedList<String>();
LinkedList<String> closingAttributes = new LinkedList<String>();
LinkedList<String> unclosedAttributes = new LinkedList<String>();
closingAttributes.addAll(this.closingAttributes);
for (String attr : closingAttributes) {
if (attr.toLowerCase().startsWith(s.toLowerCase())) {
for (String a : unclosedAttributes) {
closedAttributes.add(0, a);
this.write("</" + a.split(" ", 2)[0] + ">");
}
this.closingAttributes.removeFirstOccurrence(attr);
unclosedAttributes.clear();
this.write("</" + attr.split(" ", 2)[0] + ">");
} else {
unclosedAttributes.add(attr);
}
}
for (String attr : closedAttributes) {
this.write("<" + attr + ">");
}
}
private void closeAttributes() throws IOException {
for (String attr : closingAttributes) {
this.write("</" + attr.split(" ", 2)[0] + ">");
}
this.underline = false;
this.strikethrough = false;
this.closingAttributes.clear();
}
@Override
protected void processDeleteLine(int amount) throws IOException {
super.processDeleteLine(amount);
}
private boolean nbsp = true;
@Override public void write(int data) throws IOException {
if (data == 32) {
if (nbsp) this.out.write(BYTES_NBSP);
else super.write(data);
nbsp = !nbsp;
} else {
nbsp = false;
switch(data) {
case 13:
break;
case 34:
this.out.write(BYTES_QUOT);
break;
case 38:
this.out.write(BYTES_AMP);
break;
case 60:
this.out.write(BYTES_LT);
break;
case 62:
this.out.write(BYTES_GT);
break;
default:
super.write(data);
}
}
}
private String parseTextDecoration() {
String dec = "";
if (underline) dec += " underline";
if (strikethrough) dec += " line-through";
if (dec.length() <= 0) dec += " none";
return dec.substring(1);
}
@Override
protected void processSetAttribute(int attribute) throws IOException {
if (ansi) switch(attribute) {
case 1:
this.closeAttribute("b");
this.writeAttribute("b");
break;
case 3:
this.closeAttribute("i");
this.writeAttribute("i");
break;
case 4:
this.closeAttribute("span class=\"ansi-decoration");
this.underline = true;
this.writeAttribute("span class=\"ansi-decoration\" style=\"text-decoration: " + parseTextDecoration() + ";\"");
break;
case 9:
this.closeAttribute("span class=\"ansi-decoration");
this.strikethrough = true;
this.writeAttribute("span class=\"ansi-decoration\" style=\"text-decoration: " + parseTextDecoration() + ";\"");
break;
case 22:
this.closeAttribute("b");
break;
case 23:
this.closeAttribute("i");
break;
case 24:
this.closeAttribute("span class=\"ansi-decoration");
this.underline = false;
this.writeAttribute("span class=\"ansi-decoration\" style=\"text-decoration: " + parseTextDecoration() + ";\"");
break;
case 29:
this.closeAttribute("span class=\"ansi-decoration");
this.strikethrough = false;
this.writeAttribute("span class=\"ansi-decoration\" style=\"text-decoration: " + parseTextDecoration() + ";\"");
break;
}
}
@Override
protected void processUnknownOperatingSystemCommand(int label, String arg) {
try {
if (ansi) switch (label) {
case 99900: // Galaxi Console Exclusives 99900-99999
this.closeAttribute("a");
this.writeAttribute("a href=\"" + URLDecoder.decode(arg, "UTF-8") + "\" target=\"_blank\"");
break;
case 99901:
this.closeAttribute("a");
break;
}
} catch (IOException e) {}
}
@Override
protected void processAttributeRest() throws IOException {
this.closeAttributes();
}
private String parse8BitColor(int color) throws IOException {
if (color < 8) {
return ANSI_COLOR_MAP[color];
} else if (color < 16) {
return ANSI_BRIGHT_COLOR_MAP[color - 8];
} else if (color < 232) {
int r = (int) (Math.floor((color - 16) / 36d) * (255 / 5));
int g = (int) (Math.floor(((color - 16) % 36d) / 6d) * (255 / 5));
int b = (int) (Math.floor(((color - 16) % 36d) % 6d) * (255 / 5));
return ((r >= 16)?"":"0") + Integer.toString(r, 16) + ((g >= 16)?"":"0") + Integer.toString(g, 16) + ((b >= 16)?"":"0") + Integer.toString(b, 16);
} else if (color < 256) {
int gray = (int) ((255 / 25d) * (color - 232 + 1));
return ((gray >= 16)?"":"0") + Integer.toString(gray, 16) + ((gray >= 16)?"":"0") + Integer.toString(gray, 16) + ((gray >= 16)?"":"0") + Integer.toString(gray, 16);
} else {
throw new IOException("Invalid 8 Bit Color: " + color);
}
}
@Override
protected void processDefaultTextColor() throws IOException {
this.closeAttribute("span class=\"ansi-foreground");
}
@Override
protected void processSetForegroundColor(int color) throws IOException {
processSetForegroundColor(color, false);
}
@Override
protected void processSetForegroundColor(int color, boolean bright) throws IOException {
if (ansi) {
processDefaultTextColor();
this.writeAttribute("span class=\"ansi-foreground\" style=\"color: #" + ((!bright)?ANSI_COLOR_MAP:ANSI_BRIGHT_COLOR_MAP)[color] + ";\"");
}
}
@Override
protected void processSetForegroundColorExt(int index) throws IOException {
if (ansi) {
processDefaultTextColor();
this.writeAttribute("span class=\"ansi-foreground\" style=\"color: #" + parse8BitColor(index) + ";\"");
}
}
@Override
protected void processSetForegroundColorExt(int r, int g, int b) throws IOException {
if (ansi) {
processDefaultTextColor();
this.writeAttribute("span class=\"ansi-foreground\" style=\"color: #" + ((r >= 16)?"":"0") + Integer.toString(r, 16) + ((g >= 16)?"":"0") + Integer.toString(g, 16) + ((b >= 16)?"":"0") + Integer.toString(b, 16) + ";\"");
}
}
@Override
protected void processDefaultBackgroundColor() throws IOException {
this.closeAttribute("span class=\"ansi-background");
}
@Override
protected void processSetBackgroundColor(int color) throws IOException {
processSetBackgroundColor(color, false);
}
@Override
protected void processSetBackgroundColor(int color, boolean bright) throws IOException {
if (ansi) {
processDefaultBackgroundColor();
this.writeAttribute("span class=\"ansi-background\" style=\"background-color: #" + ((!bright)?ANSI_COLOR_MAP:ANSI_BRIGHT_COLOR_MAP)[color] + ";\"");
}
}
@Override
protected void processSetBackgroundColorExt(int index) throws IOException {
if (ansi) {
processDefaultBackgroundColor();
this.writeAttribute("span class=\"ansi-background\" style=\"background-color: #" + parse8BitColor(index) + ";\"");
}
}
@Override
protected void processSetBackgroundColorExt(int r, int g, int b) throws IOException {
if (ansi) {
processDefaultBackgroundColor();
this.writeAttribute("span class=\"ansi-background\" style=\"background-color: #" + ((r >= 16)?"":"0") + Integer.toString(r, 16) + ((g >= 16)?"":"0") + Integer.toString(g, 16) + ((b >= 16)?"":"0") + Integer.toString(b, 16) + ";\"");
}
}
@Override
public void close() throws IOException {
this.closeAttributes();
super.close();
}
}