|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.kxml2.io.KXmlParser
A simple, pull based XML parser. This classe replaces the XmlParser class and the corresponding event classes.
| Field Summary |
| Fields inherited from interface org.xmlpull.v1.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 | |
KXmlParser()
|
|
KXmlParser(int sz)
|
|
| Method Summary | |
void |
defineEntityReplacementText(java.lang.String entity,
java.lang.String value)
Set new value for entity replacement text as defined in XML 1.0 Section 4.5 Construction of Internal Entity Replacement Text. |
int |
getAttributeCount()
Returns the number of attributes on the current element; -1 if the current event is not START_TAG |
java.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 specified attribute number 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()
Current column: numbering starts from 0 (zero should be returned when parser is in START_DOCUMENT state!) It must return -1 if parser does not know current line number or can not determine it (for example in case of WBXML) |
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 feature)
Return the current value of the feature with given name. |
java.lang.String |
getInputEncoding()
Return input encoding if known or null if unknown. |
int |
getLineNumber()
Current line number: numebering starts from 1. |
java.lang.String |
getName()
For START_TAG or END_TAG returns the (local) name of the current element when namespaces are enabled or raw name when namespaces are disabled. |
java.lang.String |
getNamespace()
Returns the namespace URI of the current element (default namespace is represented as empty string). |
java.lang.String |
getNamespace(java.lang.String prefix)
Return uri for the given prefix. |
int |
getNamespaceCount(int depth)
Return position in stack of first namespace slot for element at passed depth. |
java.lang.String |
getNamespacePrefix(int pos)
Return namespace prefixes for position pos in namespace stack If pos is out of range it throw exception. |
java.lang.String |
getNamespaceUri(int pos)
Return namespace URIs for position pos in namespace stack If pos is out of range it throw exception. |
java.lang.String |
getPositionDescription()
Short text describing parser position, including a description of the current event and data source if known and if possible what parser was seeing lastly in input. |
java.lang.String |
getPrefix()
Returns the prefix of the current element or null if elemet has no prefix (is in defualt namespace). |
java.lang.Object |
getProperty(java.lang.String property)
Look up the value of a property. |
java.lang.String |
getText()
Read text content of the current event as String. |
char[] |
getTextCharacters(int[] poslen)
Get the buffer that contains text of the current event and start offset of text is passed in first slot of input int array and its length is in second slot. |
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. |
boolean |
isWhitespace()
Check if current TEXT event contains only whitespace characters. |
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). |
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. |
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. |
void |
setFeature(java.lang.String feature,
boolean value)
Use this call to change the general behaviour of the parser, such as namespace processing or doctype declaration handling. |
void |
setInput(java.io.InputStream is,
java.lang.String enc)
Set the input stream for parser. |
void |
setInput(java.io.Reader reader)
Set the input for parser. |
void |
setProperty(java.lang.String property,
java.lang.Object value)
Set the value of a property. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
public KXmlParser()
public KXmlParser(int sz)
| Method Detail |
public void setInput(java.io.Reader reader)
throws XmlPullParserException
XmlPullParser
setInput in interface XmlPullParserXmlPullParserException
public void setInput(java.io.InputStream is,
java.lang.String enc)
throws XmlPullParserException
XmlPullParserNOTE: calling this function will not result in reading any input bytes even when parser have to determine input encoding including byte order marks and detection <? xml encoding (in case when inputEncoding is null). The XMLPULL implementation MUST postpone reading of input bytes until first call to one of next() methods.
NOTE: if inputEncoding 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) but it is not required (for example when parser is constrained by memory footprint such as in J2ME environments) If encoding detection is supported then following feature http://xmlpull.org/v1/doc/features.html#detect-encoding MUST be true otherwise it must be false
setInput in interface XmlPullParseris - contains raw byte input stream of possibly
unknown encoding (when inputEncoding is null) and in such case the parser
must derive encoding from <?xml declaration or assume UTF8 or UTF16 as
described in XML 1.0
Appendix F.1 Detection Without External Encoding Information
otherwise if inputEncoding is present then it must be used
(this is consistent with
XML 1.0
Appendix F.2 Priorities in the Presence of External Encoding Information
that allows for exception only for files and in such cases inputEncoding should
be null to trigger autodetecting.
if inputStream is null the IllegalArgumentException must be thrownenc - if not null it MUST be used as encoding for inputStream
XmlPullParserExceptionpublic boolean getFeature(java.lang.String feature)
XmlPullParserNOTE: unknown features are
getFeature in interface XmlPullParser
feature - The name of feature to be retrieved.
public java.lang.String getInputEncoding()
XmlPullParser
getInputEncoding in interface XmlPullParser
public void defineEntityReplacementText(java.lang.String entity,
java.lang.String value)
throws XmlPullParserException
XmlPullParserThe motivation for this function is to allow very small implementations of XMLPULL that will work in J2ME environments and though may not be able to process DOCDECL but still can be made to work with predefined DTDs by using this function to define well known in advance entities. Additionally as XML Schemas are replacing DTDs by allowing parsers not to process DTDs it is possible to create more efficient parser implementations that can be used as underlying layer to do XML schemas validation.
NOTE: this is replacement text and it is not allowed to contain any other entity reference
NOTE: list of pre-defined entites will always contain standard XML entities (such as & < > " ') and they cannot be replaced!
defineEntityReplacementText in interface XmlPullParserXmlPullParserExceptionXmlPullParser.setInput(java.io.Reader),
XmlPullParser.FEATURE_PROCESS_DOCDECL,
XmlPullParser.FEATURE_VALIDATIONpublic java.lang.Object getProperty(java.lang.String property)
XmlPullParserNOTE: unknown features are
getProperty in interface XmlPullParser
property - The name of property to be retrieved.
public int getNamespaceCount(int depth)
XmlPullParserNOTE: default namespace is included in namespace table and is available by using null string as in getNamespace(null) (it may return null if xmlns="..." is not present) and as well by calling getNamespace() (that will never return null but "").
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.
getNamespaceCount in interface XmlPullParserXmlPullParser.getNamespacePrefix(int),
XmlPullParser.getNamespaceUri(int),
XmlPullParser.getNamespace(),
XmlPullParser.getNamespace(String)public java.lang.String getNamespacePrefix(int pos)
XmlPullParserNOTE: 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.
getNamespacePrefix in interface XmlPullParserpublic java.lang.String getNamespaceUri(int pos)
XmlPullParserNOTE: 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
getNamespaceUri in interface XmlPullParserpublic java.lang.String getNamespace(java.lang.String prefix)
XmlPullParserIt will return null if namespace could not be found.
Convenience method for
for (int i = getNamespaceCount (getDepth ())-1; i >= 0; i--) {
if (getNamespacePrefix (i).equals (prefix)) {
return getNamespaceUri (i);
}
}
return null;
NOTE: parser implementation can do more efifcient lookup (using Hashtable for exmaple).
NOTE:The 'xml' prefix is bound as defined in Namespaces in XML specification to "http://www.w3.org/XML/1998/namespace".
NOTE: The 'xmlns' prefix must be resolved to following namespace http://www.w3.org/2000/xmlns/ (visit this URL for description!).
getNamespace in interface XmlPullParserXmlPullParser.getNamespaceCount(int),
XmlPullParser.getNamespacePrefix(int),
XmlPullParser.getNamespaceUri(int)public int getDepth()
XmlPullParser
<!-- outside --> 0
<root> 1
sometext 1
<foobar> 2
</foobar> 2
</root> 1
<!-- outside --> 0
</pre>
getDepth in interface XmlPullParserpublic java.lang.String getPositionDescription()
XmlPullParser
getPositionDescription in interface XmlPullParserpublic int getLineNumber()
XmlPullParser
getLineNumber in interface XmlPullParserpublic int getColumnNumber()
XmlPullParser
getColumnNumber in interface XmlPullParser
public boolean isWhitespace()
throws XmlPullParserException
XmlPullParserNOTE: 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.
NOTE: this function can be only called for element content related events (TEXT, CDSECT or IGNORABLE_WHITESPACE) otherwise exception will be thrown!
isWhitespace in interface XmlPullParserXmlPullParserExceptionpublic java.lang.String getText()
XmlPullParserNOTE: in case of ENTITY_REF this method returns entity replacement text (or null if not available) and it is the only case when getText() and getTextCharacters() returns different values.
getText in interface XmlPullParserpublic char[] getTextCharacters(int[] poslen)
XmlPullParserNOTE: this buffer must not be modified and its content MAY change after call to next() or nextToken().
NOTE: this method must return always the same value as getText() except in case of ENTITY_REF (where getText() is replacement text and this method returns actual input buffer with entity name the same as getName()). If getText() returns null then this method returns null as well and values returned in holder MUST be -1 (both start and length).
getTextCharacters in interface XmlPullParserposlen - the 2-element int array into which
values of start offset and length will be written into frist and second slot of array.
XmlPullParser.getText()public java.lang.String getNamespace()
XmlPullParser
getNamespace in interface XmlPullParserpublic java.lang.String getName()
XmlPullParserNOTE: to reconstruct raw element name when namespaces are enabled you will need to add prefix and colon to localName if prefix is not null.
getName in interface XmlPullParserpublic java.lang.String getPrefix()
XmlPullParser
getPrefix in interface XmlPullParser
public boolean isEmptyElementTag()
throws XmlPullParserException
XmlPullParserNOTE: if parser is not on START_TAG then the exception will be thrown.
isEmptyElementTag in interface XmlPullParserXmlPullParserExceptionpublic int getAttributeCount()
XmlPullParser
getAttributeCount in interface XmlPullParserXmlPullParser.getAttributeNamespace(int),
XmlPullParser.getAttributeName(int),
XmlPullParser.getAttributePrefix(int),
XmlPullParser.getAttributeValue(int)public java.lang.String getAttributeType(int index)
XmlPullParser
getAttributeType in interface XmlPullParserpublic boolean isAttributeDefault(int index)
XmlPullParser
isAttributeDefault in interface XmlPullParserpublic java.lang.String getAttributeNamespace(int index)
XmlPullParserNOTE: 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".
getAttributeNamespace in interface XmlPullParserpublic java.lang.String getAttributeName(int index)
XmlPullParser
getAttributeName in interface XmlPullParserpublic java.lang.String getAttributePrefix(int index)
XmlPullParser
getAttributePrefix in interface XmlPullParserpublic java.lang.String getAttributeValue(int index)
XmlPullParserNOTE: 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
getAttributeValue in interface XmlPullParserXmlPullParser.defineEntityReplacementText(java.lang.String, java.lang.String)
public java.lang.String getAttributeValue(java.lang.String namespace,
java.lang.String name)
XmlPullParserNOTE: 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
getAttributeValue in interface XmlPullParsernamespace - Namespace of the attribute if namespaces are enabled otherwise must be nullname - If namespaces enabled local name of attribute otherwise just attribute name
XmlPullParser.defineEntityReplacementText(java.lang.String, java.lang.String)
public int getEventType()
throws XmlPullParserException
XmlPullParser
getEventType in interface XmlPullParserXmlPullParserExceptionXmlPullParser.next(),
XmlPullParser.nextToken()
public int next()
throws XmlPullParserException,
java.io.IOException
XmlPullParserNOTE: 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 ())
next in interface XmlPullParserXmlPullParserException
java.io.IOExceptionXmlPullParser.isEmptyElementTag(),
XmlPullParser.START_TAG,
XmlPullParser.TEXT,
XmlPullParser.END_TAG,
XmlPullParser.END_DOCUMENT
public int nextToken()
throws XmlPullParserException,
java.io.IOException
XmlPullParserIf special feature FEATURE_XML_ROUNDTRIP (identified by URI: http://xmlpull.org/v1/doc/features.html#xml-roundtrip) is true then it is possible to do XML document round trip ie. reproduce exectly on output the XML input using getText().
Here is the list of tokens that can be returned from nextToken() and what getText() and getTextCharacters() 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">]>
NOTE: returned text of token is not end-of-line normalized.
nextToken in interface XmlPullParserXmlPullParserException
java.io.IOExceptionXmlPullParser.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
public int nextTag()
throws XmlPullParserException,
java.io.IOException
XmlPullParseressentially 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;
nextTag in interface XmlPullParserXmlPullParserException
java.io.IOException
public void require(int type,
java.lang.String namespace,
java.lang.String name)
throws XmlPullParserException,
java.io.IOException
XmlPullParserEssentially it does this
if (type != getEventType()
|| (namespace != null && !namespace.equals( getNamespace () ) )
|| (name != null && !name.equals( getName() ) ) )
throw new XmlPullParserException( "expected "+ TYPES[ type ]+getPositionDescription());
require in interface XmlPullParserXmlPullParserException
java.io.IOException
public java.lang.String nextText()
throws XmlPullParserException,
java.io.IOException
XmlPullParserThe motivation for this function is to allow to parse consistently both empty elements and elements that has non empty content, for example for input:
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);
}
nextText in interface XmlPullParserXmlPullParserException
java.io.IOException
public void setFeature(java.lang.String feature,
boolean value)
throws XmlPullParserException
XmlPullParserExample: call setFeature(FEATURE_PROCESS_NAMESPACES, true) in order to switch on namespace processing. Default settings correspond to properties requested from the XML Pull Parser factory (if none were requested then all feautures are by default false).
setFeature in interface XmlPullParserXmlPullParserException - if feature is not supported or can not be set
public void setProperty(java.lang.String property,
java.lang.Object value)
throws XmlPullParserException
XmlPullParser
setProperty in interface XmlPullParserXmlPullParserException
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||