Example usage for com.google.common.base CharMatcher javaIsoControl

List of usage examples for com.google.common.base CharMatcher javaIsoControl

Introduction

In this page you can find the example usage for com.google.common.base CharMatcher javaIsoControl.

Prototype

public static CharMatcher javaIsoControl() 

Source Link

Document

Determines whether a character is an ISO control character as specified by Character#isISOControl(char) .

Usage

From source file:com.google.devtools.build.lib.cmdline.LabelValidator.java

/**
 * Performs validity checking of the specified target name. Returns null on success or an error
 * message otherwise./*from   ww  w.j  a  va2  s.  c  o  m*/
 */
@Nullable
public static String validateTargetName(String targetName) {
    // TODO(bazel-team): (2011) allow labels equaling '.' or ending in '/.' for now. If we ever
    // actually configure the target we will report an error, but they will be permitted for
    // data directories.

    // TODO(bazel-team): (2011) Get rid of this code once we have reached critical mass and can
    // pressure developers to clean up their BUILD files.

    // Code optimized for the common case: success.
    int len = targetName.length();
    if (len == 0) {
        return "empty target name";
    }
    // Forbidden start chars:
    char c = targetName.charAt(0);
    if (c == '/') {
        return "target names may not start with '/'";
    } else if (c == '.') {
        if (targetName.startsWith("../") || targetName.equals("..")) {
            return "target names may not contain up-level references '..'";
        } else if (targetName.equals(".")) {
            return null; // See comment above; ideally should be an error.
        } else if (targetName.startsWith("./")) {
            return "target names may not contain '.' as a path segment";
        }
    }

    // Give a friendly error message on CRs in target names
    if (targetName.endsWith("\r")) {
        return "target names may not end with carriage returns "
                + "(perhaps the input source is CRLF-terminated)";
    }

    for (int ii = 0; ii < len; ++ii) {
        c = targetName.charAt(ii);
        if (ALWAYS_ALLOWED_TARGET_CHARACTERS.matches(c)) {
            continue;
        }
        if (c == '.') {
            continue;
        }
        if (c == '/') {
            if (stringRegionMatch(targetName, "/../", ii)) {
                return "target names may not contain up-level references '..'";
            } else if (stringRegionMatch(targetName, "/./", ii)) {
                return "target names may not contain '.' as a path segment";
            } else if (stringRegionMatch(targetName, "//", ii)) {
                return "target names may not contain '//' path separators";
            }
            continue;
        }
        if (CharMatcher.javaIsoControl().matches(c)) {
            return "target names may not contain non-printable characters: '"
                    + String.format("\\x%02X", (int) c) + "'";
        }
        return "target names may not contain '" + c + "'";
    }
    // Forbidden end chars:
    if (c == '.') {
        if (targetName.endsWith("/..")) {
            return "target names may not contain up-level references '..'";
        } else if (targetName.endsWith("/.")) {
            return null; // See comment above; ideally should be an error.
        }
    }
    if (c == '/') {
        return "target names may not end with '/'";
    }
    return null; // ok
}