View Javadoc

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