KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > xml > dom > DOMBuilder


1 /*
2  * Copyright 1999-2005 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.dom;
17
18 import org.apache.avalon.framework.CascadingRuntimeException;
19
20 import org.apache.cocoon.xml.AbstractXMLPipe;
21
22 import org.w3c.dom.Document JavaDoc;
23 import org.w3c.dom.Node JavaDoc;
24 import org.xml.sax.SAXException JavaDoc;
25
26 import javax.xml.transform.TransformerFactory JavaDoc;
27 import javax.xml.transform.dom.DOMResult JavaDoc;
28 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
29 import javax.xml.transform.sax.TransformerHandler JavaDoc;
30
31 /**
32  * The <code>DOMBuilder</code> is a utility class that will generate a W3C
33  * DOM Document from SAX events.
34  *
35  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
36  * @version $Id: DOMBuilder.java 164046 2005-04-21 12:29:22Z vgritsenko $
37  */

38 public class DOMBuilder extends AbstractXMLPipe {
39
40     /** The default transformer factory shared by all instances */
41     protected static final SAXTransformerFactory JavaDoc FACTORY = (SAXTransformerFactory JavaDoc) TransformerFactory.newInstance();
42
43     /** The transformer factory */
44     protected SAXTransformerFactory JavaDoc factory;
45
46     /** The listener */
47     protected Listener listener;
48
49     /** The result */
50     protected DOMResult JavaDoc result;
51
52     /** The parentNode */
53     protected Node JavaDoc parentNode;
54
55     /**
56      * Construct a new instance of this DOMBuilder.
57      */

58     public DOMBuilder() {
59         this((Listener) null, (Node JavaDoc) null);
60     }
61
62     /**
63      * Construct a new instance of this DOMBuilder.
64      */

65     public DOMBuilder(SAXTransformerFactory JavaDoc factory) {
66         this(factory, null, null);
67     }
68
69     /**
70      * Construct a new instance of this DOMBuilder.
71      * @deprecated Use DOMBuilder() instead.
72      */

73     public DOMBuilder(DOMFactory factory) {
74         this((Listener) null, (Node JavaDoc) null);
75     }
76
77     /**
78      * Construct a new instance of this DOMBuilder.
79      */

80     public DOMBuilder(Listener listener) {
81         this(listener, null);
82     }
83
84     /**
85      * Construct a new instance of this DOMBuilder.
86      * @deprecated Use DOMBuilder(listener) instead.
87      */

88     public DOMBuilder(DOMFactory factory, Listener listener) {
89         this(listener, null);
90     }
91
92     /**
93      * Construct a new instance of this DOMBuilder.
94      * @deprecated Use DOMBuilder(listener, parentNode) instead.
95      */

96     public DOMBuilder(DOMFactory domFactory, Listener listener, Node JavaDoc parentNode) {
97         this(listener, parentNode);
98     }
99
100     /**
101      * Constructs a new instance that appends nodes to the given parent node.
102      * <br/>
103      * <strong>Note:</strong> You cannot use a <code>Listener<code> when appending to a
104      * <code>Node</code>, because the notification occurs at <code>endDocument()</code>
105      * which does not happen here.
106      */

107     public DOMBuilder(Node JavaDoc parentNode) {
108         this(null, parentNode);
109     }
110
111     /**
112      * Construct a new instance of this DOMBuilder.
113      */

114     public DOMBuilder(Listener listener, Node JavaDoc parentNode) {
115         this((SAXTransformerFactory JavaDoc) null, listener, parentNode);
116     }
117
118     /**
119      * Construct a new instance of this DOMBuilder.
120      */

121     public DOMBuilder(SAXTransformerFactory JavaDoc factory, Listener listener, Node JavaDoc parentNode) {
122         super();
123         this.factory = factory == null? FACTORY: factory;
124         this.listener = listener;
125         this.parentNode = parentNode;
126         setup();
127     }
128
129     /**
130      * Setup this instance transformer and result objects.
131      */

132     private void setup() {
133         try {
134             TransformerHandler JavaDoc handler = this.factory.newTransformerHandler();
135             setContentHandler(handler);
136             setLexicalHandler(handler);
137             if (this.parentNode != null) {
138                 this.result = new DOMResult JavaDoc(this.parentNode);
139             } else {
140                 this.result = new DOMResult JavaDoc();
141             }
142             handler.setResult(this.result);
143         } catch (javax.xml.transform.TransformerException JavaDoc local) {
144             throw new CascadingRuntimeException("Fatal-Error: Unable to get transformer handler", local);
145         }
146     }
147
148     /**
149      * Recycle this builder, prepare for re-use.
150      */

151     public void recycle() {
152         super.recycle();
153         setup();
154     }
155
156     /**
157      * Return the newly built Document.
158      */

159     public Document JavaDoc getDocument() {
160         if (this.result == null || this.result.getNode() == null) {
161             return null;
162         } else if (this.result.getNode().getNodeType() == Node.DOCUMENT_NODE) {
163             return (Document JavaDoc) this.result.getNode();
164         } else {
165             return this.result.getNode().getOwnerDocument();
166         }
167     }
168
169     /**
170      * Receive notification of the end of a document.
171      *
172      * @exception SAXException If this method was not called appropriately.
173      */

174     public void endDocument() throws SAXException JavaDoc {
175         super.endDocument();
176         // Notify the listener
177
notifyListener();
178     }
179
180     /**
181      * Receive notification of a successfully completed DOM tree generation.
182      */

183     protected void notifyListener() throws SAXException JavaDoc {
184         if (this.listener != null) {
185             this.listener.notify(getDocument());
186         }
187     }
188
189     /**
190      * The Listener interface must be implemented by those objects willing to
191      * be notified of a successful DOM tree generation.
192      */

193     public interface Listener {
194
195         /**
196          * Receive notification of a successfully completed DOM tree generation.
197          */

198         void notify(Document JavaDoc doc) throws SAXException JavaDoc;
199     }
200 }
201
Popular Tags