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:cn.remex.core.util.StringUtils.java

/**
 * ?????/*from   w  w w .  j a v a  2  s  .c  o  m*/
 * Check whether the given CharSequence contains any whitespace characters.
 * @param str the CharSequence to check (may be <code>null</code>) ?
 * @return <code>true</code> if the CharSequence is not empty and 
 * contains at least 1 whitespace character
 * @see java.lang.Character#isWhitespace
 */
public static boolean containsWhitespace(final CharSequence str) {
    if (!hasLength(str)) {
        return false;
    }
    int strLen = str.length();
    for (int i = 0; i < strLen; i++) {
        if (Character.isWhitespace(str.charAt(i))) {
            return true;
        }
    }
    return false;
}

From source file:net.sf.jabref.gui.autocompleter.AutoCompleteListener.java

@Override
public void keyTyped(KeyEvent e) {
    LOGGER.debug("key typed event caught " + e.getKeyCode());
    char ch = e.getKeyChar();
    if (ch == '\n') {
        // this case is handled at keyPressed(e)
        return;/*from  ww w.  java2  s . c  o  m*/
    }

    // don't do auto completion inside words
    if (!atEndOfWord((JTextComponent) e.getSource())) {
        return;
    }

    if ((e.getModifiers() | InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK) {
        // plain key or SHIFT + key is pressed, no handling of CTRL+key,  META+key, ...
        if (Character.isLetter(ch) || Character.isDigit(ch)
                || (Character.isWhitespace(ch) && completer.isSingleUnitField())) {
            JTextComponent comp = (JTextComponent) e.getSource();

            if (toSetIn == null) {
                LOGGER.debug("toSetIn is null");
            } else {
                LOGGER.debug("toSetIn: >" + toSetIn + '<');
            }

            // The case-insensitive system is a bit tricky here
            // If keyword is "TODO" and user types "tO", then this is treated as "continue" as the "O" matches the "O"
            // If keyword is "TODO" and user types "To", then this is treated as "discont" as the "o" does NOT match the "O".

            if ((toSetIn != null) && (toSetIn.length() > 1) && (ch == toSetIn.charAt(1))) {
                // User continues on the word that was suggested.
                LOGGER.debug("cont");

                toSetIn = toSetIn.substring(1);
                if (!toSetIn.isEmpty()) {
                    int cp = comp.getCaretPosition();
                    //comp.setCaretPosition(cp+1-toSetIn.);
                    comp.select((cp + 1) - toSetIn.length(), cp);
                    lastBeginning = lastBeginning + ch;

                    e.consume();
                    lastCaretPosition = comp.getCaretPosition();

                    lastCompletions = findCompletions(lastBeginning);
                    lastShownCompletion = 0;
                    for (int i = 0; i < lastCompletions.size(); i++) {
                        String lastCompletion = lastCompletions.get(i);
                        if (lastCompletion.endsWith(toSetIn)) {
                            lastShownCompletion = i;
                            break;
                        }

                    }
                    if (toSetIn.length() < 2) {
                        // User typed the last character of the autocompleted word
                        // We have to replace the automcompletion word by the typed word.
                        // This helps if the user presses "space" after the completion
                        // "space" indicates that the user does NOT want the autocompletion,
                        // but the typed word
                        String text = comp.getText();
                        comp.setText(text.substring(0, lastCaretPosition - lastBeginning.length())
                                + lastBeginning + text.substring(lastCaretPosition));
                        // there is no selected text, therefore we are not updating the selection
                        toSetIn = null;
                    }
                    return;
                }
            }

            if ((toSetIn != null) && ((toSetIn.length() <= 1) || (ch != toSetIn.charAt(1)))) {
                // User discontinues the word that was suggested.
                lastBeginning = lastBeginning + ch;

                LOGGER.debug("discont toSetIn: >" + toSetIn + "'<' lastBeginning: >" + lastBeginning + '<');

                List<String> completed = findCompletions(lastBeginning);
                if ((completed != null) && (!completed.isEmpty())) {
                    lastShownCompletion = 0;
                    lastCompletions = completed;
                    String sno = completed.get(0);
                    // toSetIn = string used for autocompletion last time
                    // this string has to be removed
                    // lastCaretPosition is the position of the caret after toSetIn.
                    int lastLen = toSetIn.length() - 1;
                    toSetIn = sno.substring(lastBeginning.length() - 1);
                    String text = comp.getText();
                    //we do not use toSetIn as we want to obey the casing of "sno"
                    comp.setText(text.substring(0, (lastCaretPosition - lastLen - lastBeginning.length()) + 1)
                            + sno + text.substring(lastCaretPosition));
                    int startSelect = (lastCaretPosition + 1) - lastLen;
                    int endSelect = (lastCaretPosition + toSetIn.length()) - lastLen;
                    comp.select(startSelect, endSelect);

                    lastCaretPosition = comp.getCaretPosition();
                    e.consume();
                    return;
                } else {
                    setUnmodifiedTypedLetters(comp, true, false);
                    e.consume();
                    toSetIn = null;
                    return;
                }
            }

            LOGGER.debug("case else");

            comp.replaceSelection("");

            StringBuffer currentword = getCurrentWord(comp);

            // only "real characters" end up here
            assert (!Character.isISOControl(ch));
            currentword.append(ch);
            startCompletion(currentword, e);
            return;
        } else {
            if (Character.isWhitespace(ch)) {
                assert (!completer.isSingleUnitField());
                LOGGER.debug("whitespace && !singleUnitField");
                // start a new search if end-of-field is reached

                // replace displayed letters with typed letters
                setUnmodifiedTypedLetters((JTextComponent) e.getSource(), false, true);
                resetAutoCompletion();
                return;
            }

            LOGGER.debug("No letter/digit/whitespace or CHAR_UNDEFINED");
            // replace displayed letters with typed letters
            setUnmodifiedTypedLetters((JTextComponent) e.getSource(), false, !Character.isISOControl(ch));
            resetAutoCompletion();
            return;
        }
    }
    resetAutoCompletion();
}

From source file:StringExaminer.java

/**
* Returns the next character that is no whitespace and leaves the position
* pointer one character after the returned one.
*
* @return/* ww  w . j  ava 2  s  .com*/
*/
public char nextNoneWhitespaceChar() {
    char next = this.nextChar();

    while ((endNotReached(next)) && (Character.isWhitespace(next))) {
        next = this.nextChar();
    }

    return next;
}

From source file:jos.parser.Parser.java

private String makeSelector(final String sig) {
    final StringBuilder sb = new StringBuilder();
    for (int i = 0; i < sig.length(); i++) {
        char c = sig.charAt(i);
        if (c == ' ') {
            continue;
        }//from ww  w .j  a  v a2 s  .  c o m
        if (c == ';') {
            break;
        } else if (c == ':') {
            sb.append(c);
            i++;
            for (; i < sig.length(); i++) {
                c = sig.charAt(i);
                if (c == ')') {
                    for (++i; i < sig.length() && Character.isWhitespace(sig.charAt(i)); i++) {
                        ;
                    }

                    for (++i; i < sig.length(); i++) {
                        if (!Character.isLetterOrDigit(sig.charAt(i))) {
                            break;
                        }
                    }
                    break;
                }
            }
        } else {
            sb.append(c);
        }
    }
    return sb.toString();
}

From source file:com.liferay.cucumber.util.StringUtil.java

public static String trimLeading(String s) {
    if (s == null) {
        return null;
    }//  www  . j  a va 2 s  .c  o  m

    if (s.length() == 0) {
        return s;
    }

    int len = s.length();

    int x = len;

    for (int i = 0; i < len; i++) {
        char c = s.charAt(i);

        if (!Character.isWhitespace(c)) {
            x = i;

            break;
        }
    }

    if (x == len) {
        return StringPool.BLANK;
    } else if (x == 0) {
        return s;
    } else {
        return s.substring(x);
    }
}

From source file:henplus.HenPlus.java

/**
 * add a new line. returns one of LINE_EMPTY, LINE_INCOMPLETE or LINE_EXECUTED.
 *//*from   w ww.ja v a2 s  .  c  o m*/
public byte executeLine(final String line) {
    byte result = LINE_EMPTY;
    /*
     * special oracle comment 'rem'ark; should be in the comment parser.
     * ONLY if it is on the beginning of the line, no whitespace.
     */
    final int startWhite = 0;
    /*
     * while (startWhite < line.length() &&
     * Character.isWhitespace(line.charAt(startWhite))) { ++startWhite; }
     */
    if (line.length() >= 3 + startWhite
            && line.substring(startWhite, startWhite + 3).toUpperCase().equals("REM")
            && (line.length() == 3 || Character.isWhitespace(line.charAt(3)))) {
        return LINE_EMPTY;
    }

    final StringBuilder lineBuf = new StringBuilder(line);
    lineBuf.append('\n');
    _commandSeparator.append(lineBuf.toString());
    result = LINE_INCOMPLETE;
    while (_commandSeparator.hasNext()) {
        String completeCommand = _commandSeparator.next();
        completeCommand = varsubst(completeCommand, _settingStore.getVariableMap());
        final Command c = _dispatcher.getCommandFrom(completeCommand);
        if (c == null) {
            _commandSeparator.consumed();
            /*
             * do not shadow successful executions with the 'line-empty'
             * message. Background is: when we consumed a command, that is
             * complete with a trailing ';', then the following newline
             * would be considered as empty command. So return only the
             * LINE_EMPTY, if we haven't got a succesfully executed line.
             */
            if (result != LINE_EXECUTED) {
                result = LINE_EMPTY;
            }
        } else if (!c.isComplete(completeCommand)) {
            _commandSeparator.cont();
            result = LINE_INCOMPLETE;
        } else {
            _dispatcher.execute(_sessionManager.getCurrentSession(), completeCommand);
            _commandSeparator.consumed();
            result = LINE_EXECUTED;
        }
    }
    return result;
}

From source file:net.sf.morph.transform.converters.TextToNumberConverter.java

/**
 * Remove any characters that should be ignored when performing the
 * conversion.//  ww  w.j a  v  a 2  s . c o  m
 * 
 * @param string
 *            the input string
 * @param locale
 *            the locale
 * @return <code>string</code>, with all characters that should be
 *         ignored removed
 */
private StringBuffer removeIgnoredCharacters(String string, Locale locale) {
    StringBuffer charactersToParse = new StringBuffer();
    DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
    for (int i = 0; i < string.length(); i++) {
        char currentChar = string.charAt(i);
        if (getWhitespaceHandling() == WHITESPACE_IGNORE && Character.isWhitespace(currentChar)) {
            continue;
        }
        if (getCurrencyHandling() == CURRENCY_IGNORE
                && Character.getType(currentChar) == Character.CURRENCY_SYMBOL) {
            continue;
        }
        if (getPercentageHandling() == PERCENTAGE_IGNORE) {
            if (currentChar == symbols.getPercent()) {
                continue;
            }
        }
        if (getParenthesesHandling() == PARENTHESES_IGNORE) {
            if (currentChar == LEFT_PARENTHESES || currentChar == RIGHT_PARENTHESES) {
                continue;
            }
        }
        charactersToParse.append(currentChar);
    }
    return charactersToParse;
}

From source file:ar.com.tadp.xml.rinzo.core.actions.VisitorAction.java

/**
 * True when there is no other char then whitespace Empty string is also
 * whitespace/*from  ww  w . j a v a 2s.c  om*/
 * 
 * @param str
 * @return
 */
private static boolean isWhitespace(String str) {
    for (int i = 0; i < str.length(); i++) {
        if (!Character.isWhitespace(str.charAt(i))) {
            return false;
        }
    }
    return true;
}

From source file:mml.handler.post.MMLPostHTMLHandler.java

/**
 * Get the next word AFTER the given element
 * @param node the node after which we seek the next word
 * @return the word//from   w  ww .  ja  v  a2 s .  c om
 */
String nextWord(Element node) {
    StringBuilder word = new StringBuilder();
    Node next = node.nextSibling();
    String text = "";
    if (next != null) {
        if (next instanceof TextNode) {
            text = ((TextNode) next).text();
        } else if (next instanceof Element) {
            text = getTextOf(next);
        }
    }
    text = text.trim();
    for (int i = 0; i < text.length(); i++)
        if (!Character.isWhitespace(text.charAt(i)))
            word.append(text.charAt(i));
        else
            break;
    return word.toString();
}

From source file:ca.uhn.fhir.narrative.BaseThymeleafNarrativeGenerator.java

static String cleanWhitespace(String theResult) {
    StringBuilder b = new StringBuilder();
    boolean inWhitespace = false;
    boolean betweenTags = false;
    boolean lastNonWhitespaceCharWasTagEnd = false;
    boolean inPre = false;
    for (int i = 0; i < theResult.length(); i++) {
        char nextChar = theResult.charAt(i);
        if (inPre) {
            b.append(nextChar);//from w w w. ja  va 2s  .c  o  m
            continue;
        } else if (nextChar == '>') {
            b.append(nextChar);
            betweenTags = true;
            lastNonWhitespaceCharWasTagEnd = true;
            continue;
        } else if (nextChar == '\n' || nextChar == '\r') {
            // if (inWhitespace) {
            // b.append(' ');
            // inWhitespace = false;
            // }
            continue;
        }

        if (betweenTags) {
            if (Character.isWhitespace(nextChar)) {
                inWhitespace = true;
            } else if (nextChar == '<') {
                if (inWhitespace && !lastNonWhitespaceCharWasTagEnd) {
                    b.append(' ');
                }
                inWhitespace = false;
                b.append(nextChar);
                inWhitespace = false;
                betweenTags = false;
                lastNonWhitespaceCharWasTagEnd = false;
                if (i + 3 < theResult.length()) {
                    char char1 = Character.toLowerCase(theResult.charAt(i + 1));
                    char char2 = Character.toLowerCase(theResult.charAt(i + 2));
                    char char3 = Character.toLowerCase(theResult.charAt(i + 3));
                    char char4 = Character
                            .toLowerCase((i + 4 < theResult.length()) ? theResult.charAt(i + 4) : ' ');
                    if (char1 == 'p' && char2 == 'r' && char3 == 'e') {
                        inPre = true;
                    } else if (char1 == '/' && char2 == 'p' && char3 == 'r' && char4 == 'e') {
                        inPre = false;
                    }
                }
            } else {
                lastNonWhitespaceCharWasTagEnd = false;
                if (inWhitespace) {
                    b.append(' ');
                    inWhitespace = false;
                }
                b.append(nextChar);
            }
        } else {
            b.append(nextChar);
        }
    }
    return b.toString();
}