1 23 package com.sun.appserv.management.util.stringifier; 24 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.Collection ; 28 import java.lang.reflect.Array ; 29 30 31 import com.sun.appserv.management.util.misc.ClassUtil; 32 import com.sun.appserv.management.util.misc.ArrayConversion; 33 34 35 39 public final class SmartStringifier implements Stringifier 40 { 41 public static final SmartStringifier DEFAULT = new SmartStringifier( "," ); 42 private final String mMultiDelim; 43 private final boolean mEncloseArrays; 44 protected StringifierRegistry mRegistry; 45 46 public 47 SmartStringifier() 48 { 49 this( "," ); 50 } 51 52 public 53 SmartStringifier( String multiDelim ) 54 { 55 this ( multiDelim, true ); 56 } 57 58 public 59 SmartStringifier( String multiDelim, boolean encloseArrays ) 60 { 61 this ( StringifierRegistryImpl.DEFAULT, multiDelim, encloseArrays ); 62 } 63 64 public 65 SmartStringifier( StringifierRegistry registry, String multiDelim, boolean encloseArrays) 66 { 67 mMultiDelim = multiDelim; 68 mEncloseArrays = encloseArrays; 69 mRegistry = registry; 70 } 71 72 public void 73 setRegistry( StringifierRegistry registry ) 74 { 75 mRegistry = registry; 76 } 77 78 79 private final static Class [] STRINGIFIER_REGISTRY_LOOKUPS = 80 { 81 Iterator .class, 82 Collection .class, 83 HashMap .class 84 }; 85 86 private Stringifier 87 getStringifier( final Object target ) 88 { 89 if ( target == null ) 90 return( null ); 91 92 final Class <?> targetClass = target.getClass(); 93 94 Stringifier stringifier = mRegistry.lookup( targetClass ); 95 96 if ( target instanceof javax.management.ObjectName ) 97 { 98 assert( stringifier != null ); 99 } 100 101 if ( stringifier == null ) 102 { 103 final int numLookups = STRINGIFIER_REGISTRY_LOOKUPS.length; 105 for( int i = 0; i < numLookups; ++i ) 106 { 107 final Class <?> theClass = STRINGIFIER_REGISTRY_LOOKUPS[ i ]; 108 109 stringifier = mRegistry.lookup( theClass ); 110 if ( stringifier != null && theClass.isAssignableFrom( target.getClass() ) ) 111 { 112 break; 113 } 114 } 115 } 116 117 if ( stringifier == null ) 118 { 119 Class tempClass = targetClass; 121 while ( tempClass != Object .class ) 122 { 123 stringifier = mRegistry.lookup( tempClass ); 124 if ( stringifier != null ) 125 { 126 break; 127 } 128 129 tempClass = tempClass.getSuperclass(); 130 } 131 132 } 133 134 if ( stringifier == null ) 135 { 136 final Class [] interfaces = targetClass.getInterfaces(); 137 if ( interfaces.length != 0 ) 138 { 139 stringifier = new InterfacesStringifier( interfaces ); 140 } 141 } 142 143 return( stringifier ); 144 } 145 146 private String 147 smartStringify( Object target ) 148 { 149 String result = null; 150 151 if ( ClassUtil.objectIsArray( target ) ) 152 { 153 Object [] theArray = null; 154 155 final Class elementClass = 156 ClassUtil.getArrayElementClass( target.getClass() ); 157 158 if ( ClassUtil.IsPrimitiveClass( elementClass ) ) 159 { 160 theArray = ArrayConversion.toAppropriateType( target ); 161 } 162 else 163 { 164 theArray = (Object [])target; 165 } 166 167 168 result = ArrayStringifier.stringify( theArray, mMultiDelim, this); 169 if ( mEncloseArrays ) 170 { 171 result = "{" + result + "}"; 172 } 173 } 174 else 175 { 176 Stringifier stringifier = getStringifier( target ); 177 178 if ( stringifier != null && stringifier.getClass() == this.getClass() ) 179 { 180 stringifier = null; 182 } 183 184 if ( stringifier != null ) 185 { 186 result = stringifier.stringify( target ); 187 } 188 } 189 190 if ( result == null ) 191 { 192 result = target.toString(); 193 } 194 195 return( result ); 196 } 197 198 public static String 199 toString( Object target ) 200 { 201 return( DEFAULT.stringify( target ) ); 202 } 203 204 public String 205 stringify( Object target ) 206 { 207 if ( target == null ) 208 { 209 return( "<null>" ); 210 } 211 212 return( smartStringify( target ) ); 213 } 214 } 215 216 | Popular Tags |