View Javadoc

1   
2   
3   /*
4    * Copyright (C) 2005 by Arno Schumacher
5    *
6    * This file is part of net.sourceforge.servletspy
7    *
8    * net.sourceforge.servletspy is free software; you can redistribute
9    * it and/or modify it under the terms of the GNU General Public 
10   * License as published by the Free Software Foundation; either 
11   * version 2, or (at your option) any later version.
12   *
13   * net.sourceforge.servletspy is distributed in the hope that it will
14   * be useful, but WITHOUT ANY WARRANTY; without even the implied 
15   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
16   * See the GNU General Public License for more details.
17   *
18   * You should have received a copy of the GNU General Public License
19   * along with this program; if not, write to the Free Software
20   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA 
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       * (non-Javadoc)
44       * @see net.sourceforge.servletspy.IContextHandler#handle(net.sourceforge.servletspy.IContext)
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       * (non-Javadoc)
85       *
86       * @see net.sourceforge.servletspy.ILifecycle#init(net.sourceforge.servletspy.config.Param[])
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      * (non-Javadoc)
105      *
106      * @see net.sourceforge.servletspy.ILifecycle#destroy()
107      */
108     public void destroy() {
109     }
110 }