-
Notifications
You must be signed in to change notification settings - Fork 188
/
PrettyPrinter.java
454 lines (417 loc) · 16.4 KB
/
PrettyPrinter.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
* Copyright (c) 2010-2018 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.util;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
/**
* @author semancik
*
*/
public class PrettyPrinter {
private static final int BYTE_ARRAY_MAX_LEN = 64;
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyy-mm-dd hh:mm:ss");
private static String defaultNamespacePrefix = null;
private static List<Class<?>> prettyPrinters = new CopyOnWriteArrayList<>();
public static void setDefaultNamespacePrefix(String prefix) {
defaultNamespacePrefix = prefix;
}
static String prettyPrintElementAsProperty(Object element) {
if (element == null) {
return "null";
}
StringBuilder sb = new StringBuilder("<");
QName elementName = JAXBUtil.getElementQName(element);
sb.append(prettyPrint(elementName));
sb.append(">");
if (element instanceof Element) {
Element domElement = (Element)element;
// TODO: this is too simplistic, expand later
sb.append(domElement.getTextContent());
} else {
sb.append(element.toString());
}
return sb.toString();
}
public static Object prettyPrintLazily(Collection<?> collection) {
if (collection == null) {
return null;
}
return new Object() {
@Override
public String toString() {
return prettyPrint(collection);
}
};
}
public static String prettyPrint(Collection<?> collection) {
return prettyPrint(collection, 0);
}
public static String prettyPrint(Collection<?> collection, int maxItems) {
if (collection == null) {
return null;
}
StringBuilder sb = new StringBuilder(DebugUtil.getCollectionOpeningSymbol(collection));
Iterator<?> iterator = collection.iterator();
int items = 0;
while (iterator.hasNext()) {
sb.append(prettyPrint(iterator.next()));
items++;
if (iterator.hasNext()) {
sb.append(",");
if (maxItems != 0 && items >= maxItems) {
sb.append("...");
break;
}
}
}
sb.append(DebugUtil.getCollectionClosingSymbol(collection));
return sb.toString();
}
public static String prettyPrint(QName qname) {
if (qname == null) {
return "null";
}
if (qname.getNamespaceURI() != null) {
if (qname.getNamespaceURI().equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
return "{"+DOMUtil.NS_W3C_XML_SCHEMA_PREFIX+":}"+qname.getLocalPart();
} else if (defaultNamespacePrefix != null && qname.getNamespaceURI().startsWith(defaultNamespacePrefix)) {
return "{..."+qname.getNamespaceURI().substring(defaultNamespacePrefix.length())+"}"+qname.getLocalPart();
}
}
return qnameToString(qname); // avoiding qname.toString because recursive call to prettyPrint from ItemName.toString
}
public static String qnameToString(QName name) {
if (name.getNamespaceURI().equals(XMLConstants.NULL_NS_URI)) {
return name.getLocalPart();
} else {
return "{" + name.getNamespaceURI() + "}" + name.getLocalPart();
}
}
/**
* Assumes that all elements in the lists have the same QName
*
* @param list
* @return
*/
public static String prettyPrint(List<Element> list) {
if (list == null) {
return "null";
}
StringBuilder sb = new StringBuilder();
if (list.size() > 0) {
Element el0 = list.get(0);
QName elQName;
if (el0.getPrefix() != null) {
elQName = new QName(el0.getNamespaceURI(), el0.getLocalName(), el0.getPrefix());
} else {
elQName = new QName(el0.getNamespaceURI(), el0.getLocalName());
}
sb.append(elQName);
sb.append("[");
Iterator<Element> iterator = list.iterator();
while (iterator.hasNext()) {
// TODO: improve for non-strings
Element el = iterator.next();
sb.append(prettyPrint(el, false));
if (iterator.hasNext()) {
sb.append(",");
}
sb.append("]");
}
} else {
sb.append("[]");
}
return sb.toString();
}
public static String prettyPrint(Node node) {
if (node instanceof Element) {
return prettyPrint((Element) node);
}
// TODO: Better print
return "Node:" + node.getNodeName();
}
public static String prettyPrint(Element element) {
return prettyPrint(element, true);
}
public static String prettyPrint(Element element, boolean displayTag) {
if (element == null) {
return "null";
}
StringBuilder sb = new StringBuilder();
if (displayTag) {
sb.append("<");
if (element.getLocalName() != null) {
sb.append(prettyPrint(new QName(element.getNamespaceURI(), element.getLocalName())));
} else {
sb.append("<null>");
}
sb.append(">");
}
NamedNodeMap attributes = element.getAttributes();
for (int i = 0; i < attributes.getLength(); i++) {
Node attr = attributes.item(i);
if ("xmlns".equals(attr.getPrefix())) {
// Don't display XML NS declarations
// they are too long for prettyPrint
continue;
}
if ((attr.getPrefix() == null || attr.getPrefix().isEmpty())
&& "xmlns".equals(attr.getLocalName())) {
// Skip default ns declaration as well
continue;
}
sb.append("@");
sb.append(attr.getLocalName());
sb.append("=");
sb.append(attr.getTextContent());
if (i < (attributes.getLength() - 1)) {
sb.append(",");
}
}
if (attributes.getLength() > 0) {
sb.append(":");
}
StringBuilder content = new StringBuilder();
Node child = element.getFirstChild();
while (child != null) {
if (child.getNodeType() == Node.TEXT_NODE) {
content.append(((Text) child).getTextContent());
} else if (child.getNodeType() == Node.COMMENT_NODE) {
// just ignore this
} else {
content = new StringBuilder("[complex content]");
break;
}
child = child.getNextSibling();
}
sb.append(content);
return sb.toString();
}
public static String prettyPrint(Date date) {
if (date == null) {
return "null";
}
return dateFormat.format(date);
}
public static String prettyPrint(Object[] value) {
return prettyPrint(Arrays.asList(value));
}
public static String prettyPrint(byte[] value) {
StringBuilder sb = new StringBuilder();
sb.append("byte[");
int printlen = value.length;
if (printlen > BYTE_ARRAY_MAX_LEN) {
printlen = BYTE_ARRAY_MAX_LEN;
}
for(int i=0; i < printlen; i++) {
sb.append(String.format("%02x", value[i] & 0xff));
}
if (value.length > BYTE_ARRAY_MAX_LEN) {
sb.append("... ");
sb.append(value.length);
sb.append(" bytes total");
}
sb.append("]");
return sb.toString();
}
public static String prettyPrint(Object value) {
if (value == null) {
return "null";
}
String out = null;
if (value instanceof JAXBElement) {
Object elementValue = ((JAXBElement)value).getValue();
out = tryPrettyPrint(elementValue);
if (out != null) {
return ("JAXBElement("+((JAXBElement)value).getName()+","+out+")");
}
}
out = tryPrettyPrint(value);
if (out == null) {
out = value.toString();
}
return out;
}
private static String tryPrettyPrint(Object value) {
if (value instanceof Class) {
Class<?> c = (Class<?>)value;
if (c.getPackage().getName().equals("com.evolveum.midpoint.xml.ns._public.common.common_3")) {
return c.getSimpleName();
}
return c.getName();
}
if (value instanceof Collection) {
return PrettyPrinter.prettyPrint((Collection<?>)value);
}
if (value.getClass().isArray()) {
Class<?> cclass = value.getClass().getComponentType();
if (cclass.isPrimitive()) {
if (cclass == byte.class) {
return PrettyPrinter.prettyPrint((byte[])value);
} else {
// TODO: something better
}
} else {
return PrettyPrinter.prettyPrint((Object[])value);
}
}
if (value instanceof Node) {
// This is interface, won't catch it using reflection
return PrettyPrinter.prettyPrint((Node)value);
}
for (Class<?> prettyPrinterClass: prettyPrinters) {
String printerValue = tryPrettyPrint(value, prettyPrinterClass);
if (printerValue != null) {
return printerValue;
}
}
// Fallback to this class
return tryPrettyPrint(value, PrettyPrinter.class);
}
private static String tryPrettyPrint(Object value, Class<?> prettyPrinterClass) {
for (Method method : prettyPrinterClass.getMethods()) {
if (method.getName().equals("prettyPrint")) {
Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes.length == 1 && parameterTypes[0].equals(value.getClass())) {
try {
return (String)method.invoke(null, value);
} catch (IllegalArgumentException e) {
return "###INTERNAL#ERROR### Illegal argument: "+e.getMessage() + "; prettyPrint method for value "+value;
} catch (IllegalAccessException e) {
return "###INTERNAL#ERROR### Illegal access: "+e.getMessage() + "; prettyPrint method for value "+value;
} catch (InvocationTargetException e) {
return "###INTERNAL#ERROR### Illegal target: "+e.getMessage() + "; prettyPrint method for value "+value;
} catch (Throwable e) {
return "###INTERNAL#ERROR### "+e.getClass().getName()+": "+e.getMessage() + "; prettyPrint method for value "+value;
}
}
}
}
return null;
}
public static String debugDump(Object value, int indent) {
if (value == null) {
return "null";
}
String out = null;
if (value instanceof DebugDumpable) {
return ((DebugDumpable)value).debugDump(indent);
}
if (value instanceof Collection) {
return DebugUtil.debugDump((Collection)value, indent);
}
out = tryDebugDumpMethod(value, indent);
if (out != null) {
return out;
}
StringBuilder sb = DebugUtil.createIndentedStringBuilder(indent);
sb.append(prettyPrint(value));
return sb.toString();
}
private static String tryDebugDumpMethod(Object value, int indent) {
for (Class<?> prettyPrinterClass: prettyPrinters) {
String printerValue = tryDebugDumpMethod(value, indent, prettyPrinterClass);
if (printerValue != null) {
return printerValue;
}
}
// Fallback to this class
return tryDebugDumpMethod(value, indent, PrettyPrinter.class);
}
private static String tryDebugDumpMethod(Object value, int indent, Class<?> prettyPrinterClass) {
for (Method method : prettyPrinterClass.getMethods()) {
if (method.getName().equals("debugDump")) {
Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes.length == 2 && parameterTypes[0].equals(value.getClass())) {
try {
return (String)method.invoke(null, value, indent);
} catch (IllegalArgumentException e) {
return "###INTERNAL#ERROR### Illegal argument: "+e.getMessage() + "; debugDump method for value "+value;
} catch (IllegalAccessException e) {
return "###INTERNAL#ERROR### Illegal access: "+e.getMessage() + "; debugDump method for value "+value;
} catch (InvocationTargetException e) {
return "###INTERNAL#ERROR### Illegal target: "+e.getMessage() + "; debugDump method for value "+value;
} catch (Throwable e) {
return "###INTERNAL#ERROR### "+e.getClass().getName()+": "+e.getMessage() + "; debugDump method for value "+value;
}
}
}
}
return null;
}
public static void shortDump(StringBuilder sb, Object value) {
if (value == null) {
sb.append("null");
return;
}
if (value instanceof ShortDumpable) {
((ShortDumpable)value).shortDump(sb);
return;
}
if (value instanceof Collection) {
DebugUtil.shortDump(sb, (Collection)value);
return;
}
if (tryShortDumpMethod(sb, value)) {
return;
}
sb.append(prettyPrint(value));
}
private static boolean tryShortDumpMethod(StringBuilder sb, Object value) {
for (Class<?> prettyPrinterClass: prettyPrinters) {
if (tryShortDumpMethod(sb, value, prettyPrinterClass)) {
return true;
}
}
// Fallback to this class
return tryShortDumpMethod(sb, value, PrettyPrinter.class);
}
private static boolean tryShortDumpMethod(StringBuilder sb, Object value, Class<?> prettyPrinterClass) {
for (Method method : prettyPrinterClass.getMethods()) {
if (method.getName().equals("shortDump")) {
Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes.length == 2 && parameterTypes[0].equals(StringBuilder.class) && parameterTypes[1].equals(value.getClass())) {
try {
method.invoke(null, sb, value);
return true;
} catch (IllegalArgumentException e) {
sb.append("###INTERNAL#ERROR### Illegal argument: "+e.getMessage() + "; shortDump method for value "+value);
} catch (IllegalAccessException e) {
sb.append("###INTERNAL#ERROR### Illegal access: "+e.getMessage() + "; shortDump method for value "+value);
} catch (InvocationTargetException e) {
sb.append("###INTERNAL#ERROR### Illegal target: "+e.getMessage() + "; shortDump method for value "+value);
} catch (Throwable e) {
sb.append("###INTERNAL#ERROR### "+e.getClass().getName()+": "+e.getMessage() + "; shortDump method for value "+value);
}
}
}
}
return false;
}
public static void registerPrettyPrinter(Class<?> printerClass) {
prettyPrinters.add(printerClass);
}
// For diagnostics only
public static List<Class<?>> getPrettyPrinters() {
return prettyPrinters;
}
}