public static Element sourceToElement ( Source source ) throws IOException { Element retElement = null ; if ( source instanceof StreamSource ) { StreamSource streamSource = ( StreamSource ) source ; InputStream ins = streamSource . getInputStream ( ) ; if ( ins != null ) { retElement = DOMUtils . parse ( ins ) ; } Reader reader = streamSource . getReader ( ) ; if ( reader != null ) { retElement = DOMUtils . parse ( new InputSource ( reader ) ) ; } } else if ( source instanceof DOMSource ) { DOMSource domSource = ( DOMSource ) source ; Node node = domSource . getNode ( ) ; if ( node instanceof Element ) { retElement = ( Element ) node ; } else if ( node instanceof Document ) { retElement = ( ( Document ) node ) . getDocumentElement ( ) ; } } else if ( source instanceof SAXSource ) { // The fact that JAXBSource derives from SAXSource is an implementation detail. // Thus in general applications are strongly discouraged from accessing methods defined on SAXSource. // The XMLReader object obtained by the getXMLReader method shall be used only for parsing the InputSource object returned by the getInputSource method. final boolean hasInputSource = ( ( SAXSource ) source ) . getInputSource ( ) != null ; final boolean hasXMLReader = ( ( SAXSource ) source ) . getXMLReader ( ) != null ; if ( hasInputSource || hasXMLReader ) { try { TransformerFactory tf = TransformerFactory . newInstance ( ) ; ByteArrayOutputStream baos = new ByteArrayOutputStream ( 1024 ) ; Transformer transformer = tf . newTransformer ( ) ; transformer . setOutputProperty ( OutputKeys . OMIT_XML_DECLARATION , "yes" ) ; transformer . setOutputProperty ( OutputKeys . METHOD , "xml" ) ; transformer . transform ( source , new StreamResult ( baos ) ) ; retElement = DOMUtils . parse ( new ByteArrayInputStream ( baos . toByteArray ( ) ) ) ; } catch ( TransformerException ex ) { throw new IOException ( ex ) ; } } } else { throw MESSAGES . sourceTypeNotImplemented ( source . getClass ( ) ) ; } return retElement ; }
Parse the contents of the provided source into an element . This uses the document builder associated with the current thread .