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:MsgPrinter.java

/**
 * Returns the position of the first character in the next word, starting
 * from 'from', if a newline is encountered first then the index of the
 * newline character plus 1 is returned. If the end of the string is
 * encountered then IS_EOS is returned. Words are defined as any
 * concatenation of 1 or more characters which are not
 * whitespace. Whitespace characters are those for which
 * Character.isWhitespace() returns true (that method is used).
 *
 * <P>Non-whitespace characters are defined as in the
 * Character.isWhitespace method (that method is used).
 *
 * @param str The string to parse/*  ww w  .  j  a  va 2s .c  o m*/
 *
 * @param from The index where to start parsing
 *
 * @return The index of the first character of the next word, or the index
 * of the newline plus 1, or IS_EOS.
 *
 *
 * */
private int nextWord(String str, int from) {
    final int len = str.length();
    char c = '\0';
    // First skip all whitespace, but new lines
    while (from < len && (c = str.charAt(from)) != '\n' && Character.isWhitespace(c)) {
        from++;
    }
    if (from >= len) {
        return IS_EOS;
    } else if (c == '\n') {
        return from + 1;
    } else {
        return from;
    }
}

From source file:graphene.util.StringUtils.java

/**
 * Check whether the given String has actual text. More specifically,
 * returns <code>true</code> if the string not <code>null</code>, its length
 * is greater than 0, and it contains at least one non-whitespace character.
 * <p/>//www . jav a  2  s. c  o  m
 * <code>StringUtils.hasText(null) == false<br/>
 * StringUtils.hasText("") == false<br/>
 * StringUtils.hasText(" ") == false<br/>
 * StringUtils.hasText("12345") == true<br/>
 * StringUtils.hasText(" 12345 ") == true</code>
 * 
 * <p>
 * Copied from the Spring Framework while retaining all license, copyright
 * and author information.
 * 
 * @param str
 *            the String to check (may be <code>null</code>)
 * @return <code>true</code> if the String is not <code>null</code>, its
 *         length is greater than 0, and it does not contain whitespace only
 * @see java.lang.Character#isWhitespace
 */
public static boolean hasText(final String str) {
    if (!hasLength(str)) {
        return false;
    }
    final int strLen = str.length();
    for (int i = 0; i < strLen; i++) {
        if (!Character.isWhitespace(str.charAt(i))) {
            return true;
        }
    }
    return false;
}

From source file:com.subgraph.vega.impl.scanner.urls.ResponseAnalyzer.java

private void checkJavascriptXSS(IInjectionModuleContext ctx, HttpUriRequest req, IHttpResponse res,
        String text) {/*from  w  w  w.  j  a  v a2  s  .  c om*/
    if (text == null)
        return;
    int lastWordIdx = 0;
    int idx = 0;
    boolean inQuote = false;
    boolean prevSpace = true;
    while (idx < text.length()) {
        idx = maybeSkipJavascriptComment(text, idx);
        if (idx >= text.length())
            return;
        char c = text.charAt(idx);
        if (!inQuote && (c == '\'' || c == '"')) {
            inQuote = true;
            if (matchStartsWith(text, lastWordIdx, "innerHTML", "open", "url", "href", "write")
                    && matchStartsWith(text, idx + 1, "//vega.invalid/", "http://vega.invalid", "vega:")) {
                alert(ctx, "vinfo-url-inject", "Injected URL in JS/CSS code", req, res);
            }
        } else if (c == '\'' || c == '"') {
            inQuote = false;
        } else if (!inQuote && text.startsWith("vvv", idx)) {
            possibleXssAlert(ctx, req, res, text, idx, "vinfo-xss-inject", "Injected syntax into JS/CSS code");
        } else if (Character.isWhitespace(c) || c == '.') {
            prevSpace = true;
        } else if (prevSpace && Character.isLetterOrDigit(c)) {
            lastWordIdx = idx;
            prevSpace = false;
        }
        idx += 1;
    }
}

From source file:de.micromata.genome.gwiki.page.impl.wiki.parser.GWikiWikiParser.java

protected boolean isDecorateStart(GWikiWikiTokens tks) {
    char pk = tks.peekToken(-1);
    if (pk != 0 && Character.isLetterOrDigit(pk) == true) {
        return false;
    }// w w w .j a v  a  2 s.  c  om
    pk = tks.peekToken(1);
    if (pk == 0) {
        return false;
    }
    if (Character.isWhitespace(pk) == true) {
        return false;
    }
    return true;

}

From source file:de.tudarmstadt.ukp.dkpro.core.io.penntree.PennTreebankCombinedReader.java

private PennTreeNode readTree(LineIterator aLi) {
    StringBuilder tree = new StringBuilder();
    while (aLi.hasNext() || lineBuffer != null) {
        String line = lineBuffer != null ? lineBuffer : aLi.nextLine();
        lineBuffer = null;/* w  w  w  . j a  v  a  2 s .  co  m*/
        if (StringUtils.isBlank(line)) {
            if (tree.length() > 0) {
                break;
            } else {
                continue;
            }
        }

        // If the next line starts at the beginning (no indentation) then expect it is a new
        // tree.
        if ((tree.length() > 0) && !Character.isWhitespace(line.charAt(0))) {
            lineBuffer = line;
            break;
        }

        tree.append(line);
        tree.append('\n'); // Actually not needed - just in case we want to debug ;)
    }

    return PennTreeUtils.parsePennTree(tree.toString());
}

From source file:com.salas.bb.utils.StringUtils.java

/**
 * Returns the first line of article./*w  w w . j a  v a2 s .c  o  m*/
 *
 * @param aText text to scan.
 * 
 * @return first text line.
 */
public static String getFirstSentense(String aText) {
    if (aText == null)
        return null;

    int size = aText.length();

    int start;
    int length;

    for (start = 0; start < size && Character.isWhitespace(aText.charAt(start)); start++)
        ;
    for (length = 0; start + length < size && (!isSentenseTerminator(aText.charAt(start + length))); length++)
        ;

    return length > 0 ? aText.substring(start, start + length).trim() : Constants.EMPTY_STRING;
}

From source file:de.tudarmstadt.ukp.dkpro.core.io.pdf.Pdf2CasConverter.java

private StringBuilder sanitize(final StringBuilder aContent) {
    int i = 0;//from w  ww . j a  v a  2 s.co  m
    int lastBreak = 0;
    while (i < aContent.length()) {
        // Check valid unicode char
        if (!isValidXMLChar(aContent.codePointAt(i))) {
            aContent.setCharAt(i, ' ');
            i++;
            continue;
        }

        // Set up how many characters we want to skip
        int seek = i + 1;

        // Do we maybe have an entity?
        if (aContent.charAt(i) == '&') {
            // REC 2006-10-21 Some PDFs seem to have entities and others
            // don't
            // so we may encounter &'s that do not introduce an entity and
            // just ignore them.
            final int end = aContent.indexOf(";", i);
            if (end != -1) {
                final String cand = aContent.substring(i, end + 1);
                String r = null;
                try {
                    if (cand.startsWith("&#x")) {
                        final int cp = Integer.parseInt(cand.substring(2, cand.length() - 1), 16);
                        r = isValidXMLChar(cp) ? String.valueOf(Character.toChars(cp)) : " ";
                    } else if (cand.startsWith("&#")) {
                        final int cp = Integer.parseInt(cand.substring(2, cand.length() - 1));
                        r = isValidXMLChar(cp) ? String.valueOf(Character.toChars(cp)) : " ";
                    } else {
                        // RE 2006-10-22 The chance that there is a & and a
                        // ;
                        // together in a string is quite big. Let's be
                        // tolerant.
                    }
                } catch (final NumberFormatException e) {
                    log.warn("Invalid numeric entity in fragment [" + cand + "] - Dropping it.");
                }

                // Expand the entity and set proper skip (if found)
                if (r != null) {
                    aContent.replace(i, i + cand.length(), r);
                    seek = i + r.length();
                }
            }
        }

        // Match against the Trie after numeric entity expansion is over
        if (substitutionTable != null) {
            final Trie<String>.Node match = substitutionTable.getNode(aContent, i);
            if (match != null) {
                aContent.replace(i, i + match.level, match.value);
                seek = i + match.value.length();
            }
        }

        // Check line breaks
        while (i < seek) {
            if (aContent.charAt(i) == '\n') {
                lastBreak = i;
            } else if (Character.isWhitespace(aContent.codePointAt(i)) && (i > (lastBreak + 79))) {
                lastBreak = i;
                aContent.replace(i, i + 1, "\n");
            }
            i++;
        }
    }

    return aContent;
}

From source file:de.micromata.genome.gwiki.page.impl.wiki.parser.GWikiWikiParser.java

protected boolean isDecorateEnd(GWikiWikiTokens tks) {
    char pk = tks.peekToken(-1);
    if (pk == 0 || Character.isWhitespace(pk) == true) {
        return false;
    }//from  w  ww.  j ava  2s. c  o  m
    pk = tks.peekToken(1);
    if (pk == 0) {
        return true;
    }
    if (Character.isLetterOrDigit(pk) == true) {
        return false;
    }
    return true;

}

From source file:com.puppycrawl.tools.checkstyle.utils.CommonUtil.java

/**
 * Returns whether the specified string contains only whitespace up to the specified index.
 *
 * @param index//  w  ww  . j  ava2 s  .  c o  m
 *            index to check up to
 * @param line
 *            the line to check
 * @return whether there is only whitespace
 */
public static boolean hasWhitespaceBefore(int index, String line) {
    boolean result = true;
    for (int i = 0; i < index; i++) {
        if (!Character.isWhitespace(line.charAt(i))) {
            result = false;
            break;
        }
    }
    return result;
}

From source file:net.metanotion.json.StreamingParser.java

private int skipWhitespace(final Reader in) throws IOException {
    int c;/*from ww w. ja  v  a  2  s.  c  o m*/
    do {
        c = in.read();
    } while (Character.isWhitespace((char) c));
    return c;
}