KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > xml > dom > DefaultDOMSerializer


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.excalibur.xml.dom;
18
19 import javax.xml.transform.Transformer JavaDoc;
20 import javax.xml.transform.TransformerConfigurationException JavaDoc;
21 import javax.xml.transform.TransformerException JavaDoc;
22 import javax.xml.transform.TransformerFactory JavaDoc;
23 import javax.xml.transform.dom.DOMSource JavaDoc;
24 import javax.xml.transform.sax.SAXResult JavaDoc;
25
26 import org.apache.avalon.framework.component.Component;
27 import org.apache.avalon.framework.logger.AbstractLogEnabled;
28 import org.w3c.dom.Document JavaDoc;
29 import org.xml.sax.ContentHandler JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31 import org.xml.sax.ext.LexicalHandler JavaDoc;
32
33 /**
34  * @avalon.component
35  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
36  */

37 public class DefaultDOMSerializer
38     extends AbstractLogEnabled
39     implements DOMSerializer, Component
40 {
41     private final TransformerFactory JavaDoc m_factory = TransformerFactory.newInstance();
42
43     public void serialize( Document JavaDoc document,
44                            ContentHandler JavaDoc contentHandler,
45                            LexicalHandler JavaDoc lexicalHandler )
46         throws SAXException JavaDoc
47     {
48         try
49         {
50             final Transformer JavaDoc transformer = m_factory.newTransformer();
51             final DOMSource JavaDoc source = new DOMSource JavaDoc( document );
52             final SAXResult JavaDoc result = new SAXResult JavaDoc( contentHandler );
53             result.setLexicalHandler( lexicalHandler );
54
55             transformer.transform( source, result );
56         }
57         catch( TransformerConfigurationException JavaDoc e )
58         {
59             getLogger().error( "Cannot create transformer", e );
60             throw new SAXException JavaDoc( e );
61         }
62         catch( TransformerException JavaDoc e )
63         {
64             getLogger().error( "Cannot serialize document", e );
65             throw new SAXException JavaDoc( e );
66         }
67     }
68 }
69
Popular Tags