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.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       * (non-Javadoc)
42       * @see net.sourceforge.servletspy.IContextHandler#handle(net.sourceforge.servletspy.IContext)
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       * (non-Javadoc)
78       * @see net.sourceforge.servletspy.ILifecycle#destroy()
79       */
80      public void destroy() {
81      }
82  
83      /*
84       * (non-Javadoc)
85       * @see net.sourceforge.servletspy.ILifecycle#init(net.sourceforge.servletspy.config.Param[])
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 }