/** * Create the DOM given a rootNode and a document builder. * This method is a replica of {@link DomSerializer#createDOM(TagNode)} excepts that it requires to give a * DocumentBuilder. * @param documentBuilder the {@link DocumentBuilder} instance to use, DocumentBuilder is not guaranteed to * be thread safe so at most the safe instance should be used only in the same thread * @param rootNode the HTML Cleaner root node to serialize * @return the W3C Document object * @throws ParserConfigurationException if there's an error during serialization */ public Document createDOM(DocumentBuilder documentBuilder, TagNode rootNode) throws ParserConfigurationException { Document document = createDocument(documentBuilder, rootNode); createSubnodes(document, document.getDocumentElement(), rootNode.getAllChildren()); return document; } }
public boolean satisfy(TagNode tagNode) { if (!isBrNode(tagNode)) { return false; } TagNode parent = tagNode.getParent(); List children = parent.getAllChildren(); int brIndex = children.indexOf(tagNode); return checkSublist(0, brIndex, children) || checkSublist (brIndex, children.size(), children); }
public boolean satisfy(TagNode tagNode) { if (!isBrNode(tagNode)) { return false; } TagNode parent = tagNode.getParent(); List children = parent.getAllChildren(); int brIndex = children.indexOf(tagNode); return checkSublist(0, brIndex, children) || checkSublist(brIndex, children.size(), children); }
private boolean markNodesToPrune(List nodeList, CleanTimeValues cleanTimeValues) { boolean nodesPruned = false; for (Object next : nodeList) { if (next instanceof TagNode && !cleanTimeValues.pruneNodeSet.contains(next)) { TagNode node = (TagNode) next; if (addIfNeededToPruneSet(node, cleanTimeValues)) { nodesPruned = true; } else if (!node.isEmpty()) { nodesPruned |= markNodesToPrune(node.getAllChildren(), cleanTimeValues); } } } return nodesPruned; }
private boolean markNodesToPrune(List nodeList, CleanTimeValues cleanTimeValues) { boolean nodesPruned = false; for (Object next :nodeList) { if(next instanceof TagNode && !cleanTimeValues.pruneNodeSet.contains(next)){ TagNode node = (TagNode) next; if(addIfNeededToPruneSet(node, cleanTimeValues)) { nodesPruned = true; } else if (!node.isEmpty()){ nodesPruned |= markNodesToPrune(node.getAllChildren(), cleanTimeValues); } } } return nodesPruned; } /**
/** * For the specified tag node, defines it's html content. This causes cleaner to * reclean given html portion and insert it inside the node instead of previous content. * @param node * @param content */ public void setInnerHtml(TagNode node, String content) { if (node != null) { String nodeName = node.getName(); StringBuilder html = new StringBuilder(); html.append("<").append(nodeName).append(" " +MARKER_ATTRIBUTE +"=''>").append(content).append("</").append(nodeName).append(">"); TagNode parent = node.getParent(); while (parent != null) { String parentName = parent.getName(); html.insert(0, "<" + parentName + ">"); html.append("</").append(parentName).append(">"); parent = parent.getParent(); } TagNode innerRootNode = clean( html.toString() ); TagNode cleanedNode = innerRootNode.findElementHavingAttribute(MARKER_ATTRIBUTE, true); if (cleanedNode != null) { node.setChildren( cleanedNode.getAllChildren() ); } } } /**
/** * For the specified tag node, defines it's html content. This causes cleaner to * reclean given html portion and insert it inside the node instead of previous content. * * @param node * @param content */ public void setInnerHtml(TagNode node, String content) { if (node != null) { String nodeName = node.getName(); StringBuilder html = new StringBuilder(); html.append("<").append(nodeName).append(" " + MARKER_ATTRIBUTE + "=''>").append(content).append("</").append(nodeName).append(">"); TagNode parent = node.getParent(); while (parent != null) { String parentName = parent.getName(); html.insert(0, "<" + parentName + ">"); html.append("</").append(parentName).append(">"); parent = parent.getParent(); } TagNode innerRootNode = clean(html.toString()); TagNode cleanedNode = innerRootNode.findElementHavingAttribute(MARKER_ATTRIBUTE, true); if (cleanedNode != null) { node.setChildren(cleanedNode.getAllChildren()); } } }
@Override protected void serialize(TagNode tagNode, Writer writer) throws IOException { serializeOpenTag(tagNode, writer, false); List tagChildren = tagNode.getAllChildren(); if ( !isMinimizedTagSyntax(tagNode) ) { Iterator childrenIt = tagChildren.iterator(); while ( childrenIt.hasNext() ) { Object item = childrenIt.next(); if (item != null) { if ( item instanceof ContentNode ) { serializeContentToken((ContentNode)item, tagNode, writer); } else { ((BaseToken)item).serialize(this, writer); } } } serializeEndTag(tagNode, writer, false); } }
@Override protected void serialize(TagNode tagNode, Writer writer) throws IOException { serializeOpenTag(tagNode, writer, false); List<? extends BaseToken> tagChildren = tagNode.getAllChildren(); if (!isMinimizedTagSyntax(tagNode)) { ListIterator<? extends BaseToken> childrenIt = tagChildren.listIterator(); while (childrenIt.hasNext()) { Object item = childrenIt.next(); if (item != null) { if (item instanceof ContentNode) { String content = ((ContentNode) item).getContent().trim(); writer.write(dontEscape(tagNode) ? content.replaceAll("]]>", "]]>") : escapeXml(content)); if (childrenIt.hasNext()) { if (!isWhitespaceString(childrenIt.next())) { writer.write("\n"); } childrenIt.previous(); } } else if (item instanceof CommentNode) { String content = ((CommentNode) item).getCommentedContent().trim(); writer.write(content); } else { ((BaseToken) item).serialize(this, writer); } } } serializeEndTag(tagNode, writer, false); } }
@Override protected void serialize(TagNode tagNode, Writer writer) throws IOException { serializeOpenTag(tagNode, writer, false); List tagChildren = tagNode.getAllChildren(); if ( !isMinimizedTagSyntax(tagNode) ) { ListIterator childrenIt = tagChildren.listIterator(); while ( childrenIt.hasNext() ) { Object item = childrenIt.next(); if (item != null) { if ( item instanceof ContentNode ) { String content = ((ContentNode) item).getContent().trim(); writer.write( dontEscape(tagNode) ? content.replaceAll("]]>", "]]>") : escapeXml(content) ); if (childrenIt.hasNext()) { if ( !isWhitespaceString(childrenIt.next()) ) { writer.write("\n"); } childrenIt.previous(); } } else if (item instanceof CommentNode) { String content = ((CommentNode) item).getCommentedContent().trim(); writer.write(content); } else { ((BaseToken)item).serialize(this, writer); } } } serializeEndTag(tagNode, writer, false); } }
protected void serialize(TagNode tagNode, Writer writer) throws IOException { serializeOpenTag(tagNode, writer, false); if (!isMinimizedTagSyntax(tagNode)) { for (Object item : tagNode.getAllChildren()) { if (item instanceof ContentNode) { String content = item.toString(); writer.write(dontEscape(tagNode) ? content : escapeText(content)); } else if (item instanceof BaseToken) { ((BaseToken) item).serialize(this, writer); } } serializeEndTag(tagNode, writer, false); } }
protected void serialize(TagNode tagNode, Writer writer) throws IOException { serializeOpenTag(tagNode, writer, false); if ( !isMinimizedTagSyntax(tagNode) ) { for (Object item: tagNode.getAllChildren()) { if ( item instanceof ContentNode) { String content = item.toString(); writer.write( dontEscape(tagNode) ? content : escapeText(content) ); } else if (item instanceof BaseToken) { ((BaseToken)item).serialize(this, writer); } } serializeEndTag(tagNode, writer, false); } }
@Override protected void serialize(TagNode tagNode, Writer writer) throws IOException { serializeOpenTag(tagNode, writer, false); List<? extends BaseToken> tagChildren = tagNode.getAllChildren(); if (!isMinimizedTagSyntax(tagNode)) { Iterator<? extends BaseToken> childrenIt = tagChildren.iterator(); while (childrenIt.hasNext()) { Object item = childrenIt.next(); if (item != null) { if (item instanceof CData) { serializeCData((CData) item, tagNode, writer); } else if (item instanceof ContentNode) { serializeContentToken((ContentNode) item, tagNode, writer); } else { ((BaseToken) item).serialize(this, writer); } } } serializeEndTag(tagNode, writer, false); } }
for (Object new_node : new_nodes) { TagNode info_node = (TagNode) new_node; whatsNew += info_node.getAllChildren().get(0).toString().trim() + "\n"; version = ver.getAllChildren().get(0).toString().trim();
private HeadlessTagNode(TagNode wrappedNode) { super(""); getAttributes().putAll(wrappedNode.getAttributes()); addChildren(wrappedNode.getAllChildren()); setDocType(wrappedNode.getDocType()); Map<String, String> nsDecls = getNamespaceDeclarations(); if (nsDecls != null) { Map<String, String> wrappedNSDecls = wrappedNode.getNamespaceDeclarations(); if (wrappedNSDecls != null) { nsDecls.putAll(wrappedNSDecls); } } } }
private HeadlessTagNode(TagNode wrappedNode) { super(""); getAttributes().putAll(wrappedNode.getAttributes()); addChildren(wrappedNode.getAllChildren()); setDocType(wrappedNode.getDocType()); Map<String, String> nsDecls = getNamespaceDeclarations(); if (nsDecls != null) { Map<String, String> wrappedNSDecls = wrappedNode.getNamespaceDeclarations(); if (wrappedNSDecls != null) { nsDecls.putAll(wrappedNSDecls); } } } }
public Document createDOM(TagNode rootNode) throws ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); DOMImplementation impl = builder.getDOMImplementation(); Document document; // // Where a DOCTYPE is supplied in the input, ensure that this is in the output DOM. See issue #27 // if (rootNode.getDocType() != null){ String qualifiedName = rootNode.getDocType().getPart1(); String publicId = rootNode.getDocType().getPublicId(); String systemId = rootNode.getDocType().getSystemId(); DocumentType documentType = impl.createDocumentType(qualifiedName, publicId, systemId); document = impl.createDocument(rootNode.getNamespaceURIOnPath(""), qualifiedName, documentType); } else { document = builder.newDocument(); Element rootElement = document.createElement(rootNode.getName()); document.appendChild(rootElement); } createSubnodes(document, (Element)document.getDocumentElement(), rootNode.getAllChildren()); return document; }
public Document createJDom(TagNode rootNode) { this.factory = new DefaultJDOMFactory(); Element rootElement = createElement(rootNode); Document document = this.factory.document(rootElement); setAttributes(rootNode, rootElement); createSubnodes(rootElement, rootNode.getAllChildren()); return document; }
private boolean isEmpty(final TagNode node) { for (BaseToken item : node.getAllChildren()) { if (item instanceof TagNode) { final TagNode childNode = (TagNode)item; if (Arrays.stream(VALID_ELEMENTS).anyMatch(e -> childNode.getName().equalsIgnoreCase(e)) || CharMatcher.invisible().negate().matchesAnyOf(childNode.getText()) || !isEmpty(childNode)) { return false; } } else if (item instanceof ContentNode) { if (CharMatcher.invisible().negate().matchesAnyOf(((ContentNode) item).getContent())) { return false; } } } return true; } }
public Document createJDom(TagNode rootNode) { this.factory = new DefaultJDOMFactory(); // // If there is no actual root node then return nothing // if (rootNode.getName() == null) return null; Element rootElement = createElement(rootNode); Document document = this.factory.document(rootElement); setAttributes(rootNode, rootElement); createSubnodes(rootElement, rootNode.getAllChildren()); return document; }