Class MXParser
- java.lang.Object
-
- org.jboss.modules.xml.MXParser
-
- All Implemented Interfaces:
XmlPullParser
public class MXParser extends java.lang.Object implements XmlPullParser
Absolutely minimal implementation of XMLPULL V1 API- Author:
- Aleksander Slominski
-
-
Field Summary
Fields Modifier and Type Field Description protected boolean
allStringsInterned
Implementation notice: the is instance variable that controls if newString() is interning.protected int
attributeCount
protected java.lang.String[]
attributeName
protected int[]
attributeNameHash
protected java.lang.String[]
attributePrefix
protected java.lang.String[]
attributeUri
protected java.lang.String[]
attributeValue
protected char[]
buf
protected int
bufAbsoluteStart
protected int
bufEnd
protected int
bufLoadFactor
protected int
bufSoftLimit
protected int
bufStart
protected int
columnNumber
protected int
depth
protected java.lang.String[]
elName
protected int[]
elNamespaceCount
protected java.lang.String[]
elPrefix
protected char[][]
elRawName
protected int[]
elRawNameEnd
protected int[]
elRawNameLine
protected java.lang.String[]
elUri
protected boolean
emptyElementTag
protected int
entityEnd
protected java.lang.String[]
entityName
protected char[][]
entityNameBuf
protected int[]
entityNameHash
protected java.lang.String
entityRefName
protected java.lang.String[]
entityReplacement
protected char[][]
entityReplacementBuf
protected int
eventType
protected static java.lang.String
FEATURE_NAMES_INTERNED
protected static java.lang.String
FEATURE_XML_ROUNDTRIP
protected char[]
charRefOneCharBuf
protected java.lang.String
inputEncoding
protected java.io.InputStream
inputStream
protected int
lineNumber
protected java.lang.String
location
protected static int
LOOKUP_MAX
protected static char
LOOKUP_MAX_CHAR
protected static boolean[]
lookupNameChar
protected static boolean[]
lookupNameStartChar
protected int
namespaceEnd
protected java.lang.String[]
namespacePrefix
protected int[]
namespacePrefixHash
protected java.lang.String[]
namespaceUri
protected static char[]
NCODING
protected static char[]
NO
protected boolean
pastEndTag
protected char[]
pc
protected int
pcEnd
protected int
pcStart
protected int
pos
protected int
posEnd
protected int
posStart
protected boolean
preventBufferCompaction
protected boolean
processNamespaces
protected static java.lang.String
PROPERTY_LOCATION
protected static java.lang.String
PROPERTY_XMLDECL_CONTENT
protected static java.lang.String
PROPERTY_XMLDECL_STANDALONE
protected static java.lang.String
PROPERTY_XMLDECL_VERSION
protected static int
READ_CHUNK_SIZE
protected java.io.Reader
reader
protected boolean
reachedEnd
protected boolean
roundtripSupported
protected boolean
seenAmpersand
protected boolean
seenDocdecl
protected boolean
seenEndTag
protected boolean
seenMarkup
protected boolean
seenRoot
protected boolean
seenStartTag
protected static char[]
TANDALONE
protected java.lang.String
text
protected boolean
tokenize
protected boolean
usePC
protected static char[]
VERSION
protected static java.lang.String
XML_URI
protected java.lang.String
xmlDeclContent
protected java.lang.Boolean
xmlDeclStandalone
protected java.lang.String
xmlDeclVersion
protected static java.lang.String
XMLNS_URI
protected static char[]
YES
-
Fields inherited from interface org.jboss.modules.xml.XmlPullParser
CDSECT, COMMENT, DOCDECL, END_DOCUMENT, END_TAG, ENTITY_REF, FEATURE_PROCESS_DOCDECL, FEATURE_PROCESS_NAMESPACES, FEATURE_REPORT_NAMESPACE_ATTRIBUTES, FEATURE_VALIDATION, IGNORABLE_WHITESPACE, NO_NAMESPACE, PROCESSING_INSTRUCTION, START_DOCUMENT, START_TAG, TEXT, TYPES
-
-
Constructor Summary
Constructors Constructor Description MXParser()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
defineEntityReplacementText(java.lang.String entityName, java.lang.String replacementText)
Set new value for entity replacement text as defined in XML 1.0 Section 4.5 Construction of Internal Entity Replacement Text.protected void
ensureAttributesCapacity(int size)
Make sure that in attributes temporary array is enough space.protected void
ensureElementsCapacity()
Make sure that we have enough space to keep element stack if passed size.protected void
ensureEntityCapacity()
protected void
ensureNamespacesCapacity(int size)
protected void
ensurePC(int end)
protected static int
fastHash(char[] ch, int off, int len)
simplistic implementation of hash function that has constant time to compute - so it also means diminishing hash quality for long strings but for XML parsing it should be good enough ...protected void
fillBuf()
int
getAttributeCount()
Returns the number of attributes of the current start tag, or -1 if the current event type is not START_TAGjava.lang.String
getAttributeName(int index)
Returns the local name of the specified attribute if namespaces are enabled or just attribute name if namespaces are disabled.java.lang.String
getAttributeNamespace(int index)
Returns the namespace URI of the attribute with the given index (starts from 0).java.lang.String
getAttributePrefix(int index)
Returns the prefix of the specified attribute Returns null if the element has no prefix.java.lang.String
getAttributeType(int index)
Returns the type of the specified attribute If parser is non-validating it MUST return CDATA.java.lang.String
getAttributeValue(int index)
Returns the given attributes value.java.lang.String
getAttributeValue(java.lang.String namespace, java.lang.String name)
Returns the attributes value identified by namespace URI and namespace localName.int
getColumnNumber()
Returns the current column number, starting from 0.int
getDepth()
Returns the current depth of the element.int
getEventType()
Returns the type of the current event (START_TAG, END_TAG, TEXT, etc.)boolean
getFeature(java.lang.String name)
Unknown properties are always returned as falsejava.lang.String
getInputEncoding()
Returns the input encoding if known, null otherwise.int
getLineNumber()
Returns the current line number, starting from 1.java.lang.String
getName()
For START_TAG or END_TAG events, the (local) name of the current element is returned when namespaces are enabled.java.lang.String
getNamespace()
Returns the namespace URI of the current element.java.lang.String
getNamespace(java.lang.String prefix)
Returns the URI corresponding to the given prefix, depending on current state of the parser.int
getNamespaceCount(int depth)
Returns the numbers of elements in the namespace stack for the given depth.java.lang.String
getNamespacePrefix(int pos)
Returns the namespace prefixe for the given position in the namespace stack.java.lang.String
getNamespaceUri(int pos)
Returns the namespace URI for the given position in the namespace stack If the position is out of range, an exception is thrown.java.lang.String
getPositionDescription()
Return string describing current position of parsers as text 'STATE [seen %s...]java.lang.String
getPrefix()
Returns the prefix of the current element.java.lang.Object
getProperty(java.lang.String name)
Look up the value of a property.java.lang.String
getText()
Returns the text content of the current event as String.char[]
getTextCharacters(int[] holderForStartAndLength)
Returns the buffer that contains the text of the current event, as well as the start offset and length relevant for the current event.boolean
isAttributeDefault(int index)
Returns if the specified attribute was not in input was declared in XML.boolean
isEmptyElementTag()
Returns true if the current event is START_TAG and the tag is degenerated (e.g.protected boolean
isNameChar(char ch)
protected boolean
isNameStartChar(char ch)
protected boolean
isS(char ch)
boolean
isWhitespace()
Checks whether the current TEXT event contains only whitespace characters.protected void
joinPC()
protected char[]
lookuEntityReplacement(int entitNameLen)
protected char
more()
protected java.lang.String
newString(char[] cbuf, int off, int len)
protected java.lang.String
newStringIntern(char[] cbuf, int off, int len)
int
next()
Get next parsing event - element content wil be coalesced and only one TEXT event must be returned for whole element content (comments and processing instructions will be ignored and emtity references must be expanded or exception mus be thrown if entity reerence can not be exapnded).protected int
nextImpl()
int
nextTag()
Call next() and return event if it is START_TAG or END_TAG otherwise throw an exception.java.lang.String
nextText()
If current event is START_TAG then if next element is TEXT then element content is returned or if next event is END_TAG then empty string is returned, otherwise exception is thrown.int
nextToken()
This method works similarly to next() but will expose additional event types (COMMENT, CDSECT, DOCDECL, ENTITY_REF, PROCESSING_INSTRUCTION, or IGNORABLE_WHITESPACE) if they are available in input.protected char
parseAttribute()
protected void
parseCDSect(boolean hadCharData)
protected void
parseComment()
protected void
parseDocdecl()
int
parseEndTag()
protected char[]
parseEntityRef()
protected int
parseEpilog()
protected boolean
parsePI()
protected int
parseProlog()
int
parseStartTag()
protected void
parseXmlDecl(char ch)
protected void
parseXmlDeclWithVersion(int versionStart, int versionEnd)
protected java.lang.String
printable(char ch)
protected java.lang.String
printable(java.lang.String s)
void
require(int type, java.lang.String namespace, java.lang.String name)
Test if the current event is of the given type and if the namespace and name do match. null will match any namespace and any name.protected char
requireInput(char ch, char[] input)
protected char
requireNextS()
protected void
reset()
protected void
resetStringCache()
void
setFeature(java.lang.String name, boolean state)
Method setFeaturevoid
setInput(java.io.InputStream inputStream, java.lang.String inputEncoding)
Sets the input stream the parser is going to process.void
setInput(java.io.Reader in)
Set the input source for parser to the given reader and resets the parser.void
setProperty(java.lang.String name, java.lang.Object value)
Set the value of a property.protected char
skipS(char ch)
void
skipSubTree()
Skip sub tree that is currently parser positioned on.
-
-
-
Field Detail
-
XML_URI
protected static final java.lang.String XML_URI
- See Also:
- Constant Field Values
-
XMLNS_URI
protected static final java.lang.String XMLNS_URI
- See Also:
- Constant Field Values
-
FEATURE_XML_ROUNDTRIP
protected static final java.lang.String FEATURE_XML_ROUNDTRIP
- See Also:
- Constant Field Values
-
FEATURE_NAMES_INTERNED
protected static final java.lang.String FEATURE_NAMES_INTERNED
- See Also:
- Constant Field Values
-
PROPERTY_XMLDECL_VERSION
protected static final java.lang.String PROPERTY_XMLDECL_VERSION
- See Also:
- Constant Field Values
-
PROPERTY_XMLDECL_STANDALONE
protected static final java.lang.String PROPERTY_XMLDECL_STANDALONE
- See Also:
- Constant Field Values
-
PROPERTY_XMLDECL_CONTENT
protected static final java.lang.String PROPERTY_XMLDECL_CONTENT
- See Also:
- Constant Field Values
-
PROPERTY_LOCATION
protected static final java.lang.String PROPERTY_LOCATION
- See Also:
- Constant Field Values
-
allStringsInterned
protected boolean allStringsInterned
Implementation notice: the is instance variable that controls if newString() is interning.NOTE: newStringIntern always returns interned strings and newString MAY return interned String depending on this variable.
NOTE: by default in this minimal implementation it is false!
-
processNamespaces
protected boolean processNamespaces
-
roundtripSupported
protected boolean roundtripSupported
-
location
protected java.lang.String location
-
lineNumber
protected int lineNumber
-
columnNumber
protected int columnNumber
-
seenRoot
protected boolean seenRoot
-
reachedEnd
protected boolean reachedEnd
-
eventType
protected int eventType
-
emptyElementTag
protected boolean emptyElementTag
-
depth
protected int depth
-
elRawName
protected char[][] elRawName
-
elRawNameEnd
protected int[] elRawNameEnd
-
elRawNameLine
protected int[] elRawNameLine
-
elName
protected java.lang.String[] elName
-
elPrefix
protected java.lang.String[] elPrefix
-
elUri
protected java.lang.String[] elUri
-
elNamespaceCount
protected int[] elNamespaceCount
-
attributeCount
protected int attributeCount
-
attributeName
protected java.lang.String[] attributeName
-
attributeNameHash
protected int[] attributeNameHash
-
attributePrefix
protected java.lang.String[] attributePrefix
-
attributeUri
protected java.lang.String[] attributeUri
-
attributeValue
protected java.lang.String[] attributeValue
-
namespaceEnd
protected int namespaceEnd
-
namespacePrefix
protected java.lang.String[] namespacePrefix
-
namespacePrefixHash
protected int[] namespacePrefixHash
-
namespaceUri
protected java.lang.String[] namespaceUri
-
entityEnd
protected int entityEnd
-
entityName
protected java.lang.String[] entityName
-
entityNameBuf
protected char[][] entityNameBuf
-
entityReplacement
protected java.lang.String[] entityReplacement
-
entityReplacementBuf
protected char[][] entityReplacementBuf
-
entityNameHash
protected int[] entityNameHash
-
READ_CHUNK_SIZE
protected static final int READ_CHUNK_SIZE
- See Also:
- Constant Field Values
-
reader
protected java.io.Reader reader
-
inputEncoding
protected java.lang.String inputEncoding
-
inputStream
protected java.io.InputStream inputStream
-
bufLoadFactor
protected int bufLoadFactor
-
buf
protected char[] buf
-
bufSoftLimit
protected int bufSoftLimit
-
preventBufferCompaction
protected boolean preventBufferCompaction
-
bufAbsoluteStart
protected int bufAbsoluteStart
-
bufStart
protected int bufStart
-
bufEnd
protected int bufEnd
-
pos
protected int pos
-
posStart
protected int posStart
-
posEnd
protected int posEnd
-
pc
protected char[] pc
-
pcStart
protected int pcStart
-
pcEnd
protected int pcEnd
-
usePC
protected boolean usePC
-
seenStartTag
protected boolean seenStartTag
-
seenEndTag
protected boolean seenEndTag
-
pastEndTag
protected boolean pastEndTag
-
seenAmpersand
protected boolean seenAmpersand
-
seenMarkup
protected boolean seenMarkup
-
seenDocdecl
protected boolean seenDocdecl
-
tokenize
protected boolean tokenize
-
text
protected java.lang.String text
-
entityRefName
protected java.lang.String entityRefName
-
xmlDeclVersion
protected java.lang.String xmlDeclVersion
-
xmlDeclStandalone
protected java.lang.Boolean xmlDeclStandalone
-
xmlDeclContent
protected java.lang.String xmlDeclContent
-
charRefOneCharBuf
protected char[] charRefOneCharBuf
-
VERSION
protected static final char[] VERSION
-
NCODING
protected static final char[] NCODING
-
TANDALONE
protected static final char[] TANDALONE
-
YES
protected static final char[] YES
-
NO
protected static final char[] NO
-
LOOKUP_MAX
protected static final int LOOKUP_MAX
- See Also:
- Constant Field Values
-
LOOKUP_MAX_CHAR
protected static final char LOOKUP_MAX_CHAR
- See Also:
- Constant Field Values
-
lookupNameStartChar
protected static boolean[] lookupNameStartChar
-
lookupNameChar
protected static boolean[] lookupNameChar
-
-
Method Detail
-
resetStringCache
protected void resetStringCache()
-
newString
protected java.lang.String newString(char[] cbuf, int off, int len)
-
newStringIntern
protected java.lang.String newStringIntern(char[] cbuf, int off, int len)
-
ensureElementsCapacity
protected void ensureElementsCapacity()
Make sure that we have enough space to keep element stack if passed size. It will always create one additional slot then current depth
-
ensureAttributesCapacity
protected void ensureAttributesCapacity(int size)
Make sure that in attributes temporary array is enough space.
-
ensureNamespacesCapacity
protected void ensureNamespacesCapacity(int size)
-
fastHash
protected static final int fastHash(char[] ch, int off, int len)
simplistic implementation of hash function that has constant time to compute - so it also means diminishing hash quality for long strings but for XML parsing it should be good enough ...
-
ensureEntityCapacity
protected void ensureEntityCapacity()
-
reset
protected void reset()
-
setFeature
public void setFeature(java.lang.String name, boolean state) throws XmlPullParserException
Method setFeature- Specified by:
setFeature
in interfaceXmlPullParser
- Parameters:
name
- a Stringstate
- a boolean- Throws:
XmlPullParserException
-
getFeature
public boolean getFeature(java.lang.String name)
Unknown properties are always returned as false- Specified by:
getFeature
in interfaceXmlPullParser
- Parameters:
name
- The name of feature to be retrieved.- Returns:
- The value of the feature.
-
setProperty
public void setProperty(java.lang.String name, java.lang.Object value) throws XmlPullParserException
Description copied from interface:XmlPullParser
Set the value of a property. The property name is any fully-qualified URI.- Specified by:
setProperty
in interfaceXmlPullParser
- Throws:
XmlPullParserException
- If the property is not supported or can not be set
-
getProperty
public java.lang.Object getProperty(java.lang.String name)
Description copied from interface:XmlPullParser
Look up the value of a property. The property name is any fully-qualified URI.NOTE: unknown properties are always returned as null.
- Specified by:
getProperty
in interfaceXmlPullParser
- Parameters:
name
- The name of property to be retrieved.- Returns:
- The value of named property.
-
setInput
public void setInput(java.io.Reader in) throws XmlPullParserException
Description copied from interface:XmlPullParser
Set the input source for parser to the given reader and resets the parser. The event type is set to the initial value START_DOCUMENT. Setting the reader to null will just stop parsing and reset parser state, allowing the parser to free internal resources such as parsing buffers.- Specified by:
setInput
in interfaceXmlPullParser
- Throws:
XmlPullParserException
-
setInput
public void setInput(java.io.InputStream inputStream, java.lang.String inputEncoding) throws XmlPullParserException
Description copied from interface:XmlPullParser
Sets the input stream the parser is going to process. This call resets the parser state and sets the event type to the initial value START_DOCUMENT.NOTE: If an input encoding string is passed, it MUST be used. Otherwise, if inputEncoding is null, the parser SHOULD try to determine input encoding following XML 1.0 specification (see below). If encoding detection is supported then following feature http://xmlpull.org/v1/doc/features.html#detect-encoding MUST be true amd otherwise it must be false
- Specified by:
setInput
in interfaceXmlPullParser
- Parameters:
inputStream
- contains a raw byte input stream of possibly unknown encoding (when inputEncoding is null).inputEncoding
- if not null it MUST be used as encoding for inputStream- Throws:
XmlPullParserException
-
getInputEncoding
public java.lang.String getInputEncoding()
Description copied from interface:XmlPullParser
Returns the input encoding if known, null otherwise. If setInput(InputStream, inputEncoding) was called with an inputEncoding value other than null, this value must be returned from this method. Otherwise, if inputEncoding is null and the parser suppports the encoding detection feature (http://xmlpull.org/v1/doc/features.html#detect-encoding), it must return the detected encoding. If setInput(Reader) was called, null is returned. After first call to next if XML declaration was present this method will return encoding declared.- Specified by:
getInputEncoding
in interfaceXmlPullParser
-
defineEntityReplacementText
public void defineEntityReplacementText(java.lang.String entityName, java.lang.String replacementText) throws XmlPullParserException
Description copied from interface:XmlPullParser
Set new value for entity replacement text as defined in XML 1.0 Section 4.5 Construction of Internal Entity Replacement Text. If FEATURE_PROCESS_DOCDECL or FEATURE_VALIDATION are set, calling this function will result in an exception -- when processing of DOCDECL is enabled, there is no need to the entity replacement text manually.The motivation for this function is to allow very small implementations of XMLPULL that will work in J2ME environments. Though these implementations may not be able to process the document type declaration, they still can work with known DTDs by using this function.
Please notes: The given value is used literally as replacement text and it corresponds to declaring entity in DTD that has all special characters escaped: left angle bracket is replaced with <, ampersnad with & and so on.
Note: The given value is the literal replacement text and must not contain any other entity reference (if it contains any entity reference there will be no further replacement).
Note: The list of pre-defined entity names will always contain standard XML entities such as amp (&), lt (<), gt (>), quot ("), and apos ('). Those cannot be redefined by this method!
- Specified by:
defineEntityReplacementText
in interfaceXmlPullParser
- Throws:
XmlPullParserException
- See Also:
XmlPullParser.setInput(java.io.Reader)
,XmlPullParser.FEATURE_PROCESS_DOCDECL
,XmlPullParser.FEATURE_VALIDATION
-
getNamespaceCount
public int getNamespaceCount(int depth) throws XmlPullParserException
Description copied from interface:XmlPullParser
Returns the numbers of elements in the namespace stack for the given depth. If namespaces are not enabled, 0 is returned.NOTE: when parser is on END_TAG then it is allowed to call this function with getDepth()+1 argument to retrieve position of namespace prefixes and URIs that were declared on corresponding START_TAG.
NOTE: to retrieve lsit of namespaces declared in current element:
XmlPullParser pp = ... int nsStart = pp.getNamespaceCount(pp.getDepth()-1); int nsEnd = pp.getNamespaceCount(pp.getDepth()); for (int i = nsStart; i < nsEnd; i++) { String prefix = pp.getNamespacePrefix(i); String ns = pp.getNamespaceUri(i); // ... }
- Specified by:
getNamespaceCount
in interfaceXmlPullParser
- Throws:
XmlPullParserException
- See Also:
XmlPullParser.getNamespacePrefix(int)
,XmlPullParser.getNamespaceUri(int)
,XmlPullParser.getNamespace()
,XmlPullParser.getNamespace(String)
-
getNamespacePrefix
public java.lang.String getNamespacePrefix(int pos) throws XmlPullParserException
Description copied from interface:XmlPullParser
Returns the namespace prefixe for the given position in the namespace stack. Default namespace declaration (xmlns='...') will have null as prefix. If the given index is out of range, an exception is thrown.Please note: when the parser is on an END_TAG, namespace prefixes that were declared in the corresponding START_TAG are still accessible although they are no longer in scope.
- Specified by:
getNamespacePrefix
in interfaceXmlPullParser
- Throws:
XmlPullParserException
-
getNamespaceUri
public java.lang.String getNamespaceUri(int pos) throws XmlPullParserException
Description copied from interface:XmlPullParser
Returns the namespace URI for the given position in the namespace stack If the position is out of range, an exception is thrown.NOTE: when parser is on END_TAG then namespace prefixes that were declared in corresponding START_TAG are still accessible even though they are not in scope
- Specified by:
getNamespaceUri
in interfaceXmlPullParser
- Throws:
XmlPullParserException
-
getNamespace
public java.lang.String getNamespace(java.lang.String prefix)
Description copied from interface:XmlPullParser
Returns the URI corresponding to the given prefix, depending on current state of the parser.If the prefix was not declared in the current scope, null is returned. The default namespace is included in the namespace table and is available via getNamespace (null).
This method is a convenience method for
for (int i = getNamespaceCount(getDepth ())-1; i >= 0; i--) { if (getNamespacePrefix(i).equals( prefix )) { return getNamespaceUri(i); } } return null;
Please note: parser implementations may provide more efifcient lookup, e.g. using a Hashtable. The 'xml' prefix is bound to "http://www.w3.org/XML/1998/namespace", as defined in the Namespaces in XML specification. Analogous, the 'xmlns' prefix is resolved to http://www.w3.org/2000/xmlns/
- Specified by:
getNamespace
in interfaceXmlPullParser
- See Also:
XmlPullParser.getNamespaceCount(int)
,XmlPullParser.getNamespacePrefix(int)
,XmlPullParser.getNamespaceUri(int)
-
getDepth
public int getDepth()
Description copied from interface:XmlPullParser
Returns the current depth of the element. Outside the root element, the depth is 0. The depth is incremented by 1 when a start tag is reached. The depth is decremented AFTER the end tag event was observed.<!-- outside --> 0 <root> 1 sometext 1 <foobar> 2 </foobar> 2 </root> 1 <!-- outside --> 0
- Specified by:
getDepth
in interfaceXmlPullParser
-
getPositionDescription
public java.lang.String getPositionDescription()
Return string describing current position of parsers as text 'STATE [seen %s...] @line:column'.- Specified by:
getPositionDescription
in interfaceXmlPullParser
-
getLineNumber
public int getLineNumber()
Description copied from interface:XmlPullParser
Returns the current line number, starting from 1. When the parser does not know the current line number or can not determine it, -1 is returned (e.g. for WBXML).- Specified by:
getLineNumber
in interfaceXmlPullParser
- Returns:
- current line number or -1 if unknown.
-
getColumnNumber
public int getColumnNumber()
Description copied from interface:XmlPullParser
Returns the current column number, starting from 0. When the parser does not know the current column number or can not determine it, -1 is returned (e.g. for WBXML).- Specified by:
getColumnNumber
in interfaceXmlPullParser
- Returns:
- current column number or -1 if unknown.
-
isWhitespace
public boolean isWhitespace() throws XmlPullParserException
Description copied from interface:XmlPullParser
Checks whether the current TEXT event contains only whitespace characters. For IGNORABLE_WHITESPACE, this is always true. For TEXT and CDSECT, false is returned when the current event text contains at least one non-white space character. For any other event type an exception is thrown.Please note: non-validating parsers are not able to distinguish whitespace and ignorable whitespace, except from whitespace outside the root element. Ignorable whitespace is reported as separate event, which is exposed via nextToken only.
- Specified by:
isWhitespace
in interfaceXmlPullParser
- Throws:
XmlPullParserException
-
getText
public java.lang.String getText()
Description copied from interface:XmlPullParser
Returns the text content of the current event as String. The value returned depends on current event type, for example for TEXT event it is element content (this is typical case when next() is used). See description of nextToken() for detailed description of possible returned values for different types of events.NOTE: in case of ENTITY_REF, this method returns the entity replacement text (or null if not available). This is the only case where getText() and getTextCharacters() return different values.
- Specified by:
getText
in interfaceXmlPullParser
- See Also:
XmlPullParser.getEventType()
,XmlPullParser.next()
,XmlPullParser.nextToken()
-
getTextCharacters
public char[] getTextCharacters(int[] holderForStartAndLength)
Description copied from interface:XmlPullParser
Returns the buffer that contains the text of the current event, as well as the start offset and length relevant for the current event. See getText(), next() and nextToken() for description of possible returned values.Please note: this buffer must not be modified and its content MAY change after a call to next() or nextToken(). This method will always return the same value as getText(), except for ENTITY_REF. In the case of ENTITY ref, getText() returns the replacement text and this method returns the actual input buffer containing the entity name. If getText() returns null, this method returns null as well and the values returned in the holder array MUST be -1 (both start and length).
- Specified by:
getTextCharacters
in interfaceXmlPullParser
- Parameters:
holderForStartAndLength
- Must hold an 2-element int array into which the start offset and length values will be written.- Returns:
- char buffer that contains the text of the current event (null if the current event has no text associated).
- See Also:
XmlPullParser.getText()
,XmlPullParser.next()
,XmlPullParser.nextToken()
-
getNamespace
public java.lang.String getNamespace()
Description copied from interface:XmlPullParser
Returns the namespace URI of the current element. The default namespace is represented as empty string. If namespaces are not enabled, an empty String ("") is always returned. The current event must be START_TAG or END_TAG; otherwise, null is returned.- Specified by:
getNamespace
in interfaceXmlPullParser
-
getName
public java.lang.String getName()
Description copied from interface:XmlPullParser
For START_TAG or END_TAG events, the (local) name of the current element is returned when namespaces are enabled. When namespace processing is disabled, the raw name is returned. For ENTITY_REF events, the entity name is returned. If the current event is not START_TAG, END_TAG, or ENTITY_REF, null is returned.Please note: To reconstruct the raw element name when namespaces are enabled and the prefix is not null, you will need to add the prefix and a colon to localName..
- Specified by:
getName
in interfaceXmlPullParser
-
getPrefix
public java.lang.String getPrefix()
Description copied from interface:XmlPullParser
Returns the prefix of the current element. If the element is in the default namespace (has no prefix), null is returned. If namespaces are not enabled, or the current event is not START_TAG or END_TAG, null is returned.- Specified by:
getPrefix
in interfaceXmlPullParser
-
isEmptyElementTag
public boolean isEmptyElementTag() throws XmlPullParserException
Description copied from interface:XmlPullParser
Returns true if the current event is START_TAG and the tag is degenerated (e.g. <foobar/>).NOTE: if the parser is not on START_TAG, an exception will be thrown.
- Specified by:
isEmptyElementTag
in interfaceXmlPullParser
- Throws:
XmlPullParserException
-
getAttributeCount
public int getAttributeCount()
Description copied from interface:XmlPullParser
Returns the number of attributes of the current start tag, or -1 if the current event type is not START_TAG- Specified by:
getAttributeCount
in interfaceXmlPullParser
- See Also:
XmlPullParser.getAttributeNamespace(int)
,XmlPullParser.getAttributeName(int)
,XmlPullParser.getAttributePrefix(int)
,XmlPullParser.getAttributeValue(int)
-
getAttributeNamespace
public java.lang.String getAttributeNamespace(int index)
Description copied from interface:XmlPullParser
Returns the namespace URI of the attribute with the given index (starts from 0). Returns an empty string ("") if namespaces are not enabled or the attribute has no namespace. Throws an IndexOutOfBoundsException if the index is out of range or the current event type is not START_TAG.NOTE: if FEATURE_REPORT_NAMESPACE_ATTRIBUTES is set then namespace attributes (xmlns:ns='...') must be reported with namespace http://www.w3.org/2000/xmlns/ (visit this URL for description!). The default namespace attribute (xmlns="...") will be reported with empty namespace.
NOTE:The xml prefix is bound as defined in Namespaces in XML specification to "http://www.w3.org/XML/1998/namespace".
- Specified by:
getAttributeNamespace
in interfaceXmlPullParser
- Parameters:
index
- zero based index of attribute- Returns:
- attribute namespace, empty string ("") is returned if namesapces processing is not enabled or namespaces processing is enabled but attribute has no namespace (it has no prefix).
-
getAttributeName
public java.lang.String getAttributeName(int index)
Description copied from interface:XmlPullParser
Returns the local name of the specified attribute if namespaces are enabled or just attribute name if namespaces are disabled. Throws an IndexOutOfBoundsException if the index is out of range or current event type is not START_TAG.- Specified by:
getAttributeName
in interfaceXmlPullParser
- Parameters:
index
- zero based index of attribute- Returns:
- attribute name (null is never returned)
-
getAttributePrefix
public java.lang.String getAttributePrefix(int index)
Description copied from interface:XmlPullParser
Returns the prefix of the specified attribute Returns null if the element has no prefix. If namespaces are disabled it will always return null. Throws an IndexOutOfBoundsException if the index is out of range or current event type is not START_TAG.- Specified by:
getAttributePrefix
in interfaceXmlPullParser
- Parameters:
index
- zero based index of attribute- Returns:
- attribute prefix or null if namespaces processing is not enabled.
-
getAttributeType
public java.lang.String getAttributeType(int index)
Description copied from interface:XmlPullParser
Returns the type of the specified attribute If parser is non-validating it MUST return CDATA.- Specified by:
getAttributeType
in interfaceXmlPullParser
- Parameters:
index
- zero based index of attribute- Returns:
- attribute type (null is never returned)
-
isAttributeDefault
public boolean isAttributeDefault(int index)
Description copied from interface:XmlPullParser
Returns if the specified attribute was not in input was declared in XML. If parser is non-validating it MUST always return false. This information is part of XML infoset:- Specified by:
isAttributeDefault
in interfaceXmlPullParser
- Parameters:
index
- zero based index of attribute- Returns:
- false if attribute was in input
-
getAttributeValue
public java.lang.String getAttributeValue(int index)
Description copied from interface:XmlPullParser
Returns the given attributes value. Throws an IndexOutOfBoundsException if the index is out of range or current event type is not START_TAG.NOTE: attribute value must be normalized (including entity replacement text if PROCESS_DOCDECL is false) as described in XML 1.0 section 3.3.3 Attribute-Value Normalization
- Specified by:
getAttributeValue
in interfaceXmlPullParser
- Parameters:
index
- zero based index of attribute- Returns:
- value of attribute (null is never returned)
- See Also:
XmlPullParser.defineEntityReplacementText(java.lang.String, java.lang.String)
-
getAttributeValue
public java.lang.String getAttributeValue(java.lang.String namespace, java.lang.String name)
Description copied from interface:XmlPullParser
Returns the attributes value identified by namespace URI and namespace localName. If namespaces are disabled namespace must be null. If current event type is not START_TAG then IndexOutOfBoundsException will be thrown.NOTE: attribute value must be normalized (including entity replacement text if PROCESS_DOCDECL is false) as described in XML 1.0 section 3.3.3 Attribute-Value Normalization
- Specified by:
getAttributeValue
in interfaceXmlPullParser
- Parameters:
namespace
- Namespace of the attribute if namespaces are enabled otherwise must be nullname
- If namespaces enabled local name of attribute otherwise just attribute name- Returns:
- value of attribute or null if attribute with given name does not exist
- See Also:
XmlPullParser.defineEntityReplacementText(java.lang.String, java.lang.String)
-
getEventType
public int getEventType()
Description copied from interface:XmlPullParser
Returns the type of the current event (START_TAG, END_TAG, TEXT, etc.)- Specified by:
getEventType
in interfaceXmlPullParser
- See Also:
XmlPullParser.next()
,XmlPullParser.nextToken()
-
require
public void require(int type, java.lang.String namespace, java.lang.String name) throws XmlPullParserException, java.io.IOException
Description copied from interface:XmlPullParser
Test if the current event is of the given type and if the namespace and name do match. null will match any namespace and any name. If the test is not passed, an exception is thrown. The exception text indicates the parser position, the expected event and the current event that is not meeting the requirement.Essentially it does this
if (type != getEventType() || (namespace != null && !namespace.equals( getNamespace () ) ) || (name != null && !name.equals( getName() ) ) ) throw new XmlPullParserException( "expected "+ TYPES[ type ]+getPositionDescription());
- Specified by:
require
in interfaceXmlPullParser
- Throws:
XmlPullParserException
java.io.IOException
-
skipSubTree
public void skipSubTree() throws XmlPullParserException, java.io.IOException
Skip sub tree that is currently parser positioned on.
NOTE: parser must be on START_TAG and when function returns parser will be positioned on corresponding END_TAG- Throws:
XmlPullParserException
java.io.IOException
-
nextText
public java.lang.String nextText() throws XmlPullParserException, java.io.IOException
Description copied from interface:XmlPullParser
If current event is START_TAG then if next element is TEXT then element content is returned or if next event is END_TAG then empty string is returned, otherwise exception is thrown. After calling this function successfully parser will be positioned on END_TAG.The motivation for this function is to allow to parse consistently both empty elements and elements that has non empty content, for example for input:
- <tag>foo</tag>
- <tag></tag> (which is equivalent to <tag/>
both input can be parsed with the same code:
p.nextTag() p.requireEvent(p.START_TAG, "", "tag"); String content = p.nextText(); p.requireEvent(p.END_TAG, "", "tag");
This function together with nextTag make it very easy to parse XML that has no mixed content.Essentially it does this
if(getEventType() != START_TAG) { throw new XmlPullParserException( "parser must be on START_TAG to read next text", this, null); } int eventType = next(); if(eventType == TEXT) { String result = getText(); eventType = next(); if(eventType != END_TAG) { throw new XmlPullParserException( "event TEXT it must be immediately followed by END_TAG", this, null); } return result; } else if(eventType == END_TAG) { return ""; } else { throw new XmlPullParserException( "parser must be on START_TAG or TEXT to read text", this, null); }
- Specified by:
nextText
in interfaceXmlPullParser
- Throws:
XmlPullParserException
java.io.IOException
-
nextTag
public int nextTag() throws XmlPullParserException, java.io.IOException
Description copied from interface:XmlPullParser
Call next() and return event if it is START_TAG or END_TAG otherwise throw an exception. It will skip whitespace TEXT before actual tag if any.essentially it does this
int eventType = next(); if(eventType == TEXT && isWhitespace()) { // skip whitespace eventType = next(); } if (eventType != START_TAG && eventType != END_TAG) { throw new XmlPullParserException("expected start or end tag", this, null); } return eventType;
- Specified by:
nextTag
in interfaceXmlPullParser
- Throws:
XmlPullParserException
java.io.IOException
-
next
public int next() throws XmlPullParserException, java.io.IOException
Description copied from interface:XmlPullParser
Get next parsing event - element content wil be coalesced and only one TEXT event must be returned for whole element content (comments and processing instructions will be ignored and emtity references must be expanded or exception mus be thrown if entity reerence can not be exapnded). If element content is empty (content is "") then no TEXT event will be reported.NOTE: empty element (such as <tag/>) will be reported with two separate events: START_TAG, END_TAG - it must be so to preserve parsing equivalency of empty element to <tag></tag>. (see isEmptyElementTag ())
- Specified by:
next
in interfaceXmlPullParser
- Throws:
XmlPullParserException
java.io.IOException
- See Also:
XmlPullParser.isEmptyElementTag()
,XmlPullParser.START_TAG
,XmlPullParser.TEXT
,XmlPullParser.END_TAG
,XmlPullParser.END_DOCUMENT
-
nextToken
public int nextToken() throws XmlPullParserException, java.io.IOException
Description copied from interface:XmlPullParser
This method works similarly to next() but will expose additional event types (COMMENT, CDSECT, DOCDECL, ENTITY_REF, PROCESSING_INSTRUCTION, or IGNORABLE_WHITESPACE) if they are available in input.If special feature FEATURE_XML_ROUNDTRIP (identified by URI: http://xmlpull.org/v1/doc/features.html#xml-roundtrip) is enabled it is possible to do XML document round trip ie. reproduce exectly on output the XML input using getText(): returned content is always unnormalized (exactly as in input). Otherwise returned content is end-of-line normalized as described XML 1.0 End-of-Line Handling and. Also when this feature is enabled exact content of START_TAG, END_TAG, DOCDECL and PROCESSING_INSTRUCTION is available.
Here is the list of tokens that can be returned from nextToken() and what getText() and getTextCharacters() returns:
- START_DOCUMENT
- null
- END_DOCUMENT
- null
- START_TAG
- null unless FEATURE_XML_ROUNDTRIP enabled and then returns XML tag, ex: <tag attr='val'>
- END_TAG
- null unless FEATURE_XML_ROUNDTRIP id enabled and then returns XML tag, ex: </tag>
- TEXT
- return element content.
Note: that element content may be delivered in multiple consecutive TEXT events. - IGNORABLE_WHITESPACE
- return characters that are determined to be ignorable white
space. If the FEATURE_XML_ROUNDTRIP is enabled all whitespace content outside root
element will always reported as IGNORABLE_WHITESPACE otherise rteporting is optional.
Note: that element content may be delevered in multiple consecutive IGNORABLE_WHITESPACE events. - CDSECT
- return text inside CDATA (ex. 'fo<o' from <!CDATA[fo<o]]>)
- PROCESSING_INSTRUCTION
- if FEATURE_XML_ROUNDTRIP is true return exact PI content ex: 'pi foo' from <?pi foo?> otherwise it may be exact PI content or concatenation of PI target, space and data so for example for <?target data?> string "target data" may be returned if FEATURE_XML_ROUNDTRIP is false.
- COMMENT
- return comment content ex. 'foo bar' from <!--foo bar-->
- ENTITY_REF
- getText() MUST return entity replacement text if PROCESS_DOCDECL is false
otherwise getText() MAY return null,
additionally getTextCharacters() MUST return entity name
(for example 'entity_name' for &entity_name;).
NOTE: this is the only place where value returned from getText() and getTextCharacters() are different
NOTE: it is user responsibility to resolve entity reference if PROCESS_DOCDECL is false and there is no entity replacement text set in defineEntityReplacementText() method (getText() will be null)
NOTE: character entities (ex.  ) and standard entities such as & < > " ' are reported as well and are not reported as TEXT tokens but as ENTITY_REF tokens! This requirement is added to allow to do roundtrip of XML documents! - DOCDECL
-
if FEATURE_XML_ROUNDTRIP is true or PROCESS_DOCDECL is false
then return what is inside of DOCDECL for example it returns:
" titlepage SYSTEM "http://www.foo.bar/dtds/typo.dtd" [<!ENTITY % active.links "INCLUDE">]"
for input document that contained:
<!DOCTYPE titlepage SYSTEM "http://www.foo.bar/dtds/typo.dtd" [<!ENTITY % active.links "INCLUDE">]>
otherwise if FEATURE_XML_ROUNDTRIP is false and PROCESS_DOCDECL is true then what is returned is undefined (it may be even null)
NOTE: there is no gurantee that there will only one TEXT or IGNORABLE_WHITESPACE event from nextToken() as parser may chose to deliver element content in multiple tokens (dividing element content into chunks)
NOTE: whether returned text of token is end-of-line normalized is depending on FEATURE_XML_ROUNDTRIP.
NOTE: XMLDecl (<?xml ...?>) is not reported but its content is available through optional properties (see class description above).
- Specified by:
nextToken
in interfaceXmlPullParser
- Throws:
XmlPullParserException
java.io.IOException
- See Also:
XmlPullParser.next()
,XmlPullParser.START_TAG
,XmlPullParser.TEXT
,XmlPullParser.END_TAG
,XmlPullParser.END_DOCUMENT
,XmlPullParser.COMMENT
,XmlPullParser.DOCDECL
,XmlPullParser.PROCESSING_INSTRUCTION
,XmlPullParser.ENTITY_REF
,XmlPullParser.IGNORABLE_WHITESPACE
-
nextImpl
protected int nextImpl() throws XmlPullParserException, java.io.IOException
- Throws:
XmlPullParserException
java.io.IOException
-
parseProlog
protected int parseProlog() throws XmlPullParserException, java.io.IOException
- Throws:
XmlPullParserException
java.io.IOException
-
parseEpilog
protected int parseEpilog() throws XmlPullParserException, java.io.IOException
- Throws:
XmlPullParserException
java.io.IOException
-
parseEndTag
public int parseEndTag() throws XmlPullParserException, java.io.IOException
- Throws:
XmlPullParserException
java.io.IOException
-
parseStartTag
public int parseStartTag() throws XmlPullParserException, java.io.IOException
- Throws:
XmlPullParserException
java.io.IOException
-
parseAttribute
protected char parseAttribute() throws XmlPullParserException, java.io.IOException
- Throws:
XmlPullParserException
java.io.IOException
-
parseEntityRef
protected char[] parseEntityRef() throws XmlPullParserException, java.io.IOException
- Throws:
XmlPullParserException
java.io.IOException
-
lookuEntityReplacement
protected char[] lookuEntityReplacement(int entitNameLen) throws XmlPullParserException, java.io.IOException
- Throws:
XmlPullParserException
java.io.IOException
-
parseComment
protected void parseComment() throws XmlPullParserException, java.io.IOException
- Throws:
XmlPullParserException
java.io.IOException
-
parsePI
protected boolean parsePI() throws XmlPullParserException, java.io.IOException
- Throws:
XmlPullParserException
java.io.IOException
-
parseXmlDecl
protected void parseXmlDecl(char ch) throws XmlPullParserException, java.io.IOException
- Throws:
XmlPullParserException
java.io.IOException
-
parseXmlDeclWithVersion
protected void parseXmlDeclWithVersion(int versionStart, int versionEnd) throws XmlPullParserException, java.io.IOException
- Throws:
XmlPullParserException
java.io.IOException
-
parseDocdecl
protected void parseDocdecl() throws XmlPullParserException, java.io.IOException
- Throws:
XmlPullParserException
java.io.IOException
-
parseCDSect
protected void parseCDSect(boolean hadCharData) throws XmlPullParserException, java.io.IOException
- Throws:
XmlPullParserException
java.io.IOException
-
fillBuf
protected void fillBuf() throws java.io.IOException, XmlPullParserException
- Throws:
java.io.IOException
XmlPullParserException
-
more
protected char more() throws java.io.IOException, XmlPullParserException
- Throws:
java.io.IOException
XmlPullParserException
-
ensurePC
protected void ensurePC(int end)
-
joinPC
protected void joinPC()
-
requireInput
protected char requireInput(char ch, char[] input) throws XmlPullParserException, java.io.IOException
- Throws:
XmlPullParserException
java.io.IOException
-
requireNextS
protected char requireNextS() throws XmlPullParserException, java.io.IOException
- Throws:
XmlPullParserException
java.io.IOException
-
skipS
protected char skipS(char ch) throws XmlPullParserException, java.io.IOException
- Throws:
XmlPullParserException
java.io.IOException
-
isNameStartChar
protected boolean isNameStartChar(char ch)
-
isNameChar
protected boolean isNameChar(char ch)
-
isS
protected boolean isS(char ch)
-
printable
protected java.lang.String printable(char ch)
-
printable
protected java.lang.String printable(java.lang.String s)
-
-