Package uk.ac.starlink.datanode.nodes
Class XMLDocument
java.lang.Object
uk.ac.starlink.datanode.nodes.XMLDocument
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 -
Constructor Summary
ConstructorsConstructorDescriptionXMLDocument
(uk.ac.starlink.util.DataSource datsrc) Constructs a new XMLDocument from a DataSource. -
Method Summary
Modifier and TypeMethodDescriptionconstructDOM
(boolean validate) Convenience method to get a DOM from this document, which either succeeds or throws a NoSuchDataException.uk.ac.starlink.util.DataSource
static String
getEncoding
(byte[] magic) Returns what appears to be the encoding of the XML stream which starts with a given magic number.getName()
static boolean
isMagic
(byte[] magic) This tests for the likely start of an XML file.parseToDOM
(boolean validate, ErrorHandler ehandler) Performs a parse on the data source represented by this object and returns the resulting DOM.
-
Field Details
-
MAGICS
-
ENCODINGS
-
-
Constructor Details
-
XMLDocument
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
-
getTopNamespaceURI
-
getTopAttributes
-
getNamespaces
-
getMessages
-
getSystemId
-
getPublicId
-
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 validatingehandler
- handler for parse errors (may be null)- Returns:
- DOM source containing the document this object describes
- Throws:
SAXException
IOException
-
constructDOM
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
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 stringsMAGICS
). The result will be one of the encoding names listed inENCODINGS
, 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
-