gnu.xml.stream

Class XMLParser

Implemented Interfaces:
NamespaceContext, XMLStreamConstants, XMLStreamReader

public class XMLParser
extends Object
implements XMLStreamReader, NamespaceContext

An XML parser. This parser supports the following additional StAX properties:
gnu.xml.stream.stringInterningBooleanIndicates whether markup strings will be interned
gnu.xml.stream.xmlBaseBooleanIndicates whether XML Base processing will be performed
gnu.xml.stream.baseURIStringReturns the base URI of the current event
See Also:
http://www.w3.org/TR/REC-xml/, http://www.w3.org/TR/xml11/, http://www.w3.org/TR/REC-xml-names, http://www.w3.org/TR/xml-names11, http://www.w3.org/TR/xmlbase/

Fields inherited from interface javax.xml.stream.XMLStreamConstants

ATTRIBUTE, CDATA, CHARACTERS, COMMENT, DTD, END_DOCUMENT, END_ELEMENT, ENTITY_DECLARATION, ENTITY_REFERENCE, NAMESPACE, NOTATION_DECLARATION, PROCESSING_INSTRUCTION, SPACE, START_DOCUMENT, START_ELEMENT

Constructor Summary

XMLParser(InputStream in, String systemId, boolean validating, boolean namespaceAware, boolean coalescing, boolean replaceERefs, boolean externalEntities, boolean supportDTD, boolean baseAware, boolean stringInterning, boolean extendedEventTypes, XMLReporter reporter, XMLResolver resolver)
Creates a new XML parser for the given input stream.
XMLParser(Reader reader, String systemId, boolean validating, boolean namespaceAware, boolean coalescing, boolean replaceERefs, boolean externalEntities, boolean supportDTD, boolean baseAware, boolean stringInterning, boolean extendedEventTypes, XMLReporter reporter, XMLResolver resolver)
Creates a new XML parser for the given character stream.

Method Summary

static String
absolutize(String base, String href)
"Absolutize" a URL.
void
close()
Frees any resources used by this parser.
int
getAttributeCount()
Returns the number of attributes on this element.
String
getAttributeLocalName(int index)
Returns the local-name of the attribute at the given index.
QName
getAttributeName(int index)
Returns the QName of the attribute at the given index.
String
getAttributeNamespace(int index)
Returns the namespace URI of the attribute at the given index.
String
getAttributePrefix(int index)
Returns the namespace prefix of the attribute at the given index.
String
getAttributeType(int index)
Returns the type of the attribute at the specified index.
String
getAttributeValue(int index)
Returns the normalized value of the attribute at the given index.
String
getAttributeValue(String namespaceURI, String localName)
Returns the normalized attribute value for the given attribute.
String
getCharacterEncodingScheme()
Returns the encoding declared in the XML declaration.
String
getElementText()
Returns the text content of a text-only element.
String
getEncoding()
Returns the input encoding.
int
getEventType()
Returns the type of the current event.
String
getLocalName()
Returns the local-name of the current element.
Location
getLocation()
Returns the current location of the parser cursor in the underlying input source.
QName
getName()
Returns the QName of the current element.
NamespaceContext
getNamespaceContext()
Returns the namespace context for the current position.
int
getNamespaceCount()
Returns the number of namespaces declared on this event.
String
getNamespacePrefix(int index)
Returns the prefix of the namespace at the given index, or null if this is the default namespace declaration.
String
getNamespaceURI()
Returns the namespace URI of the current element.
String
getNamespaceURI(int index)
Returns the URI of the namespace at the given index.
String
getNamespaceURI(String prefix)
Returns the namespace URI for the given prefix.
String
getPIData()
Returns the data of the current processing instruction event.
String
getPITarget()
Returns the target of the current processing instruction event.
String
getPrefix()
Returns the namespace prefix of the current element.
String
getPrefix(String namespaceURI)
Iterator<E>
getPrefixes(String namespaceURI)
Object
getProperty(String name)
Returns the implementation-specific feature or property of the given name.
String
getText()
Returns the string value of the current event.
char[]
getTextCharacters()
Returns the string value of the current event as a character array.
int
getTextCharacters(int sourceStart, char[] target, int targetStart, int length)
Copies the string value of the current event into the specified character array.
int
getTextLength()
Returns the length of the characters in the text character array.
int
getTextStart()
Returns the offset of the first character in the text character array.
String
getVersion()
Returns the XML version declared in the XML declaration.
boolean
hasName()
Indicates whether the current event has a name.
boolean
hasNext()
Indicates whether there are any remaining events to be read.
boolean
hasText()
Indicates whether the current event has text.
boolean
isAttributeSpecified(int index)
Indicates whether the attribute at the given index was specified in the underlying XML source or created by default.
static boolean
isChar(int c)
Indicates whether the specified Unicode character matches the Char production.
boolean
isCharacters()
Indicates whether the current event is character data.
static boolean
isCombiningChar(int c)
Indicates whether the specified Unicode character matches the CombiningChar production.
static boolean
isDigit(int c)
Indicates whether the specified Unicode character matches the Digit production.
boolean
isEndElement()
Indicates whether the current event is END_ELEMENT.
static boolean
isExtender(int c)
Indicates whether the specified Unicode character matches the Extender production.
static boolean
isLetter(int c)
Indicates whether the specified Unicode character matches the Letter production.
static boolean
isNameCharacter(int c, boolean xml11)
Indicates whether the specified Unicode character is a Name non-initial character.
static boolean
isNameStartCharacter(int c, boolean xml11)
Indicates whether the specified Unicode character is a Name start character.
boolean
isStandalone()
Returns the standalone flag declared in the XML declaration.
boolean
isStartElement()
Indicates whether the current event is START_ELEMENT.
boolean
isWhiteSpace()
Indicates whether the current event is ignorable whitespace.
static boolean
isXML11Char(int c)
Indicates whether the specified Unicode character is an XML 1.1 Char.
static boolean
isXML11RestrictedChar(int c)
Indicates whether the specified Unicode character is an XML 1.1 RestrictedChar.
static void
main(String[] args)
Simple test harness for reading an XML file.
int
next()
Returns the next parsing event.
int
nextTag()
Skips any ignorable whitespace, comments, and processing instructions until a START_ELEMENT or END_ELEMENT event is encountered.
void
require(int type, String namespaceURI, String localName)
Tests whether the current event is of the given type and namespace.
boolean
standaloneSet()
Indicates whether the standalone flag was set in the document.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

XMLParser

public XMLParser(InputStream in,
                 String systemId,
                 boolean validating,
                 boolean namespaceAware,
                 boolean coalescing,
                 boolean replaceERefs,
                 boolean externalEntities,
                 boolean supportDTD,
                 boolean baseAware,
                 boolean stringInterning,
                 boolean extendedEventTypes,
                 XMLReporter reporter,
                 XMLResolver resolver)
Creates a new XML parser for the given input stream. This constructor should be used where possible, as it allows the encoding of the XML data to be correctly determined from the stream.
Parameters:
in - the input stream
systemId - the URL from which the input stream was retrieved (necessary if there are external entities to be resolved)
validating - if the parser is to be a validating parser
namespaceAware - if the parser should support XML Namespaces
coalescing - if CDATA sections should be merged into adjacent text nodes
replaceERefs - if entity references should be automatically replaced by their replacement text (otherwise they will be reported as entity-reference events)
externalEntities - if external entities should be loaded
supportDTD - if support for the XML DTD should be enabled
baseAware - if the parser should support XML Base to resolve external entities
stringInterning - whether strings will be interned during parsing
reporter - the reporter to receive warnings during processing
resolver - the callback interface used to resolve external entities

XMLParser

public XMLParser(Reader reader,
                 String systemId,
                 boolean validating,
                 boolean namespaceAware,
                 boolean coalescing,
                 boolean replaceERefs,
                 boolean externalEntities,
                 boolean supportDTD,
                 boolean baseAware,
                 boolean stringInterning,
                 boolean extendedEventTypes,
                 XMLReporter reporter,
                 XMLResolver resolver)
Creates a new XML parser for the given character stream. This constructor is only available for compatibility with the JAXP APIs, which permit XML to be parsed from a character stream. Because the encoding specified by the character stream may conflict with that specified in the XML declaration, this method should be avoided where possible.
Parameters:
systemId - the URL from which the input stream was retrieved (necessary if there are external entities to be resolved)
validating - if the parser is to be a validating parser
namespaceAware - if the parser should support XML Namespaces
coalescing - if CDATA sections should be merged into adjacent text nodes
replaceERefs - if entity references should be automatically replaced by their replacement text (otherwise they will be reported as entity-reference events)
externalEntities - if external entities should be loaded
supportDTD - if support for the XML DTD should be enabled
baseAware - if the parser should support XML Base to resolve external entities
stringInterning - whether strings will be interned during parsing
reporter - the reporter to receive warnings during processing
resolver - the callback interface used to resolve external entities

Method Details

absolutize

public static String absolutize(String base,
                                String href)
"Absolutize" a URL. This resolves a relative URL into an absolute one.
Parameters:
base - the current base URL
href - the (absolute or relative) URL to resolve

close

public void close()
            throws XMLStreamException
Frees any resources used by this parser. This method will not close the underlying input source.
Specified by:
close in interface XMLStreamReader

getAttributeCount

public int getAttributeCount()
Returns the number of attributes on this element. This method can only be invoked on a START_ELEMENT event.
Specified by:
getAttributeCount in interface XMLStreamReader

getAttributeLocalName

public String getAttributeLocalName(int index)
Returns the local-name of the attribute at the given index.
Specified by:
getAttributeLocalName in interface XMLStreamReader

getAttributeName

public QName getAttributeName(int index)
Returns the QName of the attribute at the given index.
Specified by:
getAttributeName in interface XMLStreamReader

getAttributeNamespace

public String getAttributeNamespace(int index)
Returns the namespace URI of the attribute at the given index.
Specified by:
getAttributeNamespace in interface XMLStreamReader

getAttributePrefix

public String getAttributePrefix(int index)
Returns the namespace prefix of the attribute at the given index.
Specified by:
getAttributePrefix in interface XMLStreamReader

getAttributeType

public String getAttributeType(int index)
Returns the type of the attribute at the specified index.
Specified by:
getAttributeType in interface XMLStreamReader

getAttributeValue

public String getAttributeValue(int index)
Returns the normalized value of the attribute at the given index.
Specified by:
getAttributeValue in interface XMLStreamReader

getAttributeValue

public String getAttributeValue(String namespaceURI,
                                String localName)
Returns the normalized attribute value for the given attribute.
Specified by:
getAttributeValue in interface XMLStreamReader

getCharacterEncodingScheme

public String getCharacterEncodingScheme()
Returns the encoding declared in the XML declaration.
Specified by:
getCharacterEncodingScheme in interface XMLStreamReader

getElementText

public String getElementText()
            throws XMLStreamException
Returns the text content of a text-only element. When invoked, the current event must be START_ELEMENT. On completion, the current event will be END_ELEMENT.
Specified by:
getElementText in interface XMLStreamReader

getEncoding

public String getEncoding()
Returns the input encoding.
Specified by:
getEncoding in interface XMLStreamReader

getEventType

public int getEventType()
Returns the type of the current event.
Specified by:
getEventType in interface XMLStreamReader

getLocalName

public String getLocalName()
Returns the local-name of the current element.
Specified by:
getLocalName in interface XMLStreamReader

getLocation

public Location getLocation()
Returns the current location of the parser cursor in the underlying input source.
Specified by:
getLocation in interface XMLStreamReader

getName

public QName getName()
Returns the QName of the current element. This method is only valid on a START_ELEMENT or END_ELEMENT event.
Specified by:
getName in interface XMLStreamReader

getNamespaceContext

public NamespaceContext getNamespaceContext()
Returns the namespace context for the current position.
Specified by:
getNamespaceContext in interface XMLStreamReader

getNamespaceCount

public int getNamespaceCount()
Returns the number of namespaces declared on this event. This method is only valid on a START_ELEMENT, END_ELEMENT, or NAMESPACE event.
Specified by:
getNamespaceCount in interface XMLStreamReader

getNamespacePrefix

public String getNamespacePrefix(int index)
Returns the prefix of the namespace at the given index, or null if this is the default namespace declaration.
Specified by:
getNamespacePrefix in interface XMLStreamReader

getNamespaceURI

public String getNamespaceURI()
Returns the namespace URI of the current element.
Specified by:
getNamespaceURI in interface XMLStreamReader

getNamespaceURI

public String getNamespaceURI(int index)
Returns the URI of the namespace at the given index.
Specified by:
getNamespaceURI in interface XMLStreamReader

getNamespaceURI

public String getNamespaceURI(String prefix)
Returns the namespace URI for the given prefix.
Specified by:
getNamespaceURI in interface XMLStreamReader
getNamespaceURI in interface NamespaceContext

getPIData

public String getPIData()
Returns the data of the current processing instruction event.
Specified by:
getPIData in interface XMLStreamReader

getPITarget

public String getPITarget()
Returns the target of the current processing instruction event.
Specified by:
getPITarget in interface XMLStreamReader

getPrefix

public String getPrefix()
Returns the namespace prefix of the current element.
Specified by:
getPrefix in interface XMLStreamReader

getPrefix

public String getPrefix(String namespaceURI)
Specified by:
getPrefix in interface NamespaceContext

getPrefixes

public Iterator<E> getPrefixes(String namespaceURI)

getProperty

public Object getProperty(String name)
            throws IllegalArgumentException
Returns the implementation-specific feature or property of the given name.
Specified by:
getProperty in interface XMLStreamReader

getText

public String getText()
Returns the string value of the current event.
Specified by:
getText in interface XMLStreamReader

getTextCharacters

public char[] getTextCharacters()
Returns the string value of the current event as a character array.
Specified by:
getTextCharacters in interface XMLStreamReader

getTextCharacters

public int getTextCharacters(int sourceStart,
                             char[] target,
                             int targetStart,
                             int length)
            throws XMLStreamException
Copies the string value of the current event into the specified character array.
Specified by:
getTextCharacters in interface XMLStreamReader

getTextLength

public int getTextLength()
Returns the length of the characters in the text character array.
Specified by:
getTextLength in interface XMLStreamReader

getTextStart

public int getTextStart()
Returns the offset of the first character in the text character array.
Specified by:
getTextStart in interface XMLStreamReader

getVersion

public String getVersion()
Returns the XML version declared in the XML declaration.
Specified by:
getVersion in interface XMLStreamReader

hasName

public boolean hasName()
Indicates whether the current event has a name.
Specified by:
hasName in interface XMLStreamReader

hasNext

public boolean hasNext()
            throws XMLStreamException
Indicates whether there are any remaining events to be read.
Specified by:
hasNext in interface XMLStreamReader

hasText

public boolean hasText()
Indicates whether the current event has text.
Specified by:
hasText in interface XMLStreamReader

isAttributeSpecified

public boolean isAttributeSpecified(int index)
Indicates whether the attribute at the given index was specified in the underlying XML source or created by default.
Specified by:
isAttributeSpecified in interface XMLStreamReader

isChar

public static boolean isChar(int c)
Indicates whether the specified Unicode character matches the Char production.

isCharacters

public boolean isCharacters()
Indicates whether the current event is character data.
Specified by:
isCharacters in interface XMLStreamReader

isCombiningChar

public static boolean isCombiningChar(int c)
Indicates whether the specified Unicode character matches the CombiningChar production.

isDigit

public static boolean isDigit(int c)
Indicates whether the specified Unicode character matches the Digit production.

isEndElement

public boolean isEndElement()
Indicates whether the current event is END_ELEMENT.
Specified by:
isEndElement in interface XMLStreamReader

isExtender

public static boolean isExtender(int c)
Indicates whether the specified Unicode character matches the Extender production.

isLetter

public static boolean isLetter(int c)
Indicates whether the specified Unicode character matches the Letter production.

isNameCharacter

public static boolean isNameCharacter(int c,
                                      boolean xml11)
Indicates whether the specified Unicode character is a Name non-initial character.

isNameStartCharacter

public static boolean isNameStartCharacter(int c,
                                           boolean xml11)
Indicates whether the specified Unicode character is a Name start character.

isStandalone

public boolean isStandalone()
Returns the standalone flag declared in the XML declaration.
Specified by:
isStandalone in interface XMLStreamReader

isStartElement

public boolean isStartElement()
Indicates whether the current event is START_ELEMENT.
Specified by:
isStartElement in interface XMLStreamReader

isWhiteSpace

public boolean isWhiteSpace()
Indicates whether the current event is ignorable whitespace.
Specified by:
isWhiteSpace in interface XMLStreamReader

isXML11Char

public static boolean isXML11Char(int c)
Indicates whether the specified Unicode character is an XML 1.1 Char.

isXML11RestrictedChar

public static boolean isXML11RestrictedChar(int c)
Indicates whether the specified Unicode character is an XML 1.1 RestrictedChar.

main

public static void main(String[] args)
            throws Exception
Simple test harness for reading an XML file. args[0] is the filename of the XML file If args[1] is "-x", enable XInclude processing

next

public int next()
            throws XMLStreamException
Returns the next parsing event.
Specified by:
next in interface XMLStreamReader

nextTag

public int nextTag()
            throws XMLStreamException
Skips any ignorable whitespace, comments, and processing instructions until a START_ELEMENT or END_ELEMENT event is encountered.
Specified by:
nextTag in interface XMLStreamReader
Throws:
XMLStreamException - if an event of any other type is encountered

require

public void require(int type,
                    String namespaceURI,
                    String localName)
            throws XMLStreamException
Tests whether the current event is of the given type and namespace.
Specified by:
require in interface XMLStreamReader
Throws:
XMLStreamException - if the test fails

standaloneSet

public boolean standaloneSet()
Indicates whether the standalone flag was set in the document.
Specified by:
standaloneSet in interface XMLStreamReader

XMLParser.java -- Copyright (C) 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. Partly derived from code which carried the following notice: Copyright (c) 1997, 1998 by Microstar Software Ltd. AElfred is free for both commercial and non-commercial use and redistribution, provided that Microstar's copyright and disclaimer are retained intact. You are free to modify AElfred for your own use and to redistribute AElfred with your modifications, provided that the modifications are clearly documented. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of merchantability or fitness for a particular purpose. Please use it AT YOUR OWN RISK.