Top Description Inners Fields Constructors Methods
org.mortbay.xml

public Class XmlParser

extends Object
Class Inheritance
Imports
java.io.File, .IOException, .InputStream, java.net.URL, java.util.AbstractList, .ArrayList, .HashMap, .Iterator, .Map, .NoSuchElementException, .Stack, .StringTokenizer, javax.xml.parsers.SAXParser, .SAXParserFactory, org.mortbay.log.Log, org.mortbay.util.LazyList, org.xml.sax.Attributes, .ContentHandler, .InputSource, .SAXException, .SAXParseException, .XMLReader, org.xml.sax.helpers.DefaultHandler

XML Parser wrapper. This class wraps any standard JAXP1.1 parser with convieniant error and entity handlers and a mini dom-like document tree.

By default, the parser is created as a validating parser only if xerces is present. This can be configured by setting the "org.mortbay.xml.XmlParser.Validating" system property.

Author
Greg Wilkins (gregw)

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
XmlParser.Attribute

XML Attribute.

private class
public static class
XmlParser.Node

XML Node.

private class

Field Summary

Modifier and TypeField and Description
private String
private Map<K, V>
private Stack<E>
private SAXParser
private Map<K, V>
private String
private Object

Constructor Summary

AccessConstructor and Description
public
XmlParser()

Construct

public
XmlParser(boolean validating)

Constructor.

Method Summary

Modifier and TypeMethod and Description
public synchronized void
addContentHandler(String
Tag local or q name.
trigger
,
ContentHandler
SAX ContentHandler
observer
)

Add a ContentHandler.

public String
public String

Returns:

Returns the xpath.
getXpath
()

public synchronized XmlParser.Node
public synchronized XmlParser.Node
parse(String url)

Parse String URL.

public synchronized XmlParser.Node
parse(File file)

Parse File.

public synchronized XmlParser.Node
parse(InputStream in)

Parse InputStream.

public synchronized void
redirectEntity(String name, URL entity)

public void
setValidating(boolean validating)

public void
setXpath(String
The xpath to set.
xpath
)

Set an XPath A very simple subset of xpath is supported to select a partial tree.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

_dtdback to summary
private String _dtd
_observerMapback to summary
private Map<K, V> _observerMap
_observersback to summary
private Stack<E> _observers
_parserback to summary
private SAXParser _parser
_redirectMapback to summary
private Map<K, V> _redirectMap
_xpathback to summary
private String _xpath
_xpathsback to summary
private Object _xpaths

Constructor Detail

XmlParserback to summary
public XmlParser()

Construct

XmlParserback to summary
public XmlParser(boolean validating)

Constructor.

Method Detail

addContentHandlerback to summary
public synchronized void addContentHandler(String trigger, ContentHandler observer)

Add a ContentHandler. Add an additional _content handler that is triggered on a tag name. SAX events are passed to the ContentHandler provided from a matching start element to the corresponding end element. Only a single _content handler can be registered against each tag.

Parameters
trigger:String

Tag local or q name.

observer:ContentHandler

SAX ContentHandler

getDTDback to summary
public String getDTD()
getXpathback to summary
public String getXpath()
Returns:String

Returns the xpath.

parseback to summary
public synchronized XmlParser.Node parse(InputSource source) throws IOException, SAXException
parseback to summary
public synchronized XmlParser.Node parse(String url) throws IOException, SAXException

Parse String URL.

parseback to summary
public synchronized XmlParser.Node parse(File file) throws IOException, SAXException

Parse File.

parseback to summary
public synchronized XmlParser.Node parse(InputStream in) throws IOException, SAXException

Parse InputStream.

redirectEntityback to summary
public synchronized void redirectEntity(String name, URL entity)
setValidatingback to summary
public void setValidating(boolean validating)
setXpathback to summary
public void setXpath(String xpath)

Set an XPath A very simple subset of xpath is supported to select a partial tree. Currently only path like "/node1/nodeA | /node1/nodeB" are supported.

Parameters
xpath:String

The xpath to set.

org.mortbay.xml back to summary

public Class XmlParser.Attribute

extends Object
Class Inheritance

XML Attribute.

Field Summary

Modifier and TypeField and Description
private String
private String

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public String
public String
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

_nameback to summary
private String _name
_valueback to summary
private String _value

Constructor Detail

Attributeback to summary
pack-priv Attribute(String n, String v)

Method Detail

getNameback to summary
public String getName()
getValueback to summary
public String getValue()
org.mortbay.xml back to summary

private Class XmlParser.Handler

extends DefaultHandler
Class Inheritance

Field Summary

Modifier and TypeField and Description
private XmlParser.Node
pack-priv SAXParseException
private XmlParser.NoopHandler
pack-priv XmlParser.Node

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
characters(char[]
The characters.
buf
,
int
The start position in the character array.
offset
,
int
The number of characters to use from the character array.
len
)

Overrides org.xml.sax.helpers.DefaultHandler.characters.

Implements org.xml.sax.ContentHandler.characters.

Receive notification of character data inside an element.
pack-priv void
public void
endElement(String
The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.
uri
,
String
The local name (without prefix), or the empty string if Namespace processing is not being performed.
localName
,
String
The qualified name (with prefix), or the empty string if qualified names are not available.
qName
)

Overrides org.xml.sax.helpers.DefaultHandler.endElement.

Implements org.xml.sax.ContentHandler.endElement.

Receive notification of the end of an element.
public void
error(SAXParseException
The error information encoded as an exception.
ex
)

Overrides org.xml.sax.helpers.DefaultHandler.error.

Implements org.xml.sax.ErrorHandler.error.

Receive notification of a recoverable parser error.
public void
fatalError(SAXParseException
The error information encoded as an exception.
ex
)

Overrides org.xml.sax.helpers.DefaultHandler.fatalError.

Implements org.xml.sax.ErrorHandler.fatalError.

Report a fatal XML parsing error.
private String
public void
ignorableWhitespace(char[]
The whitespace characters.
buf
,
int
The start position in the character array.
offset
,
int
The number of characters to use from the character array.
len
)

Overrides org.xml.sax.helpers.DefaultHandler.ignorableWhitespace.

Implements org.xml.sax.ContentHandler.ignorableWhitespace.

Receive notification of ignorable whitespace in element content.
public InputSource
resolveEntity(String
The public identifier, or null if none is available.
pid
,
String
The system identifier provided in the XML document.
sid
)

Overrides org.xml.sax.helpers.DefaultHandler.resolveEntity.

Implements org.xml.sax.EntityResolver.resolveEntity.

Resolve an external entity.
public void
startElement(String
The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.
uri
,
String
The local name (without prefix), or the empty string if Namespace processing is not being performed.
localName
,
String
The qualified name (with prefix), or the empty string if qualified names are not available.
qName
,
Attributes
The attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.
attrs
)

Overrides org.xml.sax.helpers.DefaultHandler.startElement.

Implements org.xml.sax.ContentHandler.startElement.

Receive notification of the start of an element.
public void
warning(SAXParseException
The warning information encoded as an exception.
ex
)

Overrides org.xml.sax.helpers.DefaultHandler.warning.

Implements org.xml.sax.ErrorHandler.warning.

Receive notification of a parser warning.
Inherited from org.xml.sax.helpers.DefaultHandler:
endDocumentendPrefixMappingnotationDeclprocessingInstructionsetDocumentLocatorskippedEntitystartDocumentstartPrefixMappingunparsedEntityDecl

Field Detail

_contextback to summary
private XmlParser.Node _context
_errorback to summary
pack-priv SAXParseException _error
_noopback to summary
private XmlParser.NoopHandler _noop
_topback to summary
pack-priv XmlParser.Node _top

Constructor Detail

Handlerback to summary
pack-priv Handler()

Method Detail

charactersback to summary
public void characters(char[] buf, int offset, int len) throws SAXException

Overrides org.xml.sax.helpers.DefaultHandler.characters.

Implements org.xml.sax.ContentHandler.characters.

Doc from org.xml.sax.helpers.DefaultHandler.characters.

Receive notification of character data inside an element.

By default, do nothing. Application writers may override this method to take specific actions for each chunk of character data (such as adding the data to a node or buffer, or printing it to a file).

Parameters
buf:char[]

The characters.

offset:int

The start position in the character array.

len:int

The number of characters to use from the character array.

Exceptions
SAXException:
Any SAX exception, possibly wrapping another exception.
clearback to summary
pack-priv void clear()
endElementback to summary
public void endElement(String uri, String localName, String qName) throws SAXException

Overrides org.xml.sax.helpers.DefaultHandler.endElement.

Implements org.xml.sax.ContentHandler.endElement.

Doc from org.xml.sax.helpers.DefaultHandler.endElement.

Receive notification of the end of an element.

By default, do nothing. Application writers may override this method in a subclass to take specific actions at the end of each element (such as finalising a tree node or writing output to a file).

Parameters
uri:String

The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.

localName:String

The local name (without prefix), or the empty string if Namespace processing is not being performed.

qName:String

The qualified name (with prefix), or the empty string if qualified names are not available.

Exceptions
SAXException:
Any SAX exception, possibly wrapping another exception.
errorback to summary
public void error(SAXParseException ex) throws SAXException

Overrides org.xml.sax.helpers.DefaultHandler.error.

Implements org.xml.sax.ErrorHandler.error.

Doc from org.xml.sax.helpers.DefaultHandler.error.

Receive notification of a recoverable parser error.

The default implementation does nothing. Application writers may override this method in a subclass to take specific actions for each error, such as inserting the message in a log file or printing it to the console.

Parameters
ex:SAXParseException

The error information encoded as an exception.

Exceptions
SAXException:
Any SAX exception, possibly wrapping another exception.
fatalErrorback to summary
public void fatalError(SAXParseException ex) throws SAXException

Overrides org.xml.sax.helpers.DefaultHandler.fatalError.

Implements org.xml.sax.ErrorHandler.fatalError.

Doc from org.xml.sax.helpers.DefaultHandler.fatalError.

Report a fatal XML parsing error.

The default implementation throws a SAXParseException. Application writers may override this method in a subclass if they need to take specific actions for each fatal error (such as collecting all of the errors into a single report): in any case, the application must stop all regular processing when this method is invoked, since the document is no longer reliable, and the parser may no longer report parsing events.

Parameters
ex:SAXParseException

The error information encoded as an exception.

Exceptions
SAXException:
Any SAX exception, possibly wrapping another exception.
getLocationStringback to summary
private String getLocationString(SAXParseException ex)
ignorableWhitespaceback to summary
public void ignorableWhitespace(char[] buf, int offset, int len) throws SAXException

Overrides org.xml.sax.helpers.DefaultHandler.ignorableWhitespace.

Implements org.xml.sax.ContentHandler.ignorableWhitespace.

Doc from org.xml.sax.helpers.DefaultHandler.ignorableWhitespace.

Receive notification of ignorable whitespace in element content.

By default, do nothing. Application writers may override this method to take specific actions for each chunk of ignorable whitespace (such as adding data to a node or buffer, or printing it to a file).

Parameters
buf:char[]

The whitespace characters.

offset:int

The start position in the character array.

len:int

The number of characters to use from the character array.

Exceptions
SAXException:
Any SAX exception, possibly wrapping another exception.
resolveEntityback to summary
public InputSource resolveEntity(String pid, String sid)

Overrides org.xml.sax.helpers.DefaultHandler.resolveEntity.

Implements org.xml.sax.EntityResolver.resolveEntity.

Doc from org.xml.sax.helpers.DefaultHandler.resolveEntity.

Resolve an external entity.

Always return null, so that the parser will use the system identifier provided in the XML document. This method implements the SAX default behaviour: application writers can override it in a subclass to do special translations such as catalog lookups or URI redirection.

Parameters
pid:String

The public identifier, or null if none is available.

sid:String

The system identifier provided in the XML document.

Returns:InputSource

The new input source, or null to require the default behaviour.

startElementback to summary
public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException

Overrides org.xml.sax.helpers.DefaultHandler.startElement.

Implements org.xml.sax.ContentHandler.startElement.

Doc from org.xml.sax.helpers.DefaultHandler.startElement.

Receive notification of the start of an element.

By default, do nothing. Application writers may override this method in a subclass to take specific actions at the start of each element (such as allocating a new tree node or writing output to a file).

Parameters
uri:String

The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.

localName:String

The local name (without prefix), or the empty string if Namespace processing is not being performed.

qName:String

The qualified name (with prefix), or the empty string if qualified names are not available.

attrs:Attributes

The attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.

Exceptions
SAXException:
Any SAX exception, possibly wrapping another exception.
warningback to summary
public void warning(SAXParseException ex)

Overrides org.xml.sax.helpers.DefaultHandler.warning.

Implements org.xml.sax.ErrorHandler.warning.

Doc from org.xml.sax.helpers.DefaultHandler.warning.

Receive notification of a parser warning.

The default implementation does nothing. Application writers may override this method in a subclass to take specific actions for each warning, such as inserting the message in a log file or printing it to the console.

Parameters
ex:SAXParseException

The warning information encoded as an exception.

org.mortbay.xml back to summary

public Class XmlParser.Node

extends AbstractList
Class Inheritance

XML Node. Represents an XML element with optional attributes and ordered content.

Field Summary

Modifier and TypeField and Description
private XmlParser.Attribute[]
private boolean
private ArrayList<E>
pack-priv XmlParser.Node
private String
private String
Inherited from java.util.AbstractList:
modCount

Constructor Summary

AccessConstructor and Description
pack-priv
Node(XmlParser.Node parent, String tag, Attributes attrs)

Method Summary

Modifier and TypeMethod and Description
public void
add(int
index at which the specified element is to be inserted
i
,
Object
element to be inserted
o
)

Overrides java.util.AbstractList.add.

Implements java.util.List.add.

Inserts the specified element at the specified position in this list (optional operation).
public void
clear()

Overrides java.util.AbstractList.clear.

Implements java.util.List.clear, java.util.Collection.clear.

Removes all of the elements from this list (optional operation).
public Object

Returns:

Node or String.
get
(int
index of the element to return
i
)

Implements abstract java.util.AbstractList.get.

Implements java.util.List.get.

Get the ith child node or content.
public XmlParser.Node

Returns:

Node or null.
get
(String tag)

Get the first child node with the tag.

public String

Returns:

attribute or null.
getAttribute
(String name)

Get an element attribute.

public String

Returns:

attribute or null.
getAttribute
(String name, String dft)

Get an element attribute.

public XmlParser.Attribute[]
getAttributes()

Get an array of element attributes.

public XmlParser.Node
public String
public String

Returns:

results of get(tag).toString(tags).
getString
(String
The tag to get
tag
,
boolean
IF true, tags are included in the value.
tags
,
boolean
If true, trim the value.
trim
)

Get a tag as a string.

public String
public Iterator<E>

Returns:

Iterator over all child nodes with the specified tag.
iterator
(final String
The tag of the nodes.
tag
)

Iterator over named child nodes.

public int
size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.List.size, java.util.Collection.size.

Get the number of children nodes.
public synchronized String
toString()

Overrides java.util.AbstractCollection.toString.

Returns a string representation of this collection.
public synchronized String
toString(boolean
If false, only _content is shown.
tag
)

Convert to a string.

public synchronized String
toString(boolean
If false, only _content is shown.
tag
,
boolean trim)

Convert to a string.

private synchronized void
toString(StringBuilder buf, boolean tag)

Inherited from java.util.AbstractList:
addaddAllequalshashCodeindexOfiteratorlastIndexOflistIteratorlistIteratorremoveremoveRangesetsubList

Field Detail

_attrsback to summary
private XmlParser.Attribute[] _attrs
_lastStringback to summary
private boolean _lastString
_listback to summary
private ArrayList<E> _list
_parentback to summary
pack-priv XmlParser.Node _parent
_pathback to summary
private String _path
_tagback to summary
private String _tag

Constructor Detail

Nodeback to summary
pack-priv Node(XmlParser.Node parent, String tag, Attributes attrs)

Method Detail

addback to summary
public void add(int i, Object o)

Overrides java.util.AbstractList.add.

Implements java.util.List.add.

Doc from java.util.AbstractList.add.

Inserts the specified element at the specified position in this list (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Parameters
i:int

Doc from java.util.List.add.

index at which the specified element is to be inserted

o:Object

Doc from java.util.List.add.

element to be inserted

clearback to summary
public void clear()

Overrides java.util.AbstractList.clear.

Implements java.util.List.clear, java.util.Collection.clear.

Doc from java.util.AbstractList.clear.

Removes all of the elements from this list (optional operation). The list will be empty after this call returns.

getback to summary
public Object get(int i)

Implements abstract java.util.AbstractList.get.

Implements java.util.List.get.

Get the ith child node or content.

Parameters
i:int

Doc from java.util.List.get.

index of the element to return

Returns:Object

Node or String.

getback to summary
public XmlParser.Node get(String tag)

Get the first child node with the tag.

Returns:XmlParser.Node

Node or null.

getAttributeback to summary
public String getAttribute(String name)

Get an element attribute.

Returns:String

attribute or null.

getAttributeback to summary
public String getAttribute(String name, String dft)

Get an element attribute.

Returns:String

attribute or null.

getAttributesback to summary
public XmlParser.Attribute[] getAttributes()

Get an array of element attributes.

getParentback to summary
public XmlParser.Node getParent()
getPathback to summary
public String getPath()
getStringback to summary
public String getString(String tag, boolean tags, boolean trim)

Get a tag as a string.

Parameters
tag:String

The tag to get

tags:boolean

IF true, tags are included in the value.

trim:boolean

If true, trim the value.

Returns:String

results of get(tag).toString(tags).

getTagback to summary
public String getTag()
iteratorback to summary
public Iterator<E> iterator(final String tag)

Iterator over named child nodes.

Parameters
tag:String

The tag of the nodes.

Returns:Iterator<E>

Iterator over all child nodes with the specified tag.

sizeback to summary
public int size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.List.size, java.util.Collection.size.

Get the number of children nodes.

Returns:int

Doc from java.util.List.size.

the number of elements in this list

toStringback to summary
public synchronized String toString()

Overrides java.util.AbstractCollection.toString.

Doc from java.util.AbstractCollection.toString.

Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String#valueOf(Object).

Returns:String

a string representation of this collection

toStringback to summary
public synchronized String toString(boolean tag)

Convert to a string.

Parameters
tag:boolean

If false, only _content is shown.

toStringback to summary
public synchronized String toString(boolean tag, boolean trim)

Convert to a string.

Parameters
tag:boolean

If false, only _content is shown.

toStringback to summary
private synchronized void toString(StringBuilder buf, boolean tag)
org.mortbay.xml back to summary

private Class XmlParser.NoopHandler

extends DefaultHandler
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv int
pack-priv XmlParser.Handler

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
endElement(String
The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.
uri
,
String
The local name (without prefix), or the empty string if Namespace processing is not being performed.
localName
,
String
The qualified name (with prefix), or the empty string if qualified names are not available.
qName
)

Overrides org.xml.sax.helpers.DefaultHandler.endElement.

Implements org.xml.sax.ContentHandler.endElement.

Receive notification of the end of an element.
public void
startElement(String
The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.
uri
,
String
The local name (without prefix), or the empty string if Namespace processing is not being performed.
localName
,
String
The qualified name (with prefix), or the empty string if qualified names are not available.
qName
,
Attributes
The attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.
attrs
)

Overrides org.xml.sax.helpers.DefaultHandler.startElement.

Implements org.xml.sax.ContentHandler.startElement.

Receive notification of the start of an element.
Inherited from org.xml.sax.helpers.DefaultHandler:
charactersendDocumentendPrefixMappingerrorfatalErrorignorableWhitespacenotationDeclprocessingInstructionresolveEntitysetDocumentLocatorskippedEntitystartDocumentstartPrefixMappingunparsedEntityDeclwarning

Field Detail

_depthback to summary
pack-priv int _depth
_nextback to summary
pack-priv XmlParser.Handler _next

Constructor Detail

NoopHandlerback to summary
pack-priv NoopHandler(XmlParser.Handler next)

Method Detail

endElementback to summary
public void endElement(String uri, String localName, String qName) throws SAXException

Overrides org.xml.sax.helpers.DefaultHandler.endElement.

Implements org.xml.sax.ContentHandler.endElement.

Doc from org.xml.sax.helpers.DefaultHandler.endElement.

Receive notification of the end of an element.

By default, do nothing. Application writers may override this method in a subclass to take specific actions at the end of each element (such as finalising a tree node or writing output to a file).

Parameters
uri:String

The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.

localName:String

The local name (without prefix), or the empty string if Namespace processing is not being performed.

qName:String

The qualified name (with prefix), or the empty string if qualified names are not available.

Exceptions
SAXException:
Any SAX exception, possibly wrapping another exception.
startElementback to summary
public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException

Overrides org.xml.sax.helpers.DefaultHandler.startElement.

Implements org.xml.sax.ContentHandler.startElement.

Doc from org.xml.sax.helpers.DefaultHandler.startElement.

Receive notification of the start of an element.

By default, do nothing. Application writers may override this method in a subclass to take specific actions at the start of each element (such as allocating a new tree node or writing output to a file).

Parameters
uri:String

The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.

localName:String

The local name (without prefix), or the empty string if Namespace processing is not being performed.

qName:String

The qualified name (with prefix), or the empty string if qualified names are not available.

attrs:Attributes

The attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.

Exceptions
SAXException:
Any SAX exception, possibly wrapping another exception.