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

private int getElement(int c) {
    c = getC();//  ww  w.  j av  a 2s . co  m

    // Broken element

    if (c == EOF)
        return EOF;

    if (c == '!')
        return getSpecialElement();

    attribs.clear();
    c = skipSpace(c);
    if (c == EOF)
        return EOF;

    StringBuffer tag = new StringBuffer();
    if (c == '/') {
        tag.append((char) c);
        c = skipSpace(getC());
        while (c != EOF && c != '>' && !Character.isWhitespace((char) c)) {
            tag.append((char) c);
            c = getC();
        }
        token = tag.toString();
        for (;;) {
            if (c == '>' || c == -1)
                break;
            c = getC();
        }
        return ELEMENT;
    }

    while (c != EOF && c != '>' && c != '/' && !Character.isWhitespace((char) c)) {
        tag.append((char) c);
        c = getC();
    }
    if (c == EOF) {
        token = tag.toString();
        return ELEMENT;
    }

    for (;;) {
        c = skipSpace(c);
        if (c == EOF || c == '>' || c == '/')
            break;
        c = getAttrib(c);
    }
    if (c == '/') {
        tag.append((char) c);
        for (;;) {
            c = getC();
            if (c == -1 || c == '>')
                break;
        }
    }
    token = tag.toString();
    return ELEMENT;
}

From source file:com.digitalpebble.behemoth.io.warc.HttpResponse.java

private void processHeaderLine(StringBuilder line) throws IOException {

    int colonIndex = line.indexOf(":"); // key is up to colon
    if (colonIndex == -1) {
        int i;/*www .  j ava  2s  .c  om*/
        for (i = 0; i < line.length(); i++)
            if (!Character.isWhitespace(line.charAt(i)))
                break;
        if (i == line.length())
            return;
        throw new IOException("No colon in header:" + line);
    }
    String key = line.substring(0, colonIndex);

    int valueStart = colonIndex + 1; // skip whitespace
    while (valueStart < line.length()) {
        int c = line.charAt(valueStart);
        if (c != ' ' && c != '\t')
            break;
        valueStart++;
    }
    String value = line.substring(valueStart);
    headers.set(key, value);
}

From source file:importer.filters.PlayFilter.java

/**
 * Construct a list of what you know are definitely words
 * @param input the input text/*from  w  w w.j av  a 2s .c o m*/
 * @return error log
 */
private String buildWordList(String input) {
    StringTokenizer st = new StringTokenizer(input, "\n\t ", true);
    int state = 0;
    while (st.hasMoreElements()) {
        String token = st.nextToken();
        if (token.length() == 0)
            break;
        switch (state) {
        // at line-start
        case 0:
            if (!Character.isWhitespace(token.charAt(0))) {
                if (endOfSentence(token))
                    state = 2;
                else
                    state = 1;
                token = strip(token);
                if (token.length() == 0)
                    break;
                if (isWord(token) && !words.contains(token))
                    words.add(token);
            }
            break;
        //not at line-start, not after full stop
        case 1:
            if (!Character.isWhitespace(token.charAt(0))) {
                if (endOfSentence(token))
                    state = 2;
                token = strip(token);
                if (token.length() == 0)
                    break;
                if (isWord(token)) {
                    if (!words.contains(token))
                        words.add(token);
                }
            } else if (token.equals("\n"))
                state = 0;
            break;
        // after sentence end
        case 2:
            if (!Character.isWhitespace(token.charAt(0))) {
                if (!endOfSentence(token))
                    state = 1;
                token = strip(token);
                if (token.length() == 0)
                    break;
                if (isWord(token)) {
                    if (!words.contains(token))
                        words.add(token);
                }
            } else if (token.equals("\n"))
                state = 0;
            break;
        }
    }
    // this will do for now
    return "";
}

From source file:com.joliciel.jochre.search.highlight.FixedSizeSnippetFinder.java

@Override
public List<Snippet> findSnippets(int docId, Set<String> fields, Set<HighlightTerm> highlightTerms,
        int maxSnippets, int snippetSize) {
    try {//from   w w  w .j a  v a  2 s .c om
        Document doc = indexSearcher.doc(docId);
        JochreIndexDocument jochreDoc = searchService.getJochreIndexDocument(indexSearcher, docId);
        // find best snippet for each term      
        PriorityQueue<Snippet> heap = new PriorityQueue<Snippet>();

        int i = -1;
        for (HighlightTerm term : highlightTerms) {
            i++;
            String content = jochreDoc.getContents();
            CoordinateStorage coordinateStorage = jochreDoc.getCoordinateStorage();
            if (term.getStartOffset() >= content.length()) {
                String title = doc.get("title");
                String startPage = doc.get("startPage");
                String endPage = doc.get("endPage");
                LOG.debug("Content: " + content);
                throw new RuntimeException(term.toString() + " cannot fit into contents for doc " + title
                        + ", pages " + startPage + " to " + endPage + ", length: " + content.length());
            }
            List<HighlightTerm> snippetTerms = new ArrayList<HighlightTerm>();
            snippetTerms.add(term);
            int j = -1;
            boolean foundImage = false;
            for (HighlightTerm otherTerm : highlightTerms) {
                j++;
                if (j <= i)
                    continue;
                if (otherTerm.getImageIndex() != term.getImageIndex()) {
                    if (foundImage)
                        break;
                    else
                        continue;
                }
                foundImage = true;

                if (otherTerm.getStartOffset() < term.getStartOffset() + snippetSize) {
                    snippetTerms.add(otherTerm);
                } else {
                    break;
                }
            }
            HighlightTerm lastTerm = snippetTerms.get(snippetTerms.size() - 1);
            int middle = (term.getStartOffset() + lastTerm.getEndOffset()) / 2;
            int start = middle - (snippetSize / 2);
            int end = middle + (snippetSize / 2);
            if (start > term.getStartOffset())
                start = term.getStartOffset();
            if (end < lastTerm.getEndOffset())
                end = lastTerm.getEndOffset();

            if (start < 0)
                start = 0;
            if (end > content.length())
                end = content.length();

            for (int k = start; k >= 0; k--) {
                if (Character.isWhitespace(content.charAt(k))) {
                    start = k + 1;
                    break;
                }
            }
            for (int k = end; k < content.length(); k++) {
                if (Character.isWhitespace(content.charAt(k))) {
                    end = k;
                    break;
                }
            }

            int imageStartOffset = coordinateStorage.getImageStartOffset(term.getImageIndex());
            int imageEndOffset = Integer.MAX_VALUE;
            if (term.getImageIndex() + 1 < coordinateStorage.getImageCount()) {
                imageEndOffset = coordinateStorage.getImageStartOffset(term.getImageIndex() + 1);
            }

            if (start < imageStartOffset)
                start = imageStartOffset;
            if (end > imageEndOffset)
                end = imageEndOffset;

            Snippet snippet = new Snippet(docId, term.getField(), start, end);
            snippet.setHighlightTerms(snippetTerms);
            heap.add(snippet);
        }

        // if we have no snippets, add one per field type
        if (heap.isEmpty()) {
            String content = jochreDoc.getContents();
            int end = snippetSize * maxSnippets;
            if (end > content.length())
                end = content.length();
            for (int k = end; k < content.length(); k++) {
                if (Character.isWhitespace(content.charAt(k))) {
                    end = k;
                    break;
                }
            }
            Snippet snippet = new Snippet(docId, fields.iterator().next(), 0, end);
            heap.add(snippet);
        }

        List<Snippet> snippets = new ArrayList<Snippet>(maxSnippets);
        while (snippets.size() < maxSnippets && !heap.isEmpty()) {
            Snippet snippet = heap.poll();
            boolean hasOverlap = false;
            for (Snippet otherSnippet : snippets) {
                if (otherSnippet.hasOverlap(snippet))
                    hasOverlap = true;
            }
            if (!hasOverlap)
                snippets.add(snippet);
        }

        for (Snippet snippet : snippets) {
            LOG.debug("Added snippet: " + snippet.toJson());
        }
        return snippets;
    } catch (IOException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    }
}

From source file:mobile.tiis.appv2.helpers.Utils.java

public static boolean isStringBlank(CharSequence str) {
    int strLen;/* w  w  w.j  a v  a 2 s.  co m*/
    if (str == null || (strLen = str.length()) == 0) {
        return true;
    }
    for (int i = 0; i < strLen; i++) {
        if ((Character.isWhitespace(str.charAt(i)) == false)) {
            return false;
        }
    }
    return true;
}

From source file:TextPrinterExample.java

/**
 * Prints the file//from w ww. j a v  a  2s  .c o m
 */
void print() {
    // Start the print job
    if (printer.startJob(fileName)) {
        // Determine print area, with margins
        bounds = computePrintArea(printer);
        xPos = bounds.x;
        yPos = bounds.y;

        // Create the GC
        gc = new GC(printer);

        // Determine line height
        lineHeight = gc.getFontMetrics().getHeight();

        // Determine tab width--use three spaces for tabs
        int tabWidth = gc.stringExtent("   ").x;

        // Print the text
        printer.startPage();
        buf = new StringBuffer();
        char c;
        for (int i = 0, n = contents.length(); i < n; i++) {
            // Get the next character
            c = contents.charAt(i);

            // Check for newline
            if (c == '\n') {
                printBuffer();
                printNewline();
            }
            // Check for tab
            else if (c == '\t') {
                xPos += tabWidth;
            } else {
                buf.append(c);
                // Check for space
                if (Character.isWhitespace(c)) {
                    printBuffer();
                }
            }
        }
        printer.endPage();
        printer.endJob();
        gc.dispose();
    }
}

From source file:org.apache.brooklyn.util.stream.LoggingOutputStream.java

public void onLine(String line) {
    //right trim, in case there is \r or other funnies
    while (line.length() > 0 && Character.isWhitespace(line.charAt(line.length() - 1)))
        line = line.substring(0, line.length() - 1);
    //left trim, in case there is \r or other funnies
    while (line.length() > 0 && (line.charAt(0) == '\n' || line.charAt(0) == '\r'))
        line = line.substring(1);// w ww.j ava 2s . co m
    if (!line.isEmpty()) {
        if (log != null && log.isDebugEnabled())
            log.debug(logPrefix + line);
    }
}

From source file:com.puppycrawl.tools.checkstyle.checks.whitespace.MethodParamPadCheck.java

@Override
public void visitToken(DetailAST ast) {
    final DetailAST parenAST;
    if (ast.getType() == TokenTypes.METHOD_CALL) {
        parenAST = ast;/*from   ww  w .ja  v a2  s .c  om*/
    } else {
        parenAST = ast.findFirstToken(TokenTypes.LPAREN);
        // array construction => parenAST == null
        if (parenAST == null) {
            return;
        }
    }

    final String line = getLines()[parenAST.getLineNo() - 1];
    if (CommonUtils.hasWhitespaceBefore(parenAST.getColumnNo(), line)) {
        if (!allowLineBreaks) {
            log(parenAST, LINE_PREVIOUS, parenAST.getText());
        }
    } else {
        final int before = parenAST.getColumnNo() - 1;
        if (getAbstractOption() == PadOption.NOSPACE && Character.isWhitespace(line.charAt(before))) {
            log(parenAST, WS_PRECEDED, parenAST.getText());
        } else if (getAbstractOption() == PadOption.SPACE && !Character.isWhitespace(line.charAt(before))) {
            log(parenAST, WS_NOT_PRECEDED, parenAST.getText());
        }
    }
}

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

private static int skipWhitespace(final String cs, final int[] position) {
    int c;/*from   w  w w  .ja  v  a  2s  .  c o  m*/
    do {
        c = read(cs, position);
    } while (Character.isWhitespace((char) c));
    return c;
}

From source file:com.mifos.mifosxdroid.formwidgets.FormWidget.java

/**
 * takes a property name and modifies/*from   w w w  .  j  a  v a  2s  . c  o m*/
 *
 * @param s
 * @return
 */
public String toTitleCase(String s) {
    char[] chars = s.trim().toLowerCase().toCharArray();
    boolean found = false;

    for (int i = 0; i < chars.length; i++) {
        if (!found && Character.isLetter(chars[i])) {
            chars[i] = Character.toUpperCase(chars[i]);
            found = true;
        } else if (Character.isWhitespace(chars[i])) {
            found = false;
        }
    }

    return String.valueOf(chars);
}