diva.util.xml
Class XmlReader

java.lang.Object
  extended by diva.util.LoggableOp
      extended by diva.util.xml.XmlReader

public class XmlReader
extends LoggableOp

An XmlReader reads a character stream and constructs the internal data of an XmlDocument. Internally, it contains an implementation of the Aelfred XmlHandler interface to parse the character stream and construct a tree of XmlElements.

Typically, when you parse an XML document, you do not want the parse to barf on badly-formed XML with an exception. XmlReader therefore prints error and warning messages to an output stream, which is by default System.out, but can be set to another stream to allow an application to capture this output (to display it in a GUI, for example). A "verbose" flag can be used to make the XML reader print out lots and lots of other information as well. Once the parse is complete, the caller should use the getErrorCount() method to see if there were any errors.

This parser is capable of resolving external entities using either public IDs or system IDs. System IDs are usually given as a complete URL to the given file. Public IDs are given as partial pathnames to which the parser prepends a locally known location for libraries of XML files. In Diva, the partial pathname (eg "graph.dtd") is looked up in the default resource bundle in the diva.resource package. DTDs that can be recognized as "public" must therefore be entered into diva/resource/Defaults.properties file. If both IDs are given, this parser tries to use the public ID first.

Version:
$Id: XmlReader.java 57040 2010-01-27 20:52:32Z cxh $
Author:
Steve Neuendorffer, John Reekie

Nested Class Summary
 class XmlReader.Handler
          Handler is an inner class that implements the Aelfred XmlHandler interface.
 
Field Summary
private  XmlDocument _document
          The current document being parsed into
private  XmlParser _parser
          The current Aelfred parser
private  XmlElement _root
          The root of the current parse tree.
 
Fields inherited from class diva.util.LoggableOp
_verbose
 
Constructor Summary
XmlReader()
           
 
Method Summary
 int getLineNumber()
          Get the current line number.
 void parse(XmlDocument document)
          Parse the given document from the URL it contains.
 void parse(XmlDocument document, java.io.InputStream in)
          Parse the given document from the given input stream, but using the given URL to resolve external references.
 void parse(XmlDocument document, java.io.Reader in)
          Parse the given document from the given reader, but using the given URL to resolve external references.
private  void parse(XmlDocument document, java.net.URL systemId, java.net.URL publicId, java.io.Reader reader, java.io.InputStream stream, java.lang.String encoding)
          Parse the given document given a bunch of parameters.
 java.lang.String printEntityType(java.lang.String name)
          Print the type of an entity.
 
Methods inherited from class diva.util.LoggableOp
getErrorCount, getErrorStream, getWarningCount, indent, isVerbose, logError, logError, logError, logInfo, logInfo, logWarning, logWarning, logWarning, reset, setErrorStream, setVerbose, unindent
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_parser

private XmlParser _parser
The current Aelfred parser


_document

private XmlDocument _document
The current document being parsed into


_root

private XmlElement _root
The root of the current parse tree.

Constructor Detail

XmlReader

public XmlReader()
Method Detail

getLineNumber

public int getLineNumber()
Get the current line number.

Overrides:
getLineNumber in class LoggableOp

parse

public void parse(XmlDocument document)
           throws java.lang.Exception
Parse the given document from the URL it contains. If any errors or warnings occur in the XML, they will have already been printed to the error stream (see setErrorStream()). After calling this method, the caller must check getErrorCount() to see if any errors occurred, otherwise the XmlDocument may not be well-formed. Note that for XML errors, this method will not throw an exception; however, for parser internal errors or I/O exceptions, this method will throw an exception so parsing stops immediately.

Parameters:
document - The document to be parsed.
Throws:
java.lang.Exception - If the parser fails internally. This indicates a severe error, such as an I/O error, not an XML error.

parse

public void parse(XmlDocument document,
                  java.io.InputStream in)
           throws java.lang.Exception
Parse the given document from the given input stream, but using the given URL to resolve external references.

Parameters:
document - The document to be parsed.
in - The input stream.
Throws:
java.lang.Exception - If the parser fails internally. This indicates a severe error, such as an I/O error, not an XML error.
See Also:
parse(XmlDocument)

parse

public void parse(XmlDocument document,
                  java.io.Reader in)
           throws java.lang.Exception
Parse the given document from the given reader, but using the given URL to resolve external references.

Parameters:
document - The document to be parsed.
in - The Reader.
Throws:
java.lang.Exception - If the parser fails internally. This indicates a severe error, such as an I/O error, not an XML error.
See Also:
parse(XmlDocument)

printEntityType

public java.lang.String printEntityType(java.lang.String name)
Print the type of an entity.


parse

private void parse(XmlDocument document,
                   java.net.URL systemId,
                   java.net.URL publicId,
                   java.io.Reader reader,
                   java.io.InputStream stream,
                   java.lang.String encoding)
            throws java.lang.Exception
Parse the given document given a bunch of parameters. Do some messing around to figure out which version of parse to call in the Aelfred parser.

Throws:
java.lang.Exception