1 /*
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Intalio, Inc. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Intalio, Inc. Exolab is a registered
23 * trademark of Intalio, Inc.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 1999 (C) Intalio, Inc. All Rights Reserved.
42 *
43 * This file was adapted From XSL:P
44 *
45 * $Id$
46 */
47 package org.exolab.castor.xml.util;
48
49 import org.exolab.castor.xml.EventProducer;
50 import org.w3c.dom.Document;
51 import org.w3c.dom.Element;
52 import org.w3c.dom.Node;
53 import org.w3c.dom.ProcessingInstruction;
54 import org.w3c.dom.Text;
55 import org.xml.sax.AttributeList;
56 import org.xml.sax.DocumentHandler;
57
58 /**
59 * A class for converting a DOM document to SAX events
60 * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
61 * @version $Revision$ $Date: 2003-03-03 00:05:44 -0700 (Mon, 03 Mar 2003) $
62 **/
63 public class DOMEventProducer implements EventProducer {
64
65
66 private DocumentHandler _handler = null;
67
68 private Node _node = null;
69
70 /**
71 * Creates a new DOMEventProducer
72 **/
73 public DOMEventProducer() {
74 super();
75 } //-- DOMEventProducer
76
77 /**
78 * Creates a new DOMEventProducer for the given Node
79 * @param node the node to create the DOMEventProducer for.
80 **/
81 public DOMEventProducer(Node node) {
82 super();
83 this._node = node;
84 } //-- DOMEventProducer
85
86
87 /**
88 * Sets the DocumentHandler to use when firing events
89 **/
90 public void setDocumentHandler(DocumentHandler handler) {
91 this._handler = handler;
92 } //-- setDocumentHandler
93
94 /**
95 * Sets the node which is to be converted into SAX events
96 * @param node the node which is to be converted into SAX events
97 **/
98 public void setNode(Node node) {
99 this._node = node;
100 } //-- setNode
101
102 /**
103 * Starts producing the events for the Node which is to be
104 * converted into SAX events
105 **/
106 public void start()
107 throws org.xml.sax.SAXException
108 {
109 if ((_node == null) || (_handler == null)) return;
110
111 process(_node, _handler);
112
113 } //-- start
114
115 /**
116 * Walks the given DOM Document and converts it into it's corresponding
117 * SAX events
118 * @param document the Node to process into SAX events
119 * @param handler the DocumentHandler to send events to
120 **/
121 public static void process(Document document, DocumentHandler handler)
122
123 throws org.xml.sax.SAXException
124
125 {
126
127 if (document == null) return;
128
129 if (handler == null) return;
130
131
132
133 handler.startDocument();
134
135 processChildren(document, handler);
136
137 handler.endDocument();
138
139
140
141 } //-- process(Document, DocumentHandler)
142
143
144
145 /**
146
147 * Breaks down the given node into it's corresponding SAX events
148
149 * @param node the Node to process into SAX events
150
151 * @param handler the DocumentHandler to send events to
152
153 **/
154
155 public static void process(Node node, DocumentHandler handler)
156
157 throws org.xml.sax.SAXException
158
159 {
160
161
162
163 if ((node == null) || (handler == null)) return;
164
165
166
167 switch(node.getNodeType()) {
168
169 case Node.DOCUMENT_NODE:
170
171 process((Document)node, handler);
172
173 break;
174
175 case Node.DOCUMENT_FRAGMENT_NODE:
176
177 processChildren(node, handler);
178
179 break;
180
181 case Node.ELEMENT_NODE:
182
183 process((Element)node, handler);
184
185 break;
186
187 case Node.CDATA_SECTION_NODE:
188
189 case Node.TEXT_NODE:
190
191 process((Text)node, handler);
192
193 break;
194
195 case Node.PROCESSING_INSTRUCTION_NODE:
196
197 process((ProcessingInstruction)node, handler);
198
199 break;
200
201 case Node.COMMENT_NODE:
202
203
204
205 default:
206
207 break;
208
209 }
210
211
212
213 } //-- process(Node, DocumentHandler)
214
215
216
217 //-------------------/
218
219 //- Private Methods -/
220
221 //-------------------/
222
223
224
225 /**
226
227 * Breaks down the given node into it's corresponding SAX events
228
229 * @param handler the DocumentHandler to send events to
230
231 **/
232
233 private static void process(Element element, DocumentHandler handler)
234
235 throws org.xml.sax.SAXException
236
237 {
238
239
240
241 String name = element.getNodeName();
242
243 AttributeList atts
244 = new AttributeListWrapper(element.getAttributes());
245
246 handler.startElement(name, atts);
247
248 processChildren(element, handler);
249
250 handler.endElement(name);
251
252 } //-- process(Element, DocumentHandler);
253
254
255
256 /**
257
258 * Breaks down the given node into it's corresponding SAX events
259
260 * @param handler the DocumentHandler to send events to
261
262 **/
263
264 private static void process(Text text, DocumentHandler handler)
265
266 throws org.xml.sax.SAXException
267
268 {
269
270 String data = text.getData();
271
272 if ((data != null) && (data.length() > 0)) {
273
274 char[] chars = data.toCharArray();
275
276 handler.characters(chars, 0, chars.length);
277
278 }
279
280 } //-- process(Text, DocumentHandler)
281
282
283
284 /**
285
286 * Breaks down the given ProcessingInstruction into it's corresponding
287
288 * SAX event
289
290 * @param pi the processing instruction to process
291
292 * @param handler the DocumentHandler to send events to
293
294 **/
295
296 private static void process
297
298 (ProcessingInstruction pi, DocumentHandler handler)
299
300 throws org.xml.sax.SAXException
301
302 {
303
304 handler.processingInstruction(pi.getTarget(),pi.getData());
305
306
307
308 } //-- process(ProcessingInstruction, DocumentHandler);
309
310
311
312 /**
313
314 * Processes the children of the given Node
315
316 * @param node the Node to process the children of
317
318 * @param handler the DocumentHandler to send events to
319
320 **/
321
322 private static void processChildren
323
324 (Node node, DocumentHandler handler)
325
326 throws org.xml.sax.SAXException
327
328 {
329
330 Node child = node.getFirstChild();
331
332 while (child != null) {
333
334 process(child, handler);
335
336 child = child.getNextSibling();
337
338 }
339
340
341
342 } //-- processChildren
343
344
345
346
347
348 } //-- DOMEventProducer
349