List of usage examples for org.jsoup.nodes Node before
public Node before(Node node)
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(); } } } }