KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > io > SAXModifyContentHandler


1 /*
2  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
6  */

7
8 package org.dom4j.io;
9
10 import java.io.IOException JavaDoc;
11
12 import org.dom4j.DocumentFactory;
13 import org.dom4j.Element;
14 import org.dom4j.ElementHandler;
15
16 import org.xml.sax.Attributes JavaDoc;
17 import org.xml.sax.Locator JavaDoc;
18 import org.xml.sax.SAXException JavaDoc;
19
20 /**
21  * This extension of the SAXContentHandler writes SAX events immediately to the
22  * provided XMLWriter, unless some {@link org.dom4.ElementHandler}is still
23  * handling the current Element.
24  *
25  * @author Wonne Keysers (Realsoftware.be)
26  *
27  * @see org.dom4j.io.SAXContentHandler
28  */

29 class SAXModifyContentHandler extends SAXContentHandler {
30     private XMLWriter xmlWriter;
31
32     public SAXModifyContentHandler() {
33     }
34
35     public SAXModifyContentHandler(DocumentFactory documentFactory) {
36         super(documentFactory);
37     }
38
39     public SAXModifyContentHandler(DocumentFactory documentFactory,
40             ElementHandler elementHandler) {
41         super(documentFactory, elementHandler);
42     }
43
44     public SAXModifyContentHandler(DocumentFactory documentFactory,
45             ElementHandler elementHandler, ElementStack elementStack) {
46         super(documentFactory, elementHandler, elementStack);
47     }
48
49     public void setXMLWriter(XMLWriter writer) {
50         this.xmlWriter = writer;
51     }
52
53     public void startCDATA() throws SAXException JavaDoc {
54         super.startCDATA();
55
56         if (!activeHandlers() && (xmlWriter != null)) {
57             xmlWriter.startCDATA();
58         }
59     }
60
61     public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
62             throws SAXException JavaDoc {
63         super.startDTD(name, publicId, systemId);
64
65         if (xmlWriter != null) {
66             xmlWriter.startDTD(name, publicId, systemId);
67         }
68     }
69
70     public void endDTD() throws org.xml.sax.SAXException JavaDoc {
71         super.endDTD();
72
73         if (xmlWriter != null) {
74             xmlWriter.endDTD();
75         }
76     }
77
78     public void comment(char[] characters, int parm2, int parm3)
79             throws SAXException JavaDoc {
80         super.comment(characters, parm2, parm3);
81
82         if (!activeHandlers() && (xmlWriter != null)) {
83             xmlWriter.comment(characters, parm2, parm3);
84         }
85     }
86
87     public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
88         super.startEntity(name);
89
90         if (xmlWriter != null) {
91             xmlWriter.startEntity(name);
92         }
93     }
94
95     public void endCDATA() throws org.xml.sax.SAXException JavaDoc {
96         super.endCDATA();
97
98         if (!activeHandlers() && (xmlWriter != null)) {
99             xmlWriter.endCDATA();
100         }
101     }
102
103     public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
104         super.endEntity(name);
105
106         if (xmlWriter != null) {
107             xmlWriter.endEntity(name);
108         }
109     }
110
111     public void unparsedEntityDecl(String JavaDoc name, String JavaDoc publicId,
112             String JavaDoc systemId, String JavaDoc notation) throws SAXException JavaDoc {
113         super.unparsedEntityDecl(name, publicId, systemId, notation);
114
115         if (!activeHandlers() && (xmlWriter != null)) {
116             xmlWriter.unparsedEntityDecl(name, publicId, systemId, notation);
117         }
118     }
119
120     public void notationDecl(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
121             throws SAXException JavaDoc {
122         super.notationDecl(name, publicId, systemId);
123
124         if (xmlWriter != null) {
125             xmlWriter.notationDecl(name, publicId, systemId);
126         }
127     }
128
129     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName,
130             Attributes JavaDoc atts) throws SAXException JavaDoc {
131         super.startElement(uri, localName, qName, atts);
132
133         if (!activeHandlers() && (xmlWriter != null)) {
134             xmlWriter.startElement(uri, localName, qName, atts);
135         }
136     }
137
138     public void startDocument() throws SAXException JavaDoc {
139         super.startDocument();
140
141         if (xmlWriter != null) {
142             xmlWriter.startDocument();
143         }
144     }
145
146     public void ignorableWhitespace(char[] parm1, int parm2, int parm3)
147             throws SAXException JavaDoc {
148         super.ignorableWhitespace(parm1, parm2, parm3);
149
150         if (!activeHandlers() && (xmlWriter != null)) {
151             xmlWriter.ignorableWhitespace(parm1, parm2, parm3);
152         }
153     }
154
155     public void processingInstruction(String JavaDoc target, String JavaDoc data)
156             throws SAXException JavaDoc {
157         super.processingInstruction(target, data);
158
159         if (!activeHandlers() && (xmlWriter != null)) {
160             xmlWriter.processingInstruction(target, data);
161         }
162     }
163
164     public void setDocumentLocator(Locator JavaDoc locator) {
165         super.setDocumentLocator(locator);
166
167         if (xmlWriter != null) {
168             xmlWriter.setDocumentLocator(locator);
169         }
170     }
171
172     public void skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
173         super.skippedEntity(name);
174
175         if (!activeHandlers() && (xmlWriter != null)) {
176             xmlWriter.skippedEntity(name);
177         }
178     }
179
180     public void endDocument() throws SAXException JavaDoc {
181         super.endDocument();
182
183         if (xmlWriter != null) {
184             xmlWriter.endDocument();
185         }
186     }
187
188     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
189             throws SAXException JavaDoc {
190         super.startPrefixMapping(prefix, uri);
191
192         if (xmlWriter != null) {
193             xmlWriter.startPrefixMapping(prefix, uri);
194         }
195     }
196
197     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
198             throws SAXException JavaDoc {
199         ElementHandler currentHandler = getElementStack().getDispatchHandler()
200                 .getHandler(getElementStack().getPath());
201
202         super.endElement(uri, localName, qName);
203
204         if (!activeHandlers()) {
205             if (xmlWriter != null) {
206                 if (currentHandler == null) {
207                     xmlWriter.endElement(uri, localName, qName);
208                 } else if (currentHandler instanceof SAXModifyElementHandler) {
209                     SAXModifyElementHandler modifyHandler
210                             = (SAXModifyElementHandler) currentHandler;
211                     Element modifiedElement = modifyHandler
212                             .getModifiedElement();
213
214                     try {
215                         xmlWriter.write(modifiedElement);
216                     } catch (IOException JavaDoc ex) {
217                         throw new SAXModifyException(ex);
218                     }
219                 }
220             }
221         }
222     }
223
224     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
225         super.endPrefixMapping(prefix);
226
227         if (xmlWriter != null) {
228             xmlWriter.endPrefixMapping(prefix);
229         }
230     }
231
232     public void characters(char[] parm1, int parm2, int parm3)
233             throws SAXException JavaDoc {
234         super.characters(parm1, parm2, parm3);
235
236         if (!activeHandlers() && (xmlWriter != null)) {
237             xmlWriter.characters(parm1, parm2, parm3);
238         }
239     }
240
241     protected XMLWriter getXMLWriter() {
242         return this.xmlWriter;
243     }
244
245     private boolean activeHandlers() {
246         DispatchHandler handler = getElementStack().getDispatchHandler();
247
248         return handler.getActiveHandlerCount() > 0;
249     }
250 }
251
252 /*
253  * Redistribution and use of this software and associated documentation
254  * ("Software"), with or without modification, are permitted provided that the
255  * following conditions are met:
256  *
257  * 1. Redistributions of source code must retain copyright statements and
258  * notices. Redistributions must also contain a copy of this document.
259  *
260  * 2. Redistributions in binary form must reproduce the above copyright notice,
261  * this list of conditions and the following disclaimer in the documentation
262  * and/or other materials provided with the distribution.
263  *
264  * 3. The name "DOM4J" must not be used to endorse or promote products derived
265  * from this Software without prior written permission of MetaStuff, Ltd. For
266  * written permission, please contact dom4j-info@metastuff.com.
267  *
268  * 4. Products derived from this Software may not be called "DOM4J" nor may
269  * "DOM4J" appear in their names without prior written permission of MetaStuff,
270  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
271  *
272  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
273  *
274  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
275  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
276  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
277  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
278  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
279  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
280  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
281  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
282  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
283  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
284  * POSSIBILITY OF SUCH DAMAGE.
285  *
286  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
287  */

288
Popular Tags