List of usage examples for java.lang Character isJavaIdentifierStart
public static boolean isJavaIdentifierStart(int codePoint)
From source file:org.wrml.runtime.schema.SchemaBuilder.java
public static String ensureValidJavaIdentifier(final String identifier) { if (identifier == null || RESERVED_WORD_SET.contains(identifier)) { return null; }/*from w w w . j av a2 s. c o m*/ String validJavaIdentifier = identifier; int index = validJavaIdentifier.indexOf('-'); if (index > 0) { validJavaIdentifier = validJavaIdentifier.replace('-', '_'); } if (!Character.isJavaIdentifierStart(validJavaIdentifier.charAt(0))) { validJavaIdentifier = "_" + validJavaIdentifier; } final int validJavaIdentifierLength = validJavaIdentifier.length(); String replaceCharacters = ""; for (int i = 1; i < validJavaIdentifierLength; i++) { char c = validJavaIdentifier.charAt(i); if (!Character.isJavaIdentifierPart(c)) { replaceCharacters += c; } } if (!replaceCharacters.isEmpty()) { validJavaIdentifier = StringUtils.replaceChars(validJavaIdentifier, replaceCharacters, "$"); } return validJavaIdentifier; }
From source file:com.canoo.webtest.boundary.ResetScriptRunner.java
/** * Add a single object into the script context. * * @param key the name in the context this object is to stored under. * @param bean the object to be stored in the script context. *//*from ww w.ja va 2s .c om*/ public void addBean(final String key, final Object bean) { boolean isValid = key.length() > 0 && Character.isJavaIdentifierStart(key.charAt(0)); for (int i = 1; isValid && i < key.length(); i++) { isValid = Character.isJavaIdentifierPart(key.charAt(i)); } if (isValid) { fBeans.put(key, bean); } }
From source file:JavaScriptCompressor.java
/** * Creates a new <code>JavaScriptCompressor</code> instance. * //from w ww. j a va 2 s. c o m * @param script */ private JavaScriptCompressor(String script) { this.script = script; outputBuffer = new StringBuffer(script.length()); nextChar(); while (!endReached) { if (Character.isJavaIdentifierStart(ch)) { renderIdentifier(); } else if (ch == ' ') { skipWhiteSpace(); } else if (isWhitespace()) { // Compress whitespace skipWhiteSpace(); } else if ((ch == '"') || (ch == '\'')) { // Handle strings renderString(); } else if (ch == '/') { // Handle comments nextChar(); if (ch == '/') { nextChar(); skipLineComment(); } else if (ch == '*') { nextChar(); skipBlockComment(); } else { append('/'); } } else { append(ch); nextChar(); } } }
From source file:de.jcup.egradle.core.util.GradleResourceLinkCalculator.java
private GradleHyperLinkResult internalCreateLink(String line, int offsetInLine) { /* e.g. abc defg Test abc */ /* ^-- Test must be identified */ String rightSubString = line.substring(offsetInLine); StringBuilder content = new StringBuilder(); for (char c : rightSubString.toCharArray()) { if (Character.isWhitespace(c)) { break; }//from www .ja va 2 s .c o m if (c == '{') { break; } if (c == ',') { break; } if (c == '(') { break; } if (c == ')') { break; } if (c == '[') { break; } if (c == '<') { break; } if (c == '>') { break; } if (c == '.') { break; } if (!Character.isJavaIdentifierPart(c)) { return null; } content.append(c); } if (StringUtils.isBlank(content)) { return null; } String leftSubString = line.substring(0, offsetInLine); char[] leftCharsArray = leftSubString.toCharArray(); ArrayUtils.reverse(leftCharsArray); int startPos = offsetInLine; for (char c : leftCharsArray) { if (c == '(') { break; } if (c == '<') { break; } if (c == '.') { break; } if (Character.isWhitespace(c)) { break; } if (!Character.isJavaIdentifierPart(c)) { return null; } startPos--; content.insert(0, c); } String linkContent = content.toString(); char firstChar = linkContent.charAt(0); if (!Character.isJavaIdentifierStart(firstChar)) { return null; } /* * currently this calculator only supports correct Type syntax means * first char MUST be upper cased */ if (!Character.isUpperCase(firstChar)) { return null; } GradleHyperLinkResult result = new GradleHyperLinkResult(); result.linkOffsetInLine = startPos; result.linkContent = linkContent; result.linkLength = linkContent.length(); return result; }
From source file:com.yahoo.bard.webservice.config.ModuleLoader.java
/** * A method used to apply validation rules to module names found in resource property files. * Throws exceptions when invalid./*from w ww . j a va2 s .com*/ * * @param name The name under test. * * @throws SystemConfigException when a name fails a validation rule */ public static void validateModuleName(String name) throws SystemConfigException { char[] nameChars = name.toCharArray(); // Module name should not be a single character if (nameChars.length < 2) { LOG.error(INVALID_MODULE_CONFIGURATION.logFormat(MODULE_NAME_IS_TOO_SHORT, name)); throw new SystemConfigException(INVALID_MODULE_CONFIGURATION.format(MODULE_NAME_IS_TOO_SHORT, name)); } List<Character> invalidCharacters = new ArrayList<>(name.length()); if (!Character.isJavaIdentifierStart(name.charAt(0))) { invalidCharacters.add(name.charAt(0)); } name.substring(1).chars().mapToObj(charCode -> (char) charCode) .filter(character -> !Character.isJavaIdentifierPart(character) && character != '-') .forEach(invalidCharacters::add); if (!invalidCharacters.isEmpty()) { String message = String.format(ILLEGAL_CHARACTER_IN_MODULE_NAME, invalidCharacters); LOG.error(INVALID_MODULE_NAME.logFormat(name, message)); throw new SystemConfigException(INVALID_MODULE_NAME.logFormat(name, message)); } }
From source file:nl.strohalm.cyclos.utils.StringHelper.java
/** * Check if the string is a valid Java identifier. It's not checked if the string is a reserved Java word. *//* w ww.j av a 2s.c om*/ public static boolean isValidJavaIdentifier(final String string) { if (StringUtils.isEmpty(string)) { return false; } if (!Character.isJavaIdentifierStart(string.charAt(0))) { return false; } for (int i = 1; i < string.length(); i++) { if (!Character.isJavaIdentifierPart(string.charAt(i))) { return false; } } return true; }
From source file:com.wavemaker.common.util.StringUtils.java
public static String toJavaIdentifier(String s, char replacementChar) { if (ObjectUtils.isNullOrEmpty(s)) { throw new IllegalArgumentException("input cannot be null or empty"); }/* w w w . j ava 2 s . co m*/ String unquoted = unquote(s); if (unquoted.length() > 0) { s = unquoted; } // although '$' is ok, it causes issues with type generation // because of inner class confusion s = s.replace("$", ""); if (s.length() == 0) { s = "" + replacementChar; } StringBuilder rtn = new StringBuilder(); if (JAVA_KEYWORDS.contains(s.toLowerCase()) || !Character.isJavaIdentifierStart(s.charAt(0))) { rtn.append(replacementChar); } if (s.length() == 1) { if (rtn.length() > 0) { return rtn.toString(); } else { return s; } } for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (!Character.isJavaIdentifierPart(c)) { c = replacementChar; } rtn.append(c); } return rtn.toString(); }
From source file:com.jaspersoft.ireport.designer.data.fieldsproviders.ejbql.EJBQLFieldsReader.java
/** * Takes a name and returns the same if it is a Java identifier; * else it substitutes the illegal characters so that it can be an identifier * * @param name/* ww w .j a v a 2 s . co m*/ */ public static String getLiteral(String name) { if (isValidLiteral(name)) { return name; } StringBuffer buffer = new StringBuffer(name.length() + 5); char[] literalChars = new char[name.length()]; name.getChars(0, literalChars.length, literalChars, 0); for (int i = 0; i < literalChars.length; i++) { if (i == 0 && !Character.isJavaIdentifierStart(literalChars[i])) { buffer.append((int) literalChars[i]); } else if (i != 0 && !Character.isJavaIdentifierPart(literalChars[i])) { buffer.append((int) literalChars[i]); } else { buffer.append(literalChars[i]); } } return buffer.toString(); }
From source file:info.novatec.testit.livingdoc.ognl.OgnlResolution.java
private boolean startAsAnIdentifier(String token) { return Character.isJavaIdentifierStart(token.codePointAt(0)); }
From source file:PackageUtils.java
private static String removeIllegalIdentifierChars(String token) { StringBuffer newToken = new StringBuffer(); for (int i = 0; i < token.length(); i++) { char c = token.charAt(i); if (i == 0 && !Character.isJavaIdentifierStart(c)) { // prefix an '_' if the first char is illegal newToken.append("_" + c); } else if (!Character.isJavaIdentifierPart(c)) { // replace the char with an '_' if it is illegal newToken.append('_'); } else {//from w ww . ja v a2s.c om // add the legal char newToken.append(c); } } return newToken.toString(); }