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.config;
23
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.io.Reader;
27 import java.io.UnsupportedEncodingException;
28
29 import com.thoughtworks.xstream.XStream;
30
31
32 /***
33 * @author arno schumacher
34 */
35 public final class ServletSpy {
36 private static final String DEFAULT_ENCODING = "UTF-8";
37
38 protected Handler[] spyHandlers;
39
40 protected String description;
41
42 public static ServletSpy fromXML(final InputStream stream, final String encoding)
43 throws UnsupportedEncodingException {
44 if (stream == null) {
45 throw new NullPointerException("Can not process a null InputStream");
46 }
47 return fromXML(new InputStreamReader(stream,
48 encoding == null ? DEFAULT_ENCODING : encoding));
49 }
50
51 static ServletSpy fromXML(final Reader reader) {
52 XStream xstream = new XStream(
53 xstream.alias("servletSpy", ServletSpy.class);
54 xstream.alias("handler", Handler.class);
55 xstream.alias("param", Param.class);
56 ServletSpy servletSpy = new ServletSpy();
57 xstream.fromXML(reader, servletSpy);
58
59 return servletSpy;
60 }
61
62 public ServletSpy(final Handler[] handlers, final String des) {
63 this.spyHandlers = handlers;
64 this.description = des;
65 }
66
67 private ServletSpy() {
68 }
69
70 public String getDescription() {
71 return description;
72 }
73
74 public Handler[] getHandlers() {
75 return spyHandlers == null ? new Handler[0] : spyHandlers;
76 }
77
78 public String toString() {
79 StringBuffer sb = new StringBuffer();
80 sb.append('{');
81 sb.append("description=");
82 sb.append(description);
83 sb.append(", contextHandler[]={");
84 for (int i = 0; spyHandlers != null && i < spyHandlers.length; i++) {
85 sb.append('[');
86 sb.append(i);
87 sb.append("]=");
88 sb.append(spyHandlers[i]);
89 if (i + 1 != spyHandlers.length) {
90 sb.append(", ");
91 }
92 }
93 sb.append('}');
94 return sb.toString();
95 }
96 }