Java Code Examples for org.jsoup.nodes.TextNode#text()

The following examples show how to use org.jsoup.nodes.TextNode#text() . 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: OutputFormatter.java    From Xndroid with GNU General Public License v3.0 6 votes vote down vote up
private void appendTextSkipHidden(Element e, StringBuilder accum, int indent) {
    for (Node child : e.childNodes()) {
        if (unlikely(child)) {
            continue;
        }
        if (child instanceof TextNode) {
            TextNode textNode = (TextNode) child;
            String txt = textNode.text();
            accum.append(txt);
        } else if (child instanceof Element) {
            Element element = (Element) child;
            if (accum.length() > 0 && element.isBlock()
                    && !lastCharIsWhitespace(accum))
                accum.append(' ');
            else if (element.tagName().equals("br"))
                accum.append(' ');
            appendTextSkipHidden(element, accum, indent + 1);
        }
    }
}
 
Example 2
Source File: ElementOperator.java    From zongtui-webcrawler with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String operate(Element element) {
    int index = 0;
    StringBuilder accum = new StringBuilder();
    for (Node node : element.childNodes()) {
        if (node instanceof TextNode) {
            TextNode textNode = (TextNode) node;
            if (group == 0) {
                accum.append(textNode.text());
            } else if (++index == group) {
                return textNode.text();
            }
        }
    }
    return accum.toString();
}
 
Example 3
Source File: OutputFormatter.java    From JumpGo with Mozilla Public License 2.0 6 votes vote down vote up
private void appendTextSkipHidden(Element e, StringBuilder accum, int indent) {
    for (Node child : e.childNodes()) {
        if (unlikely(child)) {
            continue;
        }
        if (child instanceof TextNode) {
            TextNode textNode = (TextNode) child;
            String txt = textNode.text();
            accum.append(txt);
        } else if (child instanceof Element) {
            Element element = (Element) child;
            if (accum.length() > 0 && element.isBlock()
                    && !lastCharIsWhitespace(accum))
                accum.append(' ');
            else if (element.tagName().equals("br"))
                accum.append(' ');
            appendTextSkipHidden(element, accum, indent + 1);
        }
    }
}
 
Example 4
Source File: TruncateHtmlFilter.java    From jinjava with Apache License 2.0 6 votes vote down vote up
@Override
public void head(Node node, int depth) {
  if (node instanceof TextNode) {
    TextNode text = (TextNode) node;
    String textContent = text.text();

    if (textLen >= maxTextLen) {
      text.text("");
    } else if (textLen + textContent.length() > maxTextLen) {
      int ptr = maxTextLen - textLen;
      if (!killwords) {
        ptr = Functions.movePointerToJustBeforeLastWord(ptr, textContent) - 1;
      }

      text.text(textContent.substring(0, ptr) + ending);
      textLen = maxTextLen;
    } else {
      textLen += textContent.length();
    }
  }
}
 
Example 5
Source File: ElementOperator.java    From xsoup with MIT License 6 votes vote down vote up
@Override
public String operate(Element element) {
    int index = 0;
    StringBuilder accum = new StringBuilder();
    for (Node node : element.childNodes()) {
        if (node instanceof TextNode) {
            TextNode textNode = (TextNode) node;
            if (group == 0) {
                accum.append(textNode.text());
            } else if (++index == group) {
                return textNode.text();
            }
        }
    }
    return accum.toString();
}
 
Example 6
Source File: HtmlHelper.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
static void cleanup(Document d) {
    // https://www.chromestatus.com/feature/5756335865987072
    // Some messages contain 100 thousands of Apple spaces
    for (Element aspace : d.select(".Apple-converted-space")) {
        Node next = aspace.nextSibling();
        if (next instanceof TextNode) {
            TextNode tnode = (TextNode) next;
            tnode.text(" " + tnode.text());
            aspace.remove();
        } else
            aspace.replaceWith(new TextNode(" "));
    }
}
 
Example 7
Source File: HtmlHelper.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
static boolean truncate(Document d, boolean reformat) {
    int max = (reformat ? MAX_FORMAT_TEXT_SIZE : MAX_FULL_TEXT_SIZE);

    int length = 0;
    int images = 0;
    for (Element elm : d.select("*")) {
        if ("img".equals(elm.tagName()))
            images++;

        boolean skip = false;
        for (Node child : elm.childNodes()) {
            if (child instanceof TextNode) {
                TextNode tnode = ((TextNode) child);
                String text = tnode.getWholeText();

                if (length < max) {
                    if (length + text.length() >= max) {
                        text = text.substring(0, max - length) + " ...";
                        tnode.text(text);
                        skip = true;
                    }
                } else {
                    if (skip)
                        tnode.text("");
                }

                length += text.length();
            }
        }

        if (length >= max && !skip)
            elm.remove();
    }

    Log.i("Message size=" + length + " images=" + images);

    return (length >= max);
}
 
Example 8
Source File: M2DocHTMLParser.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Inserts the text of the given {@link TextNode}.
 * 
 * @param parent
 *            the parent {@link MList}
 * @param context
 *            the {@link Context}
 * @param node
 *            the {@link TextNode}
 */
private void insertText(MList parent, final Context context, TextNode node) {
    final String text = node.text();
    if (!text.trim().isEmpty()) {
        if (context.linkTargetURI == null) {
            final MText mText = new MTextImpl(text, context.style);
            parent.add(mText);
        } else {
            context.style.setForegroundColor(LINK_COLOR);
            final MHyperLink mLink = new MHyperLinkImpl(text, context.style, context.linkTargetURI.toString());
            parent.add(mLink);
        }
    }
}
 
Example 9
Source File: ContentExtractor.java    From ContentExtractor with GNU General Public License v2.0 4 votes vote down vote up
public CountInfo(TextNode tNode) {
    this.tNode = tNode;
    String text = tNode.text();
    this.textCount = TextUtils.countText(text);
    this.puncCount = TextUtils.countPunc(text);
}
 
Example 10
Source File: ContentExtractor.java    From WordCount with GNU General Public License v2.0 4 votes vote down vote up
public CountInfo(TextNode tNode) {
    this.tNode = tNode;
    String text = tNode.text();
    this.textCount = TextUtils.countText(text);
    this.puncCount = TextUtils.countPunc(text);
}