KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xni > PassThroughFilter


1 /*
2  * Copyright 2001, 2002,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
17 package xni;
18
19 import org.apache.xerces.xni.Augmentations;
20 import org.apache.xerces.xni.QName;
21 import org.apache.xerces.xni.NamespaceContext;
22 import org.apache.xerces.xni.XMLAttributes;
23 import org.apache.xerces.xni.XMLDocumentHandler;
24 import org.apache.xerces.xni.XMLLocator;
25 import org.apache.xerces.xni.XMLResourceIdentifier;
26 import org.apache.xerces.xni.XMLString;
27 import org.apache.xerces.xni.XNIException;
28 import org.apache.xerces.xni.parser.XMLDocumentSource;
29
30 /**
31  * This sample demonstrates how to implement a simple pass-through
32  * filter for the document "streaming" information set using XNI.
33  * This filter could be used in a pipeline of XNI parser components
34  * that communicate document events.
35  * <p>
36  * <strong>Note:</strong> This sample does not contain a
37  * <code>main</code> method and cannot be run. It is only for
38  * demonstration purposes.
39  *
40  * @author Andy Clark, IBM
41  *
42  * @version $Id: PassThroughFilter.java,v 1.9 2004/02/24 23:41:05 mrglavas Exp $
43  */

44 public class PassThroughFilter
45     implements XMLDocumentHandler {
46     
47     //
48
// Data
49
//
50

51     /** The document handler. */
52     protected XMLDocumentHandler fDocumentHandler;
53
54     /** The document source */
55     protected XMLDocumentSource fDocumentSource;
56     
57     //
58
// Public methods
59
//
60

61     /**
62      * Sets the document handler.
63      *
64      * @param handler The new document handler.
65      */

66     public void setDocumentHandler(XMLDocumentHandler handler) {
67         fDocumentHandler = handler;
68     } // setDocumentHandler(XMLDocumentHandler)
69

70     //
71
// XMLDocumentHandler methods
72
//
73

74     /**
75      * The start of the document.
76      *
77      * @param locator The document locator, or null if the document
78      * location cannot be reported during the parsing
79      * of this document. However, it is <em>strongly</em>
80      * recommended that a locator be supplied that can
81      * at least report the system identifier of the
82      * document.
83      * @param encoding The auto-detected IANA encoding name of the entity
84      * stream. This value will be null in those situations
85      * where the entity encoding is not auto-detected (e.g.
86      * internal entities or a document entity that is
87      * parsed from a java.io.Reader).
88      *
89      * @throws XNIException Thrown by handler to signal an error.
90      */

91     public void startDocument(XMLLocator locator, String JavaDoc encoding,
92                               NamespaceContext namespaceContext, Augmentations augs)
93         throws XNIException {
94         if (fDocumentHandler != null) {
95             fDocumentHandler.startDocument(locator, encoding, namespaceContext, augs);
96         }
97     } // startDocument(XMLLocator,String)
98

99     /**
100      * Notifies of the presence of an XMLDecl line in the document. If
101      * present, this method will be called immediately following the
102      * startDocument call.
103      *
104      * @param version The XML version.
105      * @param encoding The IANA encoding name of the document, or null if
106      * not specified.
107      * @param standalone The standalone value, or null if not specified.
108      *
109      * @throws XNIException Thrown by handler to signal an error.
110      */

111     public void xmlDecl(String JavaDoc version, String JavaDoc encoding,
112                         String JavaDoc standalone, Augmentations augs) throws XNIException {
113         if (fDocumentHandler != null) {
114             fDocumentHandler.xmlDecl(version, encoding, standalone, augs);
115         }
116     } // xmlDecl(String,String,String
117

118     /**
119      * Notifies of the presence of the DOCTYPE line in the document.
120      *
121      * @param rootElement The name of the root element.
122      * @param publicId The public identifier if an external DTD or null
123      * if the external DTD is specified using SYSTEM.
124      * @param systemId The system identifier if an external DTD, null
125      * otherwise.
126      *
127      * @throws XNIException Thrown by handler to signal an error.
128      */

129     public void doctypeDecl(String JavaDoc rootElement, String JavaDoc publicId,
130                             String JavaDoc systemId, Augmentations augs) throws XNIException {
131         if (fDocumentHandler != null) {
132             fDocumentHandler.doctypeDecl(rootElement, publicId, systemId, augs);
133         }
134     } // doctypeDecl(String,String,String)
135

136     /**
137      * A comment.
138      *
139      * @param text The text in the comment.
140      *
141      * @throws XNIException Thrown by application to signal an error.
142      */

143     public void comment(XMLString text, Augmentations augs) throws XNIException {
144         if (fDocumentHandler != null) {
145             fDocumentHandler.comment(text, augs);
146         }
147     } // comment(XMLString)
148

149     /**
150      * A processing instruction. Processing instructions consist of a
151      * target name and, optionally, text data. The data is only meaningful
152      * to the application.
153      * <p>
154      * Typically, a processing instruction's data will contain a series
155      * of pseudo-attributes. These pseudo-attributes follow the form of
156      * element attributes but are <strong>not</strong> parsed or presented
157      * to the application as anything other than text. The application is
158      * responsible for parsing the data.
159      *
160      * @param target The target.
161      * @param data The data or null if none specified.
162      *
163      * @throws XNIException Thrown by handler to signal an error.
164      */

165     public void processingInstruction(String JavaDoc target, XMLString data, Augmentations augs)
166         throws XNIException {
167         if (fDocumentHandler != null) {
168             fDocumentHandler.processingInstruction(target, data, augs);
169         }
170     } // processingInstruction(String,XMLString)
171

172     /**
173      * The start of an element.
174      *
175      * @param element The name of the element.
176      * @param attributes The element attributes.
177      *
178      * @throws XNIException Thrown by handler to signal an error.
179      */

180     public void startElement(QName element, XMLAttributes attributes, Augmentations augs)
181         throws XNIException {
182         if (fDocumentHandler != null) {
183             fDocumentHandler.startElement(element, attributes, augs);
184         }
185     } // startElement(QName,XMLAttributes)
186

187     /**
188      * An empty element.
189      *
190      * @param element The name of the element.
191      * @param attributes The element attributes.
192      *
193      * @throws XNIException Thrown by handler to signal an error.
194      */

195     public void emptyElement(QName element, XMLAttributes attributes, Augmentations augs)
196         throws XNIException {
197         if (fDocumentHandler != null) {
198             fDocumentHandler.emptyElement(element, attributes, augs);
199         }
200     } // emptyElement(QName,XMLAttributes)
201

202     /**
203      * The end of an element.
204      *
205      * @param element The name of the element.
206      *
207      * @throws XNIException Thrown by handler to signal an error.
208      */

209     public void endElement(QName element, Augmentations augs)
210         throws XNIException {
211         if (fDocumentHandler != null) {
212             fDocumentHandler.endElement(element, augs);
213         }
214     } // endElement(QName)
215

216     /**
217      * This method notifies the start of an entity.
218      * <p>
219      * <strong>Note:</strong> This method is not called for entity references
220      * appearing as part of attribute values.
221      *
222      * @param name The name of the entity.
223      * @param publicId The public identifier of the entity if the entity
224      * is external, null otherwise.
225      * @param systemId The system identifier of the entity if the entity
226      * is external, null otherwise.
227      * @param baseSystemId The base system identifier of the entity if
228      * the entity is external, null otherwise.
229      * @param encoding The auto-detected IANA encoding name of the entity
230      * stream. This value will be null in those situations
231      * where the entity encoding is not auto-detected (e.g.
232      * internal entities or a document entity that is
233      * parsed from a java.io.Reader).
234      *
235      * @throws XNIException Thrown by handler to signal an error.
236      */

237     public void startGeneralEntity(String JavaDoc name,
238                                    XMLResourceIdentifier identifier,
239                                    String JavaDoc encoding, Augmentations augs)
240         throws XNIException {
241         if (fDocumentHandler != null) {
242             fDocumentHandler.startGeneralEntity(name, identifier, encoding, augs);
243         }
244     } // startGeneralEntity(String,XMLResourceIdentifier,String,Augmentations)
245

246     /**
247      * Notifies of the presence of a TextDecl line in an entity. If present,
248      * this method will be called immediately following the startEntity call.
249      * <p>
250      * <strong>Note:</strong> This method will never be called for the
251      * document entity; it is only called for external general entities
252      * referenced in document content.
253      * <p>
254      * <strong>Note:</strong> This method is not called for entity references
255      * appearing as part of attribute values.
256      *
257      * @param version The XML version, or null if not specified.
258      * @param encoding The IANA encoding name of the entity.
259      *
260      * @throws XNIException Thrown by handler to signal an error.
261      */

262     public void textDecl(String JavaDoc version, String JavaDoc encoding, Augmentations augs)
263         throws XNIException {
264         if (fDocumentHandler != null) {
265             fDocumentHandler.textDecl(version, encoding, augs);
266         }
267     } // textDecl(String,String)
268

269     /**
270      * This method notifies the end of an entity.
271      * <p>
272      * <strong>Note:</strong> This method is not called for entity references
273      * appearing as part of attribute values.
274      *
275      * @param name The name of the entity.
276      *
277      * @throws XNIException Thrown by handler to signal an error.
278      */

279     public void endGeneralEntity(String JavaDoc name, Augmentations augs) throws XNIException {
280         if (fDocumentHandler != null) {
281             fDocumentHandler.endGeneralEntity(name, augs);
282         }
283     } // endGeneralEntity(String,Augmentations)
284

285     /**
286      * Character content.
287      *
288      * @param text The content.
289      *
290      * @throws XNIException Thrown by handler to signal an error.
291      */

292     public void characters(XMLString text, Augmentations augs) throws XNIException {
293         if (fDocumentHandler != null) {
294             fDocumentHandler.characters(text, augs);
295         }
296     } // characters(XMLString)
297

298     /**
299      * Ignorable whitespace. For this method to be called, the document
300      * source must have some way of determining that the text containing
301      * only whitespace characters should be considered ignorable. For
302      * example, the validator can determine if a length of whitespace
303      * characters in the document are ignorable based on the element
304      * content model.
305      *
306      * @param text The ignorable whitespace.
307      *
308      * @throws XNIException Thrown by handler to signal an error.
309      */

310     public void ignorableWhitespace(XMLString text, Augmentations augs) throws XNIException {
311         if (fDocumentHandler != null) {
312             fDocumentHandler.ignorableWhitespace(text, augs);
313         }
314     } // ignorableWhitespace(XMLString)
315

316     /**
317      * The start of a CDATA section.
318      *
319      * @throws XNIException Thrown by handler to signal an error.
320      */

321     public void startCDATA(Augmentations augs) throws XNIException {
322         if (fDocumentHandler != null) {
323             fDocumentHandler.startCDATA(augs);
324         }
325     } // startCDATA()
326

327     /**
328      * The end of a CDATA section.
329      *
330      * @throws XNIException Thrown by handler to signal an error.
331      */

332     public void endCDATA(Augmentations augs) throws XNIException {
333         if (fDocumentHandler != null) {
334             fDocumentHandler.endCDATA(augs);
335         }
336     } // endCDATA()
337

338     /**
339      * The end of the document.
340      *
341      * @throws XNIException Thrown by handler to signal an error.
342      */

343     public void endDocument(Augmentations augs) throws XNIException {
344         if (fDocumentHandler != null) {
345             fDocumentHandler.endDocument(augs);
346         }
347     } // endDocument()
348

349
350     /** Sets the document source. */
351     public void setDocumentSource(XMLDocumentSource source){
352         fDocumentSource = source;
353     }
354
355
356     /** Returns the document source. */
357     public XMLDocumentSource getDocumentSource(){
358         return fDocumentSource;
359     }
360     
361 } // class PassThroughFilter
362
Popular Tags