public abstract class TextPrefilter extends XMLFilterImpl
<text>
tags. Specifically,
this class is an implementation of XMLFilter
that provides callbacks
that are only active for <text>
tags:
handleStartTextElement(String, String, String, Attributes)
handleTextElementCharacters(char[], int, int)
handleEndTextElement(String, String, String)
All other events are passed along unchanged.
This class's
startElement(String, String, String, Attributes)
endElement(String, String, String)
methods pass all start and end
events along unchanged (including the start and end of a <text>
tag).
The characters(char[], int, int)
method passes all
character events along except those events related to <text>
elements.
characters(char[], int, int)
does not pass along any
of a text
element's content. Instead, all <text>
element content
is passed to the handleTextElementCharacters(char[], int, int)
callback.
This method is responsible for calling sendCharacters(char[],
int, int)
with the desired filtered / transformed content.
(Note that the subclass must call charHelper
and not super.characters
. Calling super.characters
would, in effect, call TextPrefilter.characters()
,
thus causing an infinite recursion.)
Take care when examining <text>
content. The character array parameter to handleTextElementCharacters(char[], int, int)
does not necessarily contain the entire content of the text element. Instead, it contains only
one buffer full of characters (typically a few kilobytes). The XML parser makes no guarantees as to how
and element's content is broken into buffers; thus, the character array may even contain partial lines, words,
or sentences.
See TextSizePrefilter
for a relatively simple implementation.
LanguagePrefilter
is a considerably more complex example because
the filter searches for a string that may get broken across calls to handleTextElementCharacters(char[], int, int)
.
Constructor and Description |
---|
TextPrefilter()
Constructor
|
TextPrefilter(XMLReader parent)
Constructor
|
Modifier and Type | Method and Description |
---|---|
void |
characters(char[] ch,
int start,
int length) |
void |
endElement(String uri,
String localName,
String qName) |
String |
getCurrentTitle()
Return the title of the current page (if available)
|
protected abstract void |
handleEndTextElement(String uri,
String localName,
String qName)
Called when an ending
</text> is reached. |
protected abstract void |
handleStartTextElement(String uri,
String localName,
String qName,
Attributes attrs)
Called at the beginning of an XML
<text> element. |
protected abstract void |
handleTextElementCharacters(char[] ch,
int start,
int length)
Called after the underlying SAX parser reads a set of
<text>
data. |
protected void |
sendCharacters(char[] ch,
int start,
int length)
Sends characters through the filter (i.e., calls
XMLFilterImpl.characters() ). |
void |
startElement(String uri,
String localName,
String qName,
Attributes attrs) |
endDocument, endPrefixMapping, error, fatalError, getContentHandler, getDTDHandler, getEntityResolver, getErrorHandler, getFeature, getParent, getProperty, ignorableWhitespace, notationDecl, parse, parse, processingInstruction, resolveEntity, setContentHandler, setDocumentLocator, setDTDHandler, setEntityResolver, setErrorHandler, setFeature, setParent, setProperty, skippedEntity, startDocument, startPrefixMapping, unparsedEntityDecl, warning
public TextPrefilter()
public TextPrefilter(XMLReader parent)
parent
- the parent XML readerpublic String getCurrentTitle()
null
if not available
(e.g., because parser is not currently inside a page
element).protected abstract void handleStartTextElement(String uri, String localName, String qName, Attributes attrs) throws SAXException
<text>
element. See XMLFilterImpl
.uri
- The element's Namespace URI, or the empty string.localName
- The element's local name, or the empty string.qName
- The element's qualified (prefixed) name, or the empty string.attrs
- The element's attributes.SAXException
- The client may throw an exception during processing.public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException
startElement
in interface ContentHandler
startElement
in class XMLFilterImpl
SAXException
protected abstract void handleTextElementCharacters(char[] ch, int start, int length) throws SAXException
<text>
data. See XMLFilterImpl
.ch
- An array of characters.start
- The starting position in the array.length
- The number of characters to use from the array.SAXException
- The client may throw an exception during processing.public void characters(char[] ch, int start, int length) throws SAXException
characters
in interface ContentHandler
characters
in class XMLFilterImpl
SAXException
protected void sendCharacters(char[] ch, int start, int length) throws SAXException
XMLFilterImpl.characters()
). Note that
calling super.characters()
from a subclass won't pass characters through the filter. It will
make a recursive call to characters(char[], int, int)
characters}.ch
- An array of characters.start
- The starting position in the array.length
- The number of characters to use from the array.SAXException
- The client may throw an exception during processing.protected abstract void handleEndTextElement(String uri, String localName, String qName) throws SAXException
</text>
is reached.uri
- The element's Namespace URI, or the empty string.localName
- The element's local name, or the empty string.qName
- The element's qualified (prefixed) name, or the empty string.SAXException
- The client may throw an exception during processing.public void endElement(String uri, String localName, String qName) throws SAXException
endElement
in interface ContentHandler
endElement
in class XMLFilterImpl
SAXException