494 * Convert import-style string to regex matching that string. If |
494 * Convert import-style string to regex matching that string. If |
495 * the string is a valid import-style string, return a regex that |
495 * the string is a valid import-style string, return a regex that |
496 * won't match anything. |
496 * won't match anything. |
497 */ |
497 */ |
498 Pattern importStringToPattern(String s) { |
498 Pattern importStringToPattern(String s) { |
499 if (s.equals("*")) { |
499 if (com.sun.tools.javac.processing.JavacProcessingEnvironment.isValidImportString(s)) { |
500 return allMatches; |
500 return com.sun.tools.javac.processing.JavacProcessingEnvironment.validImportStringToPattern(s); |
501 } else { |
501 } else { |
502 String t = s; |
502 Bark bark = Bark.instance(context); |
503 boolean star = false; |
503 bark.aptWarning("MalformedSupportedString", s); |
504 |
504 return com.sun.tools.javac.processing.JavacProcessingEnvironment.noMatches; |
505 /* |
505 } |
506 * Validate string from factory is legal. If the string |
506 } |
507 * has more than one asterisks or the asterisks does not |
|
508 * appear as the last character (preceded by a period), |
|
509 * the string is not legal. |
|
510 */ |
|
511 |
|
512 boolean valid = true; |
|
513 int index = t.indexOf('*'); |
|
514 if (index != -1) { |
|
515 // '*' must be last character... |
|
516 if (index == t.length() -1) { |
|
517 // ... and preceeding character must be '.' |
|
518 if ( index-1 >= 0 ) { |
|
519 valid = t.charAt(index-1) == '.'; |
|
520 // Strip off ".*$" for identifier checks |
|
521 t = t.substring(0, t.length()-2); |
|
522 } |
|
523 } else |
|
524 valid = false; |
|
525 } |
|
526 |
|
527 // Verify string is off the form (javaId \.)+ or javaId |
|
528 if (valid) { |
|
529 String[] javaIds = t.split("\\.", t.length()+2); |
|
530 for(String javaId: javaIds) |
|
531 valid &= isJavaIdentifier(javaId); |
|
532 } |
|
533 |
|
534 if (!valid) { |
|
535 Bark bark = Bark.instance(context); |
|
536 bark.aptWarning("MalformedSupportedString", s); |
|
537 return noMatches; // won't match any valid identifier |
|
538 } |
|
539 |
|
540 String s_prime = s.replaceAll("\\.", "\\\\."); |
|
541 |
|
542 if (s_prime.endsWith("*")) { |
|
543 s_prime = s_prime.substring(0, s_prime.length() - 1) + ".+"; |
|
544 } |
|
545 |
|
546 return Pattern.compile(s_prime); |
|
547 } |
|
548 } |
|
549 |
|
550 private static final Pattern allMatches = Pattern.compile(".*"); |
|
551 private static final Pattern noMatches = Pattern.compile("(\\P{all})+"); |
|
552 } |
507 } |