1 23 package com.sun.appserv.management.util.misc; 24 25 import java.util.Set ; 26 27 import com.sun.appserv.management.util.stringifier.SmartStringifier; 28 29 30 33 public final class StringUtil 34 { 35 private StringUtil() {} 36 37 public final static char QUOTE_CHAR = '\"'; 38 public final static String QUOTE = "" + QUOTE_CHAR; 39 40 43 public final static String LS = System.getProperty( "line.separator", "\n" ); 44 45 public static String 46 quote( Object o ) 47 { 48 return( quote( o, QUOTE_CHAR ) ); 49 } 50 51 public static String 52 quote( Object o, char leftHandChar ) 53 { 54 final String s = o == null ? "null" : SmartStringifier.toString( o ); 55 56 char leftChar = leftHandChar; 57 char rightChar = leftHandChar; 58 59 if ( leftHandChar == '(' ) 60 { 61 rightChar = ')'; 62 } 63 else if ( leftHandChar == '{' ) 64 { 65 rightChar = '}'; 66 } 67 else if ( leftHandChar == '[' ) 68 { 69 rightChar = ']'; 70 } 71 else if ( leftHandChar == '<' ) 72 { 73 rightChar = '>'; 74 } 75 else 76 { 77 } 79 80 final String out = leftChar + s + rightChar; 81 82 return( out ); 83 } 84 85 86 public static String 87 toHexString( byte theByte ) 88 { 89 String result = Integer.toHexString( ((int)theByte) & 0x000000FF ); 90 if ( result.length() == 1 ) 91 { 92 result = "0" + result; 93 } 94 return( result ); 95 } 96 97 public static String 98 toHexString( byte[] bytes ) 99 { 100 return( toHexString( bytes, null ) ); 101 } 102 103 public static String 104 toHexString( byte[] bytes, String delim ) 105 { 106 final StringBuffer buf = new StringBuffer (); 107 108 if ( bytes.length == 0 ) 109 { 110 } 112 else if ( delim == null || delim.length() == 0 ) 113 { 114 for( int i = 0; i < bytes.length; ++i ) 115 { 116 buf.append( toHexString( bytes[ i ] ) ); 117 } 118 } 119 else 120 { 121 for( int i = 0; i < bytes.length; ++i ) 122 { 123 buf.append( toHexString( bytes[ i ] ) + delim ); 124 } 125 126 buf.setLength( buf.length() - 1); 128 } 129 130 return( buf.toString() ); 131 } 132 133 public static String 134 stripSuffix( 135 final String s, 136 final String suffix ) 137 { 138 String result = s; 139 140 if ( s.endsWith( suffix ) ) 141 { 142 result = s.substring( 0, s.length() - suffix.length() ); 143 } 144 145 return( result ); 146 } 147 148 149 public static String 150 replaceSuffix( 151 final String s, 152 final String fromSuffix, 153 final String toSuffix ) 154 { 155 if ( ! s.endsWith( fromSuffix ) ) 156 { 157 throw new IllegalArgumentException ( fromSuffix ); 158 } 159 160 return( stripSuffix( s, fromSuffix ) + toSuffix ); 161 } 162 163 public static String 164 stripPrefix( 165 final String s, 166 final String prefix ) 167 { 168 String result = s; 169 170 if ( s.startsWith( prefix ) ) 171 { 172 result = s.substring( prefix.length(), s.length() ); 173 } 174 175 return( result ); 176 } 177 178 179 public static String 180 stripPrefixAndSuffix( 181 final String s, 182 final String prefix, 183 final String suffix ) 184 { 185 return stripPrefix( stripSuffix( s, suffix ), prefix ); 186 } 187 188 189 public static String 190 upperCaseFirstLetter( final String s) 191 { 192 String result = s; 193 194 if ( s.length() >= 1 ) 195 { 196 result = s.substring( 0, 1 ).toUpperCase() + s.substring( 1, s.length() ); 197 } 198 199 return( result ); 200 } 201 202 203 private static String 204 toString( final Object o) 205 { 206 String result = null; 207 208 if ( o == null ) 209 { 210 result = "null"; 211 } 212 else if ( o instanceof byte[] ) 213 { 214 final byte[] b = byte[].class.cast( o ); 215 result = "byte[] of length " + b.length; 216 } 217 else if ( o instanceof String [] ) 218 { 219 result = toString( ", ", (Object [])o); 220 } 221 else 222 { 223 result = o.toString(); 224 } 225 226 final int MAX_LENGTH = 256; 227 if ( result.length() > MAX_LENGTH ) 228 { 229 result = result.substring( 0, MAX_LENGTH - 1 ); 230 } 231 232 return result; 233 } 234 235 public static String 236 toString( final String [] args ) 237 { 238 return toString( ", ", args ); 239 } 240 241 public static String 242 toString( final String delim, final String ... args ) 243 { 244 return toString( delim, (Object [])args ); 245 } 246 247 251 public static String 252 toString( final String delim, final Object ... args ) 253 { 254 String result = null; 255 256 if ( args == null ) 257 { 258 result = "" + null; 259 } 260 else if ( args.length == 0 ) 261 { 262 result = ""; 263 } 264 else if ( args.length == 1 ) 265 { 266 result = toString( args[ 0 ] ); 267 } 268 else 269 { 270 final StringBuilder builder = new StringBuilder (); 271 272 for( int i = 0; i < args.length - 1; ++i ) 273 { 274 builder.append( toString( args[ i ] ) ); 275 builder.append( delim ); 276 } 277 builder.append( toString( args[ args.length - 1 ] ) ); 278 279 result = builder.toString(); 280 } 281 282 283 return result; 284 } 285 286 289 public static String 290 getPrefix( 291 final Set <String > prefixes, 292 final String s ) 293 { 294 String result = null; 295 for( final String prefix : prefixes ) 296 { 297 if ( s.startsWith( prefix ) ) 298 { 299 result = prefix; 300 break; 301 } 302 } 303 return result; 304 } 305 306 310 public static String 311 findAndStripPrefix( 312 final Set <String > prefixes, 313 final String s ) 314 { 315 final String prefix = getPrefix( prefixes, s ); 316 if ( prefix == null ) 317 { 318 throw new IllegalArgumentException ( s ); 319 } 320 321 return stripPrefix( s, prefix ); 322 } 323 324 private static String NEWLINE_STR = null; 325 326 public static String 327 NEWLINE() 328 { 329 if ( NEWLINE_STR == null ) 330 { 331 NEWLINE_STR = System.getProperty( "line.separator" ); 332 } 333 return NEWLINE_STR; 334 } 335 336 } 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | Popular Tags |