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.HashMap;
26 import java.util.Iterator;
27 import java.util.Map;
28
29 import net.sourceforge.servletspy.IContext;
30 import net.sourceforge.servletspy.IContextHandler;
31 import net.sourceforge.servletspy.config.Param;
32
33 /***
34 * @author arno schumacher
35 */
36 public final class MapTransformer implements IContextHandler {
37 private static final int MAX_MAP_SIZE = 64;
38
39 public static final String PARAM_MAP_SIZE = "MAX_MAP_SIZE";
40
41 private int maxMapSize = MAX_MAP_SIZE;
42
43 /*** for debugging purpose only ... */
44 private static final String[] KEYPREFIXES = new String[] {};
45
46
47
48
49
50
51
52
53
54
55 public void handle(final IContext context) throws Exception {
56 if (context.getSubject() instanceof Map) {
57 final Map current = (Map) context.getSubject();
58
59
60 final HashMap map = new HashMap( current );
61 final Iterator iterator = map.entrySet().iterator();
62
63 final int size = map.size();
64
65 if (size > maxMapSize) {
66 context.add(" Map[].size", String.valueOf(size)
67 + " / ouput truncated");
68 } else {
69 context.add(" Map[].size", String.valueOf(size));
70 }
71
72 for (int k = 0; iterator.hasNext() && k < maxMapSize; k++) {
73 Map.Entry entry = (Map.Entry) iterator.next();
74 Object key = entry.getKey();
75 Object value = entry.getValue();
76
77 if (key == null) {
78 key = "<null>";
79 }
80 for (int i = 0; i < KEYPREFIXES.length; i++) {
81 if (String.valueOf(key).startsWith(KEYPREFIXES[i])) {
82 value = String.valueOf(value);
83 }
84 }
85 context.add(" Map[" + key + "]", value);
86 }
87 } else {
88 context.proceed();
89 }
90 }
91
92
93
94
95
96
97 public void destroy() {
98 }
99
100
101
102
103
104
105 public void init(final Param[] params) {
106 for (int i = 0; i < params.length; i++) {
107 if (PARAM_MAP_SIZE.equals(params[i].getName())) {
108 int mapSize = Integer.MIN_VALUE;
109 try {
110 mapSize = Integer.parseInt(params[i].getValue());
111 } catch (Exception e) {
112 }
113 if (mapSize >= 0) {
114 maxMapSize = mapSize;
115 }
116 }
117 }
118 }
119 }