public static Node cloneNode ( Document document , Node node ) { Node nodeClone = null ; switch ( node . getNodeType ( ) ) { case Node . ELEMENT_NODE : { nodeClone = document . createElement ( ( ( Element ) node ) . getTagName ( ) ) ; NamedNodeMap namedNodeMap = node . getAttributes ( ) ; for ( int i = 0 ; i < namedNodeMap . getLength ( ) ; ++ i ) { Attr attr = ( Attr ) namedNodeMap . item ( i ) ; Attr attrClone = document . createAttribute ( attr . getName ( ) ) ; attrClone . setValue ( attr . getValue ( ) ) ; ( ( Element ) nodeClone ) . setAttributeNode ( attrClone ) ; } } break ; case Node . TEXT_NODE : nodeClone = document . createTextNode ( ( ( CharacterData ) node ) . getData ( ) ) ; break ; case Node . CDATA_SECTION_NODE : nodeClone = document . createCDATASection ( ( ( CharacterData ) node ) . getData ( ) ) ; break ; case Node . ENTITY_REFERENCE_NODE : nodeClone = document . createEntityReference ( node . getNodeName ( ) ) ; break ; case Node . PROCESSING_INSTRUCTION_NODE : nodeClone = document . createProcessingInstruction ( ( ( ProcessingInstruction ) node ) . getTarget ( ) , ( ( ProcessingInstruction ) node ) . getData ( ) ) ; break ; case Node . COMMENT_NODE : nodeClone = document . createComment ( ( ( CharacterData ) node ) . getData ( ) ) ; break ; case Node . DOCUMENT_FRAGMENT_NODE : nodeClone = document . createDocumentFragment ( ) ; break ; case Node . DOCUMENT_NODE : case Node . DOCUMENT_TYPE_NODE : case Node . NOTATION_NODE : case Node . ATTRIBUTE_NODE : case Node . ENTITY_NODE : Assert . isTrue ( false , Policy . bind ( "assert.notSupported" ) ) ; //$NON-NLS-1$ break ; default : Assert . isTrue ( false , Policy . bind ( "assert.unknownNodeType" ) ) ; //$NON-NLS-1$ } return nodeClone ; }
Returns a clone of the given node . The given document becomes the owner document of the clone .