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:it.geosolutions.httpproxy.utils.Utils.java

/**
 * @param ch/*from  ww w . j ava  2 s . c  om*/
 * @return
 */
final static int escapeHtmlFull(int ch) {
    if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9') {
        // safe
        return ch;
    } else if (Character.isWhitespace(ch)) {
        if (ch != '\n' && ch != '\r' && ch != '\t')
            // safe
            return ch;
    } else if (Character.isDefined(ch)) {
        // safe
        return ch;
    } else if (Character.isISOControl(ch)) {
        // paranoid version:isISOControl which are not isWhitespace
        // removed !
        // do nothing do not include in output !
        return -1;
    } else if (Character.isHighSurrogate((char) ch)) {
        // do nothing do not include in output !
        return -1;
    } else if (Character.isLowSurrogate((char) ch)) {
        // wrong char[] sequence, //TODO: LOG !!!
        return -1;
    }

    return -1;
}

From source file:Main.java

/**
 * This class converts a DOM representation node to text.
 * @param node/*from  w w w .ja  v a  2s .c o  m*/
 * @return
 * @throws Exception
 */
public static final String toText(Node node) throws Exception {
    String ret = null;
    try {
        // Transform the DOM represent to text
        ByteArrayOutputStream xmlstr = new ByteArrayOutputStream();
        DOMSource source = new DOMSource(node);
        //source.setNode(node);

        StreamResult result = new StreamResult(xmlstr);
        Transformer trans = TransformerFactory.newInstance().newTransformer();
        trans.transform(source, result);

        xmlstr.close();

        ret = new String(xmlstr.toByteArray());

        if ((node instanceof Document) == false) {
            // Strip off any <?xml> header
            int index = ret.indexOf("<?xml");
            if (index != -1) {
                index = ret.indexOf("<", 1);
                if (index != -1) {
                    ret = ret.substring(index);
                } else {
                    index = ret.indexOf("?>");
                    if (index != -1) {
                        index += 2;

                        // Remove any trailing whitespaces after XML header
                        while (index < ret.length() && Character.isWhitespace(ret.charAt(index))) {
                            index++;
                        }

                        ret = ret.substring(index);
                    }
                }
            }
        }

    } catch (Exception e) {
        throw new Exception("Failed to transform DOM representation into text", e);
    }
    if (ret != null) {
        return format(ret);
    }
    return ret;
}

From source file:StringUtilities.java

/**
 * Clean the passed string. Replace whitespace characters with a single
 * space. If a <TT>null</TT> string passed return an empty string. E.G.
 * replace//from ww  w  .  ja  va 2s . co  m
 *
 * [pre]
 * \t\tselect\t* from\t\ttab01
 * [/pre]
 *
 * with
 *
 * [pre]
 * select * from tab01
 * [/pre]
 *
 * @param str String to be cleaned.
 *
 * @return  Cleaned string.
 */
public static String cleanString(String str) {
    final StringBuffer buf = new StringBuffer(str.length());
    char prevCh = ' ';

    for (int i = 0, limit = str.length(); i < limit; ++i) {
        char ch = str.charAt(i);
        if (Character.isWhitespace(ch)) {
            if (!Character.isWhitespace(prevCh)) {
                buf.append(' ');
            }
        } else {
            buf.append(ch);
        }
        prevCh = ch;
    }

    return buf.toString();
}

From source file:de.jcup.egradle.core.util.GradleResourceLinkCalculator.java

private GradleHyperLinkResult internalCreateLink(String line, int offsetInLine) {
    /* e.g. abc defg Test abc */
    /* ^-- Test must be identified */
    String rightSubString = line.substring(offsetInLine);
    StringBuilder content = new StringBuilder();
    for (char c : rightSubString.toCharArray()) {
        if (Character.isWhitespace(c)) {
            break;
        }/*from  ww  w  .  j a v a  2s .c  o  m*/
        if (c == '{') {
            break;
        }
        if (c == ',') {
            break;
        }
        if (c == '(') {
            break;
        }
        if (c == ')') {
            break;
        }
        if (c == '[') {
            break;
        }
        if (c == '<') {
            break;
        }
        if (c == '>') {
            break;
        }
        if (c == '.') {
            break;
        }
        if (!Character.isJavaIdentifierPart(c)) {
            return null;
        }
        content.append(c);
    }
    if (StringUtils.isBlank(content)) {
        return null;
    }
    String leftSubString = line.substring(0, offsetInLine);
    char[] leftCharsArray = leftSubString.toCharArray();

    ArrayUtils.reverse(leftCharsArray);
    int startPos = offsetInLine;
    for (char c : leftCharsArray) {
        if (c == '(') {
            break;
        }
        if (c == '<') {
            break;
        }
        if (c == '.') {
            break;
        }
        if (Character.isWhitespace(c)) {
            break;
        }
        if (!Character.isJavaIdentifierPart(c)) {
            return null;
        }
        startPos--;
        content.insert(0, c);
    }
    String linkContent = content.toString();

    char firstChar = linkContent.charAt(0);
    if (!Character.isJavaIdentifierStart(firstChar)) {
        return null;
    }
    /*
     * currently this calculator only supports correct Type syntax means
     * first char MUST be upper cased
     */
    if (!Character.isUpperCase(firstChar)) {
        return null;
    }
    GradleHyperLinkResult result = new GradleHyperLinkResult();
    result.linkOffsetInLine = startPos;
    result.linkContent = linkContent;
    result.linkLength = linkContent.length();
    return result;
}

From source file:com.baifendian.swordfish.common.utils.CommonUtil.java

/**
 * sql ?, ?, ?? ";"//from  w  w w .ja v a2s  .  c o m
 */
public static List<String> sqlSplit(String sql) {
    if (StringUtils.isEmpty(sql)) {
        return Collections.EMPTY_LIST;
    }

    List<String> r = new ArrayList<>();

    Status status = Status.START;
    StringBuffer buffer = new StringBuffer();

    for (int i = 0; i < sql.length(); ++i) {
        char c = sql.charAt(i);
        char nextChar = ((i + 1) < sql.length()) ? sql.charAt(i + 1) : ' '; // add at 2017/1/6

        boolean skip = false;

        switch (status) {
        case START: {
            if (c == ';') {
                status = Status.END;
                skip = true; //  ;
            } else if (c == '\'') {
                status = Status.SINGLE_QUOTE;
            } else if (c == '"') {
                status = Status.QUOTE;
            } else if (c == '-' && nextChar == '-') { // add at 2017/1/6
                status = Status.COMMENT;
                ++i; // add at 2017/1/6
                skip = true;
            } else if (Character.isWhitespace(c)) {
                status = Status.BLANK;
            }
        }
            break;
        case BLANK: {
            if (c == ';') {
                status = Status.END;
                skip = true; //  ;
            } else if (c == '"') {
                status = Status.QUOTE;
            } else if (c == '\'') {
                status = Status.SINGLE_QUOTE;
            } else if (c == '-' && nextChar == '-') { // add at 2017/1/6)
                status = Status.COMMENT;
                ++i; // add at 2017/1/6
                skip = true;
            } else if (!Character.isWhitespace(c)) {
                status = Status.START;
            } else {
                skip = true;
            }
        }
            break;
        case END: {
            if (c == '"') {
                status = Status.QUOTE;
            } else if (c == '\'') {
                status = Status.SINGLE_QUOTE;
            } else if (Character.isWhitespace(c)) {
                status = Status.BLANK;
            } else if (c == '-' && nextChar == '-') { // add at 2017/1/6)
                status = Status.COMMENT;
                ++i; // add at 2017/1/6
                skip = true;
            } else if (c != ';') {
                status = Status.START;
            } else {
                skip = true;
            }
        }
            break;
        case QUOTE: {
            if (c == '"') {
                status = Status.START;
            } else if (c == '\\') {
                status = Status.BACKSLASH_FOR_QUOTE;
            }
        }
            break;
        case SINGLE_QUOTE: {
            if (c == '\'') {
                status = Status.START;
            } else if (c == '\\') {
                status = Status.BACKSLASH_FOR_SINGLE_QUOTE;
            }
        }
            break;
        case BACKSLASH_FOR_QUOTE: {
            status = Status.QUOTE;
        }
            break;
        case BACKSLASH_FOR_SINGLE_QUOTE: {
            status = Status.SINGLE_QUOTE;
        }
            break;
        case COMMENT: {
            if (c != '\r' && c != '\n') {
                status = Status.COMMENT;
                skip = true;
            } else {
                status = Status.START;
            }
        }
            break;
        }

        if (!skip) {
            //  white space ??
            if (Character.isWhitespace(c)) {
                buffer.append(' ');
            } else {
                buffer.append(c);
            }
        }

        if (status == Status.END) {
            String sub = buffer.toString();
            if (!StringUtils.isWhitespace(sub)) {
                r.add(sub.trim());
            }
            buffer = new StringBuffer();
        }
    }

    String sub = buffer.toString();
    if (!StringUtils.isWhitespace(sub)) {
        r.add(sub.trim());
    }

    return r;
}

From source file:au.org.ala.bhl.WordLists.java

public static List<String> sanitize(String text) {
    String[] tokens = text.split("\\s");

    List<String> words = new ArrayList<String>();

    for (String token : tokens) {
        if (!StringUtils.isEmpty(token)) {

            StringBuilder b = new StringBuilder();
            for (int i = 0; i < token.length(); ++i) {
                char ch = token.charAt(i);
                if (".,;:{}[]()&$!@#`~;\"'".indexOf(ch) >= 0) {
                    continue;
                }//from  w  w  w.ja va 2s .  c om

                if (Character.isWhitespace(ch)) {
                    continue;
                }

                if ("-".indexOf(ch) >= 0) {
                    if (b.length() > 0) {
                        words.add(b.toString());
                    }
                    b = new StringBuilder();
                    continue;
                }

                if (!Character.isLetter(ch)) {
                    // Throw away this token because it contains some other non-letter (numbers etc)
                    b = new StringBuilder();
                    break;
                }

                b.append(ch);
            }

            // Only consider words greater than one letter.
            if (b.length() > 1) {
                words.add(b.toString());
            }
        }
    }
    return words;

}

From source file:Comments.java

public static String removeComment(String input) {

    StringBuffer sb = new StringBuffer(input);
    char NQ = ' ', quote = NQ;
    int len = sb.length();
    for (int j = 0, lineno = 1; j < len; j++) {
        if (sb.charAt(j) == '\n')
            ++lineno;/*from  w w  w.j  a  va 2 s  . co m*/

        if (quote != NQ) {
            if (sb.charAt(j) == quote) {
                quote = NQ;
            } else if (sb.charAt(j) == '\\') {
                j++;
                //fix for  "123\\\r\n123" 
                if (sb.charAt(j) == '\r')
                    j++;
                // if(sb.charAt(j) == '\n') j++;
            } else if (sb.charAt(j) == '\n') {
                throw new IllegalStateException("Unterminated string at line " + lineno);
            }
        } else if (sb.charAt(j) == '/' && j + 1 < len && (sb.charAt(j + 1) == '*' || sb.charAt(j + 1) == '/')) {
            int l = j;
            boolean eol = sb.charAt(++j) == '/';
            while (++j < len) {
                if (sb.charAt(j) == '\n')
                    ++lineno;

                if (eol) {
                    if (sb.charAt(j) == '\n') {
                        sb.delete(l, sb.charAt(j - 1) == '\r' ? j - 1 : j);
                        len = sb.length();
                        j = l;
                        break;
                    }
                } else if (sb.charAt(j) == '*' && j + 1 < len && sb.charAt(j + 1) == '/') {
                    sb.delete(l, j + 2);
                    len = sb.length();
                    j = l;
                    break;
                }
            }
        } else if (sb.charAt(j) == '\'' || sb.charAt(j) == '"') {
            quote = sb.charAt(j);
        } else if (sb.charAt(j) == '/') { // regex
            boolean regex = false;
            for (int k = j;;) {
                if (--k < 0) {
                    regex = true;
                    break;
                }

                char ck = sb.charAt(k);
                if (!Character.isWhitespace(ck)) {
                    regex = ck == '(' || ck == ',' || ck == '=' || ck == ':' || ck == '?' || ck == '{'
                            || ck == '[' || ck == ';' || ck == '!' || ck == '&' || ck == '|' || ck == '^'
                            || (ck == 'n' && k > 4 && "return".equals(sb.substring(k - 5, k + 1)))
                            || (ck == 'e' && k > 2 && "case".equals(sb.substring(k - 3, k + 1)));
                    break;
                }
            }
            if (regex) {
                while (++j < len && sb.charAt(j) != '/') {
                    if (sb.charAt(j) == '\\')
                        j++;
                    else if (sb.charAt(j) == '\n') {
                        throw new IllegalStateException("Unterminated regex at line " + lineno);
                    }
                }
            }
        }
    }
    return sb.toString();
}

From source file:de.micromata.genome.gwiki.utils.CommaListParser.java

/**
 * return index on non whitespace./*from   w w  w. java2 s. c  o m*/
 *
 * @param input the input
 * @param position the position
 * @return the int
 */
private static int skeepWs(String input, int position) {
    for (int i = position; i < input.length(); ++i) {
        if (Character.isWhitespace(input.charAt(i)) == false) {
            return i;
        }
    }
    return input.length();
}

From source file:StringUtils.java

/**
 * Reformats a string where lines that are longer than <tt>width</tt>
 * are split apart at the earliest wordbreak or at maxLength, whichever is
 * sooner. If the width specified is less than 5 or greater than the input
 * Strings length the string will be returned as is.
 * <p/>/*  w w  w.j  a v a2 s  .  co  m*/
 * Please note that this method can be lossy - trailing spaces on wrapped
 * lines may be trimmed.
 *
 * @param input the String to reformat.
 * @param width the maximum length of any one line.
 * @return a new String with reformatted as needed.
 */
public static String wordWrap(String input, int width, Locale locale) {
    // protect ourselves
    if (input == null) {
        return "";
    } else if (width < 5) {
        return input;
    } else if (width >= input.length()) {
        return input;
    }

    StringBuilder buf = new StringBuilder(input);
    boolean endOfLine = false;
    int lineStart = 0;

    for (int i = 0; i < buf.length(); i++) {
        if (buf.charAt(i) == '\n') {
            lineStart = i + 1;
            endOfLine = true;
        }

        // handle splitting at width character
        if (i > lineStart + width - 1) {
            if (!endOfLine) {
                int limit = i - lineStart - 1;
                BreakIterator breaks = BreakIterator.getLineInstance(locale);
                breaks.setText(buf.substring(lineStart, i));
                int end = breaks.last();

                // if the last character in the search string isn't a space,
                // we can't split on it (looks bad). Search for a previous
                // break character
                if (end == limit + 1) {
                    if (!Character.isWhitespace(buf.charAt(lineStart + end))) {
                        end = breaks.preceding(end - 1);
                    }
                }

                // if the last character is a space, replace it with a \n
                if (end != BreakIterator.DONE && end == limit + 1) {
                    buf.replace(lineStart + end, lineStart + end + 1, "\n");
                    lineStart = lineStart + end;
                }
                // otherwise, just insert a \n
                else if (end != BreakIterator.DONE && end != 0) {
                    buf.insert(lineStart + end, '\n');
                    lineStart = lineStart + end + 1;
                } else {
                    buf.insert(i, '\n');
                    lineStart = i + 1;
                }
            } else {
                buf.insert(i, '\n');
                lineStart = i + 1;
                endOfLine = false;
            }
        }
    }

    return buf.toString();
}

From source file:Main.java

/**
 * <p>Deletes all whitespaces from a String as defined by
 * {@link Character#isWhitespace(char)}.</p>
 *
 * <pre>/*from   w  ww  .j ava2  s  .  co m*/
 * StringUtils.deleteWhitespace(null)         = null
 * StringUtils.deleteWhitespace("")           = ""
 * StringUtils.deleteWhitespace("abc")        = "abc"
 * StringUtils.deleteWhitespace("   ab  c  ") = "abc"
 * </pre>
 *
 * @param str  the String to delete whitespace from, may be null
 * @return the String without whitespaces, {@code null} if null String input
 */
public static String deleteWhitespace(String str) {
    if (isEmpty(str)) {
        return str;
    }
    int sz = str.length();
    char[] chs = new char[sz];
    int count = 0;
    for (int i = 0; i < sz; i++) {
        if (!Character.isWhitespace(str.charAt(i))) {
            chs[count++] = str.charAt(i);
        }
    }
    if (count == sz) {
        return str;
    }
    return new String(chs, 0, count);
}