1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.sourceforge.servletspy.handler.container;
23
24 import java.lang.reflect.Array;
25
26 import net.sourceforge.servletspy.IContext;
27 import net.sourceforge.servletspy.IContextHandler;
28 import net.sourceforge.servletspy.config.Param;
29
30 /***
31 * @author arno schumacher
32 */
33 public final class ArrayTransformer implements IContextHandler {
34 public static final int MAX_ARRAY_SIZE = 64;
35
36 public static final String PARAM_ARRAY_SIZE = "MAX_ARRAY_SIZE";
37
38 private int maxArraySize = MAX_ARRAY_SIZE;
39
40
41
42
43
44 public void handle(final IContext context) throws Exception {
45 if (context.getSubject().getClass().isArray()) {
46 final int size = Array.getLength(context.getSubject());
47
48 if (size > maxArraySize) {
49 context.add(" [].length", String.valueOf(size)
50 + " / ouput truncated");
51 } else {
52 context.add(" [].length", String.valueOf(size));
53 }
54
55 final int stringDigits = String.valueOf(
56 Math.min(maxArraySize, size)).length();
57 final char[] prefixChars = new char[stringDigits];
58 for (int i = 0; i < stringDigits; i++) {
59 prefixChars[i] = ' ';
60 }
61 final String prefix = new String(prefixChars);
62
63 for (int k = 0; k < size && k < maxArraySize; k++) {
64 final String indexRaw = prefix + k;
65 final String index = indexRaw.substring(indexRaw.length()
66 - stringDigits);
67 final String key = " [" + index + "]";
68 Object prop = Array.get(context.getSubject(), k);
69 context.add(key, prop);
70 }
71 } else {
72 context.proceed();
73 }
74 }
75
76
77
78
79
80 public void destroy() {
81 }
82
83
84
85
86
87 public void init(final Param[] params) {
88 for (int i = 0; i < params.length; i++) {
89 if (PARAM_ARRAY_SIZE.equals(params[i].getName())) {
90 int arraySize = Integer.MIN_VALUE;
91 try {
92 arraySize = Integer.parseInt(params[i].getValue());
93 } catch (Exception e) {
94 }
95 if (arraySize >= 0) {
96 maxArraySize = arraySize;
97 }
98 }
99 }
100 }
101 }