1 23 24 29 package com.sun.enterprise.admin.dottedname; 30 31 import java.util.Collections ; 32 import java.util.Arrays ; 33 import java.util.List ; 34 import java.util.ArrayList ; 35 36 import com.sun.enterprise.admin.util.Tokenizer; 37 import com.sun.enterprise.admin.util.TokenizerImpl; 38 39 40 class SpecialChars 41 { 42 public final static String WILDCARDS = "*"; 43 public final static char BACKSLASH = '\\'; 44 public final static String SPECIALS = "(){}[];<>@$#"; 45 public final static String LEGAL_CHARS = "abcdefghijklmnopqrstuvwxyz" + 46 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + 47 "0123456789" + 48 "-_./" + 49 BACKSLASH + 50 WILDCARDS + 51 SPECIALS; 52 }; 53 54 56 final class ParsedDottedName implements java.io.Serializable 57 { 58 public final String mDomain; 59 public final String mScope; 60 public final List mParts; 61 62 public 63 ParsedDottedName( String domain, String scope, List parts ) 64 { 65 if ( domain.length() != 0 ) 66 { 67 checkLegalNamePart( domain ); 68 } 69 70 checkLegalNamePart( scope ); 71 72 for( int i = 0; i < parts.size(); ++i ) 73 { 74 checkLegalNamePart( (String )parts.get( i ) ); 75 } 76 77 mDomain = domain; 78 mScope = scope; 79 mParts = Collections.unmodifiableList( parts ); 80 } 81 82 83 boolean 84 isLegalChar( final char theChar ) 85 { 86 return( SpecialChars.LEGAL_CHARS.indexOf( theChar ) >= 0 ); 87 } 88 89 void 90 checkLegalChar( final char theChar ) 91 { 92 if ( ! isLegalChar( theChar ) ) 93 { 94 final String msg = DottedNameStrings.getString( 95 DottedNameStrings.ILLEGAL_CHARACTER_KEY, 96 "'" + theChar + "'" ); 97 98 throw new IllegalArgumentException ( msg ); 99 } 100 } 101 102 void 103 checkLegalNamePart( String part ) 104 { 105 final int length = part.length(); 106 107 if ( length == 0 ) 108 { 109 final String msg = DottedNameStrings.getString( 110 DottedNameStrings.MISSING_EXPECTED_NAME_PART_KEY ); 111 112 throw new IllegalArgumentException ( msg ); 113 } 114 115 for( int i = 0; i < length; ++i ) 116 { 117 final char theChar = part.charAt( i ); 118 119 checkLegalChar( theChar ); 120 } 121 } 122 123 public boolean 124 equals( Object other ) 125 { 126 boolean equals = false; 127 128 if ( ! (other instanceof ParsedDottedName) ) 129 { 130 equals = false; 131 } 132 else if ( other == this ) 133 { 134 equals = true; 135 } 136 else 137 { 138 equals = this.toString().equals( other.toString() ); 139 } 140 141 return( equals ); 142 } 143 144 public String 145 toString() 146 { 147 return( DottedName.toString( mDomain, mScope, mParts ) ); 148 } 149 } 150 151 158 public final class DottedName implements java.io.Serializable 159 { 160 final String mSourceString; 161 final ParsedDottedName mParsed; 162 163 public final static String WILDCARDS = SpecialChars.WILDCARDS; 164 public final static char BACKSLASH = SpecialChars.BACKSLASH; 165 public final static String SPECIALS = SpecialChars.SPECIALS; 166 public final static String LEGAL_CHARS = SpecialChars.LEGAL_CHARS; 167 168 public 169 DottedName( String sourceString ) 170 { 171 mSourceString = sourceString; 172 try 173 { 174 mParsed = parse( sourceString ); 175 } 176 catch( com.sun.enterprise.admin.util.TokenizerException e ) 177 { 178 final String msg = DottedNameStrings.getString( 179 DottedNameStrings.MALFORMED_DOTTED_NAME_KEY, 180 sourceString ); 181 182 throw new IllegalArgumentException ( msg ); 183 } 184 185 checkWellFormed( sourceString, toStringFromParsed( mParsed ) ); 186 } 187 188 193 private void 194 checkWellFormed( String sourceString, String correctValue ) 195 { 196 if ( ! sourceString.equals( correctValue ) ) 197 { 198 final String msg = DottedNameStrings.getString( 199 DottedNameStrings.MALFORMED_DOTTED_NAME_KEY, 200 sourceString ); 201 202 throw new IllegalArgumentException ( msg ); 203 } 204 } 205 206 public static String 207 toString( DottedName dn, int numParts ) 208 { 209 return( toString( dn.getDomain(), dn.getScope(), dn.getParts(), numParts )); 210 } 211 212 public static String 213 toString( String domain, String scope, List parts ) 214 { 215 return( toString( domain, scope, parts, parts.size() )); 216 } 217 218 public static String 219 toString( String domain, String scope, List parts, int numParts ) 220 { 221 final StringBuffer buf = new StringBuffer (); 222 223 if ( domain.length() != 0 ) 224 { 225 buf.append( domain + ":" ); 226 } 227 228 buf.append( escapePart( scope ) ); 229 230 for( int i = 0; i < numParts; ++ i ) 231 { 232 final String unescapedPart = (String )parts.get( i ); 233 234 buf.append( "." + escapePart( unescapedPart ) ); 235 } 236 237 return( buf.toString() ); 238 } 239 240 public String 241 toString() 242 { 243 return( mSourceString ); 244 } 245 246 public static String 247 toString(final String domain, 248 final String scope, 249 final List parts, 250 final boolean needsEscaping ) 251 { 252 if(needsEscaping){ 253 return toString(domain,scope,parts); 254 } 255 256 final StringBuffer buf = new StringBuffer (); 257 258 if ( domain.length() != 0 ) 259 { 260 buf.append( domain + ":" ); 261 } 262 263 buf.append( scope ); 264 265 for( int i = 0; i < parts.size(); ++ i ) 266 { 267 buf.append( "." + parts.get( i ) ); 268 } 269 270 return( buf.toString() ); 271 } 272 273 static boolean 274 needsEscaping( String part ) 275 { 276 boolean needsEscaping = false; 277 278 final int numChars = part.length(); 279 for( int i = 0; i < numChars; ++i ) 280 { 281 final char theChar = part.charAt( i ); 282 283 if ( ESCAPEABLE_CHARS.indexOf( theChar ) >= 0 ) 284 { 285 needsEscaping = true; 286 break; 287 } 288 } 289 290 return( needsEscaping ); 291 } 292 293 public static String 294 escapePart( String part ) 295 { 296 String result = part; 297 298 if ( needsEscaping( part ) ) 301 { 302 final int numChars = part.length(); 303 final StringBuffer buf = new StringBuffer (); 304 305 for( int i = 0; i < numChars; ++i ) 306 { 307 final char theChar = part.charAt( i ); 308 309 if ( ESCAPEABLE_CHARS.indexOf( theChar ) >= 0 ) 310 { 311 buf.append( ESCAPE_CHAR ); 312 } 313 buf.append( theChar ); 314 } 315 316 return( buf.toString() ); 317 } 318 319 return( result ); 320 } 321 322 323 324 static String 325 toStringFromParsed( ParsedDottedName pn ) 326 { 327 return( toString( pn.mDomain, pn.mScope, pn.mParts, pn.mParts.size() ) ); 328 } 329 330 331 private final static String NO_DOMAIN = ""; 332 private final static char DOMAIN_DELIM = ':'; 333 private final static char SEPARATOR = '.'; 334 private final static char ESCAPE_CHAR = SpecialChars.BACKSLASH; 335 private final static String ESCAPEABLE_CHARS = "" + SEPARATOR + ESCAPE_CHAR; 336 337 338 339 static ParsedDottedName 340 parse( String sourceString ) 341 throws com.sun.enterprise.admin.util.TokenizerException 342 { 343 final Tokenizer tk = new TokenizerImpl( sourceString, "" + SEPARATOR, 344 false, ESCAPE_CHAR, ESCAPEABLE_CHARS ); 345 346 final String [] tokens = tk.getTokens(); 347 348 if ( tokens.length == 0 ) 349 { 350 final String msg = DottedNameStrings.getString( 351 DottedNameStrings.DOTTED_NAME_MUST_HAVE_ONE_PART_KEY, 352 sourceString ); 353 354 throw new IllegalArgumentException ( msg ); 355 } 356 357 361 final int domainDelimIndex = tokens[ 0 ].indexOf( DOMAIN_DELIM ); 363 String scope = null; 364 String domain = NO_DOMAIN; 365 if ( domainDelimIndex >= 0 ) 366 { 367 domain = tokens[ 0 ].substring( 0, domainDelimIndex ); 368 scope = tokens[ 0 ].substring( domainDelimIndex + 1, tokens[ 0 ].length() ); 369 } 370 else 371 { 372 scope = tokens[ 0 ]; 373 } 374 375 final ArrayList parts = new ArrayList (); 376 for( int i = 1; i < tokens.length; ++i ) 377 { 378 parts.add( tokens[ i ] ); 379 } 380 381 final ParsedDottedName parsedName = new ParsedDottedName( domain, 382 scope, parts ); 383 384 return( parsedName ); 385 } 386 387 public String 388 getDomain() 389 { 390 return( mParsed.mDomain ); 391 } 392 393 public String 394 getScope() 395 { 396 return( mParsed.mScope ); 397 } 398 399 400 406 public List 407 getParts() 408 { 409 return( Collections.unmodifiableList( mParsed.mParts ) ); 410 } 411 412 417 public String 418 getPart( int i ) 419 { 420 return( (String )mParsed.mParts.get( i ) ); 421 } 422 423 public static boolean 424 isWildcardName( final String name ) 425 { 426 boolean isWild = false; 427 final int numWilds = SpecialChars.WILDCARDS.length(); 428 429 for( int i = 0; i < numWilds; ++i ) 430 { 431 final int wildChar = SpecialChars.WILDCARDS.charAt( i ); 432 433 if ( name.indexOf( wildChar ) >= 0 ) 434 { 435 isWild = true; 436 break; 437 } 438 439 } 440 return( isWild ); 441 } 442 443 public boolean 444 isWildcardName() 445 { 446 return( isWildcardName( toString() ) ); 447 } 448 449 450 public boolean 451 equals( Object other ) 452 { 453 boolean equals = false; 454 455 if ( ! (other instanceof DottedName) ) 456 { 457 equals = false; 458 } 459 else if ( other == this ) 460 { 461 equals = true; 462 } 463 else 464 { 465 equals = this.toString().equals( other.toString() ); 466 } 467 468 return( equals ); 469 } 470 } 471 472 473 474 475 476 | Popular Tags |