Example usage for org.jsoup.nodes Node before

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

Introduction

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

Prototype

public Node before(Node node) 

Source Link

Document

Insert the specified node into the DOM before this node (i.e.

Usage

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 ww w. java  2 s.  c o m
                node.remove();
            }
        }
    }
}