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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
20
import org.apache.xerces.xni.Augmentations;
21
import org.apache.xerces.xni.QName;
22
import org.apache.xerces.xni.NamespaceContext;
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;
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.
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.
41
* @author Andy Clark, IBM
43
* @version $Id: PassThroughFilter.java,v 1.2 2009/12/10 03:18:44 matthewoliver Exp $
45
public class PassThroughFilter
46
implements XMLDocumentHandler {
52
/** The document handler. */
53
protected XMLDocumentHandler fDocumentHandler;
55
/** The document source */
56
protected XMLDocumentSource fDocumentSource;
63
* Sets the document handler.
65
* @param handler The new document handler.
67
public void setDocumentHandler(XMLDocumentHandler handler) {
68
fDocumentHandler = handler;
69
} // setDocumentHandler(XMLDocumentHandler)
72
// XMLDocumentHandler methods
76
* The start of the document.
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
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).
90
* @throws XNIException Thrown by handler to signal an error.
92
public void startDocument(XMLLocator locator, String encoding,
93
NamespaceContext namespaceContext, Augmentations augs)
95
if (fDocumentHandler != null) {
96
fDocumentHandler.startDocument(locator, encoding, namespaceContext, augs);
98
} // startDocument(XMLLocator,String)
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.
105
* @param version The XML version.
106
* @param encoding The IANA encoding name of the document, or null if
108
* @param standalone The standalone value, or null if not specified.
110
* @throws XNIException Thrown by handler to signal an error.
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);
117
} // xmlDecl(String,String,String
120
* Notifies of the presence of the DOCTYPE line in the document.
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
128
* @throws XNIException Thrown by handler to signal an error.
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);
135
} // doctypeDecl(String,String,String)
140
* @param text The text in the comment.
142
* @throws XNIException Thrown by application to signal an error.
144
public void comment(XMLString text, Augmentations augs) throws XNIException {
145
if (fDocumentHandler != null) {
146
fDocumentHandler.comment(text, augs);
148
} // comment(XMLString)
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.
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.
161
* @param target The target.
162
* @param data The data or null if none specified.
164
* @throws XNIException Thrown by handler to signal an error.
166
public void processingInstruction(String target, XMLString data, Augmentations augs)
167
throws XNIException {
168
if (fDocumentHandler != null) {
169
fDocumentHandler.processingInstruction(target, data, augs);
171
} // processingInstruction(String,XMLString)
174
* The start of an element.
176
* @param element The name of the element.
177
* @param attributes The element attributes.
179
* @throws XNIException Thrown by handler to signal an error.
181
public void startElement(QName element, XMLAttributes attributes, Augmentations augs)
182
throws XNIException {
183
if (fDocumentHandler != null) {
184
fDocumentHandler.startElement(element, attributes, augs);
186
} // startElement(QName,XMLAttributes)
191
* @param element The name of the element.
192
* @param attributes The element attributes.
194
* @throws XNIException Thrown by handler to signal an error.
196
public void emptyElement(QName element, XMLAttributes attributes, Augmentations augs)
197
throws XNIException {
198
if (fDocumentHandler != null) {
199
fDocumentHandler.emptyElement(element, attributes, augs);
201
} // emptyElement(QName,XMLAttributes)
204
* The end of an element.
206
* @param element The name of the element.
208
* @throws XNIException Thrown by handler to signal an error.
210
public void endElement(QName element, Augmentations augs)
211
throws XNIException {
212
if (fDocumentHandler != null) {
213
fDocumentHandler.endElement(element, augs);
215
} // endElement(QName)
218
* This method notifies the start of an entity.
220
* <strong>Note:</strong> This method is not called for entity references
221
* appearing as part of attribute values.
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).
236
* @throws XNIException Thrown by handler to signal an error.
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);
245
} // startGeneralEntity(String,XMLResourceIdentifier,String,Augmentations)
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.
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.
255
* <strong>Note:</strong> This method is not called for entity references
256
* appearing as part of attribute values.
258
* @param version The XML version, or null if not specified.
259
* @param encoding The IANA encoding name of the entity.
261
* @throws XNIException Thrown by handler to signal an error.
263
public void textDecl(String version, String encoding, Augmentations augs)
264
throws XNIException {
265
if (fDocumentHandler != null) {
266
fDocumentHandler.textDecl(version, encoding, augs);
268
} // textDecl(String,String)
271
* This method notifies the end of an entity.
273
* <strong>Note:</strong> This method is not called for entity references
274
* appearing as part of attribute values.
276
* @param name The name of the entity.
278
* @throws XNIException Thrown by handler to signal an error.
280
public void endGeneralEntity(String name, Augmentations augs) throws XNIException {
281
if (fDocumentHandler != null) {
282
fDocumentHandler.endGeneralEntity(name, augs);
284
} // endGeneralEntity(String,Augmentations)
289
* @param text The content.
291
* @throws XNIException Thrown by handler to signal an error.
293
public void characters(XMLString text, Augmentations augs) throws XNIException {
294
if (fDocumentHandler != null) {
295
fDocumentHandler.characters(text, augs);
297
} // characters(XMLString)
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
307
* @param text The ignorable whitespace.
309
* @throws XNIException Thrown by handler to signal an error.
311
public void ignorableWhitespace(XMLString text, Augmentations augs) throws XNIException {
312
if (fDocumentHandler != null) {
313
fDocumentHandler.ignorableWhitespace(text, augs);
315
} // ignorableWhitespace(XMLString)
318
* The start of a CDATA section.
320
* @throws XNIException Thrown by handler to signal an error.
322
public void startCDATA(Augmentations augs) throws XNIException {
323
if (fDocumentHandler != null) {
324
fDocumentHandler.startCDATA(augs);
329
* The end of a CDATA section.
331
* @throws XNIException Thrown by handler to signal an error.
333
public void endCDATA(Augmentations augs) throws XNIException {
334
if (fDocumentHandler != null) {
335
fDocumentHandler.endCDATA(augs);
340
* The end of the document.
342
* @throws XNIException Thrown by handler to signal an error.
344
public void endDocument(Augmentations augs) throws XNIException {
345
if (fDocumentHandler != null) {
346
fDocumentHandler.endDocument(augs);
351
/** Sets the document source. */
352
public void setDocumentSource(XMLDocumentSource source){
353
fDocumentSource = source;
357
/** Returns the document source. */
358
public XMLDocumentSource getDocumentSource(){
359
return fDocumentSource;
362
} // class PassThroughFilter