Example usage for java.lang Character isJavaIdentifierPart

List of usage examples for java.lang Character isJavaIdentifierPart

Introduction

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

Prototype

public static boolean isJavaIdentifierPart(int codePoint) 

Source Link

Document

Determines if the character (Unicode code point) may be part of a Java identifier as other than the first character.

Usage

From source file:com.github.magicsky.sya.checkers.TestSourceReader.java

/**
 * Reads a section in comments form the source of the given class. The section
 * is started with '// {tag}' and ends with the first line not started by '//'
 *
 * @since 4.0//from   w  w  w. j  a  va  2s.com
 */
public static String readTaggedComment(Bundle bundle, String srcRoot, Class clazz, final String tag)
        throws IOException {
    IPath filePath = new Path(srcRoot + '/' + clazz.getName().replace('.', '/') + ".java");

    InputStream in = FileLocator.openStream(bundle, filePath, false);
    LineNumberReader reader = new LineNumberReader(new InputStreamReader(in));
    boolean found = false;
    final StringBuilder content = new StringBuilder();
    try {
        String line = reader.readLine();
        while (line != null) {
            line = line.trim();
            if (line.startsWith("//")) {
                line = line.substring(2);
                if (found) {
                    content.append(line);
                    content.append('\n');
                } else {
                    line = line.trim();
                    if (line.startsWith("{" + tag)) {
                        if (line.length() == tag.length() + 1
                                || !Character.isJavaIdentifierPart(line.charAt(tag.length() + 1))) {
                            found = true;
                        }
                    }
                }
            } else if (found) {
                break;
            }
            line = reader.readLine();
        }
    } finally {
        reader.close();
    }
    Assert.assertTrue("Tag '" + tag + "' is not defined inside of '" + filePath + "'.", found);
    return content.toString();
}

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

/**
 * Returns trus if str represents a valid Java identifier.
 *//*from w w  w .  j  a v  a 2  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.netspective.sparx.util.xml.XmlSource.java

/**
 * Given a text string, return a string that would be suitable for that string to be used
 * as a Java identifier (as a variable or method name). Depending upon whether ucaseInitial
 * is set, the string starts out with a lowercase or uppercase letter. Then, the rule is
 * to convert all periods into underscores and title case any words separated by
 * underscores. This has the effect of removing all underscores and creating mixed case
 * words. For example, Person_Address becomes personAddress or PersonAddress depending upon
 * whether ucaseInitial is set to true or false. Person.Address would become Person_Address.
 *//*from  w  w  w  . ja v  a2 s .  co m*/
public static String xmlTextToJavaIdentifier(String xml, boolean ucaseInitial) {
    if (xml == null || xml.length() == 0)
        return xml;

    StringBuffer identifier = new StringBuffer();
    char ch = xml.charAt(0);
    identifier.append(ucaseInitial ? Character.toUpperCase(ch) : Character.toLowerCase(ch));

    boolean uCase = false;
    for (int i = 1; i < xml.length(); i++) {
        ch = xml.charAt(i);
        if (ch == '.') {
            identifier.append('_');
        } else if (ch != '_' && Character.isJavaIdentifierPart(ch)) {
            identifier.append(Character.isUpperCase(ch) ? ch
                    : (uCase ? Character.toUpperCase(ch) : Character.toLowerCase(ch)));
            uCase = false;
        } else
            uCase = true;
    }
    return identifier.toString();
}

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();//from  w w w.  j  a va  2  s  .  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: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 w w w  .j a v a2  s. com
        }
    }
    return sb.toString();
}

From source file:org.openanzo.test.QueryTestSuiteBase.java

@SuppressWarnings("all")
protected QueryType getQueryType(String query) {
    String lc = query.toLowerCase();
    TreeMap<Integer, QueryType> m = new TreeMap<Integer, QueryType>();
    m.put(Integer.valueOf(lc.indexOf("select")), QueryType.SELECT);
    m.put(Integer.valueOf(lc.indexOf("construct")), QueryType.CONSTRUCT);
    m.put(Integer.valueOf(lc.indexOf("ask")), QueryType.ASK);
    m.put(Integer.valueOf(lc.indexOf("describe")), QueryType.DESCRIBE);
    for (Entry<Integer, QueryType> e : m.entrySet()) {
        int index = e.getKey();
        if (index > -1) {
            // determine if this index is inside a BASE or PREFIX clause. We do this by determining if
            // a colon immediately follows it or it is inside angle brackets
            int x;
            for (x = index + 3; Character.isJavaIdentifierPart(lc.charAt(x)); x++)
                ; //earliest the colon could be, if we're in ask
            if (lc.charAt(x) != ':') {
                int lt = lc.lastIndexOf('<', index);
                int gt = lc.lastIndexOf('>', index);
                if (lt == -1 || gt > lt) {
                    // no < before us, or else there's a > after the last <
                    return e.getValue();
                }/*from www. j a va  2s.  com*/
            }
        }
    }
    fail("Unknown query type for query: " + query);
    return null;
}

From source file:com.netspective.sparx.util.xml.XmlSource.java

/**
 * Given a text string, return a string that would be suitable for that string to be used
 * as a Java constant (public static final XXX). The rule is to basically take every letter
 * or digit and return it in uppercase and every non-letter or non-digit as an underscore.
 *//* ww w . ja v  a2 s  .c  o m*/
public static String xmlTextToJavaConstant(String xml) {
    if (xml == null || xml.length() == 0)
        return xml;

    StringBuffer constant = new StringBuffer();
    for (int i = 0; i < xml.length(); i++) {
        char ch = xml.charAt(i);
        constant.append(Character.isJavaIdentifierPart(ch) ? Character.toUpperCase(ch) : '_');
    }
    return constant.toString();
}

From source file:hudson.tasks.junit.CaseResult.java

/**
 * Gets the version of {@link #getName()} that's URL-safe.
 *///from ww  w  . j  a v a 2s  .  c om
public @Override synchronized String getSafeName() {
    if (safeName != null) {
        return safeName;
    }
    StringBuilder buf = new StringBuilder(testName);
    for (int i = 0; i < buf.length(); i++) {
        char ch = buf.charAt(i);
        if (!Character.isJavaIdentifierPart(ch))
            buf.setCharAt(i, '_');
    }
    Collection<CaseResult> siblings = (classResult == null ? Collections.<CaseResult>emptyList()
            : classResult.getChildren());
    return safeName = uniquifyName(siblings, buf.toString());
}

From source file:com.cisco.step.jenkins.plugins.jenkow.WfUtil.java

/**
 * Produces a valid workflow ID out of a given string.
 * Replaces each invalid character with an underscore.
 */// w  w  w. j ava  2  s  .co  m
static String mkWorkflowId(String s) {
    if (s == null || s.length() < 1)
        return s;

    // TODO 5: figure out what's the actual definition for valid workflow IDs
    StringBuffer sb = new StringBuffer();

    char c = s.charAt(0);
    sb.append(Character.isJavaIdentifierStart(c) ? c : '_');
    for (int i = 1, n = s.length(); i < n; i++) {
        c = s.charAt(i);
        sb.append(Character.isJavaIdentifierPart(c) ? c : '_');
    }

    return sb.toString();
}

From source file:bear.plugins.groovy.GroovyCodeCompleter.java

static List<Token> tokenize(String script, int start, int end) {
    Token currentToken;//from  w ww .  j ava2 s . c o  m

    int nonSpaceStart = firstNonSpace(script, start, 1);

    List<Token> tokens = Lists.newArrayList(currentToken = new Token(nonSpaceStart));

    char matchingQuote = 0;

    int openedBrackets = 0;
    int closedBrackets = 0;

    for (int pos = nonSpaceStart; pos < end; pos++) {
        char ch = script.charAt(pos);

        //check string mode, copied
        if (ch == '\"' || ch == '\'') {
            matchingQuote = closeOpenQuotes(matchingQuote, ch);

            continue;
        }

        if (matchingQuote != 0)
            continue;

        if (ch == '(' && !currentToken.method) {
            currentToken.method = true;
            int nonSpace = firstNonSpace(script, pos - 1, -1) + 1;
            currentToken.name = script.substring(currentToken.start, nonSpace);
        }

        final boolean isBracket;

        switch (ch) {
        case ')':
            closedBrackets++;
            isBracket = true;
            break;

        case '(':
            openedBrackets++;
            isBracket = true;
            break;
        default:
            isBracket = false;
        }

        if (openedBrackets > closedBrackets) {
            continue;
        }

        if (openedBrackets > 0 && openedBrackets == closedBrackets) {
            currentToken.end = pos + 1;
            openedBrackets = 0;
            closedBrackets = 0;
        }

        if (isBracket)
            continue;

        if (ch != '.' && !Character.isJavaIdentifierPart(ch) && !currentToken.method) {
            //spaces after identifier, in method there can be anything
            currentToken.end = pos;
            currentToken.name = currentToken.text(script);
            continue;
        }

        boolean atEnd = pos >= end - 1;
        boolean isDot = ch == '.';

        if (isDot || atEnd) {
            if (currentToken.end == -1) { //end is not -1 for a method
                if (atEnd) {
                    if (isDot) {
                        currentToken.end = pos;
                    } else {
                        currentToken.end = end;
                    }
                } else {
                    currentToken.end = firstNonSpace(script, pos - 1, -1) + 1;
                }

                currentToken.name = currentToken.text(script);
            }

            int nonSpace = firstNonSpace(script, pos + 1, 1);

            pos = nonSpace;

            atEnd = pos >= end - 1;

            if (!atEnd) {
                tokens.add(currentToken = new Token(nonSpace));
            }

            continue;
        }

    }
    return tokens;
}