KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.xml.transform.Transformer JavaDoc;
19 import javax.xml.transform.TransformerFactory JavaDoc;
20 import javax.xml.transform.TransformerException JavaDoc;
21 import javax.xml.transform.TransformerConfigurationException JavaDoc;
22 import javax.xml.transform.dom.DOMSource JavaDoc;
23 import javax.xml.transform.sax.SAXResult JavaDoc;
24 import org.xml.sax.Attributes JavaDoc;
25 import org.xml.sax.ContentHandler JavaDoc;
26 import org.xml.sax.Locator JavaDoc;
27 import org.xml.sax.SAXException JavaDoc;
28 import org.xml.sax.ext.LexicalHandler JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30
31 /**
32  * A special purpose <code>XMLConsumer</code> which can:
33  * <ul>
34  * <li>Trim empty characters if
35  * {@link #setIgnoreEmptyCharacters(boolean) ignoreEmptyCharacters} is set.
36  * <li>Ignore root element if
37  * {@link #setIgnoreRootElement(boolean) ignoreRootElement} is set.
38  * <li>Ignore startDocument, endDocument events.
39  * <li>Ignore startDTD, endDTD, and all comments within DTD.
40  * </ul>
41  *
42  * <p>It is more complicated version of {@link EmbeddedXMLPipe} which, except
43  * being used to include other files into the SAX events stream, can perform
44  * optional operations described above.</p>
45  *
46  * @see EmbeddedXMLPipe
47  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
48  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
49  * @version CVS $Id: IncludeXMLConsumer.java 326894 2005-10-20 11:42:20Z cziegeler $
50  */

51 public class IncludeXMLConsumer implements XMLConsumer {
52
53     /** The TrAX factory for serializing xml */
54     private static final TransformerFactory JavaDoc FACTORY = TransformerFactory.newInstance();
55
56     private final ContentHandler JavaDoc contentHandler;
57     private final LexicalHandler JavaDoc lexicalHandler;
58
59     private boolean ignoreEmptyCharacters;
60     private boolean ignoreRootElement;
61     private int ignoreRootElementCount;
62     private boolean inDTD;
63
64     /**
65      * Constructor
66      */

67     public IncludeXMLConsumer (XMLConsumer consumer) {
68         this.contentHandler = consumer;
69         this.lexicalHandler = consumer;
70     }
71
72     /**
73      * Constructor
74      */

75     public IncludeXMLConsumer (ContentHandler JavaDoc contentHandler, LexicalHandler JavaDoc lexicalHandler) {
76         this.contentHandler = contentHandler;
77         this.lexicalHandler = lexicalHandler;
78     }
79
80     /**
81      * Constructor
82      */

83     public IncludeXMLConsumer (ContentHandler JavaDoc contentHandler) {
84         this.contentHandler = contentHandler;
85         this.lexicalHandler = contentHandler instanceof LexicalHandler JavaDoc ? (LexicalHandler JavaDoc)contentHandler : null;
86     }
87
88     /**
89      * Utility method to stream a DOM node into the provided content handler,
90      * lexical handler.
91      *
92      * @param node The DOM Node to be included
93      * @param contentHandler The SAX ContentHandler receiving the information
94      * @param lexicalHandler The SAX LexicalHandler receiving the information (optional)
95      */

96     public static void includeNode(Node JavaDoc node,
97                                    ContentHandler JavaDoc contentHandler,
98                                    LexicalHandler JavaDoc lexicalHandler)
99     throws SAXException JavaDoc {
100         if (node != null) {
101             if (node.getNodeType() == Node.TEXT_NODE){
102                 String JavaDoc value = node.getNodeValue();
103                 contentHandler.characters(value.toCharArray(), 0, value.length());
104             } else {
105                 try {
106                     IncludeXMLConsumer filter = new IncludeXMLConsumer(contentHandler, lexicalHandler);
107                     Transformer JavaDoc transformer = FACTORY.newTransformer();
108                     DOMSource JavaDoc source = new DOMSource JavaDoc(node);
109                     SAXResult JavaDoc result = new SAXResult JavaDoc(filter);
110                     result.setLexicalHandler(filter);
111                     transformer.transform(source, result);
112                 } catch (TransformerConfigurationException JavaDoc e) {
113                     throw new SAXException JavaDoc("TransformerConfigurationException", e);
114                 } catch (TransformerException JavaDoc e) {
115                     throw new SAXException JavaDoc("TransformerException", e);
116                 }
117             }
118         }
119     }
120
121     /**
122      * Control SAX event handling.
123      * If set to <code>true</code> all empty characters events are ignored.
124      * The default is <code>false</code>.
125      */

126     public void setIgnoreEmptyCharacters(boolean value) {
127         this.ignoreEmptyCharacters = value;
128     }
129
130     /**
131      * Control SAX event handling.
132      * If set to <code>true</code> the root element is ignored.
133      * The default is <code>false</code>.
134      */

135     public void setIgnoreRootElement(boolean value) {
136         this.ignoreRootElement = value;
137         this.ignoreRootElementCount = 0;
138     }
139
140     //
141
// ContentHandler interface
142
//
143

144     public void setDocumentLocator(Locator JavaDoc loc) {
145         this.contentHandler.setDocumentLocator(loc);
146     }
147
148     public void startDocument() throws SAXException JavaDoc {
149         // Ignored
150
}
151
152     public void endDocument() throws SAXException JavaDoc {
153         // Ignored
154
}
155
156     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc {
157         this.contentHandler.startPrefixMapping(prefix, uri);
158     }
159
160     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
161         this.contentHandler.endPrefixMapping(prefix);
162     }
163
164     public void startElement(String JavaDoc uri, String JavaDoc local, String JavaDoc qName, Attributes JavaDoc attr) throws SAXException JavaDoc {
165         if (this.ignoreRootElement == false ||
166             this.ignoreRootElementCount > 0) {
167             this.contentHandler.startElement(uri,local,qName,attr);
168         }
169         this.ignoreRootElementCount++;
170     }
171
172     public void endElement(String JavaDoc uri, String JavaDoc local, String JavaDoc qName) throws SAXException JavaDoc {
173         this.ignoreRootElementCount--;
174         if (!this.ignoreRootElement || this.ignoreRootElementCount > 0) {
175             this.contentHandler.endElement(uri, local, qName);
176         }
177     }
178
179     public void characters(char[] ch, int start, int end) throws SAXException JavaDoc {
180         if (this.ignoreEmptyCharacters) {
181             String JavaDoc text = new String JavaDoc(ch, start, end).trim();
182             if (text.length() > 0) {
183                 this.contentHandler.characters(text.toCharArray(), 0, text.length());
184             }
185         } else {
186             this.contentHandler.characters(ch, start, end);
187         }
188     }
189
190     public void ignorableWhitespace(char[] ch, int start, int end) throws SAXException JavaDoc {
191         if (!this.ignoreEmptyCharacters) {
192             this.contentHandler.ignorableWhitespace(ch, start, end);
193         }
194     }
195
196     public void processingInstruction(String JavaDoc name, String JavaDoc value) throws SAXException JavaDoc {
197         this.contentHandler.processingInstruction(name, value);
198     }
199
200     public void skippedEntity(String JavaDoc ent) throws SAXException JavaDoc {
201         this.contentHandler.skippedEntity(ent);
202     }
203
204     //
205
// LexicalHandler interface
206
//
207

208     public void startDTD(String JavaDoc name, String JavaDoc public_id, String JavaDoc system_id)
209     throws SAXException JavaDoc {
210         // Ignored
211
this.inDTD = true;
212     }
213
214     public void endDTD() throws SAXException JavaDoc {
215         // Ignored
216
this.inDTD = false;
217     }
218
219     public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
220         if (lexicalHandler != null) {
221             lexicalHandler.startEntity(name);
222         }
223     }
224
225     public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
226         if (lexicalHandler != null) {
227             lexicalHandler.endEntity(name);
228         }
229     }
230
231     public void startCDATA() throws SAXException JavaDoc {
232         if (lexicalHandler != null) {
233             lexicalHandler.startCDATA();
234         }
235     }
236
237     public void endCDATA() throws SAXException JavaDoc {
238         if (lexicalHandler != null) {
239             lexicalHandler.endCDATA();
240         }
241     }
242
243     public void comment(char ary[], int start, int length) throws SAXException JavaDoc {
244         if (!inDTD && lexicalHandler != null) {
245             lexicalHandler.comment(ary,start,length);
246         }
247     }
248 }
249
Popular Tags