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:org.zenoss.zep.index.impl.solr.SolrQueryBuilder.java

private static String escape(String s, char[] escaped, boolean quote) {
    StringBuilder sb = new StringBuilder();
    final int len = s.length();
    boolean whitespace = false;
    for (int i = 0; i < len; i++) {
        final char c = s.charAt(i);
        if (Character.isWhitespace(c))
            whitespace = true;//from  w  ww .  j  av  a  2s  . c o  m
        if (Arrays.binarySearch(escaped, c) >= 0)
            sb.append('\\');
        sb.append(c);
    }
    if (quote && (whitespace || sb.length() == 0))
        return '"' + sb.append('"').toString();
    else
        return sb.toString();
}

From source file:com.curecomp.primefaces.migrator.PrimefacesMigration.java

private static String rtrim(String s) {
    int i = s.length() - 1;
    while (i >= 0 && Character.isWhitespace(s.charAt(i))) {
        i--;//from w  ww  . j a v a2s  .  c o m
    }
    return s.substring(0, i + 1);
}

From source file:com.g3net.tool.StringUtils.java

/**
 * Trim leading and trailing whitespace from the given String.
 * /*from  w  w  w. ja v  a  2 s.  co m*/
 * @param str
 *            the String to check
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimWhitespace(String str) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuilder buf = new StringBuilder(str);
    while (buf.length() > 0 && Character.isWhitespace(buf.charAt(0))) {
        buf.deleteCharAt(0);
    }
    while (buf.length() > 0 && Character.isWhitespace(buf.charAt(buf.length() - 1))) {
        buf.deleteCharAt(buf.length() - 1);
    }
    return buf.toString();
}

From source file:com.vsct.dt.hesperides.templating.models.Property.java

/**
 * Copy first word./*from   w  ww  .j  av a  2 s.  c  o  m*/
 *
 * @param str string
 * @param len len string
 * @param start position to start
 *
 * @return substring of str
 */
private static TemporaryValueProperty copyFirstWord(final String str, final int len, final int start) {
    // In fact, never return null cause @ is copied.
    String result = null;
    // Last char
    final int lastCharPos = len - 1;

    for (int index = start; index < len && result == null; index++) {
        // Search blank char or if is last char
        if (Character.isWhitespace(str.charAt(index))) {
            result = str.substring(start, index);
        } else if (index == lastCharPos) {
            result = str.substring(start, index + 1);
        }
    }

    return new TemporaryValueProperty(result, result == null ? 0 : result.length());
}

From source file:XmlReader.java

private void useEncodingDecl(PushbackInputStream pb, String encoding) throws IOException {
    byte buffer[] = new byte[MAXPUSHBACK];
    int len;/*from   w ww . j  a va 2  s  .  c  o  m*/
    Reader r;
    int c;

    //
    // Buffer up a bunch of input, and set up to read it in
    // the specified encoding ... we can skip the first four
    // bytes since we know that "<?xm" was read to determine
    // what encoding to use!
    //
    len = pb.read(buffer, 0, buffer.length);
    pb.unread(buffer, 0, len);
    r = new InputStreamReader(new ByteArrayInputStream(buffer, 4, len), encoding);

    //
    // Next must be "l" (and whitespace) else we conclude
    // error and choose UTF-8.
    //
    if ((c = r.read()) != 'l') {
        setEncoding(pb, "UTF-8");
        return;
    }

    //
    // Then, we'll skip any
    //  S version="..."   [or single quotes]
    // bit and get any subsequent 
    //  S encoding="..."  [or single quotes]
    //
    // We put an arbitrary size limit on how far we read; lots
    // of space will break this algorithm.
    //
    StringBuffer buf = new StringBuffer();
    StringBuffer keyBuf = null;
    String key = null;
    boolean sawEq = false;
    char quoteChar = 0;
    boolean sawQuestion = false;

    XmlDecl: for (int i = 0; i < MAXPUSHBACK - 5; ++i) {
        if ((c = r.read()) == -1)
            break;

        // ignore whitespace before/between "key = 'value'"
        if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
            continue;

        // ... but require at least a little!
        if (i == 0)
            break;

        // terminate the loop ASAP
        if (c == '?')
            sawQuestion = true;
        else if (sawQuestion) {
            if (c == '>')
                break;
            sawQuestion = false;
        }

        // did we get the "key =" bit yet?
        if (key == null || !sawEq) {
            if (keyBuf == null) {
                if (Character.isWhitespace((char) c))
                    continue;
                keyBuf = buf;
                buf.setLength(0);
                buf.append((char) c);
                sawEq = false;
            } else if (Character.isWhitespace((char) c)) {
                key = keyBuf.toString();
            } else if (c == '=') {
                if (key == null)
                    key = keyBuf.toString();
                sawEq = true;
                keyBuf = null;
                quoteChar = 0;
            } else
                keyBuf.append((char) c);
            continue;
        }

        // space before quoted value
        if (Character.isWhitespace((char) c))
            continue;
        if (c == '"' || c == '\'') {
            if (quoteChar == 0) {
                quoteChar = (char) c;
                buf.setLength(0);
                continue;
            } else if (c == quoteChar) {
                if ("encoding".equals(key)) {
                    assignedEncoding = buf.toString();

                    // [81] Encname ::= [A-Za-z] ([A-Za-z0-9._]|'-')*
                    for (i = 0; i < assignedEncoding.length(); i++) {
                        c = assignedEncoding.charAt(i);
                        if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
                            continue;
                        if (i == 0)
                            break XmlDecl;
                        if (i > 0 && (c == '-' || (c >= '0' && c <= '9') || c == '.' || c == '_'))
                            continue;
                        // map illegal names to UTF-8 default
                        break XmlDecl;
                    }

                    setEncoding(pb, assignedEncoding);
                    return;

                } else {
                    key = null;
                    continue;
                }
            }
        }
        buf.append((char) c);
    }

    setEncoding(pb, "UTF-8");
}

From source file:com.vmware.vfabric.hyperic.plugin.vfws.PWSServerDetector.java

private String findHostName(String installPath, String filename, String host) {
    String hostname = null;/* www  .  ja  va 2s.  co m*/

    File file = new File(installPath + "/" + filename);
    BufferedReader reader = null;

    if (!file.exists()) {
        _log.debug(file.getAbsolutePath() + " doesn't exist");
        return host;
    }
    try {
        reader = new BufferedReader(new FileReader(file));
        String line;
        while ((line = reader.readLine()) != null) {
            if (line.length() != 0) {
                char chr = line.charAt(0);
                if ((chr != '#') && (chr != '<') && (!Character.isWhitespace(chr))) {
                    int ix = line.indexOf('#');
                    if (ix != -1) {
                        line = line.substring(0, ix);
                    }
                    line = line.trim();
                    String[] ent = StringUtil.explodeQuoted(line);
                    if ("SERVERNAME".equals(ent[0].toUpperCase())) {
                        if (ent.length > 1) {
                            line = ent[1];
                            ix = line.indexOf(':');
                            if (ix != -1) {
                                line = line.substring(0, ix);
                            }
                            line = line.trim();
                            hostname = line;
                        }
                    } else if ("INCLUDE".equals(ent[0].toUpperCase())) {
                        String hostname2 = findHostName(installPath, ent[1], null);
                        if (hostname2 != null)
                            hostname = hostname2;
                    }
                }
            }
        }
    } catch (IOException e) {
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
    }

    if (hostname != null) {
        ssl_hostname = hostname;
        return hostname;
    }
    return host;
}

From source file:com.google.dart.tools.ui.internal.text.dart.DartAutoIndentStrategy_NEW.java

/**
 * Cuts the visual equivalent of <code>toDelete</code> characters out of the indentation of line
 * <code>line</code> in <code>document</code>. Leaves leading comment signs alone.
 * //from  w  w  w  .java  2 s .c o  m
 * @param document the document
 * @param line the line
 * @param toDelete the number of space equivalents to delete
 * @param tabLength the length of a tab
 * @throws BadLocationException on concurrent document modification
 */
private void cutIndent(Document document, int line, int toDelete, int tabLength) throws BadLocationException {
    IRegion region = document.getLineInformation(line);
    int from = region.getOffset();
    int endOffset = region.getOffset() + region.getLength();

    // go behind line comments
    while (from < endOffset - 2 && document.get(from, 2).equals(LINE_COMMENT)) {
        from += 2;
    }

    int to = from;
    while (toDelete > 0 && to < endOffset) {
        char ch = document.getChar(to);
        if (!Character.isWhitespace(ch)) {
            break;
        }
        toDelete -= computeVisualLength(ch, tabLength);
        if (toDelete >= 0) {
            to++;
        } else {
            break;
        }
    }

    document.replace(from, to - from, ""); //$NON-NLS-1$
}

From source file:Unsigned.java

/**
 * Parse a binary number, skipping leading whitespace. Does not throw an
 * exception; if no object can be parsed, index is unchanged!
 * //w w w. j  av a  2 s .  c  o m
 * @param source
 *            the string to parse
 * @param status
 *            the string index to start at
 * @return The binary number as a Long object.
 * 
 * @since 1.0
 */
public Object parseObject(String source, ParsePosition status) {
    int start = status.getIndex();
    boolean success = false;
    boolean skipWhitespace = true;
    StringBuffer buffer = new StringBuffer();

    StringCharacterIterator iter = new StringCharacterIterator(source, start);

    for (char c = iter.current(); c != CharacterIterator.DONE; c = iter.next()) {
        if (skipWhitespace && Character.isWhitespace(c)) {
            // skip whitespace
            continue;
        }
        skipWhitespace = false;

        if ((c == '1') || (c == '0')) {
            success = true;
            buffer.append(c);
        } else {
            break;
        }
    }

    if (!success) {
        return (null);
    }

    // convert binary to long
    if (buffer.length() > 64) {
        // larger than a long, error
        return (null);
    }

    long result = 0;
    buffer.reverse();
    int length = buffer.length();
    for (int i = 0; i < length; i++) {
        result += (buffer.charAt(i) == '1') ? 1 << i : 0;
    }
    status.setIndex(iter.getIndex());
    return (new Long(result));
}

From source file:com.google.gdt.eclipse.designer.builders.GwtBuilder.java

/**
 * Removes annotations of service {@link TypeDeclaration}.
 *///from w ww  .j  a  v a2  s  . c o  m
private static void removeAnnotations(TypeDeclaration serviceType, String source, MultiTextEdit edits) {
    int typePos = serviceType.getStartPosition();
    {
        Javadoc javadoc = serviceType.getJavadoc();
        if (javadoc != null) {
            typePos = javadoc.getStartPosition() + javadoc.getLength();
            while (Character.isWhitespace(source.charAt(typePos))) {
                typePos++;
            }
        }
    }
    int pureTypePos = StringUtils.indexOfAny(source,
            new String[] { "public ", "protected ", "class ", "interface " });
    if (pureTypePos != -1 && pureTypePos != typePos) {
        edits.addChild(new DeleteEdit(typePos, pureTypePos - typePos));
    }
}

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

/**
 * Parse a span with a class or not/*from   w w  w. j  a va2  s . co m*/
 * @param span the span in HTML
 */
private void parseSpan(Element span) throws JSONException {
    if (span.hasText()) {
        int offset = sb.length();
        String name = span.attr("class");
        Range r = new Range(name, offset, 0);
        if (name == null || name.length() == 0)
            name = "span";
        if (isMilestone(name)) {
            pages.add(r);
            sb.append(span.text());
            sb.append("\n");
            pages.updateLen(r, sb.length() - offset);
            prevWasMilestone = true;
        } else if (name.equals("soft-hyphen")) {
            stil.add(r);
            // get previous word
            int i = sb.length() - 1;
            while (i > 0 && !Character.isWhitespace(sb.charAt(i)))
                i--;
            if (i > 0)
                i++;
            String prev = clean(sb.substring(i), true);
            // get next word
            String next = clean(nextWord(span), false);
            if (this.speller.isHardHyphen(prev, next))
                r.name = "hard-hyphen";
            sb.append(span.text());
            stil.updateLen(r, sb.length() - offset);
        } else // span may contain other spans
        {
            stil.add(r);
            List<Node> children = span.childNodes();
            for (Node child : children) {
                if (child instanceof Element) {
                    String nName = child.nodeName().toLowerCase();
                    if (nName.equals("span"))
                        parseSpan((Element) child);
                    else
                        parseOtherElement((Element) child);
                } else if (child instanceof TextNode) {
                    TextNode tn = (TextNode) child;
                    sb.append(tn.text());
                }
            }
            if (isLineFormat(name))
                ensure(1, false);
            stil.updateLen(r, sb.length() - offset);
        }
    }
    // else strangely no text: ignore it
}