Java Code Examples for org.w3c.dom.Node#cloneNode()
The following examples show how to use
org.w3c.dom.Node#cloneNode() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NodeTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
@Test public void testImportNode() throws Exception { Document document = createDOMWithNS("Node02.xml"); Document otherDocument = createDOMWithNS("ElementSample01.xml"); NodeList otherNodeList = otherDocument.getElementsByTagName("body"); Node importedNode = otherNodeList.item(0); Node clone = importedNode.cloneNode(true); Node retNode = document.importNode(importedNode, true); assertTrue(clone.isEqualNode(importedNode)); //verify importedNode is not changed assertNotEquals(retNode, importedNode); assertTrue(importedNode.isEqualNode(retNode)); retNode = document.importNode(importedNode, false); assertTrue(clone.isEqualNode(importedNode)); //verify importedNode is not changed assertEquals(retNode.getNodeName(), importedNode.getNodeName()); assertFalse(importedNode.isEqualNode(retNode)); }
Example 2
Source File: XMLUtil.java From netbeans with Apache License 2.0 | 5 votes |
/** * Copy elements from one document to another attaching at the specified element * and translating the namespace. * * @param from copy the children of this element (exclusive) * @param to where to attach the copied elements * @param newNamespace destination namespace * * @since 8.4 */ public static void copyDocument(Element from, Element to, String newNamespace) { Document doc = to.getOwnerDocument(); NodeList nl = from.getChildNodes(); int length = nl.getLength(); for (int i = 0; i < length; i++) { Node node = nl.item(i); Node newNode = null; if (Node.ELEMENT_NODE == node.getNodeType()) { Element oldElement = (Element) node; newNode = doc.createElementNS(newNamespace, oldElement.getTagName()); NamedNodeMap m = oldElement.getAttributes(); Element newElement = (Element) newNode; for (int index = 0; index < m.getLength(); index++) { Node attr = m.item(index); newElement.setAttribute(attr.getNodeName(), attr.getNodeValue()); } copyDocument(oldElement, newElement, newNamespace); } else { newNode = node.cloneNode(true); newNode = to.getOwnerDocument().importNode(newNode, true); } if (newNode != null) { to.appendChild(newNode); } } }
Example 3
Source File: mxRootChangeCodec.java From blog-codes with Apache License 2.0 | 5 votes |
/** * Reads the cells into the graph model. All cells are children of the root * element in the node. */ public Node beforeDecode(mxCodec dec, Node node, Object into) { if (into instanceof mxRootChange) { mxRootChange change = (mxRootChange) into; if (node.getFirstChild() != null && node.getFirstChild().getNodeType() == Node.ELEMENT_NODE) { // Makes sure the original node isn't modified node = node.cloneNode(true); Node tmp = node.getFirstChild(); change.setRoot(dec.decodeCell(tmp, false)); Node tmp2 = tmp.getNextSibling(); tmp.getParentNode().removeChild(tmp); tmp = tmp2; while (tmp != null) { tmp2 = tmp.getNextSibling(); if (tmp.getNodeType() == Node.ELEMENT_NODE) { dec.decodeCell(tmp, true); } tmp.getParentNode().removeChild(tmp); tmp = tmp2; } } } return node; }
Example 4
Source File: CajaTreeBuilder.java From caja with Apache License 2.0 | 5 votes |
@Override protected Node shallowClone(Node node) { Node clone = node.cloneNode(false); if (needsDebugData) { Nodes.setFilePositionFor(clone, Nodes.getFilePositionFor(node)); } switch (node.getNodeType()) { case Node.ATTRIBUTE_NODE: if (needsDebugData) { Nodes.setFilePositionForValue( (Attr) clone, Nodes.getFilePositionForValue((Attr) node)); } break; case Node.ELEMENT_NODE: Element el = (Element) node; Element cloneEl = (Element) clone; NamedNodeMap attrs = el.getAttributes(); for (int i = 0, n = attrs.getLength(); i < n; ++i) { Attr a = (Attr) attrs.item(i); Attr cloneA = cloneEl.getAttributeNodeNS( a.getNamespaceURI(), a.getLocalName()); if (needsDebugData) { Nodes.setFilePositionFor(cloneA, Nodes.getFilePositionFor(a)); Nodes.setFilePositionForValue( cloneA, Nodes.getFilePositionForValue(a)); } } break; case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: if (needsDebugData) { Text t = (Text) node; Nodes.setRawText(t, Nodes.getRawText(t)); } break; } return clone; }
Example 5
Source File: mxCodec.java From blog-codes with Apache License 2.0 | 5 votes |
/** * Decodes the given XML node. The optional "into" argument specifies an * existing object to be used. If no object is given, then a new * instance is created using the constructor from the codec. * * The function returns the passed in object or the new instance if no * object was given. * * @param node XML node to be decoded. * @param into Optional object to be decodec into. * @return Returns an object that represents the given node. */ public Object decode(Node node, Object into) { Object obj = null; if (node != null && node.getNodeType() == Node.ELEMENT_NODE) { mxObjectCodec codec = mxCodecRegistry.getCodec(node.getNodeName()); try { if (codec != null) { obj = codec.decode(this, node, into); } else { obj = node.cloneNode(true); ((Element) obj).removeAttribute("as"); } } catch (Exception e) { log.log(Level.FINEST, "Cannot decode " + node.getNodeName(), e); } } return obj; }
Example 6
Source File: RangeImpl.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
/** * Utility method for traversing a single node when * we know a-priori that the node if fully * selected. * * @param n The node to be traversed. * * @param how Specifies what type of traversal is being * requested (extract, clone, or delete). * Legal values for this argument are: * * <ol> * <li><code>EXTRACT_CONTENTS</code> - will simply * return the original node. * * <li><code>CLONE_CONTENTS</code> - will leave the * context tree of the range undisturbed, but will * return a cloned node. * * <li><code>DELETE_CONTENTS</code> - will delete the * node from it's parent, but will return null. * </ol> * * @return Returns a node that is the result of visiting the node. * If the traversal operation is * <code>DELETE_CONTENTS</code> the return value is null. */ private Node traverseFullySelected( Node n, int how ) { switch( how ) { case CLONE_CONTENTS: return n.cloneNode( true ); case EXTRACT_CONTENTS: if ( n.getNodeType()==Node.DOCUMENT_TYPE_NODE ) { // TBD: This should be a HIERARCHY_REQUEST_ERR throw new DOMException( DOMException.HIERARCHY_REQUEST_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null)); } return n; case DELETE_CONTENTS: n.getParentNode().removeChild(n); return null; } return null; }
Example 7
Source File: ProcessorUtil.java From cxf with Apache License 2.0 | 4 votes |
public static Node cloneNode(Document document, Node node, boolean deep) throws DOMException { if (document == null || node == null) { return null; } int type = node.getNodeType(); if (node.getOwnerDocument() == document) { return node.cloneNode(deep); } Node clone; switch (type) { case Node.CDATA_SECTION_NODE: clone = document.createCDATASection(node.getNodeValue()); break; case Node.COMMENT_NODE: clone = document.createComment(node.getNodeValue()); break; case Node.ENTITY_REFERENCE_NODE: clone = document.createEntityReference(node.getNodeName()); break; case Node.ELEMENT_NODE: clone = document.createElementNS(node.getNamespaceURI(), node.getNodeName()); NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Attr attr = (Attr)attributes.item(i); Attr attrnew = ((Element)clone).getOwnerDocument().createAttributeNS(attr.getNamespaceURI(), attr.getNodeName()); attrnew.setValue(attr.getNodeValue()); ((Element)clone).setAttributeNodeNS(attrnew); } break; case Node.TEXT_NODE: clone = document.createTextNode(node.getNodeValue()); break; default: return null; } if (deep && type == Node.ELEMENT_NODE) { Node child = node.getFirstChild(); while (child != null) { clone.appendChild(cloneNode(document, child, true)); child = child.getNextSibling(); } } return clone; }
Example 8
Source File: RangeImpl.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
/** * Utility method for traversing a single node when * we know a-priori that the node if fully * selected. * * @param n The node to be traversed. * * @param how Specifies what type of traversal is being * requested (extract, clone, or delete). * Legal values for this argument are: * * <ol> * <li><code>EXTRACT_CONTENTS</code> - will simply * return the original node. * * <li><code>CLONE_CONTENTS</code> - will leave the * context tree of the range undisturbed, but will * return a cloned node. * * <li><code>DELETE_CONTENTS</code> - will delete the * node from it's parent, but will return null. * </ol> * * @return Returns a node that is the result of visiting the node. * If the traversal operation is * <code>DELETE_CONTENTS</code> the return value is null. */ private Node traverseFullySelected( Node n, int how ) { switch( how ) { case CLONE_CONTENTS: return n.cloneNode( true ); case EXTRACT_CONTENTS: if ( n.getNodeType()==Node.DOCUMENT_TYPE_NODE ) { // TBD: This should be a HIERARCHY_REQUEST_ERR throw new DOMException( DOMException.HIERARCHY_REQUEST_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null)); } return n; case DELETE_CONTENTS: n.getParentNode().removeChild(n); return null; } return null; }
Example 9
Source File: JAXBDataBinding.java From cxf with Apache License 2.0 | 4 votes |
public Node cloneNode(Document document, Node node, boolean deep) throws DOMException { if (document == null || node == null) { return null; } int type = node.getNodeType(); if (node.getOwnerDocument() == document) { return node.cloneNode(deep); } Node clone; switch (type) { case Node.CDATA_SECTION_NODE: clone = document.createCDATASection(node.getNodeValue()); break; case Node.COMMENT_NODE: clone = document.createComment(node.getNodeValue()); break; case Node.ENTITY_REFERENCE_NODE: clone = document.createEntityReference(node.getNodeName()); break; case Node.ELEMENT_NODE: clone = document.createElement(node.getNodeName()); NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { ((Element)clone).setAttributeNS(attributes.item(i).getNamespaceURI(), attributes.item(i).getNodeName(), attributes.item(i).getNodeValue()); } try { clone.setUserData("location", node.getUserData("location"), null); } catch (Throwable t) { //non DOM level 3 } break; case Node.TEXT_NODE: clone = document.createTextNode(node.getNodeValue()); break; default: return null; } if (deep && type == Node.ELEMENT_NODE) { Node child = node.getFirstChild(); while (child != null) { clone.appendChild(cloneNode(document, child, true)); child = child.getNextSibling(); } } return clone; }
Example 10
Source File: RangeImpl.java From JDKSourceCode1.8 with MIT License | 4 votes |
/** * Utility method for traversing a single node when * we know a-priori that the node if fully * selected. * * @param n The node to be traversed. * * @param how Specifies what type of traversal is being * requested (extract, clone, or delete). * Legal values for this argument are: * * <ol> * <li><code>EXTRACT_CONTENTS</code> - will simply * return the original node. * * <li><code>CLONE_CONTENTS</code> - will leave the * context tree of the range undisturbed, but will * return a cloned node. * * <li><code>DELETE_CONTENTS</code> - will delete the * node from it's parent, but will return null. * </ol> * * @return Returns a node that is the result of visiting the node. * If the traversal operation is * <code>DELETE_CONTENTS</code> the return value is null. */ private Node traverseFullySelected( Node n, int how ) { switch( how ) { case CLONE_CONTENTS: return n.cloneNode( true ); case EXTRACT_CONTENTS: if ( n.getNodeType()==Node.DOCUMENT_TYPE_NODE ) { // TBD: This should be a HIERARCHY_REQUEST_ERR throw new DOMException( DOMException.HIERARCHY_REQUEST_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null)); } return n; case DELETE_CONTENTS: n.getParentNode().removeChild(n); return null; } return null; }
Example 11
Source File: AttributeMap.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
private final Node remove(AttrImpl attr, int index, boolean addDefault) { CoreDocumentImpl ownerDocument = ownerNode.ownerDocument(); String name = attr.getNodeName(); if (attr.isIdAttribute()) { ownerDocument.removeIdentifier(attr.getValue()); } if (hasDefaults() && addDefault) { // If there's a default, add it instead NamedNodeMapImpl defaults = ((ElementImpl) ownerNode).getDefaultAttributes(); Node d; if (defaults != null && (d = defaults.getNamedItem(name)) != null && findNamePoint(name, index+1) < 0) { NodeImpl clone = (NodeImpl)d.cloneNode(true); if (d.getLocalName() !=null){ // we must rely on the name to find a default attribute // ("test:attr"), but while copying it from the DOCTYPE // we should not loose namespace URI that was assigned // to the attribute in the instance document. ((AttrNSImpl)clone).namespaceURI = attr.getNamespaceURI(); } clone.ownerNode = ownerNode; clone.isOwned(true); clone.isSpecified(false); nodes.set(index, clone); if (attr.isIdAttribute()) { ownerDocument.putIdentifier(clone.getNodeValue(), (ElementImpl)ownerNode); } } else { nodes.remove(index); } } else { nodes.remove(index); } // changed(true); // remove reference to owner attr.ownerNode = ownerDocument; attr.isOwned(false); // make sure it won't be mistaken with defaults in case it's // reused attr.isSpecified(true); attr.isIdAttribute(false); // notify document ownerDocument.removedAttrNode(attr, ownerNode, name); return attr; }
Example 12
Source File: XmlUtility.java From terracotta-platform with Apache License 2.0 | 4 votes |
public static Node getClonedParentDocFromRootNode(Node rootNode) { Node parentNode = rootNode.getParentNode(); return parentNode.cloneNode(true); }
Example 13
Source File: AttributeMap.java From Bytecoder with Apache License 2.0 | 4 votes |
private final Node remove(AttrImpl attr, int index, boolean addDefault) { CoreDocumentImpl ownerDocument = ownerNode.ownerDocument(); String name = attr.getNodeName(); if (attr.isIdAttribute()) { ownerDocument.removeIdentifier(attr.getValue()); } if (hasDefaults() && addDefault) { // If there's a default, add it instead NamedNodeMapImpl defaults = ((ElementImpl) ownerNode).getDefaultAttributes(); Node d; if (defaults != null && (d = defaults.getNamedItem(name)) != null && findNamePoint(name, index+1) < 0) { NodeImpl clone = (NodeImpl)d.cloneNode(true); if (d.getLocalName() !=null){ // we must rely on the name to find a default attribute // ("test:attr"), but while copying it from the DOCTYPE // we should not loose namespace URI that was assigned // to the attribute in the instance document. ((AttrNSImpl)clone).namespaceURI = attr.getNamespaceURI(); } clone.ownerNode = ownerNode; clone.isOwned(true); clone.isSpecified(false); nodes.set(index, clone); if (attr.isIdAttribute()) { ownerDocument.putIdentifier(clone.getNodeValue(), (ElementImpl)ownerNode); } } else { nodes.remove(index); } } else { nodes.remove(index); } // changed(true); // remove reference to owner attr.ownerNode = ownerDocument; attr.isOwned(false); // make sure it won't be mistaken with defaults in case it's // reused attr.isSpecified(true); attr.isIdAttribute(false); // notify document ownerDocument.removedAttrNode(attr, ownerNode, name); return attr; }
Example 14
Source File: AttributeMap.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
private final Node remove(AttrImpl attr, int index, boolean addDefault) { CoreDocumentImpl ownerDocument = ownerNode.ownerDocument(); String name = attr.getNodeName(); if (attr.isIdAttribute()) { ownerDocument.removeIdentifier(attr.getValue()); } if (hasDefaults() && addDefault) { // If there's a default, add it instead NamedNodeMapImpl defaults = ((ElementImpl) ownerNode).getDefaultAttributes(); Node d; if (defaults != null && (d = defaults.getNamedItem(name)) != null && findNamePoint(name, index+1) < 0) { NodeImpl clone = (NodeImpl)d.cloneNode(true); if (d.getLocalName() !=null){ // we must rely on the name to find a default attribute // ("test:attr"), but while copying it from the DOCTYPE // we should not loose namespace URI that was assigned // to the attribute in the instance document. ((AttrNSImpl)clone).namespaceURI = attr.getNamespaceURI(); } clone.ownerNode = ownerNode; clone.isOwned(true); clone.isSpecified(false); nodes.set(index, clone); if (attr.isIdAttribute()) { ownerDocument.putIdentifier(clone.getNodeValue(), (ElementImpl)ownerNode); } } else { nodes.remove(index); } } else { nodes.remove(index); } // changed(true); // remove reference to owner attr.ownerNode = ownerDocument; attr.isOwned(false); // make sure it won't be mistaken with defaults in case it's // reused attr.isSpecified(true); attr.isIdAttribute(false); // notify document ownerDocument.removedAttrNode(attr, ownerNode, name); return attr; }
Example 15
Source File: AttributeMap.java From jdk1.8-source-analysis with Apache License 2.0 | 4 votes |
private final Node remove(AttrImpl attr, int index, boolean addDefault) { CoreDocumentImpl ownerDocument = ownerNode.ownerDocument(); String name = attr.getNodeName(); if (attr.isIdAttribute()) { ownerDocument.removeIdentifier(attr.getValue()); } if (hasDefaults() && addDefault) { // If there's a default, add it instead NamedNodeMapImpl defaults = ((ElementImpl) ownerNode).getDefaultAttributes(); Node d; if (defaults != null && (d = defaults.getNamedItem(name)) != null && findNamePoint(name, index+1) < 0) { NodeImpl clone = (NodeImpl)d.cloneNode(true); if (d.getLocalName() !=null){ // we must rely on the name to find a default attribute // ("test:attr"), but while copying it from the DOCTYPE // we should not loose namespace URI that was assigned // to the attribute in the instance document. ((AttrNSImpl)clone).namespaceURI = attr.getNamespaceURI(); } clone.ownerNode = ownerNode; clone.isOwned(true); clone.isSpecified(false); nodes.set(index, clone); if (attr.isIdAttribute()) { ownerDocument.putIdentifier(clone.getNodeValue(), (ElementImpl)ownerNode); } } else { nodes.remove(index); } } else { nodes.remove(index); } // changed(true); // remove reference to owner attr.ownerNode = ownerDocument; attr.isOwned(false); // make sure it won't be mistaken with defaults in case it's // reused attr.isSpecified(true); attr.isIdAttribute(false); // notify document ownerDocument.removedAttrNode(attr, ownerNode, name); return attr; }
Example 16
Source File: mxChildChangeCodec.java From blog-codes with Apache License 2.0 | 4 votes |
/** * Reads the cells into the graph model. All cells are children of the root * element in the node. */ public Node beforeDecode(mxCodec dec, Node node, Object into) { if (into instanceof mxChildChange) { mxChildChange change = (mxChildChange) into; if (node.getFirstChild() != null && node.getFirstChild().getNodeType() == Node.ELEMENT_NODE) { // Makes sure the original node isn't modified node = node.cloneNode(true); Node tmp = node.getFirstChild(); change.setChild(dec.decodeCell(tmp, false)); Node tmp2 = tmp.getNextSibling(); tmp.getParentNode().removeChild(tmp); tmp = tmp2; while (tmp != null) { tmp2 = tmp.getNextSibling(); if (tmp.getNodeType() == Node.ELEMENT_NODE) { // Ignores all existing cells because those do not need // to be re-inserted into the model. Since the encoded // version of these cells contains the new parent, this // would leave to an inconsistent state on the model // (ie. a parent change without a call to // parentForCellChanged). String id = ((Element) tmp).getAttribute("id"); if (dec.lookup(id) == null) { dec.decodeCell(tmp, true); } } tmp.getParentNode().removeChild(tmp); tmp = tmp2; } } else { String childRef = ((Element) node).getAttribute("child"); change.setChild((mxICell) dec.getObject(childRef)); } } return node; }
Example 17
Source File: RangeImpl.java From Bytecoder with Apache License 2.0 | 3 votes |
/** * Utility method for traversing a single node when * we know a-priori that the node if partially * selected and is not a text node. * * @param n The node to be traversed. * * @param how Specifies what type of traversal is being * requested (extract, clone, or delete). * Legal values for this argument are: * * <ol> * <li><code>EXTRACT_CONTENTS</code> - will simply * return the original node. * * <li><code>CLONE_CONTENTS</code> - will leave the * context tree of the range undisturbed, but will * return a cloned node. * * <li><code>DELETE_CONTENTS</code> - will delete the * node from it's parent, but will return null. * </ol> * * @return Returns a node that is the result of visiting the node. * If the traversal operation is * <code>DELETE_CONTENTS</code> the return value is null. */ private Node traversePartiallySelected( Node n, int how ) { switch( how ) { case DELETE_CONTENTS: return null; case CLONE_CONTENTS: case EXTRACT_CONTENTS: return n.cloneNode( false ); } return null; }
Example 18
Source File: RangeImpl.java From openjdk-8 with GNU General Public License v2.0 | 3 votes |
/** * Utility method for traversing a single node when * we know a-priori that the node if partially * selected and is not a text node. * * @param n The node to be traversed. * * @param how Specifies what type of traversal is being * requested (extract, clone, or delete). * Legal values for this argument are: * * <ol> * <li><code>EXTRACT_CONTENTS</code> - will simply * return the original node. * * <li><code>CLONE_CONTENTS</code> - will leave the * context tree of the range undisturbed, but will * return a cloned node. * * <li><code>DELETE_CONTENTS</code> - will delete the * node from it's parent, but will return null. * </ol> * * @return Returns a node that is the result of visiting the node. * If the traversal operation is * <code>DELETE_CONTENTS</code> the return value is null. */ private Node traversePartiallySelected( Node n, int how ) { switch( how ) { case DELETE_CONTENTS: return null; case CLONE_CONTENTS: case EXTRACT_CONTENTS: return n.cloneNode( false ); } return null; }
Example 19
Source File: RangeImpl.java From jdk1.8-source-analysis with Apache License 2.0 | 3 votes |
/** * Utility method for traversing a single node when * we know a-priori that the node if partially * selected and is not a text node. * * @param n The node to be traversed. * * @param how Specifies what type of traversal is being * requested (extract, clone, or delete). * Legal values for this argument are: * * <ol> * <li><code>EXTRACT_CONTENTS</code> - will simply * return the original node. * * <li><code>CLONE_CONTENTS</code> - will leave the * context tree of the range undisturbed, but will * return a cloned node. * * <li><code>DELETE_CONTENTS</code> - will delete the * node from it's parent, but will return null. * </ol> * * @return Returns a node that is the result of visiting the node. * If the traversal operation is * <code>DELETE_CONTENTS</code> the return value is null. */ private Node traversePartiallySelected( Node n, int how ) { switch( how ) { case DELETE_CONTENTS: return null; case CLONE_CONTENTS: case EXTRACT_CONTENTS: return n.cloneNode( false ); } return null; }
Example 20
Source File: RangeImpl.java From openjdk-jdk8u with GNU General Public License v2.0 | 3 votes |
/** * Utility method for traversing a single node when * we know a-priori that the node if partially * selected and is not a text node. * * @param n The node to be traversed. * * @param how Specifies what type of traversal is being * requested (extract, clone, or delete). * Legal values for this argument are: * * <ol> * <li><code>EXTRACT_CONTENTS</code> - will simply * return the original node. * * <li><code>CLONE_CONTENTS</code> - will leave the * context tree of the range undisturbed, but will * return a cloned node. * * <li><code>DELETE_CONTENTS</code> - will delete the * node from it's parent, but will return null. * </ol> * * @return Returns a node that is the result of visiting the node. * If the traversal operation is * <code>DELETE_CONTENTS</code> the return value is null. */ private Node traversePartiallySelected( Node n, int how ) { switch( how ) { case DELETE_CONTENTS: return null; case CLONE_CONTENTS: case EXTRACT_CONTENTS: return n.cloneNode( false ); } return null; }