KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > xml > AbstractDOMFragment


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

16 package org.apache.cocoon.xml;
17
18 import org.apache.cocoon.xml.dom.DOMStreamer;
19 import org.w3c.dom.Document JavaDoc;
20 import org.w3c.dom.Node JavaDoc;
21 import org.xml.sax.ContentHandler JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23
24 import javax.xml.parsers.DocumentBuilder JavaDoc;
25 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
26 import javax.xml.parsers.ParserConfigurationException JavaDoc;
27
28 /**
29  * Abstract implementation of {@link XMLFragment} for objects that are more
30  * easily represented as a DOM.
31  *
32  * <p>The {@link #toSAX} method is implemented by streaming (using a
33  * {@link DOMStreamer}) the results of the {@link #toDOM} that must be
34  * implemented by concrete subclasses.</p>
35  *
36  * @author <a HREF="mailto:sylvain.wallez@anyware-tech.com">Sylvain Wallez</a>
37  * @version CVS $Id: AbstractDOMFragment.java 53978 2004-10-07 14:25:45Z vgritsenko $
38  */

39 public abstract class AbstractDOMFragment implements XMLFragment {
40
41     /**
42      * Generates SAX events representing the object's state by serializing the
43      * result of <code>toDOM()</code>.
44      */

45     public void toSAX(ContentHandler JavaDoc handler) throws SAXException JavaDoc {
46         // The ComponentManager is unknown here : use JAXP to create a document
47
DocumentBuilder JavaDoc builder;
48         try {
49             builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
50         } catch (ParserConfigurationException JavaDoc e) {
51             throw new SAXException JavaDoc("Couldn't get a DocumentBuilder", e);
52         }
53
54         Document JavaDoc doc = builder.newDocument();
55
56         // Create a DocumentFragment that will hold the results of toDOM()
57
// (which can create several top-level elements)
58
Node JavaDoc df = doc.createDocumentFragment();
59
60         // Build the DOM representation of this object
61
try {
62             toDOM(df);
63         } catch(Exception JavaDoc e) {
64             throw new SAXException JavaDoc("Exception while converting object to DOM", e);
65         }
66
67         // Stream the document fragment
68
handler.startDocument();
69         new DOMStreamer(handler).stream(df);
70         handler.endDocument();
71     }
72 }
73
Popular Tags