Example usage for java.lang Character isJavaIdentifierStart

List of usage examples for java.lang Character isJavaIdentifierStart

Introduction

In this page you can find the example usage for java.lang Character isJavaIdentifierStart.

Prototype

public static boolean isJavaIdentifierStart(int codePoint) 

Source Link

Document

Determines if the character (Unicode code point) is permissible as the first character in a Java identifier.

Usage

From source file:org.matonto.rdf.orm.generate.SourceGenerator.java

/**
 * Simple method to strip whitespaces from the name. It will also ensure it
 * is a valid class or field name./*from  www .j  a  v  a2s  .  com*/
 *
 * @param input The input string
 * @return The stripped and cleaned output name
 */
protected static String stripWhiteSpace(final String input) {
    StringBuilder builder = new StringBuilder();
    boolean lastIsWhiteSpace = false;
    boolean first = true;
    for (char c : input.toCharArray()) {
        if (first && !Character.isJavaIdentifierStart(c) && Character.isJavaIdentifierPart(c)) {
            builder.append("_");
            builder.append(c);
            first = false;
        } else if (Character.isWhitespace(c)) {
            lastIsWhiteSpace = true;
        } else if (Character.isJavaIdentifierPart(c)) {
            builder.append(lastIsWhiteSpace ? StringUtils.capitalize(c + "") : c);
            lastIsWhiteSpace = false;
            first = false;
        }
    }
    return builder.toString();
}

From source file:net.sourceforge.squirrel_sql.fw.util.StringUtilities.java

public static String javaNormalize(String text) {
    StringBuffer buf = new StringBuffer(text.length());

    if (Character.isJavaIdentifierStart(text.charAt(0))) {
        buf.append(text.charAt(0));/*from  ww  w.ja  va2 s.c  o  m*/
    } else {
        buf.append('_');
    }

    for (int i = 1; i < text.length(); ++i) {
        if (Character.isLetterOrDigit(text.charAt(i))) {
            buf.append(text.charAt(i));
        } else {
            buf.append('_');
        }
    }

    String ret = buf.toString();

    return ret;
}

From source file:org.nsesa.editor.app.xsd.FileClassOverlayGenerator.java

/**
 * Generate overlay resources for element types.
 *
 * @param elementClasses         the map with overlay classes for each namespace.
 * @param packageNameGenerator   the package name generator
 * @param directoryNameGenerator the directory name generator
 */// w  w w.  java 2s.  c o  m
private void generateOverlayResources(final Map<String, List<OverlayClass>> elementClasses,
        final PackageNameGenerator packageNameGenerator, final PackageNameGenerator directoryNameGenerator) {
    // create the resource for each namespace
    for (final Map.Entry<String, List<OverlayClass>> entry : elementClasses.entrySet()) {
        String factoryName = directoryNameGenerator.getPackageName(entry.getKey()).replace("_", "");
        if (!Character.isJavaIdentifierStart(factoryName.charAt(0))) {
            factoryName = "_" + factoryName;
        }

        final String className = StringUtils.capitalize(factoryName) + "OverlayLocalizableResource";
        final File file = new File(generatedSourcesDirectory, className + ".java");

        final Map<String, Object> context = new HashMap<String, Object>();

        final OverlayClass factoryClass = new OverlayClass(className, null, OverlayType.Unknown);
        factoryClass.setClassName(className);
        factoryClass.setNamespaceURI(entry.getKey());
        factoryClass.setPackageName(
                basePackageName.endsWith(".") ? basePackageName.substring(0, basePackageName.length() - 1)
                        : basePackageName);

        context.put("overlayClass", factoryClass);
        context.put("factoryName", factoryName);
        context.put("overlayClasses", entry.getValue());
        context.put("packageNameGenerator", packageNameGenerator);
        writeToFile(file, context, OVERLAY_RESOURCE_TEMPLATE_NAME);
    }
}

From source file:org.apache.airavata.common.utils.StringUtil.java

/**
 * Converts a specified string to a Java identifier.
 * //from w w  w . j a  v a 2 s. com
 * @param name
 * @return the Java identifier
 */
public static String convertToJavaIdentifier(String name) {

    final char REPLACE_CHAR = '_';

    if (name == null || name.length() == 0) {
        return "" + REPLACE_CHAR;
    }

    StringBuilder buf = new StringBuilder();

    char c = name.charAt(0);
    if (!Character.isJavaIdentifierStart(c)) {
        // Add _ at the beggining instead of replacing it to _. This is
        // more readable if the name is like 3D_Model.
        buf.append(REPLACE_CHAR);
    }

    for (int i = 0; i < name.length(); i++) {
        c = name.charAt(i);
        if (Character.isJavaIdentifierPart(c)) {
            buf.append(c);
        } else {
            buf.append(REPLACE_CHAR);
        }
    }

    return buf.toString();
}

From source file:com.xwtec.xwserver.util.json.util.JSONUtils.java

/**
 * Returns trus if str represents a valid Java identifier.
 *//*from  ww w  .jav  a2  s . co m*/
public static boolean isJavaIdentifier(String str) {
    if (str.length() == 0 || !Character.isJavaIdentifierStart(str.charAt(0))) {
        return false;
    }
    for (int i = 1; i < str.length(); i++) {
        if (!Character.isJavaIdentifierPart(str.charAt(i))) {
            return false;
        }
    }
    return true;
}

From source file:com.sshtools.j2ssh.util.ExtensionClassLoader.java

private byte[] loadClassFromDirectory(File dir, String name, ClassCacheEntry cache) throws IOException {
    // Translate class name to file name
    String classFileName = name.replace('.', File.separatorChar) + ".class";

    // Check for garbage input at beginning of file name
    // i.e. ../ or similar
    if (!Character.isJavaIdentifierStart(classFileName.charAt(0))) {
        // Find real beginning of class name
        int start = 1;

        while (!Character.isJavaIdentifierStart(classFileName.charAt(start++))) {
            ;/*w  w  w.  j a v  a2  s .c o  m*/
        }

        classFileName = classFileName.substring(start);
    }

    File classFile = new File(dir, classFileName);

    if (classFile.exists()) {
        if (cache != null) {
            cache.origin = classFile;

        }
        InputStream in = new FileInputStream(classFile);

        try {
            return loadBytesFromStream(in, (int) classFile.length());
        } finally {
            in.close();
        }
    } else {
        // Not found
        return null;
    }
}

From source file:net.sf.ufsc.ServiceLoader.java

private int parseLine(Class<?> service, URL u, BufferedReader r, int lc, List<String> names)
        throws IOException, ServiceConfigurationError {
    String ln = r.readLine();/* w w  w  .j a  v a 2s  .co m*/
    if (ln == null) {
        return -1;
    }
    int ci = ln.indexOf('#');
    if (ci >= 0)
        ln = ln.substring(0, ci);
    ln = ln.trim();
    int n = ln.length();
    if (n != 0) {
        if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0))
            fail(service, u, lc, "Illegal configuration-file syntax");
        int cp = ln.codePointAt(0);
        if (!Character.isJavaIdentifierStart(cp))
            fail(service, u, lc, "Illegal provider-class name: " + ln);
        for (int i = Character.charCount(cp); i < n; i += Character.charCount(cp)) {
            cp = ln.codePointAt(i);
            if (!Character.isJavaIdentifierPart(cp) && (cp != '.'))
                fail(service, u, lc, "Illegal provider-class name: " + ln);
        }
        if (!providers.containsKey(ln) && !names.contains(ln))
            names.add(ln);
    }
    return lc + 1;
}

From source file:org.nsesa.editor.app.xsd.FileClassOverlayGenerator.java

/**
 * Generate overlay messages for element types based on xsd documentation.
 *
 * @param elementClasses         the map with overlay classes for each namespace.
 * @param packageNameGenerator   the package name generator
 * @param directoryNameGenerator the directory name generator
 *//*from ww w .j ava2  s . c  om*/
private void generateOverlayMessages(final Map<String, List<OverlayClass>> elementClasses,
        final PackageNameGenerator packageNameGenerator, final PackageNameGenerator directoryNameGenerator) {
    // create overlay message properties file for each namespace
    for (final Map.Entry<String, List<OverlayClass>> entry : elementClasses.entrySet()) {
        String factoryName = directoryNameGenerator.getPackageName(entry.getKey()).replace("_", "");
        if (!Character.isJavaIdentifierStart(factoryName.charAt(0))) {
            factoryName = "_" + factoryName;
        }

        final String className = StringUtils.capitalize(factoryName) + "OverlayMessages";
        final File classFile = new File(generatedSourcesDirectory, className + ".java");
        final File propFile = new File(generatedSourcesDirectory, className + ".properties");

        final Map<String, Object> context = new HashMap<String, Object>();

        final OverlayClass factoryClass = new OverlayClass(className, null, OverlayType.Unknown);
        factoryClass.setClassName(className);
        factoryClass.setNamespaceURI(entry.getKey());
        factoryClass.setPackageName(
                basePackageName.endsWith(".") ? basePackageName.substring(0, basePackageName.length() - 1)
                        : basePackageName);

        context.put("overlayClass", factoryClass);
        context.put("overlayClasses", entry.getValue());
        context.put("packageNameGenerator", packageNameGenerator);

        writeToFile(classFile, context, OVERLAY_MESSAGE_TEMPLATE_NAME);
        writeToFile(propFile, context, OVERLAY_MESSAGE_PROP_TEMPLATE_NAME);

    }
}

From source file:com.sshtools.j2ssh.util.DynamicClassLoader.java

private byte[] loadClassFromDirectory(File dir, String name, ClassCacheEntry cache) throws IOException {
    // Translate class name to file name
    String classFileName = name.replace('.', File.separatorChar) + ".class";

    // Check for garbage input at beginning of file name
    // i.e. ../ or similar
    if (!Character.isJavaIdentifierStart(classFileName.charAt(0))) {
        // Find real beginning of class name
        int start = 1;

        while (!Character.isJavaIdentifierStart(classFileName.charAt(start++))) {
            ;/*from   w ww  .  j  ava2 s .c  om*/
        }

        classFileName = classFileName.substring(start);
    }

    File classFile = new File(dir, classFileName);

    if (classFile.exists()) {
        cache.origin = classFile;

        InputStream in = new FileInputStream(classFile);

        try {
            return loadBytesFromStream(in, (int) classFile.length());
        } finally {
            in.close();
        }
    } else {
        // Not found
        return null;
    }
}

From source file:pt.webdetails.cpk.CpkApi.java

private String sanitizePluginId(String pluginId) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < pluginId.length(); i++) {
        char c = pluginId.charAt(i);
        if ((Character.isJavaIdentifierStart(c) && i == 0) || (Character.isJavaIdentifierPart(c) && i > 0)) {
            sb.append(c);//from  ww  w. jav a 2  s.c o m
        }
    }
    return sb.toString();
}