public void copy ( boolean isFragmentMode ) throws XMLStreamException { int ev = isFragmentMode ? XMLStreamConstants . START_ELEMENT : XMLStreamConstants . START_DOCUMENT ; reader . require ( ev , null , null ) ; int depth = 0 ; ev = reader . getEventType ( ) ; while ( true ) { switch ( ev ) { case XMLStreamConstants . START_ELEMENT : { writer . writeStartElement ( nonNull ( reader . getPrefix ( ) ) , // fixup bug where woodstox-3.2.7 returns null reader . getLocalName ( ) , nonNull ( reader . getNamespaceURI ( ) ) ) ; // Saxon requires nonNull copyAttributes ( ) ; copyNamespaces ( ) ; depth ++ ; break ; } case XMLStreamConstants . END_ELEMENT : { writer . writeEndElement ( ) ; depth -- ; if ( isFragmentMode && depth == 0 ) { writer . flush ( ) ; return ; // we're done } break ; } case XMLStreamConstants . ATTRIBUTE : { // can happen as part of an XPath result sequence, or similar copyAttribute ( 0 ) ; break ; } case XMLStreamConstants . START_DOCUMENT : { copyStartDocument ( ) ; break ; } case XMLStreamConstants . END_DOCUMENT : { writer . writeEndDocument ( ) ; writer . flush ( ) ; return ; // we're done } case XMLStreamConstants . PROCESSING_INSTRUCTION : { writer . writeProcessingInstruction ( reader . getPITarget ( ) , reader . getPIData ( ) ) ; break ; } case XMLStreamConstants . COMMENT : { writer . writeComment ( reader . getText ( ) ) ; break ; } case XMLStreamConstants . CDATA : { writer . writeCData ( reader . getText ( ) ) ; break ; } case XMLStreamConstants . SPACE : case XMLStreamConstants . CHARACTERS : { copyText ( ) ; break ; } case XMLStreamConstants . ENTITY_REFERENCE : { // writer.writeEntityRef(reader.getLocalName()); // don't expand the ref copyText ( ) ; // expand the ref (safer) break ; } case XMLStreamConstants . DTD : { copyDTD ( ) ; break ; } case XMLStreamConstants . ENTITY_DECLARATION : break ; // ignore (handled by XMLStreamConstants.DTD) case XMLStreamConstants . NOTATION_DECLARATION : break ; // ignore (handled by XMLStreamConstants.DTD) case XMLStreamConstants . NAMESPACE : { // can happen as part of an XPath result sequence, or similar writer . writeNamespace ( reader . getPrefix ( ) , reader . getNamespaceURI ( ) ) ; break ; } default : { throw new XMLStreamException ( "Unrecognized event type: " + reader . getEventType ( ) ) ; } } ev = reader . next ( ) ; } }
Reads all events from the reader and pipes them into the writer .