View Javadoc

1   
2   /*
3    * Copyright (C) 2005 by Arno Schumacher
4    *
5    * This file is part of net.sourceforge.servletspy
6    *
7    * net.sourceforge.servletspy is free software; you can redistribute
8    * it and/or modify it under the terms of the GNU General Public 
9    * License as published by the Free Software Foundation; either 
10   * version 2, or (at your option) any later version.
11   *
12   * net.sourceforge.servletspy is distributed in the hope that it will
13   * be useful, but WITHOUT ANY WARRANTY; without even the implied 
14   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
15   * See the GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA 
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( /* new import com.thoughtworks.xstream.io.xml.DomDriver() */ );
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  }