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:compare.handler.get.CorCode.java

private boolean pairHasText(Pair p) {
    char[] data = p.getChars();
    for (int i = 0; i < data.length; i++) {
        if (!Character.isWhitespace(data[i])) {
            return true;
        }//ww w  . j  a  v a 2  s .  c om
    }
    return false;
}

From source file:net.sf.jabref.importer.TextAnalyzer.java

private static String clean(String s) {
    boolean found = false;
    int left = 0;
    int right = s.length() - 1;
    while (!found && (left < s.length())) {
        char c = s.charAt(left);
        if (Character.isWhitespace(c) || (c == '.') || (c == ',') || (c == '(') || (c == ':') || (c == ')')) {
            left++;//  w ww  .  ja va2  s  .c  om
        } else {
            found = true;
        }
    }
    found = false;
    while (!found && (right > left)) {
        char c = s.charAt(right);
        if (Character.isWhitespace(c) || (c == '.') || (c == ',') || (c == ')') || (c == ':') || (c == '(')) {
            right--;
        } else {
            found = true;
        }
    }
    //Util.pr(s+"\n"+left+" "+right);
    return s.substring(left, Math.min(right + 1, s.length()));
}

From source file:com.tasktop.internal.hp.qc.core.model.comments.mylyn3_8.HtmlStreamTokenizer_m3_8.java

/**
 * Parses an HTML tag out of a string of characters.
 */// ww w.j  a v a 2s .  co m
private static void parseTag(String s, HtmlTag_m3_8 tag, boolean escapeValues) throws ParseException {

    int i = 0;
    for (; i < s.length() && Character.isWhitespace(s.charAt(i)); i++) {
        // just move forward
    }
    if (i == s.length()) {
        throw new ParseException("parse empty tag", 0); //$NON-NLS-1$
    }

    int start = i;
    for (; i < s.length() && !Character.isWhitespace(s.charAt(i)); i++) {
        // just move forward
    }
    tag.setTagName(s.substring(start, i));

    for (; i < s.length() && Character.isWhitespace(s.charAt(i)); i++) {
        // just move forward
    }
    if (i == s.length()) {
        return;
    } else {
        parseAttributes(tag, s, i, escapeValues);
        return;
    }
}

From source file:de.l3s.boilerpipe.sax.BoilerpipeHTMLContentHandler.java

public void characters(char[] ch, int start, int length) throws SAXException {
    textElementIdx++;//w  ww.jav a2 s  . com

    if (flush) {
        flushBlock();
        flush = false;
    }

    if (inIgnorableElement != 0) {
        return;
    }

    char c;
    boolean startWhitespace = false;
    boolean endWhitespace = false;
    if (length == 0) {
        return;
    }

    final int end = start + length;
    for (int i = start; i < end; i++) {
        if (Character.isWhitespace(ch[i])) {
            ch[i] = ' ';
        }
    }
    while (start < end) {
        c = ch[start];
        if (c == ' ') {
            startWhitespace = true;
            start++;
            length--;
        } else {
            break;
        }
    }
    while (length > 0) {
        c = ch[start + length - 1];
        if (c == ' ') {
            endWhitespace = true;
            length--;
        } else {
            break;
        }
    }
    if (length == 0) {
        if (startWhitespace || endWhitespace) {
            if (!sbLastWasWhitespace) {
                textBuffer.append(' ');
                tokenBuffer.append(' ');
            }
            sbLastWasWhitespace = true;
        } else {
            sbLastWasWhitespace = false;
        }
        lastEvent = Event.WHITESPACE;
        return;
    }
    if (startWhitespace) {
        if (!sbLastWasWhitespace) {
            textBuffer.append(' ');
            tokenBuffer.append(' ');
        }
    }

    if (blockTagLevel == -1) {
        blockTagLevel = tagLevel;
    }

    textBuffer.append(ch, start, length);
    tokenBuffer.append(ch, start, length);
    if (endWhitespace) {
        textBuffer.append(' ');
        tokenBuffer.append(' ');
    }

    sbLastWasWhitespace = endWhitespace;
    lastEvent = Event.CHARACTERS;

    currentContainedTextElements.set(textElementIdx);
}

From source file:com.opendoorlogistics.core.utils.strings.Strings.java

/**
 * Standardised version of a string value. 
 * Calculation is optimised as much as possible.
 * @param s//from   w w  w .  j a  v a  2s . co  m
 * @return
 */
public static String std(String s) {
    if (s == null) {
        return "";
    }

    int n = s.length();
    StringBuilder b = new StringBuilder(n);

    // find first non-whitespace
    int firstNonWS = n;
    for (int i = 0; i < n; i++) {
        char c = s.charAt(i);
        if (!Character.isWhitespace(c)) {
            firstNonWS = i;
            break;
        }
    }

    // get last non-whitespace char
    int lastNonWS = -1;
    for (int i = n - 1; i >= 0; i--) {
        char c = s.charAt(i);
        if (!Character.isWhitespace(c)) {
            lastNonWS = i;
            break;
        }
    }

    boolean inWhiteSpace = false;
    char c;
    for (int i = firstNonWS; i <= lastNonWS; i++) {
        c = Character.toLowerCase(s.charAt(i));

        if (inWhiteSpace) {
            if (Character.isWhitespace(c)) {
                // never add two whitespaces in a row
            } else {
                // no longer in whitespace
                inWhiteSpace = false;
                b.append(c);
            }
        } else {
            if (Character.isWhitespace(c)) {
                // always treat whitespace as a space
                b.append(' ');
                inWhiteSpace = true;
            } else {
                b.append(c);
            }
        }

    }

    return b.toString();
}

From source file:Base64.java

/**
 * Decode a Base-64 string into a byte array.
 * //from   ww  w .ja va  2s  .co m
 * @param b64
 *            The Base-64 encoded string.
 * @return The decoded bytes.
 * @throws java.io.IOException
 *             If the argument is not a valid Base-64 encoding.
 */
public static byte[] decode(String b64) throws IOException {
    ByteArrayOutputStream result = new ByteArrayOutputStream(b64.length() / 3);
    int state = 0, i;
    byte temp = 0;

    for (i = 0; i < b64.length(); i++) {
        if (Character.isWhitespace(b64.charAt(i))) {
            continue;
        }
        if (b64.charAt(i) == BASE_64_PAD) {
            break;
        }

        int pos = BASE_64.indexOf(b64.charAt(i));
        if (pos < 0) {
            throw new IOException("non-Base64 character " + b64.charAt(i));
        }
        switch (state) {
        case 0:
            temp = (byte) (pos - BASE_64.indexOf('A') << 2);
            state = 1;
            break;

        case 1:
            temp |= (byte) (pos - BASE_64.indexOf('A') >>> 4);
            result.write(temp);
            temp = (byte) ((pos - BASE_64.indexOf('A') & 0x0f) << 4);
            state = 2;
            break;

        case 2:
            temp |= (byte) ((pos - BASE_64.indexOf('A') & 0x7f) >>> 2);
            result.write(temp);
            temp = (byte) ((pos - BASE_64.indexOf('A') & 0x03) << 6);
            state = 3;
            break;

        case 3:
            temp |= (byte) (pos - BASE_64.indexOf('A') & 0xff);
            result.write(temp);
            state = 0;
            break;

        default:
            throw new Error("this statement should be unreachable");
        }
    }

    if (i < b64.length() && b64.charAt(i) == BASE_64_PAD) {
        switch (state) {
        case 0:
        case 1:
            throw new IOException("malformed Base64 sequence");

        case 2:
            for (; i < b64.length(); i++) {
                if (!Character.isWhitespace(b64.charAt(i))) {
                    break;
                }
            }
            // We must see a second pad character here.
            if (b64.charAt(i) != BASE_64_PAD) {
                throw new IOException("malformed Base64 sequence");
            }
            i++;
            // Fall-through.

        case 3:
            i++;
            for (; i < b64.length(); i++) {
                // We should only see whitespace after this.
                if (!Character.isWhitespace(b64.charAt(i))) {
                    System.out.println(Character.valueOf(b64.charAt(i)));
                    throw new IOException("malformed Base64 sequence");
                }
            }
        }
    } else {
        if (state != 0) {
            throw new IOException("malformed Base64 sequence");
        }
    }

    return result.toByteArray();
}

From source file:com.gargoylesoftware.htmlunit.html.DomText.java

/**
 * Indicates if the provided character can by "typed" in the element.
 * @param c the character//  w ww  .j a v a  2s  . c  o m
 * @return {@code true} if it is accepted
 */
protected boolean acceptChar(final char c) {
    // This range is this is private use area
    // see http://www.unicode.org/charts/PDF/UE000.pdf
    return (c < '\uE000' || c > '\uF8FF') && (c == ' ' || !Character.isWhitespace(c));
}

From source file:cloudeventbus.codec.Decoder.java

private int skipWhiteSpace(int messageIndex, String command) {
    while (messageIndex < command.length() && Character.isWhitespace(command.charAt(messageIndex))) {
        messageIndex++;//from www.  jav a  2s  .c o m
    }
    return messageIndex;
}

From source file:ca.simplegames.micro.utils.StringUtils.java

/**
 * Trim trailing whitespace from the given String.
 *
 * @param str the String to check// w  ww.jav a2 s.  com
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimTrailingWhitespace(String str) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuffer buf = new StringBuffer(str);
    while (buf.length() > 0 && Character.isWhitespace(buf.charAt(buf.length() - 1))) {
        buf.deleteCharAt(buf.length() - 1);
    }
    return buf.toString();
}

From source file:com.xie.javacase.json.XMLTokener.java

/**
 * Get the next XML Token. These tokens are found inside of angle
 * brackets. It may be one of these characters: <code>/ > = ! ?</code> or it
 * may be a string wrapped in single quotes or double quotes, or it may be a
 * name./* www  .  j a  v a2s  .  c  o m*/
 * @return a String or a Character.
 * @throws org.json.JSONException If the XML is not well formed.
 */
public Object nextToken() throws JSONException {
    char c;
    char q;
    StringBuffer sb;
    do {
        c = next();
    } while (Character.isWhitespace(c));
    switch (c) {
    case 0:
        throw syntaxError("Misshaped element");
    case '<':
        throw syntaxError("Misplaced '<'");
    case '>':
        return XML.GT;
    case '/':
        return XML.SLASH;
    case '=':
        return XML.EQ;
    case '!':
        return XML.BANG;
    case '?':
        return XML.QUEST;

    // Quoted string

    case '"':
    case '\'':
        q = c;
        sb = new StringBuffer();
        for (;;) {
            c = next();
            if (c == 0) {
                throw syntaxError("Unterminated string");
            }
            if (c == q) {
                return sb.toString();
            }
            if (c == '&') {
                sb.append(nextEntity(c));
            } else {
                sb.append(c);
            }
        }
    default:

        // Name

        sb = new StringBuffer();
        for (;;) {
            sb.append(c);
            c = next();
            if (Character.isWhitespace(c)) {
                return sb.toString();
            }
            switch (c) {
            case 0:
                return sb.toString();
            case '>':
            case '/':
            case '=':
            case '!':
            case '?':
            case '[':
            case ']':
                back();
                return sb.toString();
            case '<':
            case '"':
            case '\'':
                throw syntaxError("Bad character in a name");
            }
        }
    }
}