Skip to content

Commit

Permalink
HV-376: Use Character class methods instead of regex categories.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpollet committed Feb 7, 2011
1 parent bc71d86 commit 5af0d1d
Showing 1 changed file with 34 additions and 2 deletions.
Expand Up @@ -44,7 +44,7 @@ public final class PathImpl implements Path, Serializable {
*
* @see <a href="http://www.regexplanet.com/simple/index.jsp">Regular expression tester</a>
*/
private static final Pattern PATH_PATTERN = Pattern.compile( "(\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*)(\\[(\\w*)\\])?(\\.(.*))*" );
private static final Pattern PATH_PATTERN = Pattern.compile( "([^\\[\\.]+)(\\[(\\w*)\\])?(\\.(.*))*" );
private static final int PROPERTY_NAME_GROUP = 1;
private static final int INDEXED_GROUP = 2;
private static final int INDEX_GROUP = 3;
Expand Down Expand Up @@ -285,8 +285,12 @@ private static PathImpl parseProperty(String property) {
Matcher matcher = PATH_PATTERN.matcher( tmp );
if ( matcher.matches() ) {

// create the node
String value = matcher.group( PROPERTY_NAME_GROUP );
if ( !isValidJavaIdentifier( value ) ) {
throw new IllegalArgumentException( value + " is not a valid Java Identifier" );
}

// create the node
path.addNode( value );

// is the node indexable
Expand Down Expand Up @@ -320,4 +324,32 @@ private static PathImpl parseProperty(String property) {

return path;
}

/**
* Validate that the given identifier is a valid Java identifier according to the Java Language Specification,
* <a href="http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.8">chapter 3.8</a>
*
* @param identifier the identifier string
* @return true if the given identifier is a valid Java Identifier
*
* @throws IllegalArgumentException if the identifier is null
*/
private static boolean isValidJavaIdentifier(String identifier) {
Contracts.assertNotNull( identifier, "identifier param cannot be null" );

final char[] identifierChars = identifier.toCharArray();

if ( identifierChars.length == 0 || !Character.isJavaIdentifierStart( (int) identifierChars[0] ) ) {
return false;
}

for ( int i = 1; i < identifierChars.length; i++ ) {
if ( !Character.isJavaIdentifierPart( (int) identifierChars[i] ) ) {
return false;
}
}

return true;
}

}

0 comments on commit 5af0d1d

Please sign in to comment.