Example usage for org.jsoup.nodes Node remove

List of usage examples for org.jsoup.nodes Node remove

Introduction

In this page you can find the example usage for org.jsoup.nodes Node remove.

Prototype

public void remove() 

Source Link

Document

Remove (delete) this node from the DOM tree.

Usage

From source file:Main.java

private static void removeComments(Node node) {
    for (int i = 0; i < node.childNodes().size();) {
        Node child = node.childNode(i);
        if (child.nodeName().equals("#comment"))
            child.remove();
        else {/* ww w  .  j av  a 2  s  . c om*/
            removeComments(child);
            i++;
        }
    }
}

From source file:damo.three.ie.util.HtmlUtilities.java

private static void removeComments(Node node) {
    for (int i = 0; i < node.childNodes().size();) {
        Node child = node.childNode(i);
        if (child.nodeName().equals("#comment")) {
            child.remove();
        } else {/*from   ww  w.  j ava2  s  .c o m*/
            removeComments(child);
            i++;
        }
    }
}

From source file:com.screenslicer.core.scrape.ProcessPage.java

private static void trim(Element body) {
    final List<Node> toRemove = new ArrayList<Node>();
    body.traverse(new NodeVisitor() {
        @Override//from  w  ww  .j av  a 2 s. c om
        public void tail(Node n, int d) {
        }

        @Override
        public void head(Node node, int d) {
            if (Util.isHidden(node)) {
                toRemove.add(node);
            }
        }
    });
    for (Node node : toRemove) {
        node.remove();
    }
}

From source file:com.zacwolf.commons.email.Email.java

public static void removeComments(org.jsoup.nodes.Node node) {
    for (int i = 0; i < node.childNodes().size(); i++) {
        org.jsoup.nodes.Node child = node.childNode(i);
        if (child.nodeName().equals("#comment"))
            child.remove();
        else/*from   w ww .j a  va  2s . c om*/
            removeComments(child);
    }
}

From source file:com.sfs.DataFilter.java

/**
 * Removes the comments./*from w ww  .j  a va2  s  .co  m*/
 *
 * @param node the node
 */
private static void removeComments(Node node) {
    for (int i = 0; i < node.childNodes().size();) {
        Node child = node.childNode(i);
        if (child.nodeName().equals("#comment"))
            child.remove();
        else {
            removeComments(child);
            i++;
        }
    }
}

From source file:com.mycollab.core.utils.StringUtils.java

private static void replaceHtml(Node element) {
    List<Node> elements = element.childNodes();
    Pattern compile = Pattern.compile("(?:https?|ftps?)://[\\w/%.-][/\\??\\w=?\\w?/%.-]?[/\\?&\\w=?\\w?/%.-]*");
    for (int i = elements.size() - 1; i >= 0; i--) {
        Node node = elements.get(i);
        if (node instanceof TextNode) {
            String value = ((TextNode) node).text();
            Matcher matcher = compile.matcher(value);
            if (matcher.find()) {
                value = value.replaceAll(
                        "(?:https?|ftps?)://[\\w/%.-][/\\??\\w=?\\w?/%.-]?[/\\?&\\w=?\\w?/%.-]*",
                        "<a href=\"$0\" target=\"_blank\">$0</a>");
                Document newDoc = Jsoup.parse(value);
                List<Node> childs = newDoc.body().childNodes();
                for (int j = 0; j < childs.size(); j++) {
                    Node childNode = childs.get(j).clone();
                    node.before(childNode);
                }//from   www.  j  a  v  a 2 s .c o m
                node.remove();
            }
        }
    }
}

From source file:com.aestasit.markdown.slidery.converters.TextTemplateConverter.java

private void removeComments(Node parent) {
    List<Node> nodesToRemove = new ArrayList<Node>();
    for (Node child : parent.childNodes()) {
        if (child.nodeName().equals("#comment")) {
            nodesToRemove.add(child);/*from w  w w  .  j a  v  a 2s  . c o  m*/
        }
    }
    for (Node node : nodesToRemove) {
        node.remove();
    }
}

From source file:org.b3log.symphony.util.Markdowns.java

/**
 * Converts the specified markdown text to HTML.
 *
 * @param markdownText the specified markdown text
 * @return converted HTML, returns an empty string "" if the specified markdown text is "" or {@code null}, returns
 * 'markdownErrorLabel' if exception/*from   w  ww .j av  a  2 s .  c om*/
 */
public static String toHTML(final String markdownText) {
    if (Strings.isEmptyOrNull(markdownText)) {
        return "";
    }

    final String cachedHTML = getHTML(markdownText);
    if (null != cachedHTML) {
        return cachedHTML;
    }

    final ExecutorService pool = Executors.newSingleThreadExecutor();
    final long[] threadId = new long[1];

    final Callable<String> call = () -> {
        threadId[0] = Thread.currentThread().getId();

        String html = LANG_PROPS_SERVICE.get("contentRenderFailedLabel");

        if (MARKED_AVAILABLE) {
            html = toHtmlByMarked(markdownText);
            if (!StringUtils.startsWith(html, "<p>")) {
                html = "<p>" + html + "</p>";
            }
        } else {
            com.vladsch.flexmark.ast.Node document = PARSER.parse(markdownText);
            html = RENDERER.render(document);
            if (!StringUtils.startsWith(html, "<p>")) {
                html = "<p>" + html + "</p>";
            }
        }

        final Document doc = Jsoup.parse(html);
        final List<org.jsoup.nodes.Node> toRemove = new ArrayList<>();
        doc.traverse(new NodeVisitor() {
            @Override
            public void head(final org.jsoup.nodes.Node node, int depth) {
                if (node instanceof org.jsoup.nodes.TextNode) {
                    final org.jsoup.nodes.TextNode textNode = (org.jsoup.nodes.TextNode) node;
                    final org.jsoup.nodes.Node parent = textNode.parent();

                    if (parent instanceof Element) {
                        final Element parentElem = (Element) parent;

                        if (!parentElem.tagName().equals("code")) {
                            String text = textNode.getWholeText();
                            boolean nextIsBr = false;
                            final org.jsoup.nodes.Node nextSibling = textNode.nextSibling();
                            if (nextSibling instanceof Element) {
                                nextIsBr = "br".equalsIgnoreCase(((Element) nextSibling).tagName());
                            }

                            if (null != userQueryService) {
                                try {
                                    final Set<String> userNames = userQueryService.getUserNames(text);
                                    for (final String userName : userNames) {
                                        text = text.replace('@' + userName + (nextIsBr ? "" : " "),
                                                "@<a href='" + Latkes.getServePath() + "/member/" + userName
                                                        + "'>" + userName + "</a> ");
                                    }
                                    text = text.replace("@participants ",
                                            "@<a href='https://hacpai.com/article/1458053458339' class='ft-red'>participants</a> ");
                                } finally {
                                    JdbcRepository.dispose();
                                }
                            }

                            if (text.contains("@<a href=")) {
                                final List<org.jsoup.nodes.Node> nodes = Parser.parseFragment(text, parentElem,
                                        "");
                                final int index = textNode.siblingIndex();

                                parentElem.insertChildren(index, nodes);
                                toRemove.add(node);
                            } else {
                                textNode.text(Pangu.spacingText(text));
                            }
                        }
                    }
                }
            }

            @Override
            public void tail(org.jsoup.nodes.Node node, int depth) {
            }
        });

        toRemove.forEach(node -> node.remove());

        doc.select("pre>code").addClass("hljs");
        doc.select("a").forEach(a -> {
            String src = a.attr("href");
            if (!StringUtils.startsWithIgnoreCase(src, Latkes.getServePath())) {
                try {
                    src = URLEncoder.encode(src, "UTF-8");
                } catch (final Exception e) {
                }
                a.attr("href", Latkes.getServePath() + "/forward?goto=" + src);
                a.attr("target", "_blank");
            }
        });
        doc.outputSettings().prettyPrint(false);

        String ret = doc.select("body").html();
        ret = StringUtils.trim(ret);

        // cache it
        putHTML(markdownText, ret);

        return ret;
    };

    Stopwatchs.start("Md to HTML");
    try {
        final Future<String> future = pool.submit(call);

        return future.get(MD_TIMEOUT, TimeUnit.MILLISECONDS);
    } catch (final TimeoutException e) {
        LOGGER.log(Level.ERROR, "Markdown timeout [md=" + markdownText + "]");
        Callstacks.printCallstack(Level.ERROR, new String[] { "org.b3log" }, null);

        final Set<Thread> threads = Thread.getAllStackTraces().keySet();
        for (final Thread thread : threads) {
            if (thread.getId() == threadId[0]) {
                thread.stop();

                break;
            }
        }
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Markdown failed [md=" + markdownText + "]", e);
    } finally {
        pool.shutdownNow();

        Stopwatchs.end();
    }

    return LANG_PROPS_SERVICE.get("contentRenderFailedLabel");
}

From source file:sk.svec.jan.acb.extraction.Finder.java

public Node removeNodes(Node root, Node nodeToRemove) {
    Node node = root;
    Node ntr = nodeToRemove;//w w  w .  j  a  va 2 s  . com
    int depth = 0;

    while (node != null) {
        if (node.equals(ntr)) {
            node.remove();
            return root;
        }
        if (node.childNodeSize() > 0) {
            node = node.childNode(0);
            depth++;
        } else {
            while (node.nextSibling() == null && depth > 0) {
                node = node.parentNode();
                depth--;
            }

            if (node == root) {
                break;
            }
            node = node.nextSibling();
        }

    }
    return root;
}