Java Code Examples for org.jsoup.helper.StringUtil#in()

The following examples show how to use org.jsoup.helper.StringUtil#in() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HtmlToPlainText.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void head(Node node, int depth) {
	String name = node.nodeName();
	if (node instanceof TextNode) {
		append(((TextNode) node).text()); // TextNodes carry all user-readable text in the DOM.
	} else if (name.equals("ul")) {
		listNesting++;
	} else if (name.equals("li")) {
		append("\n ");
		for (int i = 1; i < listNesting; i++) {
			append("  ");
		}
		if (listNesting == 1) {
			append("* ");
		} else {
			append("- ");
		}
	} else if (name.equals("dt")) {
		append("  ");
	} else if (StringUtil.in(name, "p", "h1", "h2", "h3", "h4", "h5", "tr")) {
		append("\n");
	}
}
 
Example 2
Source File: HtmlToPlainText.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void tail(Node node, int depth) {
	String name = node.nodeName();
	if (StringUtil.in(name, "br", "dd", "dt", "p", "h1", "h2", "h3", "h4", "h5")) {
		append("\n");
	} else if (StringUtil.in(name, "th", "td")) {
		append(" ");
	} else if (name.equals("a")) {
		append(String.format(" <%s>", node.absUrl("href")));
	} else if (name.equals("ul")) {
		listNesting--;
	}
}
 
Example 3
Source File: HtmlTreeBuilder.java    From jsoup-learning with MIT License 5 votes vote down vote up
void popStackToClose(String... elNames) {
    Iterator<Element> it = stack.descendingIterator();
    while (it.hasNext()) {
        Element next = it.next();
        if (StringUtil.in(next.nodeName(), elNames)) {
            it.remove();
            break;
        } else {
            it.remove();
        }
    }
}
 
Example 4
Source File: HtmlTreeBuilder.java    From jsoup-learning with MIT License 5 votes vote down vote up
private void clearStackToContext(String... nodeNames) {
    Iterator<Element> it = stack.descendingIterator();
    while (it.hasNext()) {
        Element next = it.next();
        if (StringUtil.in(next.nodeName(), nodeNames) || next.nodeName().equals("html"))
            break;
        else
            it.remove();
    }
}
 
Example 5
Source File: HtmlTreeBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
void popStackToClose(String... elNames) {
    for (int pos = stack.size() -1; pos >= 0; pos--) {
        Element next = stack.get(pos);
        stack.remove(pos);
        if (StringUtil.in(next.nodeName(), elNames))
            break;
    }
}
 
Example 6
Source File: HtmlToPlainText.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void append(String text) {
    if (text.startsWith("\n"))
        width = 0; // reset counter if starts with a newline. only from formats above, not in natural text
    if (text.equals(" ") &&
            (accum.length() == 0 || StringUtil.in(accum.substring(accum.length() - 1), " ", "\n")))
        return; // don't accumulate long runs of empty spaces

    if (text.length() + width > maxWidth) { // won't fit, needs to wrap
        String words[] = text.split("\\s+");
        for (int i = 0; i < words.length; i++) {
            String word = words[i];
            boolean last = i == words.length - 1;
            if (!last) // insert a space if not the last word
                word = word + " ";
            if (word.length() + width > maxWidth) { // wrap and reset counter
                accum.append("\n").append(word);
                width = word.length();
            } else {
                accum.append(word);
                width += word.length();
            }
        }
    } else { // fits as is, without need to wrap text
        accum.append(text);
        width += text.length();
    }
}
 
Example 7
Source File: HtmlToPlainText.java    From echo with Apache License 2.0 5 votes vote down vote up
public void tail(Node node, int depth) {
  String name = node.nodeName();
  if (StringUtil.in(name, "br", "dd", "dt", "p", "h1", "h2", "h3", "h4", "h5")) {
    append("\n");
  } else if (name.equals("a")) {
    append(String.format(" <%s>", node.absUrl("href")));
  }
}
 
Example 8
Source File: HtmlToPlainText.java    From echo with Apache License 2.0 5 votes vote down vote up
public void head(Node node, int depth) {
  String name = node.nodeName();
  if (node instanceof TextNode) {
    append(((TextNode) node).text()); // TextNodes carry all user-readable text in the DOM.
  } else if (name.equals("li")) {
    append("\n * ");
  } else if (name.equals("dt")) {
    append("  ");
  } else if (StringUtil.in(name, "p", "h1", "h2", "h3", "h4", "h5", "tr")) {
    append("\n");
  }
}
 
Example 9
Source File: XTokenQueue.java    From zongtui-webcrawler with GNU General Public License v2.0 5 votes vote down vote up
public static String trimQuotes(String str) {
    Validate.isTrue(str != null && str.length() > 0);
    String quote = str.substring(0, 1);
    if (StringUtil.in(quote, "\"", "'")) {
        Validate.isTrue(str.endsWith(quote), "Quote" + " for " + str + " is incomplete!");
        str = str.substring(1, str.length() - 1);
    }
    return str;
}
 
Example 10
Source File: HtmlTreeBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void clearStackToContext(String... nodeNames) {
    for (int pos = stack.size() -1; pos >= 0; pos--) {
        Element next = stack.get(pos);
        if (StringUtil.in(next.nodeName(), nodeNames) || next.nodeName().equals("html"))
            break;
        else
            stack.remove(pos);
    }
}
 
Example 11
Source File: HtmlTreeBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
boolean inSelectScope(String targetName) {
    for (int pos = stack.size() -1; pos >= 0; pos--) {
        Element el = stack.get(pos);
        String elName = el.nodeName();
        if (elName.equals(targetName))
            return true;
        if (!StringUtil.in(elName, TagSearchSelectScope)) // all elements except
            return false;
    }
    Validate.fail("Should not be reachable");
    return false;
}
 
Example 12
Source File: HtmlTreeBuilder.java    From jsoup-learning with MIT License 5 votes vote down vote up
boolean isSpecial(Element el) {
    // todo: mathml's mi, mo, mn
    // todo: svg's foreigObject, desc, title
    String name = el.nodeName();
    return StringUtil.in(name, "address", "applet", "area", "article", "aside", "base", "basefont", "bgsound",
            "blockquote", "body", "br", "button", "caption", "center", "col", "colgroup", "command", "dd",
            "details", "dir", "div", "dl", "dt", "embed", "fieldset", "figcaption", "figure", "footer", "form",
            "frame", "frameset", "h1", "h2", "h3", "h4", "h5", "h6", "head", "header", "hgroup", "hr", "html",
            "iframe", "img", "input", "isindex", "li", "link", "listing", "marquee", "menu", "meta", "nav",
            "noembed", "noframes", "noscript", "object", "ol", "p", "param", "plaintext", "pre", "script",
            "section", "select", "style", "summary", "table", "tbody", "td", "textarea", "tfoot", "th", "thead",
            "title", "tr", "ul", "wbr", "xmp");
}
 
Example 13
Source File: HtmlTreeBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private boolean inSpecificScope(String[] targetNames, String[] baseTypes, String[] extraTypes) {
    for (int pos = stack.size() -1; pos >= 0; pos--) {
        Element el = stack.get(pos);
        String elName = el.nodeName();
        if (StringUtil.in(elName, targetNames))
            return true;
        if (StringUtil.in(elName, baseTypes))
            return false;
        if (extraTypes != null && StringUtil.in(elName, extraTypes))
            return false;
    }
    Validate.fail("Should not be reachable");
    return false;
}
 
Example 14
Source File: XTokenQueue.java    From xsoup with MIT License 5 votes vote down vote up
public static String trimQuotes(String str) {
    Validate.isTrue(str != null && str.length() > 0);
    String quote = str.substring(0, 1);
    if (StringUtil.in(quote, "\"", "'")) {
        Validate.isTrue(str.endsWith(quote), "Quote" + " for " + str + " is incomplete!");
        str = str.substring(1, str.length() - 1);
    }
    return str;
}
 
Example 15
Source File: HtmlTreeBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private boolean inSpecificScope(String[] targetNames, String[] baseTypes, String[] extraTypes) {
    for (int pos = stack.size() -1; pos >= 0; pos--) {
        Element el = stack.get(pos);
        String elName = el.nodeName();
        if (StringUtil.in(elName, targetNames))
            return true;
        if (StringUtil.in(elName, baseTypes))
            return false;
        if (extraTypes != null && StringUtil.in(elName, extraTypes))
            return false;
    }
    Validate.fail("Should not be reachable");
    return false;
}
 
Example 16
Source File: HtmlTreeBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
boolean inSelectScope(String targetName) {
    for (int pos = stack.size() -1; pos >= 0; pos--) {
        Element el = stack.get(pos);
        String elName = el.nodeName();
        if (elName.equals(targetName))
            return true;
        if (!StringUtil.in(elName, TagSearchSelectScope)) // all elements except
            return false;
    }
    Validate.fail("Should not be reachable");
    return false;
}
 
Example 17
Source File: HtmlToPlainText.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void tail(Node node, int depth) {
    String name = node.nodeName();
    if (StringUtil.in(name, "br", "dd", "dt", "p", "h1", "h2", "h3", "h4", "h5"))
        append("\n");
    else if (name.equals("a"))
        append(String.format(" <%s>", node.absUrl("href")));
}
 
Example 18
Source File: HtmlTreeBuilder.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
boolean isSpecial(Element el) {
    // todo: mathml's mi, mo, mn
    // todo: svg's foreigObject, desc, title
    String name = el.nodeName();
    return StringUtil.in(name, TagSearchSpecial);
}
 
Example 19
Source File: HtmlTreeBuilder.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
boolean isSpecial(Element el) {
    // todo: mathml's mi, mo, mn
    // todo: svg's foreigObject, desc, title
    String name = el.nodeName();
    return StringUtil.in(name, TagSearchSpecial);
}
 
Example 20
Source File: HtmlTreeBuilder.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 11.2.5.2 Closing elements that have implied end tags<p/>
 When the steps below require the UA to generate implied end tags, then, while the current node is a dd element, a
 dt element, an li element, an option element, an optgroup element, a p element, an rp element, or an rt element,
 the UA must pop the current node off the stack of open elements.

 @param excludeTag If a step requires the UA to generate implied end tags but lists an element to exclude from the
 process, then the UA must perform the above steps as if that element was not in the above list.
 */
void generateImpliedEndTags(String excludeTag) {
    while ((excludeTag != null && !currentElement().nodeName().equals(excludeTag)) &&
            StringUtil.in(currentElement().nodeName(), TagSearchEndTags))
        pop();
}