/** * Checks if the char is either a US ASCII char, an underscore, * of a digit. * * @param c char to test. * @return true if the char is a valid ADQL identifier char, false otherwise. */ public static boolean isValidIdentifierCharacter(char c) { if (c == '_' || isAsciiLetter(c) || Character.isDigit(c)) { return true; } return false; } }
/** * Validates that the given String is a valid ADQL identifier. * * @param identifier String to be tested. * @throws ADQLIdentifierException */ public static void checkValidIdentifier(String identifier) throws ADQLIdentifierException { // identifier shouldn't be null and cannot be an empty string. if (identifier == null || identifier.trim().isEmpty()) { throw new ADQLIdentifierException("Identifier is null or empty"); } // identifier cannot start with, contain, or end, with a space. if (identifier.startsWith(" ") || identifier.contains(" ") || identifier.endsWith(" ")) { throw new ADQLIdentifierException("Identifier contains spaces"); } // identifier must start with a letter {aA-zZ}. if (!isAsciiLetter(identifier.charAt(0))) { throw new ADQLIdentifierException("Identifier must start with a letter"); } // subsequent characters must be letters, underscores, or digits. for (int i = 1; i < identifier.length(); i++) { if (!isValidIdentifierCharacter(identifier.charAt(i))) { throw new ADQLIdentifierException("Identifier contains an invalid character " + identifier.charAt(i)); } } }