1 23 24 package com.sun.enterprise.admin.meta.naming; 25 26 import java.util.Iterator ; 27 import java.util.Vector ; 28 import java.util.Stack ; 29 import java.util.EmptyStackException ; 30 31 32 import javax.management.MalformedObjectNameException ; 33 34 42 43 public class NameParser 44 { 45 private String mString = null; 46 private Vector mNameParts = null; 47 48 49 52 53 public NameParser() 54 { 55 mNameParts = new Vector (); 56 } 57 58 59 67 68 public void parseIt(String parseString) throws MalformedObjectNameException 69 { 70 mString = parseString; 72 parseStringForNameParts(); 73 parseNameParts(); 74 if(! isWildcardCharValid()) 75 { 76 77 String msg = ( "admin.common.invalid_wild-card_char_placement" ); 78 throw new MalformedObjectNameException ( msg ); 79 } 80 } 81 82 83 91 92 public Iterator getParts() 93 { 94 return ( mNameParts.iterator() ); 95 } 96 97 98 99 private String removeEscapes(String str) 100 { 101 int idx; 102 while((idx=str.indexOf(Tokens.kEscapeChar))>=0) 103 if(idx==0) 104 str = str.substring(1); 105 else 106 str = str.substring(0, idx)+str.substring(idx+1); 107 return str; 108 109 } 110 118 private void parseStringForNameParts() throws MalformedObjectNameException 119 { 120 int counter = 0; 121 int begin = counter; 122 String nameString = null; 123 124 while(counter < mString.length()) 125 { 126 char parseChar = mString.charAt(counter); 127 if(isValidChar(parseChar)) 128 { 129 boolean gotDelimiter = isDelimiterChar(mString, counter); 130 if(gotDelimiter) 131 { 132 nameString = mString.substring(begin, counter); 133 begin = counter + 1; 134 mNameParts.addElement(removeEscapes(nameString)); 135 } 137 } 138 else 139 { 140 String msg = "invalidchar"; throw new MalformedObjectNameException ( msg ); 142 } 143 counter++; 144 } 145 nameString = mString.substring(begin); 146 mNameParts.addElement(removeEscapes(nameString)); 147 } 149 150 151 156 157 private void parseNameParts() throws MalformedObjectNameException 158 { 159 Iterator partsIter = getParts(); 160 boolean canReduce = false; 161 162 while(partsIter.hasNext()) 163 { 164 String aNamePartString = (String ) partsIter.next(); 165 canReduce = reduceNamePart(aNamePartString); 166 if(! canReduce) 167 { 168 String msg = "invalidname"+mString; throw new MalformedObjectNameException ( msg ); 170 } 171 } 172 } 173 174 175 185 186 private boolean isWildcardCharValid() 187 { 188 boolean isWildcardCharValid = true; 189 String starString = new String (new char[]{Tokens.kWildCardChar}); 190 191 195 for(int i = 0 ; i < mNameParts.size(); i++) 196 { 197 String aPart = (String ) mNameParts.elementAt(i); 198 if(aPart.indexOf(Tokens.kWildCardChar) != -1 && 199 ! aPart.equals(starString)) 200 { 201 isWildcardCharValid = false; 202 break; 203 } 204 } 205 206 return isWildcardCharValid; 207 } 208 209 214 215 private boolean isValidChar(char aChar) 216 { 217 return ( Character.isLetter(aChar) || 218 Character.isDigit(aChar) || 219 this.isPermissibleChar(aChar)|| 220 this.isSpecialChar(aChar) 221 ); 222 223 } 224 225 226 234 235 private boolean isPermissibleChar(char aChar) 236 { 237 boolean isPermissibleChar = false; 238 239 if (aChar == Tokens.kSubScriptBeginnerChar || 240 aChar == Tokens.kSubScriptEnderChar || 241 aChar == Tokens.kDelimiterChar || 242 aChar == Tokens.kEscapeChar || 243 aChar == Tokens.kWildCardChar) 244 { 245 isPermissibleChar = true; 246 } 247 return isPermissibleChar; 248 } 249 250 251 256 257 private boolean isSpecialChar(char aChar) 258 { 259 return ( Tokens.kSpecialsString.indexOf(aChar) != -1 ); 260 } 261 262 263 266 267 private boolean isNonZeroDigit(char aChar) 268 { 269 return ( Tokens.kNonZeroDigitsString.indexOf(aChar) != -1 ); 270 } 271 272 273 278 279 private boolean reduceNamePart(String npString) 280 { 281 boolean canReduce = true; 282 283 if (isSubscriptPresent(npString)) 284 { 285 canReduce = isSubscriptValid(npString); 286 } 287 if (canReduce) 288 { 289 String subscriptLessString = removeSubscript(npString); 290 canReduce = isSubscriptLessStringValid(subscriptLessString); 291 } 292 return ( canReduce ); 293 } 294 295 296 308 309 private boolean isDelimiterChar(String aString, int position) 310 { 311 boolean isDelim = false; 312 313 315 if(aString.charAt(position) == Tokens.kDelimiterChar) 316 { 317 if(position == 0 || 318 aString.charAt(position - 1) != Tokens.kEscapeChar 319 ) 320 { 321 isDelim = true; 322 } 323 } 324 return ( isDelim ); 325 } 326 327 328 339 340 private boolean isValidIndexString(String index) 341 { 342 boolean isValidIndex = true; 343 344 if (index != null && index.length() > 0) 345 { 346 try 347 { 348 int intValue = Integer.parseInt(index); 349 if((intValue == 0 && index.length() != 1) || 350 (intValue > 0 && index.charAt(0) == Tokens.kZeroDigitChar) || 351 (intValue < 0) 352 ) 353 { 354 isValidIndex = false; 355 } 356 } 357 catch(NumberFormatException e) 358 { 359 isValidIndex = false; 361 } 362 } 363 else 364 { 365 isValidIndex = false; 366 } 367 368 return ( isValidIndex ); 369 } 370 371 372 380 381 private boolean isSubscriptPresent(String npString) 382 { 383 boolean subscriptPresent = false; 384 385 if(npString.indexOf(Tokens.kSubScriptBeginnerChar) != -1 || 386 npString.indexOf(Tokens.kSubScriptEnderChar) != -1 387 ) 388 { 389 subscriptPresent = true; 390 } 391 return ( subscriptPresent ); 392 } 393 394 395 417 418 private boolean isSubscriptValid(String npString) 419 { 420 boolean subscriptValid = true; 421 422 boolean subscriptOrdered = isSubscriptOrdered(npString); 423 if(subscriptOrdered) 424 { 425 int leftPos = npString.indexOf(Tokens.kSubScriptBeginnerChar); 426 int rightPos = npString.lastIndexOf(Tokens.kSubScriptEnderChar); 427 428 String indexString = npString.substring(leftPos + 1, rightPos); 429 if(! isValidIndexString(indexString)) 430 { 431 subscriptValid = false; 432 } 433 boolean lastCharIsRightSquareBracket = 434 npString.charAt(npString.length() - 1) == Tokens.kSubScriptEnderChar; 435 436 if(! lastCharIsRightSquareBracket) 437 { 438 subscriptValid = false; 439 } 440 } 441 else 442 { 443 subscriptValid = false; 444 } 445 446 return ( subscriptValid ); 447 } 448 449 461 462 private boolean isSubscriptOrdered(String npString) 463 { 464 boolean subscriptOrdered = true; 465 int index = 0; 466 Stack charStack = new Stack (); 467 468 if(isSubscriptPresent(npString)) 469 { 470 while(index < npString.length()) 471 { 472 char ch = npString.charAt(index); 473 if(ch == Tokens.kSubScriptBeginnerChar) 474 { 475 charStack.push(new Character (ch)); 476 } 477 else if(ch == Tokens.kSubScriptEnderChar) 478 { 479 if(! charStack.empty()) 480 { 481 Character poppedChar = (Character )charStack.pop(); 482 if(poppedChar.charValue() != Tokens.kSubScriptBeginnerChar) 483 { 484 subscriptOrdered = false; 485 break; 486 } 487 } 488 else 489 { 490 subscriptOrdered = false; 491 break; 492 } 493 } 494 index++; 495 } 496 if(! charStack.empty()) 497 { 498 subscriptOrdered = false; 499 } 500 } 501 else 502 { 503 subscriptOrdered = false; 504 } 505 506 return ( subscriptOrdered ); 507 } 508 509 520 521 private String removeSubscript(String npString) 522 { 523 String subscriptLessString = null; 524 int leftIndex = npString.indexOf(Tokens.kSubScriptBeginnerChar); 525 526 if (npString != null || npString.length() > 0) 527 { 528 if(leftIndex != -1) 529 { 530 subscriptLessString = npString.substring(0, leftIndex); 531 } 532 else 533 { 534 subscriptLessString = npString; 535 } 536 } 537 return ( subscriptLessString ); 538 } 539 540 541 549 550 private boolean isSubscriptLessStringValid(String npString) 551 { 552 boolean remStringValid = false; 553 554 if(npString != null && npString.length() > 0) 555 { 556 558 560 boolean onlyDelimiterEscaped = isOnlyDelimiterEscaped(npString); 561 boolean containsEscape = npString.indexOf(Tokens.kEscapeChar) != -1; 562 boolean isEscapeValid = ! containsEscape || 563 containsEscape && onlyDelimiterEscaped; 564 565 boolean noMoreStars = npString.indexOf(Tokens.kMoreWildCardsString) == -1; 566 567 if( isEscapeValid && 568 noMoreStars 569 ) 570 { 571 remStringValid = true; 572 } 573 } 574 return ( remStringValid ); 575 } 576 577 578 589 590 private boolean isOnlyDelimiterEscaped(String npString) 591 { 592 boolean onlyDelimiterEscaped = true; 593 594 if(npString != null && npString.length() > 0) 595 { 596 int index = 0; 597 int strlength = npString.length(); 598 599 while(index < strlength) 600 { 601 char ch = npString.charAt(index); 602 if(ch == Tokens.kEscapeChar) 603 { 604 int nextIndex = index + 1; 605 if (nextIndex >= strlength || 606 npString.charAt(nextIndex) != Tokens.kDelimiterChar) 607 { 608 onlyDelimiterEscaped = false; 609 break; } 611 } 612 index++; 613 } 614 } 615 else 616 { 617 onlyDelimiterEscaped = false; 618 } 619 return ( onlyDelimiterEscaped ); 620 } 621 622 } 623 | Popular Tags |