KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tools > testrecorder > shared > Util


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18
19 package org.apache.beehive.netui.tools.testrecorder.shared;
20
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpSession JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 public class Util {
31
32     static final int REPORT_SESSION = 0;
33     static final int REPORT_REQUEST = 1;
34
35     private static final String JavaDoc LT = "<";
36     private static final String JavaDoc LT_END = "</";
37     private static final String JavaDoc GT = ">";
38
39     private Util() {
40     }
41
42     public static List JavaDoc getListOfKeys( Map JavaDoc map ) {
43         List JavaDoc keys = null;
44         if ( map != null ) {
45             Set JavaDoc keySet = map.keySet();
46             if ( keySet.size() > 0 ) {
47                 keys = new ArrayList JavaDoc( keySet.size() );
48                 keys.addAll( keySet );
49             }
50         }
51         if ( keys == null ) {
52             keys = new ArrayList JavaDoc();
53         }
54         return keys;
55     }
56
57     public static String JavaDoc toString( Map JavaDoc map ) {
58         Set JavaDoc keySet = null;
59         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
60
61         if ( map != null ) {
62             keySet = map.keySet();
63             sb.append( "Map(" + keySet.size() + ")[ " );
64         }
65         else {
66             sb.append( "Map[ " );
67         }
68
69         if ( map != null ) {
70             Iterator JavaDoc it = keySet.iterator();
71             Object JavaDoc key = null;
72             int i = 0;
73             for ( i = 0; it.hasNext(); i++ ) {
74                 key = it.next();
75                 if ( i != 0 ) {
76                     sb.append( "; " );
77                 }
78                 sb.append( "key(" + key + "), value( " + map.get( key ) + " )" );
79             }
80             if ( i == 0 ) {
81                 sb.append( "empty" );
82             }
83         }
84         else {
85             sb.append( "null" );
86         }
87
88         sb.append( " ]" );
89         return sb.toString();
90     }
91
92     public static String JavaDoc toString( Object JavaDoc[] objects ) {
93         if ( objects == null ) {
94             return null;
95         }
96         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( 16 * objects.length );
97         sb.append( "[ " );
98         int i = 0;
99         for ( i = 0; i < objects.length; i++ ) {
100             sb.append( ", [" + i + "]( " + objects[i] + " )" );
101         }
102         if ( i == 0 ) {
103             sb.append( "EMPTY" );
104         }
105         sb.append( " ]" );
106         return sb.toString();
107     }
108
109     public static String JavaDoc toString( Iterator JavaDoc it, int size ) {
110         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( 16 * size );
111         sb.append( "[ " );
112         int i = 0;
113         for ( i = 0; it.hasNext(); i++ ) {
114             sb.append( ", [" + i + "]( " + it.next() + " )" );
115         }
116         if ( i == 0 ) {
117             sb.append( "EMPTY" );
118         }
119         sb.append( " ]" );
120         return sb.toString();
121     }
122
123     public static final String JavaDoc genStartTag( final String JavaDoc tagName ) {
124         return LT + tagName + GT;
125     }
126
127     public static final String JavaDoc genStartTag( final String JavaDoc tagName, final String JavaDoc attributes ) {
128         if ( attributes == null ) {
129             return genStartTag( tagName );
130         }
131         return LT + tagName + " " + attributes + GT;
132     }
133
134     public static final String JavaDoc genEndTag( final String JavaDoc tagName ) {
135         return LT_END + tagName + GT;
136     }
137
138     public static StringBuffer JavaDoc reportSessionAttributes( StringBuffer JavaDoc sb,
139             HttpServletRequest JavaDoc request, String JavaDoc id, String JavaDoc separator ) {
140         return reportAttributes( sb, request, id, separator, REPORT_SESSION );
141     }
142
143     public static StringBuffer JavaDoc reportRequestAttributes( StringBuffer JavaDoc sb,
144             HttpServletRequest JavaDoc request, String JavaDoc id, String JavaDoc separator ) {
145         return reportAttributes( sb, request, id, separator, REPORT_REQUEST );
146     }
147
148     static StringBuffer JavaDoc reportAttributes( StringBuffer JavaDoc sb,
149             HttpServletRequest JavaDoc request, String JavaDoc id, String JavaDoc separator, int type ) {
150         HttpSession JavaDoc session = request.getSession();
151         Enumeration JavaDoc e = session.getAttributeNames();
152         String JavaDoc key = null;
153         Object JavaDoc value = null;
154         while ( e.hasMoreElements() ) {
155             key = (String JavaDoc) e.nextElement();
156             switch ( type ) {
157                 case REPORT_REQUEST:
158                     value = request.getAttribute( key );
159                     break;
160                 case REPORT_SESSION:
161                     value = request.getAttribute( key );
162                     break;
163                 default:
164                     throw new RuntimeException JavaDoc();
165             }
166             reportAttribute( sb, key, value, id, separator );
167         }
168         return sb;
169     }
170
171     public static StringBuffer JavaDoc reportAttribute( StringBuffer JavaDoc sb, String JavaDoc key,
172             Object JavaDoc value, String JavaDoc id, String JavaDoc separator ) {
173         sb.append( id + " [ key( " + key + " ), value(" + value.getClass().getName() + ")( " +
174                 value + " ) ]" + separator );
175         return sb;
176     }
177
178 }
179
Popular Tags