1 23 24 29 30 package com.sun.appserv.management.util.misc; 31 32 33 import java.text.MessageFormat ; 34 35 import com.sun.appserv.management.util.misc.ClassUtil; 36 37 import com.sun.appserv.management.util.stringifier.Stringifier; 38 import com.sun.appserv.management.util.stringifier.SmartStringifier; 39 import com.sun.appserv.management.util.stringifier.StringifierRegistry; 40 import com.sun.appserv.management.util.stringifier.StringifierRegistryImpl; 41 42 45 public final class Formatter implements StringSource 46 { 47 final StringSource mStringSource; 48 final StringifierRegistry mRegistry; 49 50 public 51 Formatter( StringSource source ) 52 { 53 mStringSource = source; 54 mRegistry = StringifierRegistryImpl.DEFAULT; 55 } 56 57 public Object 58 prepare( Object o ) 59 { 60 Object result = o; 61 62 if ( mRegistry.lookup( o.getClass() ) != null || 63 ClassUtil.objectIsArray( o ) ) 64 { 65 result = SmartStringifier.toString( o ); 66 } 67 68 return( result ); 69 } 70 71 76 private Object [] 77 prepare( Object [] objects ) 78 { 79 final Object [] results = new Object [ objects.length ]; 80 81 for( int i = 0; i < objects.length; ++i ) 82 { 83 results[ i ] = prepare( objects[ i ] ); 84 } 85 86 return( results ); 87 } 88 89 95 public String 96 format( String key, Object [] objects ) 97 { 98 return( MessageFormat.format( getString( key ), prepare( objects ) ) ); 99 } 100 101 public String 102 format( String key, Object o1 ) 103 { 104 return( format( key, new Object [] { o1 } ) ); 105 } 106 107 public String 108 format( String key, Object o1, Object o2) 109 { 110 return( format( key, new Object [] { o1, o2 } ) ); 111 } 112 113 public String 114 format( String key, Object o1, Object o2, Object o3) 115 { 116 return( format( key, new Object [] { o1, o2, o3} ) ); 117 } 118 119 public String 120 format( String key, Object o1, Object o2, Object o3, Object o4) 121 { 122 return( format( key, new Object [] { o1, o2, o3, o4 } ) ); 123 } 124 125 126 public String 127 getString( String id ) 128 { 129 return( mStringSource.getString( id ) ); 130 } 131 132 public String 133 getString( String id, String defaultValue) 134 { 135 return( mStringSource.getString( id, defaultValue ) ); 136 } 137 } 138 139 140 | Popular Tags |