1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package xni;
19 
20 import org.apache.xerces.xni.Augmentations;
21 import org.apache.xerces.xni.NamespaceContext;
22 import org.apache.xerces.xni.QName;
23 import org.apache.xerces.xni.XMLAttributes;
24 import org.apache.xerces.xni.XMLDocumentHandler;
25 import org.apache.xerces.xni.XMLLocator;
26 import org.apache.xerces.xni.XMLResourceIdentifier;
27 import org.apache.xerces.xni.XMLString;
28 import org.apache.xerces.xni.XNIException;
29 import org.apache.xerces.xni.parser.XMLDocumentSource;
30 
31 /**
32  * This sample demonstrates how to implement a simple pass-through
33  * filter for the document "streaming" information set using XNI.
34  * This filter could be used in a pipeline of XNI parser components
35  * that communicate document events.
36  * <p>
37  * <strong>Note:</strong> This sample does not contain a
38  * <code>main</code> method and cannot be run. It is only for
39  * demonstration purposes.
40  *
41  * @author Andy Clark, IBM
42  *
43  * @version $Id: PassThroughFilter.java 699895 2008-09-28 21:21:24Z mrglavas $
44  */
45 public class PassThroughFilter
46     implements XMLDocumentHandler {
47 
48     //
49     // Data
50     //
51 
52     /** The document handler. */
53     protected XMLDocumentHandler fDocumentHandler;
54 
55     /** The document source */
56     protected XMLDocumentSource fDocumentSource;
57 
58     //
59     // Public methods
60     //
61 
62     /**
63      * Sets the document handler.
64      *
65      * @param handler The new document handler.
66      */
setDocumentHandler(XMLDocumentHandler handler)67     public void setDocumentHandler(XMLDocumentHandler handler) {
68         fDocumentHandler = handler;
69     } // setDocumentHandler(XMLDocumentHandler)
70 
71     //
72     // XMLDocumentHandler methods
73     //
74 
75     /**
76      * The start of the document.
77      *
78      * @param locator  The document locator, or null if the document
79      *                 location cannot be reported during the parsing
80      *                 of this document. However, it is <em>strongly</em>
81      *                 recommended that a locator be supplied that can
82      *                 at least report the system identifier of the
83      *                 document.
84      * @param encoding The auto-detected IANA encoding name of the entity
85      *                 stream. This value will be null in those situations
86      *                 where the entity encoding is not auto-detected (e.g.
87      *                 internal entities or a document entity that is
88      *                 parsed from a java.io.Reader).
89      *
90      * @throws XNIException Thrown by handler to signal an error.
91      */
startDocument(XMLLocator locator, String encoding, NamespaceContext namespaceContext, Augmentations augs)92     public void startDocument(XMLLocator locator, String encoding,
93                               NamespaceContext namespaceContext, Augmentations augs)
94         throws XNIException {
95         if (fDocumentHandler != null) {
96             fDocumentHandler.startDocument(locator, encoding, namespaceContext, augs);
97 	    }
98     } // startDocument(XMLLocator,String)
99 
100     /**
101      * Notifies of the presence of an XMLDecl line in the document. If
102      * present, this method will be called immediately following the
103      * startDocument call.
104      *
105      * @param version    The XML version.
106      * @param encoding   The IANA encoding name of the document, or null if
107      *                   not specified.
108      * @param standalone The standalone value, or null if not specified.
109      *
110      * @throws XNIException Thrown by handler to signal an error.
111      */
xmlDecl(String version, String encoding, String standalone, Augmentations augs)112     public void xmlDecl(String version, String encoding,
113                         String standalone, Augmentations augs) throws XNIException {
114         if (fDocumentHandler != null) {
115             fDocumentHandler.xmlDecl(version, encoding, standalone, augs);
116 	    }
117     } // xmlDecl(String,String,String
118 
119     /**
120      * Notifies of the presence of the DOCTYPE line in the document.
121      *
122      * @param rootElement The name of the root element.
123      * @param publicId    The public identifier if an external DTD or null
124      *                    if the external DTD is specified using SYSTEM.
125      * @param systemId    The system identifier if an external DTD, null
126      *                    otherwise.
127      *
128      * @throws XNIException Thrown by handler to signal an error.
129      */
doctypeDecl(String rootElement, String publicId, String systemId, Augmentations augs)130     public void doctypeDecl(String rootElement, String publicId,
131                             String systemId, Augmentations augs) throws XNIException {
132         if (fDocumentHandler != null) {
133             fDocumentHandler.doctypeDecl(rootElement, publicId, systemId, augs);
134         }
135     } // doctypeDecl(String,String,String)
136 
137     /**
138      * A comment.
139      *
140      * @param text The text in the comment.
141      *
142      * @throws XNIException Thrown by application to signal an error.
143      */
comment(XMLString text, Augmentations augs)144     public void comment(XMLString text, Augmentations augs) throws XNIException {
145         if (fDocumentHandler != null) {
146             fDocumentHandler.comment(text, augs);
147         }
148     } // comment(XMLString)
149 
150     /**
151      * A processing instruction. Processing instructions consist of a
152      * target name and, optionally, text data. The data is only meaningful
153      * to the application.
154      * <p>
155      * Typically, a processing instruction's data will contain a series
156      * of pseudo-attributes. These pseudo-attributes follow the form of
157      * element attributes but are <strong>not</strong> parsed or presented
158      * to the application as anything other than text. The application is
159      * responsible for parsing the data.
160      *
161      * @param target The target.
162      * @param data   The data or null if none specified.
163      *
164      * @throws XNIException Thrown by handler to signal an error.
165      */
processingInstruction(String target, XMLString data, Augmentations augs)166     public void processingInstruction(String target, XMLString data, Augmentations augs)
167         throws XNIException {
168         if (fDocumentHandler != null) {
169             fDocumentHandler.processingInstruction(target, data, augs);
170         }
171     } // processingInstruction(String,XMLString)
172 
173     /**
174      * The start of an element.
175      *
176      * @param element    The name of the element.
177      * @param attributes The element attributes.
178      *
179      * @throws XNIException Thrown by handler to signal an error.
180      */
startElement(QName element, XMLAttributes attributes, Augmentations augs)181     public void startElement(QName element, XMLAttributes attributes, Augmentations augs)
182         throws XNIException {
183         if (fDocumentHandler != null) {
184             fDocumentHandler.startElement(element, attributes, augs);
185         }
186     } // startElement(QName,XMLAttributes)
187 
188     /**
189      * An empty element.
190      *
191      * @param element    The name of the element.
192      * @param attributes The element attributes.
193      *
194      * @throws XNIException Thrown by handler to signal an error.
195      */
emptyElement(QName element, XMLAttributes attributes, Augmentations augs)196     public void emptyElement(QName element, XMLAttributes attributes, Augmentations augs)
197         throws XNIException {
198         if (fDocumentHandler != null) {
199             fDocumentHandler.emptyElement(element, attributes, augs);
200         }
201     } // emptyElement(QName,XMLAttributes)
202 
203     /**
204      * The end of an element.
205      *
206      * @param element The name of the element.
207      *
208      * @throws XNIException Thrown by handler to signal an error.
209      */
endElement(QName element, Augmentations augs)210     public void endElement(QName element, Augmentations augs)
211         throws XNIException {
212         if (fDocumentHandler != null) {
213             fDocumentHandler.endElement(element, augs);
214         }
215     } // endElement(QName)
216 
217     /**
218      * This method notifies the start of an entity.
219      * <p>
220      * <strong>Note:</strong> This method is not called for entity references
221      * appearing as part of attribute values.
222      *
223      * @param name     The name of the entity.
224      * @param publicId The public identifier of the entity if the entity
225      *                 is external, null otherwise.
226      * @param systemId The system identifier of the entity if the entity
227      *                 is external, null otherwise.
228      * @param baseSystemId The base system identifier of the entity if
229      *                     the entity is external, null otherwise.
230      * @param encoding The auto-detected IANA encoding name of the entity
231      *                 stream. This value will be null in those situations
232      *                 where the entity encoding is not auto-detected (e.g.
233      *                 internal entities or a document entity that is
234      *                 parsed from a java.io.Reader).
235      *
236      * @throws XNIException Thrown by handler to signal an error.
237      */
startGeneralEntity(String name, XMLResourceIdentifier identifier, String encoding, Augmentations augs)238     public void startGeneralEntity(String name,
239                                    XMLResourceIdentifier identifier,
240                                    String encoding, Augmentations augs)
241         throws XNIException {
242         if (fDocumentHandler != null) {
243             fDocumentHandler.startGeneralEntity(name, identifier, encoding, augs);
244         }
245     } // startGeneralEntity(String,XMLResourceIdentifier,String,Augmentations)
246 
247     /**
248      * Notifies of the presence of a TextDecl line in an entity. If present,
249      * this method will be called immediately following the startEntity call.
250      * <p>
251      * <strong>Note:</strong> This method will never be called for the
252      * document entity; it is only called for external general entities
253      * referenced in document content.
254      * <p>
255      * <strong>Note:</strong> This method is not called for entity references
256      * appearing as part of attribute values.
257      *
258      * @param version  The XML version, or null if not specified.
259      * @param encoding The IANA encoding name of the entity.
260      *
261      * @throws XNIException Thrown by handler to signal an error.
262      */
textDecl(String version, String encoding, Augmentations augs)263     public void textDecl(String version, String encoding, Augmentations augs)
264         throws XNIException {
265         if (fDocumentHandler != null) {
266             fDocumentHandler.textDecl(version, encoding, augs);
267         }
268     } // textDecl(String,String)
269 
270     /**
271      * This method notifies the end of an entity.
272      * <p>
273      * <strong>Note:</strong> This method is not called for entity references
274      * appearing as part of attribute values.
275      *
276      * @param name The name of the entity.
277      *
278      * @throws XNIException Thrown by handler to signal an error.
279      */
endGeneralEntity(String name, Augmentations augs)280     public void endGeneralEntity(String name, Augmentations augs) throws XNIException {
281         if (fDocumentHandler != null) {
282             fDocumentHandler.endGeneralEntity(name, augs);
283         }
284     } // endGeneralEntity(String,Augmentations)
285 
286     /**
287      * Character content.
288      *
289      * @param text The content.
290      *
291      * @throws XNIException Thrown by handler to signal an error.
292      */
characters(XMLString text, Augmentations augs)293     public void characters(XMLString text, Augmentations augs) throws XNIException {
294         if (fDocumentHandler != null) {
295             fDocumentHandler.characters(text, augs);
296         }
297     } // characters(XMLString)
298 
299     /**
300      * Ignorable whitespace. For this method to be called, the document
301      * source must have some way of determining that the text containing
302      * only whitespace characters should be considered ignorable. For
303      * example, the validator can determine if a length of whitespace
304      * characters in the document are ignorable based on the element
305      * content model.
306      *
307      * @param text The ignorable whitespace.
308      *
309      * @throws XNIException Thrown by handler to signal an error.
310      */
ignorableWhitespace(XMLString text, Augmentations augs)311     public void ignorableWhitespace(XMLString text, Augmentations augs) throws XNIException {
312         if (fDocumentHandler != null) {
313             fDocumentHandler.ignorableWhitespace(text, augs);
314         }
315     } // ignorableWhitespace(XMLString)
316 
317     /**
318      * The start of a CDATA section.
319      *
320      * @throws XNIException Thrown by handler to signal an error.
321      */
startCDATA(Augmentations augs)322     public void startCDATA(Augmentations augs) throws XNIException {
323         if (fDocumentHandler != null) {
324             fDocumentHandler.startCDATA(augs);
325         }
326     } // startCDATA()
327 
328     /**
329      * The end of a CDATA section.
330      *
331      * @throws XNIException Thrown by handler to signal an error.
332      */
endCDATA(Augmentations augs)333     public void endCDATA(Augmentations augs) throws XNIException {
334         if (fDocumentHandler != null) {
335             fDocumentHandler.endCDATA(augs);
336         }
337     } // endCDATA()
338 
339     /**
340      * The end of the document.
341      *
342      * @throws XNIException Thrown by handler to signal an error.
343      */
endDocument(Augmentations augs)344     public void endDocument(Augmentations augs) throws XNIException {
345         if (fDocumentHandler != null) {
346             fDocumentHandler.endDocument(augs);
347         }
348     } // endDocument()
349 
350 
351     /** Sets the document source. */
setDocumentSource(XMLDocumentSource source)352     public void setDocumentSource(XMLDocumentSource source){
353         fDocumentSource = source;
354     }
355 
356 
357     /** Returns the document source. */
getDocumentSource()358     public XMLDocumentSource getDocumentSource(){
359         return fDocumentSource;
360     }
361 
362 } // class PassThroughFilter
363