public class SafeContentHandler extends ContentHandlerDecorator
characters(char[], int, int)
or
ignorableWhitespace(char[], int, int)
) passed to the decorated
content handler contain only valid XML characters. All invalid characters
are replaced with the Unicode replacement character U+FFFD (though a
subclass may change this by overriding the writeReplacement(Output)
method).
The XML standard defines the following Unicode character ranges as valid XML characters:
#x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
Note that currently this class only detects those invalid characters whose UTF-16 representation fits a single char. Also, this class does not ensure that the UTF-16 encoding of incoming characters is correct.
Modifier and Type | Class and Description |
---|---|
protected static interface |
SafeContentHandler.Output
Internal interface that allows both character and
ignorable whitespace content to be filtered the same way.
|
Constructor and Description |
---|
SafeContentHandler(ContentHandler handler) |
Modifier and Type | Method and Description |
---|---|
void |
characters(char[] ch,
int start,
int length) |
void |
endDocument() |
void |
endElement(String uri,
String localName,
String name) |
void |
ignorableWhitespace(char[] ch,
int start,
int length) |
protected boolean |
isInvalid(int ch)
Checks whether the given Unicode character is an invalid XML character
and should be replaced for output.
|
void |
startElement(String uri,
String localName,
String name,
Attributes atts) |
protected void |
writeReplacement(SafeContentHandler.Output output)
Outputs the replacement for an invalid character.
|
endPrefixMapping, handleException, processingInstruction, setContentHandler, setDocumentLocator, skippedEntity, startDocument, startPrefixMapping, toString
error, fatalError, notationDecl, resolveEntity, unparsedEntityDecl, warning
public SafeContentHandler(ContentHandler handler)
protected boolean isInvalid(int ch)
Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
ch
- charactertrue
if the character should be replaced,
false
otherwiseprotected void writeReplacement(SafeContentHandler.Output output) throws SAXException
output
- where the replacement is written toSAXException
- if the replacement could not be writtenpublic void startElement(String uri, String localName, String name, Attributes atts) throws SAXException
startElement
in interface ContentHandler
startElement
in class ContentHandlerDecorator
SAXException
public void endElement(String uri, String localName, String name) throws SAXException
endElement
in interface ContentHandler
endElement
in class ContentHandlerDecorator
SAXException
public void endDocument() throws SAXException
endDocument
in interface ContentHandler
endDocument
in class ContentHandlerDecorator
SAXException
public void characters(char[] ch, int start, int length) throws SAXException
characters
in interface ContentHandler
characters
in class ContentHandlerDecorator
SAXException
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException
ignorableWhitespace
in interface ContentHandler
ignorableWhitespace
in class ContentHandlerDecorator
SAXException
Copyright © 2007–2023 The Apache Software Foundation. All rights reserved.