Package com.itextpdf.tool.xml.parser
Class XMLParser
- java.lang.Object
-
- com.itextpdf.tool.xml.parser.XMLParser
-
public class XMLParser extends java.lang.ObjectReads an XML file. Attach aXMLParserListenerfor receiving events.
-
-
Field Summary
Fields Modifier and Type Field Description private java.nio.charset.Charsetcharsetprivate StateControllercontrollerprivate booleandecodeSpecialCharsprivate java.util.List<XMLParserListener>listenersprivate XMLParserMemorymemoryprivate ParserMonitormonitorprivate Statestateprivate TagStatetagStateprivate java.lang.Stringtext
-
Constructor Summary
Constructors Constructor Description XMLParser()Constructs a default XMLParser ready for HTML/XHTML processing.XMLParser(boolean b, XMLParserListener listener)Constructs a new Parser with the default jvm charset.XMLParser(boolean isHtml, XMLParserListener listener, java.nio.charset.Charset charset)Construct a XMLParser with the given XMLParserConfig.XMLParser(boolean isHtml, java.nio.charset.Charset charset)Constructs a XMLParser.XMLParser(XMLParserListener listener)Constructs a new Parser with HTML parsing set to true and the default jvm charset.XMLParser(XMLParserListener listener, java.nio.charset.Charset charset)Construct an XMLParser with the given XMLParserConfig ready for HTML/XHTML processing..
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description XMLParseraddListener(XMLParserListener pl)If noParserListeneris added, parsing with the parser seems useless no?XMLParserappend(char character)XMLParserappend(char[] bytes)XMLParserappend(java.lang.String string)Appends the given string to the buffer.intbufferSize()java.lang.StringbufferToString()private voidcallText()Call this method to submit the text to listeners.voidcomment()Triggered for comments.java.lang.Stringcurrent()Returns the current content of the text buffer.charcurrentLastChar()java.lang.StringcurrentTag()Get the current tagTagStatecurrentTagState()Get the state of the current tagprivate voidcurrentTagState(TagState state)Set the state of the current tagjava.io.InputStreamReaderdetectEncoding(java.io.InputStream in)Detects encoding from a stream.voidendElement()Triggered when a closing tag has been encountered.voidflush()Flushes the currently stored data in the buffer.java.nio.charset.CharsetgetCharset()Returns the current used character set.booleanisDecodeSpecialChars()XMLParserMemorymemory()Returns the XMLParserMemory.voidparse(java.io.InputStream in)Parse an InputStream with default encoding setvoidparse(java.io.InputStream in, boolean detectEncoding)Parse an InputStream that optionally detects encoding from the streamvoidparse(java.io.InputStream in, java.nio.charset.Charset charSet)Parses an InputStream using the given encodingvoidparse(java.io.Reader reader)Parse an Readerprivate voidparseWithReader(java.io.Reader reader)The actual parse methodXMLParserremoveListener(XMLParserListener pl)Removes a Listener from the list of listeners.StateControllerselectState()The state controller of the parservoidsetDecodeSpecialChars(boolean decodeSpecialChars)Determines whether special chars like > will be decodedvoidsetMonitor(ParserMonitor monitor)protected voidsetState(State state)Set the current state.voidstartElement()Triggered when an opening tag has been encountered.voidtext(java.lang.String bs)Triggered when content has been encountered.voidunknownData()Triggered when the UnknownState encountered anything before encountering a tag.
-
-
-
Field Detail
-
state
private State state
-
controller
private final StateController controller
-
listeners
private final java.util.List<XMLParserListener> listeners
-
memory
private final XMLParserMemory memory
-
monitor
private ParserMonitor monitor
-
text
private java.lang.String text
-
tagState
private TagState tagState
-
charset
private java.nio.charset.Charset charset
-
decodeSpecialChars
private boolean decodeSpecialChars
-
-
Constructor Detail
-
XMLParser
public XMLParser()
Constructs a default XMLParser ready for HTML/XHTML processing.
-
XMLParser
public XMLParser(boolean isHtml, java.nio.charset.Charset charset)Constructs a XMLParser.- Parameters:
isHtml- false if this parser is not going to parse HTML and whitespace should be submitted as text too.charset- charset
-
XMLParser
public XMLParser(XMLParserListener listener, java.nio.charset.Charset charset)
Construct an XMLParser with the given XMLParserConfig ready for HTML/XHTML processing..- Parameters:
listener- the listenercharset- the Charset
-
XMLParser
public XMLParser(boolean isHtml, XMLParserListener listener, java.nio.charset.Charset charset)Construct a XMLParser with the given XMLParserConfig.- Parameters:
isHtml- false if this parser is not going to parse HTML and whitespace should be submitted as text too.listener- the listenercharset- the Charset to use
-
XMLParser
public XMLParser(boolean b, XMLParserListener listener)Constructs a new Parser with the default jvm charset.- Parameters:
b- true if HTML is being parsedlistener- the XMLParserListener
-
XMLParser
public XMLParser(XMLParserListener listener)
Constructs a new Parser with HTML parsing set to true and the default jvm charset.- Parameters:
listener- the XMLParserListener
-
-
Method Detail
-
addListener
public XMLParser addListener(XMLParserListener pl)
If noParserListeneris added, parsing with the parser seems useless no?- Parameters:
pl- theXMLParserListener- Returns:
- the parser
-
removeListener
public XMLParser removeListener(XMLParserListener pl)
Removes a Listener from the list of listeners.- Parameters:
pl- theXMLParserListenerto remove- Returns:
- the parser
-
parse
public void parse(java.io.InputStream in) throws java.io.IOExceptionParse an InputStream with default encoding set- Parameters:
in- the InputStream to parse- Throws:
java.io.IOException- if IO went wrong
-
parse
public void parse(java.io.InputStream in, boolean detectEncoding) throws java.io.IOExceptionParse an InputStream that optionally detects encoding from the stream- Parameters:
in- the InputStream to parsedetectEncoding- true if encoding should be detected from the stream- Throws:
java.io.IOException- if IO went wrong
-
parse
public void parse(java.io.InputStream in, java.nio.charset.Charset charSet) throws java.io.IOExceptionParses an InputStream using the given encoding- Parameters:
in- the stream to readcharSet- to use for the constructed reader.- Throws:
java.io.IOException- if reading fails
-
parse
public void parse(java.io.Reader reader) throws java.io.IOExceptionParse an Reader- Parameters:
reader- the reader- Throws:
java.io.IOException- if IO went wrong
-
parseWithReader
private void parseWithReader(java.io.Reader reader) throws java.io.IOExceptionThe actual parse method- Parameters:
reader-- Throws:
java.io.IOException
-
detectEncoding
public java.io.InputStreamReader detectEncoding(java.io.InputStream in) throws java.io.IOException, java.io.UnsupportedEncodingExceptionDetects encoding from a stream.- Parameters:
in- the stream- Returns:
- a Reader with the deduced encoding.
- Throws:
java.io.IOException- if IO went wrongjava.io.UnsupportedEncodingException- if unsupported encoding was detected
-
setState
protected void setState(State state)
Set the current state.- Parameters:
state- the current state
-
append
public XMLParser append(char character)
- Parameters:
character- the character to append- Returns:
- the parser
-
selectState
public StateController selectState()
The state controller of the parser- Returns:
StateController
-
unknownData
public void unknownData()
Triggered when the UnknownState encountered anything before encountering a tag.
-
flush
public void flush()
Flushes the currently stored data in the buffer.
-
current
public java.lang.String current()
Returns the current content of the text buffer.- Returns:
- current buffer content
-
memory
public XMLParserMemory memory()
Returns the XMLParserMemory.- Returns:
- the memory
-
startElement
public void startElement()
Triggered when an opening tag has been encountered.
-
callText
private void callText()
Call this method to submit the text to listeners.
-
endElement
public void endElement()
Triggered when a closing tag has been encountered.
-
text
public void text(java.lang.String bs)
Triggered when content has been encountered.- Parameters:
bs- the content
-
comment
public void comment()
Triggered for comments.
-
currentLastChar
public char currentLastChar()
- Returns:
- the current last character of the buffer or ' ' if none.
-
currentTag
public java.lang.String currentTag()
Get the current tag- Returns:
- the current tag.
-
currentTagState
public TagState currentTagState()
Get the state of the current tag- Returns:
- the state of the current tag
-
currentTagState
private void currentTagState(TagState state)
Set the state of the current tag- Parameters:
state- the state of the current tag
-
setMonitor
public void setMonitor(ParserMonitor monitor)
- Parameters:
monitor- the monitor to set
-
setDecodeSpecialChars
public void setDecodeSpecialChars(boolean decodeSpecialChars)
Determines whether special chars like > will be decoded- Parameters:
decodeSpecialChars- true to decode, false to not decode
-
isDecodeSpecialChars
public boolean isDecodeSpecialChars()
-
bufferToString
public java.lang.String bufferToString()
- Returns:
- the current buffer as a String
-
append
public XMLParser append(char[] bytes)
- Parameters:
bytes- the byte array to append- Returns:
- this instance of the XMLParser
-
bufferSize
public int bufferSize()
- Returns:
- the size of the buffer
-
append
public XMLParser append(java.lang.String string)
Appends the given string to the buffer.- Parameters:
string- the String to append- Returns:
- this instance of the XMLParser
-
getCharset
public java.nio.charset.Charset getCharset()
Returns the current used character set.- Returns:
- the charset
-
-