1 17 package org.apache.excalibur.xml.dom; 18 19 import javax.xml.transform.Transformer ; 20 import javax.xml.transform.TransformerConfigurationException ; 21 import javax.xml.transform.TransformerException ; 22 import javax.xml.transform.TransformerFactory ; 23 import javax.xml.transform.dom.DOMSource ; 24 import javax.xml.transform.sax.SAXResult ; 25 26 import org.apache.avalon.framework.component.Component; 27 import org.apache.avalon.framework.logger.AbstractLogEnabled; 28 import org.w3c.dom.Document ; 29 import org.xml.sax.ContentHandler ; 30 import org.xml.sax.SAXException ; 31 import org.xml.sax.ext.LexicalHandler ; 32 33 37 public class DefaultDOMSerializer 38 extends AbstractLogEnabled 39 implements DOMSerializer, Component 40 { 41 private final TransformerFactory m_factory = TransformerFactory.newInstance(); 42 43 public void serialize( Document document, 44 ContentHandler contentHandler, 45 LexicalHandler lexicalHandler ) 46 throws SAXException 47 { 48 try 49 { 50 final Transformer transformer = m_factory.newTransformer(); 51 final DOMSource source = new DOMSource ( document ); 52 final SAXResult result = new SAXResult ( contentHandler ); 53 result.setLexicalHandler( lexicalHandler ); 54 55 transformer.transform( source, result ); 56 } 57 catch( TransformerConfigurationException e ) 58 { 59 getLogger().error( "Cannot create transformer", e ); 60 throw new SAXException ( e ); 61 } 62 catch( TransformerException e ) 63 { 64 getLogger().error( "Cannot serialize document", e ); 65 throw new SAXException ( e ); 66 } 67 } 68 } 69 | Popular Tags |