/** * ** Returns true if the specified character is a valid character to use in ** an ID ** @param ch * The character ** @return True if the specified character is a valid character to use in ** an * ID */ public static boolean isValidIDChar(char ch) { // At a minimum, avoid the following special chars: // $ - substitution character // {} - have had problems using this character in MySQL // % - MySQL wildcard character // * - generic wildcard character // \ - escape character // ? - just don't use it // , - will get confused as a field separator // | - will get confused as a field separator // / - will get confused as a field separator // = - will get confused as a key=value separator // "'` - quotation characters // # - possible beginning of comment // ~ - just don't use it // ? - just don't use it // ^ - just don't use it // Pending possibles: // ! - Looks like '|'? // - - ? // + - ? // @abc,#abc,_abc,.abc,&abc if (Character.isLetterOrDigit(ch)) { return true; } else if ((ch == '.') || (ch == '_')) { // definately accept these return true; } else if ((ch == '@') || (ch == '&') || (ch == '-')) { // we'll consider these return true; } else { return false; } }
private static String FilterID(String id) { if (id == null) { return null; } else { StringBuffer newID = new StringBuffer(); int st = 0; for (int i = 0; i < id.length(); i++) { char ch = Character.toLowerCase(id.charAt(i)); if (Character.isLetterOrDigit(ch)) { newID.append(ch); st = 1; } else if (st == 1) { newID.append("_"); st = 0; } else { // ignore char } } while ((newID.length() > 0) && (newID.charAt(newID.length() - 1) == '_')) { newID.setLength(newID.length() - 1); } return newID.toString(); } }