1 19 package org.netbeans.mdrshell; 20 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 import java.io.PrintStream ; 24 25 30 31 class Support { 32 33 35 private Support() { 36 } 37 38 40 public static void ensurePosition( StringBuffer sb, int position ) { 41 42 if ( sb.length() < position ) { 43 44 while ( sb.length() < position ) { 45 sb.append( " " ); 46 } 47 } 48 else if ( sb.length() > position ) { 49 sb.setLength( position ); 50 } 51 } 52 53 54 56 public static void defaultPrint( PrintStream ps, Object object ) { 57 defaultPrint( ps, 0, object ); 58 } 59 60 63 public static void defaultPrint( PrintStream ps, int offset, Object object ) { 64 65 if ( object == null ) { 66 ps.println("null"); 67 return; 68 } 69 70 if ( object instanceof java.util.Iterator ) { 71 printIndent( ps, offset ); 72 ps.print( "ITERATOR : {" ); 73 for( Iterator it = (Iterator )object; it.hasNext(); ) { 74 defaultPrint( ps, offset + 2, it.next() ); 75 } 76 ps.println(); 77 printIndent( ps, offset ); 78 ps.println( "} // end of ITERATOR" ); 79 } 80 else if ( object.getClass().isArray() ) { 81 printIndent( ps, offset ); 82 ps.print( "ARRAY : [" ); 83 for ( int i = 0; i < java.lang.reflect.Array.getLength( object ); i++ ) { 84 defaultPrint( ps, offset + 2, java.lang.reflect.Array.get( object, i ) ); 86 } 88 ps.println(); 89 printIndent( ps, offset ); 90 ps.println( "] // end of ARRAY" ); 91 } 92 else if ( object instanceof Throwable ) { 93 ((Throwable )object).printStackTrace(); 94 } 95 else if ( object instanceof Collection ) { 96 printIndent( ps, offset ); 97 ps.print( "COLLECTION : {" ); 98 for( Iterator it = ((Collection )object).iterator(); it.hasNext(); ) { 99 defaultPrint( ps, offset + 2, it.next() ); 100 } 101 ps.println(); 102 printIndent( ps, offset ); 103 ps.println( "} // end of COLLECTION" ); 104 } 105 else { 106 printIndent( ps, offset ); 107 ps.println( object.toString() ); 108 } 109 110 } 111 112 113 private static void printIndent( PrintStream ps, int indent ) { 114 for( ; indent > 0; indent -- ) { 115 ps.print( " " ); 116 } 117 } 118 119 } 120 | Popular Tags |