Class SimpleXMLParser
java.lang.Object
org.openpdf.text.xml.simpleparser.SimpleXMLParser
A simple XML and HTML parser. This parser is, like the SAX parser, an event based parser, but with much less
functionality.
The parser can:
- It recognizes the encoding used
- It recognizes all the elements' start tags and end tags
- It lists attributes, where attribute values can be enclosed in single or double quotes
- It recognizes the
<[CDATA[ ... ]]>construct - It recognizes the standard entities: &, <, >, ", and ', as well as numeric entities
- It maps lines ending in
\r\nand\rto\non input, in accordance with the XML Specification, Section 2.11
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intprivate static final intprivate static final int(package private) Stringthe attribute key.current attributes(package private) Stringthe attribute value.private static final int(package private) intThe current character.(package private) intthe column where the current character occurs(package private) SimpleXMLDocHandlerCommentThe handler to which we are going to forward comments.private static final int(package private) SimpleXMLDocHandlerThe handler to which we are going to forward document content(package private) StringBuffercurrent entity (whatever is encountered between & and ;)private static final int(package private) booleanwas the last character equivalent to a newline?private static final int(package private) booleanAre we parsing HTML?private static final int(package private) intthe line we are currently reading(package private) intKeeps track of the number of tags that are open.(package private) booleanA boolean indicating if the next character should be taken into account if it's a space character.private static final int(package private) intThe previous character.private static final int(package private) intthe quote character that was used to open the quote.private static final intthe state stack(package private) intthe current state(package private) Stringcurrent tagnameprivate static final intprivate static final int(package private) StringBuffercurrent text (whatever is encountered between tags)private static final intprivate static final intpossible states -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateSimpleXMLParser(SimpleXMLDocHandler doc, SimpleXMLDocHandlerComment comment, boolean html) Creates a Simple XML parser object. -
Method Summary
Modifier and TypeMethodDescriptiondetectCharsetFromBOM(byte[] bom) Detect charset from BOM, as per Unicode FAQ.private voiddoTag()Sets the name of the tag.private voidflush()Flushes the text that is currently in the buffer.private static StringgetDeclaredEncoding(String decl) private voidDoes the actual parsing.private voidinitTag()Initialized the tag name and attributes.static voidparse(SimpleXMLDocHandler doc, InputStream in) Parses the XML document firing the events to the handler.static voidparse(SimpleXMLDocHandler doc, Reader r) static voidparse(SimpleXMLDocHandler doc, SimpleXMLDocHandlerComment comment, Reader r, boolean html) Parses the XML document firing the events to the handler.private voidprocessTag(boolean start) processes the tag.private intGets a state from the stackprivate voidsaveState(int s) Adds a state to the stack.private voidThrows an exception
-
Field Details
-
UNKNOWN
private static final int UNKNOWNpossible states- See Also:
-
TEXT
private static final int TEXT- See Also:
-
TAG_ENCOUNTERED
private static final int TAG_ENCOUNTERED- See Also:
-
EXAMIN_TAG
private static final int EXAMIN_TAG- See Also:
-
TAG_EXAMINED
private static final int TAG_EXAMINED- See Also:
-
IN_CLOSETAG
private static final int IN_CLOSETAG- See Also:
-
SINGLE_TAG
private static final int SINGLE_TAG- See Also:
-
CDATA
private static final int CDATA- See Also:
-
COMMENT
private static final int COMMENT- See Also:
-
PI
private static final int PI- See Also:
-
ENTITY
private static final int ENTITY- See Also:
-
QUOTE
private static final int QUOTE- See Also:
-
ATTRIBUTE_KEY
private static final int ATTRIBUTE_KEY- See Also:
-
ATTRIBUTE_EQUAL
private static final int ATTRIBUTE_EQUAL- See Also:
-
ATTRIBUTE_VALUE
private static final int ATTRIBUTE_VALUE- See Also:
-
stack
the state stack -
character
int characterThe current character. -
previousCharacter
int previousCharacterThe previous character. -
lines
int linesthe line we are currently reading -
columns
int columnsthe column where the current character occurs -
eol
boolean eolwas the last character equivalent to a newline? -
nowhite
boolean nowhiteA boolean indicating if the next character should be taken into account if it's a space character. When nospace is false, the previous character wasn't whitespace.- Since:
- 2.1.5
-
state
int statethe current state -
html
boolean htmlAre we parsing HTML? -
text
StringBuffer textcurrent text (whatever is encountered between tags) -
entity
StringBuffer entitycurrent entity (whatever is encountered between & and ;) -
tag
String tagcurrent tagname -
attributes
current attributes -
doc
The handler to which we are going to forward document content -
comment
SimpleXMLDocHandlerComment commentThe handler to which we are going to forward comments. -
nested
int nestedKeeps track of the number of tags that are open. -
quoteCharacter
int quoteCharacterthe quote character that was used to open the quote. -
attributekey
String attributekeythe attribute key. -
attributevalue
String attributevaluethe attribute value.
-
-
Constructor Details
-
SimpleXMLParser
Creates a Simple XML parser object. Call go(BufferedReader) immediately after creation.
-
-
Method Details
-
parse
public static void parse(SimpleXMLDocHandler doc, SimpleXMLDocHandlerComment comment, Reader r, boolean html) throws IOException Parses the XML document firing the events to the handler.- Parameters:
doc- the document handlercomment-commentr- the document. The encoding is already resolved. The reader is not closedhtml-html- Throws:
IOException- on error
-
detectCharsetFromBOM
Detect charset from BOM, as per Unicode FAQ. -
parse
Parses the XML document firing the events to the handler.- Parameters:
doc- the document handlerin- the document. The encoding is deduced from the stream. The stream is not closed- Throws:
IOException- on error
-
getDeclaredEncoding
-
parse
- Throws:
IOException
-
go
Does the actual parsing. Perform this immediately after creating the parser object.- Throws:
IOException
-
restoreState
private int restoreState()Gets a state from the stack- Returns:
- the previous state
-
saveState
private void saveState(int s) Adds a state to the stack.- Parameters:
s- a state to add to the stack
-
flush
private void flush()Flushes the text that is currently in the buffer. The text can be ignored, added to the document as content or as comment,... depending on the current state. -
initTag
private void initTag()Initialized the tag name and attributes. -
doTag
private void doTag()Sets the name of the tag. -
processTag
private void processTag(boolean start) processes the tag.- Parameters:
start- if true we are dealing with a tag that has just been opened; if false we are closing a tag.
-
throwException
Throws an exception- Throws:
IOException
-