1 20 package org.enhydra.barracuda.plankton.data; 21 22 import java.util.*; 23 import java.io.*; 24 25 import org.apache.log4j.Logger; 26 27 import org.enhydra.barracuda.plankton.*; 28 29 32 public class CollectionsUtil { 33 34 private static byte[] sep = System.getProperty("line.separator").getBytes(); 35 36 41 public static void printStackTrace(Map map) { 42 printStackTrace(map, System.out); 43 } 44 45 51 public static void printStackTrace(Map map, Logger extLogger) { 52 printStackTrace(map, 0, extLogger, null); 53 } 54 55 61 public static void printStackTrace(Map map, OutputStream out) { 62 if (out==null) out = System.out; 63 printStackTrace(map, 0, null, out); 64 } 65 66 86 public static void printStackTrace(Map map, int depth, Logger extLogger, OutputStream out) { 87 if (depth<0) depth = 0; 88 if (depth>25) depth = 25; 89 String spaces = " "; 90 String inset = spaces.substring(0,depth*3); 91 if (map==null) { 92 print (extLogger, out, inset+"map: null"); 93 return; 94 } 95 print (extLogger, out, inset+map.getClass().getName() + "@" + Integer.toHexString(map.hashCode())); 96 print (extLogger, out, inset+" properties: "); 97 Object keys[] = map.keySet().toArray(); 98 if (keys!=null) { 99 int max = keys.length; 100 for (int i=0; i<max; i++) { 101 Object value = map.get(keys[i]); 102 if (value instanceof Map) { 103 print (extLogger, out, inset+" [k]:"+keys[i]+" [v]: ...(Map)"); 104 printStackTrace((Map) value, depth+2, extLogger, out); 105 } else if (value instanceof List) { 106 List l = (List) value; 107 print (extLogger, out, inset+" [k]:"+keys[i]+" [v]: ...(List - "+l.size()+" items)"); 108 if (l.size()>0) printStackTrace(l, depth+2, extLogger, out); 109 } else if (value instanceof Object []) { 110 Object [] objarray = (Object []) value; 111 print (extLogger, out, inset+" [k]:"+keys[i]+" [v]: ...(Object[] - "+objarray.length+" items)"); 112 if (objarray.length>0) printStackTrace(objarray, depth+2, extLogger, out); 113 } else if (value instanceof StateMap) { 114 print (extLogger, out, inset+" [k]:"+keys[i]+" [v]: ...(StateMap)"); 115 printStackTrace((StateMap) value, depth+2, extLogger, out); 116 } else { 117 print (extLogger, out, inset+" [k]:"+keys[i]+" [v]:"+value); 118 } 119 } 120 } 121 print (extLogger, out, inset+" /end properties"); 122 print (extLogger, out, inset+"/end @" + Integer.toHexString(map.hashCode())); 123 if (out!=null) try {out.flush();} catch (IOException ioe) {} 124 } 125 126 protected static void printStackTrace(StateMap map, int depth, Logger extLogger, OutputStream out) { 127 if (depth<0) depth = 0; 128 if (depth>25) depth = 25; 129 String spaces = " "; 130 String inset = spaces.substring(0,depth*3); 131 if (map==null) { 132 print (extLogger, out, inset+"map: null"); 133 return; 134 } 135 print (extLogger, out, inset+map.getClass().getName() + "@" + Integer.toHexString(map.hashCode())); 136 print (extLogger, out, inset+" properties: "); 137 List skeys = map.getStateKeys(); 138 if (skeys!=null && skeys.size()>0) { 139 Object keys[] = skeys.toArray(); 140 int max = keys.length; 141 for (int i=0; i<max; i++) { 142 Object value = map.getState(keys[i]); 143 if (value instanceof Map) { 144 print (extLogger, out, inset+" [k]:"+keys[i]+" [v]: ...(Map)"); 145 printStackTrace((Map) value, depth+2, extLogger, out); 146 } else if (value instanceof List) { 147 List l = (List) value; 148 print (extLogger, out, inset+" [k]:"+keys[i]+" [v]: ...(List - "+l.size()+" items)"); 149 if (l.size()>0) printStackTrace(l, depth+2, extLogger, out); 150 } else if (value instanceof Object []) { 151 Object [] objarray = (Object []) value; 152 print (extLogger, out, inset+" [k]:"+keys[i]+" [v]: ...(Object[] - "+objarray.length+" items)"); 153 if (objarray.length>0) printStackTrace(objarray, depth+2, extLogger, out); 154 } else if (value instanceof StateMap) { 155 print (extLogger, out, inset+" [k]:"+keys[i]+" [v]: ...(StateMap)"); 156 printStackTrace((StateMap) value, depth+2, extLogger, out); 157 } else { 158 print (extLogger, out, inset+" [k]:"+keys[i]+" [v]:"+value); 159 } 160 } 161 } 162 print (extLogger, out, inset+" /end properties"); 163 print (extLogger, out, inset+"/end @" + Integer.toHexString(map.hashCode())); 164 if (out!=null) try {out.flush();} catch (IOException ioe) {} 165 } 166 167 172 public static void printStackTrace(List list) { 173 printStackTrace(list, System.out); 174 } 175 176 182 public static void printStackTrace(List list, Logger extLogger) { 183 printStackTrace(list, 0, extLogger, null); 184 } 185 186 192 public static void printStackTrace(List list, OutputStream out) { 193 if (out==null) out = System.out; 194 printStackTrace(list, 0, null, out); 195 } 196 197 208 public static void printStackTrace(List list, int depth, Logger extLogger, OutputStream out) { 209 if (depth<0) depth = 0; 210 if (depth>25) depth = 25; 211 String spaces = " "; 212 String inset = spaces.substring(0,depth*3); 213 if (list==null) { 214 print (extLogger, out, inset+"list: null"); 215 return; 216 } 217 print (extLogger, out, inset+list.getClass().getName() + "@" + Integer.toHexString(list.hashCode())); 218 print (extLogger, out, inset+" items: "); 219 Object items[] = list.toArray(); 220 if (items!=null) { 221 int max = items.length; 222 for (int i=0; i<max; i++) { 223 if (items[i] instanceof Map) { 224 print (extLogger, out, inset+" ["+i+"]: ...(Map)"); 225 printStackTrace((Map) items[i], depth+2, extLogger, out); 226 } else if (items[i] instanceof List) { 227 List l = (List) items[i]; 228 print (extLogger, out, inset+" ["+i+"]: ...(List - "+l.size()+" items)"); 229 if (l.size()>0) printStackTrace(l, depth+2, extLogger, out); 230 } else if (items[i] instanceof Object []) { 231 Object [] objarray = (Object []) items[i]; 232 print (extLogger, out, inset+" ["+i+"]: ...(Object[] - "+objarray.length+" items)"); 233 if (objarray.length>0) printStackTrace(objarray, depth+2, extLogger, out); 234 } else if (items[i] instanceof StateMap) { 235 print (extLogger, out, inset+" ["+i+"]: ...(StateMap)"); 236 printStackTrace((StateMap) items[i], depth+2, extLogger, out); 237 } else { 238 print (extLogger, out, inset+" ["+i+"]: "+items[i]); 239 } 240 } 241 } 242 print (extLogger, out, inset+" /end items"); 243 print (extLogger, out, inset+"/end @" + Integer.toHexString(list.hashCode())); 244 if (out!=null) try {out.flush();} catch (IOException ioe) {} 245 } 246 247 252 public static void printStackTrace(Object [] objarray) { 253 printStackTrace(objarray, System.out); 254 } 255 256 262 public static void printStackTrace(Object [] objarray, Logger extLogger) { 263 printStackTrace(objarray, 0, extLogger, null); 264 } 265 266 272 public static void printStackTrace(Object [] objarray, OutputStream out) { 273 if (out==null) out = System.out; 274 printStackTrace(objarray, 0, null, out); 275 } 276 277 288 public static void printStackTrace(Object [] objarray, int depth, Logger extLogger, OutputStream out) { 289 if (depth<0) depth = 0; 290 if (depth>25) depth = 25; 291 String spaces = " "; 292 String inset = spaces.substring(0,depth*3); 293 print (extLogger, out, inset+objarray.getClass().getName() + "@" + Integer.toHexString(objarray.hashCode())); 294 print (extLogger, out, inset+" items: "); 295 if (objarray!=null) { 296 int max = objarray.length; 297 for (int i=0; i<max; i++) { 298 if (objarray[i] instanceof Map) { 299 print (extLogger, out, inset+" ["+i+"]: ...(Map)"); 300 printStackTrace((Map) objarray[i], depth+2, extLogger, out); 301 } else if (objarray[i] instanceof List) { 302 List l = (List) objarray[i]; 303 print (extLogger, out, inset+" ["+i+"]: ...(List - "+l.size()+" items)"); 304 if (l.size()>0) printStackTrace(l, depth+2, extLogger, out); 305 } else { 306 print (extLogger, out, inset+" ["+i+"]: "+objarray[i]); 307 } 308 } 309 } 310 print (extLogger, out, inset+" /end items"); 311 print (extLogger, out, inset+"/end @" + Integer.toHexString(objarray.hashCode())); 312 if (out!=null) try {out.flush();} catch (IOException ioe) {} 313 } 314 315 protected static void print(Logger extLogger, OutputStream out, String s) { 316 if (extLogger!=null) { 317 if (extLogger.isDebugEnabled()) extLogger.debug(s); 318 } else if (out!=null) { 319 try { 320 out.write(s.getBytes()); 321 out.write(sep); 322 } catch (IOException ioe) {} 323 } 324 } 325 326 } 327 | Popular Tags |