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