Example usage for java.lang Character isWhitespace

List of usage examples for java.lang Character isWhitespace

Introduction

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

Prototype

public static boolean isWhitespace(int codePoint) 

Source Link

Document

Determines if the specified character (Unicode code point) is white space according to Java.

Usage

From source file:com.puppycrawl.tools.checkstyle.api.Utils.java

/**
 * Returns the length of a string ignoring all trailing whitespace. It is a
 * pity that there is not a trim() like method that only removed the
 * trailing whitespace./*from  ww w. j  a  v  a2s .  c o  m*/
 * @param line the string to process
 * @return the length of the string ignoring all trailing whitespace
 **/
public static int lengthMinusTrailingWhitespace(String line) {
    int len = line.length();
    for (int i = len - 1; i >= 0; i--) {
        if (!Character.isWhitespace(line.charAt(i))) {
            break;
        }
        len--;
    }
    return len;
}

From source file:com.rathravane.drumlin.util.JsonBodyReader.java

/**
 * read the bytes for objects. If the bytes contain a single JSON object and the path is
 * not null, the objects are loaded from the value named by path rather than the top-level
 * object.//  ww w .jav  a  2  s.  c om
 *  
 * @param bytes
 * @param path
 * @return a list of 0 or more JSON objects
 * @throws IOException
 * @throws JSONException
 */
public static List<JSONObject> readBodyForObjects(final byte[] bytes, String path)
        throws IOException, JSONException {
    final LinkedList<JSONObject> result = new LinkedList<JSONObject>();

    // determine the first token in the stream to decide if we're reading a single
    // object or an array.
    boolean isSingleObject = false;
    {
        final ByteArrayInputStream s = new ByteArrayInputStream(bytes);
        final JSONTokener t = new JSONTokener(s);

        char c = t.next();
        while (Character.isWhitespace(c))
            c = t.next();

        switch (c) {
        case '{':
            isSingleObject = true;
            break;
        case '[':
            isSingleObject = false;
            break;
        default:
            throw new JSONException("Expected an object or an array of objects.");
        }
        s.close();
    }

    if (isSingleObject) {
        final String jsonStream = new String(bytes, utf8);
        final JSONObject o = new JSONObject(jsonStream);

        if (path != null) {
            final Object oo = o.opt(path);
            if (oo instanceof JSONObject) {
                result.add((JSONObject) oo);
            } else if (oo instanceof JSONArray) {
                result.addAll(readArrayForObjects((JSONArray) oo));
            } else {
                throw new JSONException("Couldn't read object at path [" + path + "].");
            }
        } else {
            result.add(o);
        }
    } else {
        final String jsonStream = new String(bytes);
        final JSONArray a = new JSONArray(jsonStream);
        result.addAll(readArrayForObjects(a));
    }

    return result;
}

From source file:Main.java

/**
 * Removes excess indention from the provided text.  Also trims blank lines
 * from the beginning and end of the text.
 * //from   w ww .  ja  v  a 2s .c  o m
 * @param text The text to unindent.
 * @return The reformatted text.
 */
public static String unindentTextData(String text) {
    // Exclude blank lines at the beginning.
    int start = 0;
    while (start < text.length()) {
        if (!Character.isWhitespace(text.charAt(start)))
            break;
        else
            ++start;
    }
    if (start == text.length())
        return ""; //$NON-NLS-1$
    while (true) {
        if (text.charAt(start) == '\n') {
            ++start;
            break;
        } else if (start == 0)
            break;
        else
            --start;
    }
    // Exclude blank lines at the end.
    int end = text.length();
    while (Character.isWhitespace(text.charAt(end - 1)))
        --end;
    while (true) {
        if (text.charAt(end - 1) == '\n') {
            --end;
            if (text.charAt(end - 1) == '\r')
                --end;
            break;
        } else if (end == text.length())
            break;
        else
            ++end;
    }
    // Determine the indent that was used for the first line.
    int indentLength = 0;
    while (Character.isWhitespace(text.charAt(start + indentLength)))
        ++indentLength;
    // Validate the indent against all other lines.
    int index = start + indentLength, lineCount = 1;
    while (true) {
        // Check to see if any indent exists.
        if (indentLength == 0)
            return text.substring(start, end);
        // Move to the next line.
        while (index < end && text.charAt(index) != '\n')
            ++index;
        ++index;
        if (index >= end)
            break;
        ++lineCount;
        // Validate the indent.
        for (int i = 0; i < indentLength; ++i)
            if (text.charAt(start + i) != text.charAt(index + i))
                indentLength = i;
        index += indentLength;
    }
    // Remove the indent and return the results.
    StringBuffer result = new StringBuffer((end - start) - (indentLength * lineCount));
    for (int i = start, j = 0; i < end; ++i) {
        while (j < indentLength) {
            ++i;
            ++j;
        }
        char c = text.charAt(i);
        result.append(c);
        if (c == '\n')
            j = 0;
    }
    return result.toString();
}

From source file:net.solarnetwork.domain.BasicLocation.java

private static boolean hasText(String s) {
    if (s == null || s.length() < 1) {
        return false;
    }//from w  w  w  .  j  ava2 s.  c  o  m
    int strLen = s.length();
    for (int i = 0; i < strLen; i++) {
        if (!Character.isWhitespace(s.charAt(i))) {
            return true;
        }
    }
    return false;
}

From source file:com.medallia.tiny.Strings.java

/** Return the given string with any white space on the right side removed */
public static String trimRight(String s) {
    int k = s.length();
    while (k > 0 && Character.isWhitespace(s.charAt(k - 1)))
        k--;/*from  www. jav a 2 s  . c  om*/
    return s.substring(0, k);
}

From source file:com.homeadvisor.kafdrop.config.ini.IniFileReader.java

/**
 * Searches for a separator character directly before a quoting character.
 * If the first non-whitespace character before a quote character is a
 * separator, it is considered the "real" separator in this line - even if
 * there are other separators before.// w w w .jav a 2 s .c  o  m
 *
 * @param line       the line to be investigated
 * @param quoteIndex the index of the quote character
 * @return the index of the separator before the quote or &lt; 0 if there is
 * none
 */
private static int findSeparatorBeforeQuote(String line, int quoteIndex) {
    int index = quoteIndex - 1;
    while (index >= 0 && Character.isWhitespace(line.charAt(index))) {
        index--;
    }

    if (index >= 0 && SEPARATOR_CHARS.indexOf(line.charAt(index)) < 0) {
        index = -1;
    }

    return index;
}

From source file:StringUtil.java

/**
 * Deletes all whitespace from a String.
 * Whitespace is defined by Character.isWhitespace
 *
 * @param str  String target to delete whitespace from
 * @return the text without whitespace/*from w  w  w  .j  a  va2  s .c  om*/
 * @throws NullPointerException
 */
public static String deleteWhitespace(String str) {
    StringBuffer buffer = new StringBuffer();
    int sz = str.length();
    for (int i = 0; i < sz; i++) {
        if (!Character.isWhitespace(str.charAt(i))) {
            buffer.append(str.charAt(i));
        }
    }
    return buffer.toString();
}

From source file:org.vietspider.server.handler.cms.metas.FilterContentHandler.java

public String handle(final HttpRequest request, final HttpResponse response, final HttpContext context,
        String... params) throws Exception {
    String pattern = params[1];//from w  w w  .  j  a  va2  s .  c o  m
    int idx = pattern.indexOf('=');
    if (idx < 0 || idx >= pattern.length() - 1) {
        throw new IndexOutOfBoundsException("Incorrect parammeter");
    }

    String filter = URLDecoder.decode(pattern.substring(idx + 1), "UTF-8");

    MetaList metas = new MetaList();
    metas.setAction("FILTER");
    metas.setCurrentPage(Integer.parseInt(params[0]));

    File file = UtilFile.getFile("system/plugin/filter", NameConverter.encode(filter));
    if (file.exists() && file.length() > 0) {
        String text = new String(RWData.getInstance().load(file), Application.CHARSET);

        int start = 0;
        while (start < text.length()) {
            char c = text.charAt(start);
            if (Character.isLetterOrDigit(c))
                break;
            start++;
        }

        int end = text.length() - 1;
        while (end > 0) {
            char c = text.charAt(end);
            if (Character.isLetterOrDigit(c))
                break;
            end--;
        }

        StringBuilder builder = new StringBuilder("\"");
        for (int i = start; i <= end; i++) {
            char c = text.charAt(i);
            if (c == ',') {
                builder.append("\" AND \"");
            } else if (c == '\n') {
                builder.append("\" OR \"");
            } else if (Character.isLetterOrDigit(c)) {
                builder.append(c);
            } else if (Character.isSpaceChar(c) || Character.isWhitespace(c)) {
                builder.append(' ');
            }
        }
        builder.append("\"");

        //      System.out.println(" thay co filter "+ builder);

        ArticleSearchQuery query = new ArticleSearchQuery();
        query.setPattern(builder.toString());

        if (Application.LICENSE != Install.PERSONAL) {
            query.setHighlightStart("no");
            ArticleIndexStorage.getInstance().search(metas, query);
        }
    }

    metas.setTitle(filter);
    metas.setUrl("?text=" + pattern.substring(idx + 1));

    return write(request, response, context, metas, params);
}

From source file:eulermind.importer.LineNode.java

LineNode(String line) {
    super();//from w w  w.  jav a2s  .  c  o  m

    int textStart = 0;
    int textEnd = 0;

    for (textStart = 0; textStart < line.length()
            && Character.isWhitespace(line.charAt(textStart)); textStart++) {
        m_indent += (line.charAt(textStart) == '\t') ? 4 : 1;
    }

    for (textEnd = line.length(); textEnd > textStart
            && Character.isWhitespace(line.charAt(textEnd - 1)); textEnd--) {
    }

    m_trimLine = line.substring(textStart, textEnd);

    m_blankLines = m_trimLine.length() == 0 ? 1 : 0;
}

From source file:de.uniwue.info6.misc.StringTools.java

/**
 *
 *
 * @param text/*from w w w  .ja va2s .  c om*/
 * @param leftIndex
 * @return
 */
public static boolean trailingCharacter(String text, int index, boolean left) {
    boolean hasValidTrailingCharacter = false;

    if ((left && index == 0) || (!left && index == text.length()))
        return true;

    while ((left && index != 0) || (!left && index != text.length())) {
        Character c = left ? text.charAt(--index) : text.charAt(index++);
        if (!Character.isWhitespace(c)) {
            if (c == '.')
                return left ? false : true;
            if (c == ';' || c == ',' || c == '`' || c == ')' || c == '(')
                hasValidTrailingCharacter = true;
            index = left ? 0 : text.length();
        } else {
            hasValidTrailingCharacter = true;
        }
    }

    if (hasValidTrailingCharacter)
        return true;
    return false;
}