1 7 8 package com.sun.corba.se.impl.orbutil ; 9 10 import java.util.Arrays ; 11 12 public abstract class ObjectWriter { 13 public static ObjectWriter make( boolean isIndenting, 14 int initialLevel, int increment ) 15 { 16 if (isIndenting) 17 return new IndentingObjectWriter( initialLevel, increment ) ; 18 else 19 return new SimpleObjectWriter() ; 20 } 21 22 public abstract void startObject( Object obj ) ; 23 24 public abstract void startElement() ; 25 26 public abstract void endElement() ; 27 28 public abstract void endObject( String str ) ; 29 30 public abstract void endObject() ; 31 32 public String toString() { return result.toString() ; } 33 34 public void append( boolean arg ) { result.append( arg ) ; } 35 36 public void append( char arg ) { result.append( arg ) ; } 37 38 public void append( short arg ) { result.append( arg ) ; } 39 40 public void append( int arg ) { result.append( arg ) ; } 41 42 public void append( long arg ) { result.append( arg ) ; } 43 44 public void append( float arg ) { result.append( arg ) ; } 45 46 public void append( double arg ) { result.append( arg ) ; } 47 48 public void append( String arg ) { result.append( arg ) ; } 49 50 54 protected StringBuffer result ; 55 56 protected ObjectWriter() 57 { 58 result = new StringBuffer () ; 59 } 60 61 protected void appendObjectHeader( Object obj ) 62 { 63 result.append( obj.getClass().getName() ) ; 64 result.append( "<" ) ; 65 result.append( System.identityHashCode( obj ) ) ; 66 result.append( ">" ) ; 67 Class compClass = obj.getClass().getComponentType() ; 68 69 if (compClass != null) { 70 result.append( "[" ) ; 71 if (compClass == boolean.class) { 72 boolean[] arr = (boolean[])obj ; 73 result.append( arr.length ) ; 74 result.append( "]" ) ; 75 } else if (compClass == byte.class) { 76 byte[] arr = (byte[])obj ; 77 result.append( arr.length ) ; 78 result.append( "]" ) ; 79 } else if (compClass == short.class) { 80 short[] arr = (short[])obj ; 81 result.append( arr.length ) ; 82 result.append( "]" ) ; 83 } else if (compClass == int.class) { 84 int[] arr = (int[])obj ; 85 result.append( arr.length ) ; 86 result.append( "]" ) ; 87 } else if (compClass == long.class) { 88 long[] arr = (long[])obj ; 89 result.append( arr.length ) ; 90 result.append( "]" ) ; 91 } else if (compClass == char.class) { 92 char[] arr = (char[])obj ; 93 result.append( arr.length ) ; 94 result.append( "]" ) ; 95 } else if (compClass == float.class) { 96 float[] arr = (float[])obj ; 97 result.append( arr.length ) ; 98 result.append( "]" ) ; 99 } else if (compClass == double.class) { 100 double[] arr = (double[])obj ; 101 result.append( arr.length ) ; 102 result.append( "]" ) ; 103 } else { java.lang.Object [] arr = (java.lang.Object [])obj ; 105 result.append( arr.length ) ; 106 result.append( "]" ) ; 107 } 108 } 109 110 result.append( "(" ) ; 111 } 112 113 121 private static class IndentingObjectWriter extends ObjectWriter { 122 private int level ; 123 private int increment ; 124 125 public IndentingObjectWriter( int initialLevel, int increment ) 126 { 127 this.level = initialLevel ; 128 this.increment = increment ; 129 startLine() ; 130 } 131 132 private void startLine() 133 { 134 char[] fill = new char[ level * increment ] ; 135 Arrays.fill( fill, ' ' ) ; 136 result.append( fill ) ; 137 } 138 139 public void startObject( java.lang.Object obj ) 140 { 141 appendObjectHeader( obj ) ; 142 level++ ; 143 } 144 145 public void startElement() 146 { 147 result.append( "\n" ) ; 148 startLine() ; 149 } 150 151 public void endElement() 152 { 153 } 154 155 public void endObject( String str ) 156 { 157 level-- ; 158 result.append( str ) ; 159 result.append( ")" ) ; 160 } 161 162 public void endObject( ) 163 { 164 level-- ; 165 result.append( "\n" ) ; 166 startLine() ; 167 result.append( ")" ) ; 168 } 169 } 170 171 private static class SimpleObjectWriter extends ObjectWriter { 172 public void startObject( java.lang.Object obj ) 173 { 174 appendObjectHeader( obj ) ; 175 result.append( " " ) ; 176 } 177 178 public void startElement() 179 { 180 result.append( " " ) ; 181 } 182 183 public void endObject( String str ) 184 { 185 result.append( str ) ; 186 result.append( ")" ) ; 187 } 188 189 public void endElement() 190 { 191 } 192 193 public void endObject() 194 { 195 result.append( ")" ) ; 196 } 197 } 198 } 199 | Popular Tags |