1 18 19 package org.apache.beehive.netui.tools.testrecorder.shared; 20 21 import javax.servlet.http.HttpServletRequest ; 22 import javax.servlet.http.HttpSession ; 23 import java.util.Iterator ; 24 import java.util.Enumeration ; 25 import java.util.Map ; 26 import java.util.Set ; 27 import java.util.List ; 28 import java.util.ArrayList ; 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 LT = "<"; 36 private static final String LT_END = "</"; 37 private static final String GT = ">"; 38 39 private Util() { 40 } 41 42 public static List getListOfKeys( Map map ) { 43 List keys = null; 44 if ( map != null ) { 45 Set keySet = map.keySet(); 46 if ( keySet.size() > 0 ) { 47 keys = new ArrayList ( keySet.size() ); 48 keys.addAll( keySet ); 49 } 50 } 51 if ( keys == null ) { 52 keys = new ArrayList (); 53 } 54 return keys; 55 } 56 57 public static String toString( Map map ) { 58 Set keySet = null; 59 StringBuffer sb = new StringBuffer (); 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 it = keySet.iterator(); 71 Object 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 toString( Object [] objects ) { 93 if ( objects == null ) { 94 return null; 95 } 96 StringBuffer sb = new StringBuffer ( 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 toString( Iterator it, int size ) { 110 StringBuffer sb = new StringBuffer ( 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 genStartTag( final String tagName ) { 124 return LT + tagName + GT; 125 } 126 127 public static final String genStartTag( final String tagName, final String attributes ) { 128 if ( attributes == null ) { 129 return genStartTag( tagName ); 130 } 131 return LT + tagName + " " + attributes + GT; 132 } 133 134 public static final String genEndTag( final String tagName ) { 135 return LT_END + tagName + GT; 136 } 137 138 public static StringBuffer reportSessionAttributes( StringBuffer sb, 139 HttpServletRequest request, String id, String separator ) { 140 return reportAttributes( sb, request, id, separator, REPORT_SESSION ); 141 } 142 143 public static StringBuffer reportRequestAttributes( StringBuffer sb, 144 HttpServletRequest request, String id, String separator ) { 145 return reportAttributes( sb, request, id, separator, REPORT_REQUEST ); 146 } 147 148 static StringBuffer reportAttributes( StringBuffer sb, 149 HttpServletRequest request, String id, String separator, int type ) { 150 HttpSession session = request.getSession(); 151 Enumeration e = session.getAttributeNames(); 152 String key = null; 153 Object value = null; 154 while ( e.hasMoreElements() ) { 155 key = (String ) 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 (); 165 } 166 reportAttribute( sb, key, value, id, separator ); 167 } 168 return sb; 169 } 170 171 public static StringBuffer reportAttribute( StringBuffer sb, String key, 172 Object value, String id, String separator ) { 173 sb.append( id + " [ key( " + key + " ), value(" + value.getClass().getName() + ")( " + 174 value + " ) ]" + separator ); 175 return sb; 176 } 177 178 } 179 | Popular Tags |