/
HTMLogger.java
303 lines (273 loc) · 11.1 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package net.ME1312.SubServers.Console.Library;
import net.ME1312.SubServers.Bungee.Library.Container;
import org.fusesource.jansi.AnsiOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.LinkedList;
/**
* HTML Log Stream Class
*/
public class HTMLogger extends AnsiOutputStream {
private static final String[] ANSI_COLOR_MAP = new String[]{"000000", "cd0000", "25bc24", "d7d700", "0000c3", "be00be", "00a5dc", "cccccc"};
private static final String[] ANSI_BRIGHT_COLOR_MAP = new String[]{"808080", "ff0000", "31e722", "ffff00", "0000ff", "ff00ff", "00c8ff", "ffffff"};
private static final byte[] BYTES_NBSP = " ".getBytes();
private static final byte[] BYTES_QUOT = """.getBytes();
private static final byte[] BYTES_AMP = "&".getBytes();
private static final byte[] BYTES_LT = "<".getBytes();
private static final byte[] BYTES_GT = ">".getBytes();
private LinkedList<String> closingAttributes = new LinkedList<String>();
private OutputStream raw;
protected boolean ansi = true;
private boolean underline = false;
private boolean strikethrough = false;
/**
* Parse data from an OutputStream
*
* @param raw OutputStream
* @return HTMLogger
*/
public static HTMLogger wrap(OutputStream raw) {
return wrap(raw, new HTMConstructor<HTMLogger>() {
@Override
public HTMLogger construct(OutputStream raw1, OutputStream wrapped) {
return new HTMLogger(raw1, wrapped);
}
});
}
/**
* Parse data from an OutputStream
*
* @param raw OutputStream
* @param constructor Implementing Constructor
* @param <T> Logger Type
* @return HTMLogger
*/
public static <T extends HTMLogger> T wrap(final OutputStream raw, HTMConstructor<T> constructor) {
final Container<T> html = new Container<T>(null);
html.set(constructor.construct(raw, new OutputStream() {
private boolean nbsp = false;
@Override
public void write(int data) throws IOException {
if (data == 32) {
if (nbsp) raw.write(BYTES_NBSP);
else raw.write(data);
nbsp = !nbsp;
} else {
nbsp = false;
switch(data) {
case 34:
raw.write(BYTES_QUOT);
break;
case 38:
raw.write(BYTES_AMP);
break;
case 60:
raw.write(BYTES_LT);
break;
case 62:
raw.write(BYTES_GT);
break;
case 10:
html.get().closeAttributes();
default:
raw.write(data);
}
}
}
}));
return html.get();
}
protected HTMLogger(final OutputStream raw, OutputStream wrapped) {
super(wrapped);
this.raw = raw;
}
public interface HTMConstructor<T extends HTMLogger> {
T construct(OutputStream raw, OutputStream wrapped);
}
private void write(String s) throws IOException {
raw.write(s.getBytes());
}
private void writeAttribute(String s) throws IOException {
write("<" + s + ">");
closingAttributes.add(0, s);
}
protected 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(closingAttributes);
for (String attr : closingAttributes) {
if (attr.toLowerCase().startsWith(s.toLowerCase())) {
for (String a : unclosedAttributes) {
closedAttributes.add(0, a);
write("</" + a.split(" ", 2)[0] + ">");
}
closingAttributes.removeFirstOccurrence(attr);
unclosedAttributes.clear();
write("</" + attr.split(" ", 2)[0] + ">");
} else {
unclosedAttributes.add(attr);
}
}
for (String attr : closedAttributes) {
write("<" + attr + ">");
}
}
protected void closeAttributes() throws IOException {
for (String attr : closingAttributes) {
write("</" + attr.split(" ", 2)[0] + ">");
}
underline = false;
strikethrough = false;
closingAttributes.clear();
}
@Override
protected void processDeleteLine(int amount) throws IOException {
super.processDeleteLine(amount);
}
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:
closeAttribute("b");
writeAttribute("b");
break;
case 3:
closeAttribute("i");
writeAttribute("i");
break;
case 4:
closeAttribute("span class=\"ansi-decoration");
underline = true;
writeAttribute("span class=\"ansi-decoration\" style=\"text-decoration: " + parseTextDecoration() + ";\"");
break;
case 9:
closeAttribute("span class=\"ansi-decoration");
strikethrough = true;
writeAttribute("span class=\"ansi-decoration\" style=\"text-decoration: " + parseTextDecoration() + ";\"");
break;
case 22:
closeAttribute("b");
break;
case 23:
closeAttribute("i");
break;
case 24:
closeAttribute("span class=\"ansi-decoration");
underline = false;
writeAttribute("span class=\"ansi-decoration\" style=\"text-decoration: " + parseTextDecoration() + ";\"");
break;
case 29:
closeAttribute("span class=\"ansi-decoration");
strikethrough = false;
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
closeAttribute("a");
writeAttribute("a href=\"" + arg + "\" target=\"_blank\"");
break;
case 99901:
closeAttribute("a");
break;
}
} catch (Exception e) {}
}
@Override
protected void processAttributeRest() throws IOException {
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 {
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();
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();
writeAttribute("span class=\"ansi-foreground\" style=\"color: #" + parse8BitColor(index) + ";\"");
}
}
@Override
protected void processSetForegroundColorExt(int r, int g, int b) throws IOException {
if (ansi) {
processDefaultTextColor();
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 {
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();
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();
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();
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 {
closeAttributes();
super.close();
raw.close();
}
}