Class XMLDocument


  • public class XMLDocument
    extends java.lang.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 Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String[] ENCODINGS  
      static java.lang.String[] MAGICS  
    • Constructor Summary

      Constructors 
      Constructor Description
      XMLDocument​(uk.ac.starlink.util.DataSource datsrc)
      Constructs a new XMLDocument from a DataSource.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      javax.xml.transform.dom.DOMSource constructDOM​(boolean validate)
      Convenience method to get a DOM from this document, which either succeeds or throws a NoSuchDataException.
      uk.ac.starlink.util.DataSource getDataSource()  
      static java.lang.String getEncoding​(byte[] magic)
      Returns what appears to be the encoding of the XML stream which starts with a given magic number.
      java.util.List getMessages()  
      java.lang.String getName()  
      java.util.Collection getNamespaces()  
      java.lang.String getPublicId()  
      java.lang.String getSystemId()  
      org.xml.sax.Attributes getTopAttributes()  
      java.lang.String getTopLocalName()  
      java.lang.String getTopNamespaceURI()  
      static boolean isMagic​(byte[] magic)
      This tests for the likely start of an XML file.
      javax.xml.transform.dom.DOMSource parseToDOM​(boolean validate, org.xml.sax.ErrorHandler ehandler)
      Performs a parse on the data source represented by this object and returns the resulting DOM.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • MAGICS

        public static final java.lang.String[] MAGICS
      • ENCODINGS

        public static final java.lang.String[] ENCODINGS
    • Constructor Detail

      • 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 Detail

      • getDataSource

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

        public java.lang.String getTopLocalName()
      • getTopNamespaceURI

        public java.lang.String getTopNamespaceURI()
      • getTopAttributes

        public org.xml.sax.Attributes getTopAttributes()
      • getNamespaces

        public java.util.Collection getNamespaces()
      • getMessages

        public java.util.List getMessages()
      • getSystemId

        public java.lang.String getSystemId()
      • getPublicId

        public java.lang.String getPublicId()
      • getName

        public java.lang.String getName()
      • parseToDOM

        public javax.xml.transform.dom.DOMSource parseToDOM​(boolean validate,
                                                            org.xml.sax.ErrorHandler ehandler)
                                                     throws org.xml.sax.SAXException,
                                                            java.io.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:
        org.xml.sax.SAXException
        java.io.IOException
      • constructDOM

        public javax.xml.transform.dom.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 java.lang.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