public Node insertBefore(Node newChild, Node refChild) throws DOMException { LeafNodeImpl refChildImpl = (LeafNodeImpl) refChild; if (refChildImpl == null) { return appendChild(newChild); } if (refChildImpl.document != document) { throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, null); } if (refChildImpl.parent != this) { throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, null); } return insertChildAt(newChild, refChildImpl.index); }
public String getTextContent() throws DOMException { Node child = getFirstChild(); if (child == null) { return ""; } Node next = child.getNextSibling(); if (next == null) { return hasTextContent(child) ? child.getTextContent() : ""; } StringBuilder buf = new StringBuilder(); getTextContent(buf); return buf.toString(); }
/** * Replaces this node with a semantically equivalent text node. This node * will be removed from the DOM tree and the new node inserted in its place. * * @return the replacement node. */ public TextImpl replaceWithText() { TextImpl replacement = new TextImpl(document, getData()); parent.insertBefore(replacement, this); parent.removeChild(this); return replacement; } }
/** * Removes {@code oldChild} and adds {@code newChild} in its place. This * is not atomic. */ public Node replaceChild(Node newChild, Node oldChild) throws DOMException { int index = ((LeafNodeImpl) oldChild).index; removeChild(oldChild); insertChildAt(newChild, index); return oldChild; }
void getTextContent(StringBuilder buf) throws DOMException { Node child = getFirstChild(); while (child != null) { if (hasTextContent(child)) { ((NodeImpl) child).getTextContent(buf); } child = child.getNextSibling(); } }
/** * Inserts {@code newChild} at {@code index}. If it is already child of * another node, it is removed from there. */ Node insertChildAt(Node newChild, int index) throws DOMException { if (newChild instanceof DocumentFragment) { NodeList toAdd = newChild.getChildNodes(); for (int i = 0; i < toAdd.getLength(); i++) { insertChildAt(toAdd.item(i), index + i); } return newChild; } LeafNodeImpl toInsert = (LeafNodeImpl) newChild; if (toInsert.document != null && document != null && toInsert.document != document) { throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, null); } if (toInsert.isParentOf(this)) { throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, null); } if (toInsert.parent != null) { int oldIndex = toInsert.index; toInsert.parent.children.remove(oldIndex); toInsert.parent.refreshIndices(oldIndex); } children.add(index, toInsert); toInsert.parent = this; refreshIndices(index); return newChild; }
void getElementsByTagNameNS(NodeListImpl out, String namespaceURI, String localName) { for (NodeImpl node : children) { if (node.getNodeType() == Node.ELEMENT_NODE) { ElementImpl element = (ElementImpl) node; if (matchesNameOrWildcard(namespaceURI, element.getNamespaceURI()) && matchesNameOrWildcard(localName, element.getLocalName())) { out.add(element); } element.getElementsByTagNameNS(out, namespaceURI, localName); } } }
public Node appendChild(Node newChild) throws DOMException { return insertChildAt(newChild, children.size()); }
public Node removeChild(Node oldChild) throws DOMException { LeafNodeImpl oldChildImpl = (LeafNodeImpl) oldChild; if (oldChildImpl.document != document) { throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, null); } if (oldChildImpl.parent != this) { throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, null); } int index = oldChildImpl.index; children.remove(index); oldChildImpl.parent = null; refreshIndices(index); return oldChild; }
/** * Normalize the text nodes within this subtree. Although named similarly, * this method is unrelated to Document.normalize. */ @Override public final void normalize() { Node next; for (Node node = getFirstChild(); node != null; node = next) { next = node.getNextSibling(); node.normalize(); if (node.getNodeType() == Node.TEXT_NODE) { ((TextImpl) node).minimize(); } } }
/** * Tries to remove this node using itself and the previous node as context. * If this node's text is empty, this node is removed and null is returned. * If the previous node exists and is a text node, this node's text will be * appended to that node's text and this node will be removed. * * <p>Although this method alters the structure of the DOM tree, it does * not alter the document's semantics. * * @return the node holding this node's text and the end of the operation. * Can be null if this node contained the empty string. */ public final TextImpl minimize() { if (getLength() == 0) { parent.removeChild(this); return null; } Node previous = getPreviousSibling(); if (previous == null || previous.getNodeType() != Node.TEXT_NODE) { return this; } TextImpl previousText = (TextImpl) previous; previousText.buffer.append(buffer); parent.removeChild(this); return previousText; } }
/** * Removes {@code oldChild} and adds {@code newChild} in its place. This * is not atomic. */ public Node replaceChild(Node newChild, Node oldChild) throws DOMException { int index = ((LeafNodeImpl) oldChild).index; removeChild(oldChild); insertChildAt(newChild, index); return oldChild; }
void getTextContent(StringBuilder buf) throws DOMException { Node child = getFirstChild(); while (child != null) { if (hasTextContent(child)) { ((NodeImpl) child).getTextContent(buf); } child = child.getNextSibling(); } }
/** * Inserts {@code newChild} at {@code index}. If it is already child of * another node, it is removed from there. */ Node insertChildAt(Node newChild, int index) throws DOMException { if (newChild instanceof DocumentFragment) { NodeList toAdd = newChild.getChildNodes(); for (int i = 0; i < toAdd.getLength(); i++) { insertChildAt(toAdd.item(i), index + i); } return newChild; } LeafNodeImpl toInsert = (LeafNodeImpl) newChild; if (toInsert.document != null && document != null && toInsert.document != document) { throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, null); } if (toInsert.isParentOf(this)) { throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, null); } if (toInsert.parent != null) { int oldIndex = toInsert.index; toInsert.parent.children.remove(oldIndex); toInsert.parent.refreshIndices(oldIndex); } children.add(index, toInsert); toInsert.parent = this; refreshIndices(index); return newChild; }
void getElementsByTagName(NodeListImpl out, String name) { for (NodeImpl node : children) { if (node.getNodeType() == Node.ELEMENT_NODE) { ElementImpl element = (ElementImpl) node; if (matchesNameOrWildcard(name, element.getNodeName())) { out.add(element); } element.getElementsByTagName(out, name); } } }
/** * Document elements may have at most one root element and at most one DTD * element. */ @Override public Node insertChildAt(Node toInsert, int index) { if (toInsert instanceof Element && getDocumentElement() != null) { throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "Only one root element allowed"); } if (toInsert instanceof DocumentType && getDoctype() != null) { throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "Only one DOCTYPE element allowed"); } return super.insertChildAt(toInsert, index); }
public Node removeChild(Node oldChild) throws DOMException { LeafNodeImpl oldChildImpl = (LeafNodeImpl) oldChild; if (oldChildImpl.document != document) { throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, null); } if (oldChildImpl.parent != this) { throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, null); } int index = oldChildImpl.index; children.remove(index); oldChildImpl.parent = null; refreshIndices(index); return oldChild; }
/** * Normalize the text nodes within this subtree. Although named similarly, * this method is unrelated to Document.normalize. */ @Override public final void normalize() { Node next; for (Node node = getFirstChild(); node != null; node = next) { next = node.getNextSibling(); node.normalize(); if (node.getNodeType() == Node.TEXT_NODE) { ((TextImpl) node).minimize(); } } }
/** * Tries to remove this node using itself and the previous node as context. * If this node's text is empty, this node is removed and null is returned. * If the previous node exists and is a text node, this node's text will be * appended to that node's text and this node will be removed. * * <p>Although this method alters the structure of the DOM tree, it does * not alter the document's semantics. * * @return the node holding this node's text and the end of the operation. * Can be null if this node contained the empty string. */ public final TextImpl minimize() { if (getLength() == 0) { parent.removeChild(this); return null; } Node previous = getPreviousSibling(); if (previous == null || previous.getNodeType() != Node.TEXT_NODE) { return this; } TextImpl previousText = (TextImpl) previous; previousText.buffer.append(buffer); parent.removeChild(this); return previousText; } }
public String getTextContent() throws DOMException { Node child = getFirstChild(); if (child == null) { return ""; } Node next = child.getNextSibling(); if (next == null) { return hasTextContent(child) ? child.getTextContent() : ""; } StringBuilder buf = new StringBuilder(); getTextContent(buf); return buf.toString(); }