1 23 24 package com.sun.enterprise.admin.common; 25 26 import java.util.Iterator ; 27 import java.util.Vector ; 28 import java.util.Stack ; 29 import java.util.EmptyStackException ; 30 31 import com.sun.enterprise.admin.util.SOMLocalStringsManager; 33 34 42 43 public class NameParser 44 { 45 private String mString = null; 46 private Vector mNameParts = null; 47 48 private static SOMLocalStringsManager localizedStrMgr = 50 SOMLocalStringsManager.getManager( NameParser.class ); 51 52 55 56 public NameParser() 57 { 58 mNameParts = new Vector (); 59 } 60 61 62 70 71 public void parseIt(String parseString) throws MalformedNameException 72 { 73 mString = parseString; 75 parseStringForNameParts(); 76 parseNameParts(); 77 if(! isWildcardCharValid()) 78 { 79 80 String msg = localizedStrMgr.getString( "admin.common.invalid_wild-card_char_placement" ); 81 throw new MalformedNameException( msg ); 82 } 83 } 84 85 86 94 95 public Iterator getParts() 96 { 97 return ( mNameParts.iterator() ); 98 } 99 100 101 102 private String removeEscapes(String str) 103 { 104 int idx; 105 while((idx=str.indexOf(Tokens.kEscapeChar))>=0) 106 if(idx==0) 107 str = str.substring(1); 108 else 109 str = str.substring(0, idx)+str.substring(idx+1); 110 return str; 111 112 } 113 121 private void parseStringForNameParts() throws MalformedNameException 122 { 123 int counter = 0; 124 int begin = counter; 125 String nameString = null; 126 127 while(counter < mString.length()) 128 { 129 char parseChar = mString.charAt(counter); 130 if(isValidChar(parseChar)) 131 { 132 boolean gotDelimiter = isDelimiterChar(mString, counter); 133 if(gotDelimiter) 134 { 135 nameString = mString.substring(begin, counter); 136 begin = counter + 1; 137 mNameParts.addElement(removeEscapes(nameString)); 138 } 140 } 141 else 142 { 143 String msg = localizedStrMgr.getString( "admin.common.invalid_char_encountered", new String ( parseChar + "" ) ); 144 throw new MalformedNameException( msg ); 145 } 146 counter++; 147 } 148 nameString = mString.substring(begin); 149 mNameParts.addElement(removeEscapes(nameString)); 150 } 152 153 154 159 160 private void parseNameParts() throws MalformedNameException 161 { 162 Iterator partsIter = getParts(); 163 boolean canReduce = false; 164 165 while(partsIter.hasNext()) 166 { 167 String aNamePartString = (String ) partsIter.next(); 168 canReduce = reduceNamePart(aNamePartString); 169 if(! canReduce) 170 { 171 String msg = localizedStrMgr.getString( "admin.common.invalid_name", mString ); 172 throw new MalformedNameException( msg ); 173 } 174 } 175 } 176 177 178 188 189 private boolean isWildcardCharValid() 190 { 191 boolean isWildcardCharValid = true; 192 String starString = new String (new char[]{Tokens.kWildCardChar}); 193 194 198 for(int i = 0 ; i < mNameParts.size(); i++) 199 { 200 String aPart = (String ) mNameParts.elementAt(i); 201 if(aPart.indexOf(Tokens.kWildCardChar) != -1 && 202 ! aPart.equals(starString)) 203 { 204 isWildcardCharValid = false; 205 break; 206 } 207 } 208 209 return isWildcardCharValid; 210 } 211 212 217 218 private boolean isValidChar(char aChar) 219 { 220 return ( Character.isLetter(aChar) || 221 Character.isDigit(aChar) || 222 this.isPermissibleChar(aChar)|| 223 this.isSpecialChar(aChar) 224 ); 225 226 } 227 228 229 237 238 private boolean isPermissibleChar(char aChar) 239 { 240 boolean isPermissibleChar = false; 241 242 if (aChar == Tokens.kSubScriptBeginnerChar || 243 aChar == Tokens.kSubScriptEnderChar || 244 aChar == Tokens.kDelimiterChar || 245 aChar == Tokens.kEscapeChar || 246 aChar == Tokens.kWildCardChar) 247 { 248 isPermissibleChar = true; 249 } 250 return isPermissibleChar; 251 } 252 253 254 259 260 private boolean isSpecialChar(char aChar) 261 { 262 return ( Tokens.kSpecialsString.indexOf(aChar) != -1 ); 263 } 264 265 266 269 270 private boolean isNonZeroDigit(char aChar) 271 { 272 return ( Tokens.kNonZeroDigitsString.indexOf(aChar) != -1 ); 273 } 274 275 276 281 282 private boolean reduceNamePart(String npString) 283 { 284 boolean canReduce = true; 285 286 if (isSubscriptPresent(npString)) 287 { 288 canReduce = isSubscriptValid(npString); 289 } 290 if (canReduce) 291 { 292 String subscriptLessString = removeSubscript(npString); 293 canReduce = isSubscriptLessStringValid(subscriptLessString); 294 } 295 return ( canReduce ); 296 } 297 298 299 311 312 private boolean isDelimiterChar(String aString, int position) 313 { 314 boolean isDelim = false; 315 316 318 if(aString.charAt(position) == Tokens.kDelimiterChar) 319 { 320 if(position == 0 || 321 aString.charAt(position - 1) != Tokens.kEscapeChar 322 ) 323 { 324 isDelim = true; 325 } 326 } 327 return ( isDelim ); 328 } 329 330 331 342 343 private boolean isValidIndexString(String index) 344 { 345 boolean isValidIndex = true; 346 347 if (index != null && index.length() > 0) 348 { 349 try 350 { 351 int intValue = Integer.parseInt(index); 352 if((intValue == 0 && index.length() != 1) || 353 (intValue > 0 && index.charAt(0) == Tokens.kZeroDigitChar) || 354 (intValue < 0) 355 ) 356 { 357 isValidIndex = false; 358 } 359 } 360 catch(NumberFormatException e) 361 { 362 isValidIndex = false; 364 } 365 } 366 else 367 { 368 isValidIndex = false; 369 } 370 371 return ( isValidIndex ); 372 } 373 374 375 383 384 private boolean isSubscriptPresent(String npString) 385 { 386 boolean subscriptPresent = false; 387 388 if(npString.indexOf(Tokens.kSubScriptBeginnerChar) != -1 || 389 npString.indexOf(Tokens.kSubScriptEnderChar) != -1 390 ) 391 { 392 subscriptPresent = true; 393 } 394 return ( subscriptPresent ); 395 } 396 397 398 420 421 private boolean isSubscriptValid(String npString) 422 { 423 boolean subscriptValid = true; 424 425 boolean subscriptOrdered = isSubscriptOrdered(npString); 426 if(subscriptOrdered) 427 { 428 int leftPos = npString.indexOf(Tokens.kSubScriptBeginnerChar); 429 int rightPos = npString.lastIndexOf(Tokens.kSubScriptEnderChar); 430 431 String indexString = npString.substring(leftPos + 1, rightPos); 432 if(! isValidIndexString(indexString)) 433 { 434 subscriptValid = false; 435 } 436 boolean lastCharIsRightSquareBracket = 437 npString.charAt(npString.length() - 1) == Tokens.kSubScriptEnderChar; 438 439 if(! lastCharIsRightSquareBracket) 440 { 441 subscriptValid = false; 442 } 443 } 444 else 445 { 446 subscriptValid = false; 447 } 448 449 return ( subscriptValid ); 450 } 451 452 464 465 private boolean isSubscriptOrdered(String npString) 466 { 467 boolean subscriptOrdered = true; 468 int index = 0; 469 Stack charStack = new Stack (); 470 471 if(isSubscriptPresent(npString)) 472 { 473 while(index < npString.length()) 474 { 475 char ch = npString.charAt(index); 476 if(ch == Tokens.kSubScriptBeginnerChar) 477 { 478 charStack.push(new Character (ch)); 479 } 480 else if(ch == Tokens.kSubScriptEnderChar) 481 { 482 if(! charStack.empty()) 483 { 484 Character poppedChar = (Character )charStack.pop(); 485 if(poppedChar.charValue() != Tokens.kSubScriptBeginnerChar) 486 { 487 subscriptOrdered = false; 488 break; 489 } 490 } 491 else 492 { 493 subscriptOrdered = false; 494 break; 495 } 496 } 497 index++; 498 } 499 if(! charStack.empty()) 500 { 501 subscriptOrdered = false; 502 } 503 } 504 else 505 { 506 subscriptOrdered = false; 507 } 508 509 return ( subscriptOrdered ); 510 } 511 512 523 524 private String removeSubscript(String npString) 525 { 526 String subscriptLessString = null; 527 int leftIndex = npString.indexOf(Tokens.kSubScriptBeginnerChar); 528 529 if (npString != null || npString.length() > 0) 530 { 531 if(leftIndex != -1) 532 { 533 subscriptLessString = npString.substring(0, leftIndex); 534 } 535 else 536 { 537 subscriptLessString = npString; 538 } 539 } 540 return ( subscriptLessString ); 541 } 542 543 544 552 553 private boolean isSubscriptLessStringValid(String npString) 554 { 555 boolean remStringValid = false; 556 557 if(npString != null && npString.length() > 0) 558 { 559 561 563 boolean onlyDelimiterEscaped = isOnlyDelimiterEscaped(npString); 564 boolean containsEscape = npString.indexOf(Tokens.kEscapeChar) != -1; 565 boolean isEscapeValid = ! containsEscape || 566 containsEscape && onlyDelimiterEscaped; 567 568 boolean noMoreStars = npString.indexOf(Tokens.kMoreWildCardsString) == -1; 569 570 if( isEscapeValid && 571 noMoreStars 572 ) 573 { 574 remStringValid = true; 575 } 576 } 577 return ( remStringValid ); 578 } 579 580 581 592 593 private boolean isOnlyDelimiterEscaped(String npString) 594 { 595 boolean onlyDelimiterEscaped = true; 596 597 if(npString != null && npString.length() > 0) 598 { 599 int index = 0; 600 int strlength = npString.length(); 601 602 while(index < strlength) 603 { 604 char ch = npString.charAt(index); 605 if(ch == Tokens.kEscapeChar) 606 { 607 int nextIndex = index + 1; 608 if (nextIndex >= strlength || 609 npString.charAt(nextIndex) != Tokens.kDelimiterChar) 610 { 611 onlyDelimiterEscaped = false; 612 break; } 614 } 615 index++; 616 } 617 } 618 else 619 { 620 onlyDelimiterEscaped = false; 621 } 622 return ( onlyDelimiterEscaped ); 623 } 624 625 } 626 | Popular Tags |