Class XMLDocument

java.lang.Object
uk.ac.starlink.datanode.nodes.XMLDocument

public class XMLDocument extends Object
Holds a DataSource which is known to contain an XML document. DataNode implementations can provide constructors which take one of these. The idea is that the node building machinery finds out whether a DataSource represents XML (perhaps by doing a SAX parse) and stashes some basic information about it in this object - name of top-level element, IDs of the DTD, maybe a set of validation errors etc - so that the quick view can display them without any further work. It deliberately doesn't store the DOM, since keeping DOMs for all the nodes we've encountered can take up too much memory - it violates DataNode's rule about allocating large memory resources in a node's constructor before getChildren or maybe configureDetail have been called. DataNode constructors which do operate on an XMLDocument should not construct and cache a DOM themselves, though other DataNode methods may do so.
Author:
Mark Taylor (Starlink)
  • Field Details

    • MAGICS

      public static final String[] MAGICS
    • ENCODINGS

      public static final String[] ENCODINGS
  • Constructor Details

    • XMLDocument

      public XMLDocument(uk.ac.starlink.util.DataSource datsrc) throws NoSuchDataException
      Constructs a new XMLDocument from a DataSource. Enough work is done to check that the document in question appears to contain XML - if it does not, a NoSuchDataException is thrown.
      Parameters:
      datsrc - data source
      Throws:
      NoSuchDataException - if datsrc doesn't contain XML
  • Method Details

    • getDataSource

      public uk.ac.starlink.util.DataSource getDataSource()
    • getTopLocalName

      public String getTopLocalName()
    • getTopNamespaceURI

      public String getTopNamespaceURI()
    • getTopAttributes

      public Attributes getTopAttributes()
    • getNamespaces

      public Collection getNamespaces()
    • getMessages

      public List getMessages()
    • getSystemId

      public String getSystemId()
    • getPublicId

      public String getPublicId()
    • getName

      public String getName()
    • parseToDOM

      public DOMSource parseToDOM(boolean validate, ErrorHandler ehandler) throws SAXException, IOException
      Performs a parse on the data source represented by this object and returns the resulting DOM. Note that this object does not cache such a DOM (the point of it is to stop DOMs being held on to), so only do this if you need it, and if you're going to carry on needing it, cache it.
      Parameters:
      validate - whether the parse should be validating
      ehandler - handler for parse errors (may be null)
      Returns:
      DOM source containing the document this object describes
      Throws:
      SAXException
      IOException
    • constructDOM

      public DOMSource constructDOM(boolean validate) throws NoSuchDataException
      Convenience method to get a DOM from this document, which either succeeds or throws a NoSuchDataException.
      Throws:
      NoSuchDataException
    • isMagic

      public static boolean isMagic(byte[] magic)
      This tests for the likely start of an XML file. It's just a guess though - it can come up with false positives and (worse) false negatives.
      Parameters:
      magic - buffer containing the first few bytes of the stream
      Returns:
      true iff this looks like an XML file
    • getEncoding

      public static String getEncoding(byte[] magic)
      Returns what appears to be the encoding of the XML stream which starts with a given magic number. This is based on how we expect an XML stream to start in terms of Unicode characters (one of the strings MAGICS). The result will be one of the encoding names listed in ENCODINGS, or null if it doesn't look like the start of an XML stream in any of these encodings.
      Parameters:
      magic - buffer containing the first few bytes of the stream
      Returns:
      name of a supported encoding in which this looks like XML