You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by mi...@apache.org on 2002/09/03 22:26:59 UTC
cvs commit: jakarta-avalon-excalibur/xmlutil/src/java/org/apache/excalibur/xml/dom DOMSerializer.java DefaultDOMSerializer.java
mirceatoma 2002/09/03 13:26:59
Added: xmlutil/src/java/org/apache/excalibur/xml/dom
DOMSerializer.java DefaultDOMSerializer.java
Log:
Add component that transform a DOM document to a stream of SAX events.
Revision Changes Path
1.1 jakarta-avalon-excalibur/xmlutil/src/java/org/apache/excalibur/xml/dom/DOMSerializer.java
Index: DOMSerializer.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.excalibur.xml.dom;
import org.w3c.dom.Document;
import org.xml.sax.ContentHandler;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.SAXException;
/**
* Converts a DOM document to a stream of SAX events.
*
* @author <a href="mailto:mirceatoma@apache.org">Mircea Toma</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/09/03 20:26:59 $
*/
public interface DOMSerializer
{
String ROLE = DOMSerializer.class.getName();
void serialize( Document document, ContentHandler contentHandler, LexicalHandler lexicalHandler ) throws SAXException;
}
1.1 jakarta-avalon-excalibur/xmlutil/src/java/org/apache/excalibur/xml/dom/DefaultDOMSerializer.java
Index: DefaultDOMSerializer.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.excalibur.xml.dom;
import org.w3c.dom.Document;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.ext.LexicalHandler;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.component.Component;
public class DefaultDOMSerializer extends AbstractLogEnabled implements DOMSerializer, Component
{
private final TransformerFactory m_factory = TransformerFactory.newInstance();
public void serialize( Document document, ContentHandler contentHandler, LexicalHandler lexicalHandler ) throws SAXException
{
try
{
final Transformer transformer = m_factory.newTransformer();
final DOMSource source = new DOMSource(document);
final SAXResult result = new SAXResult(contentHandler);
result.setLexicalHandler(lexicalHandler);
transformer.transform(source, result);
}
catch (TransformerConfigurationException e)
{
getLogger().error("Cannot create transformer", e);
throw new SAXException(e);
}
catch (TransformerException e)
{
getLogger().error("Cannot serialize document", e);
throw new SAXException(e);
}
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>