Top Description Inners Fields Constructors Methods
org.hsqldb.jdbc

public Class JDBCSQLXML

extends Object
implements SQLXML
Class Inheritance
All Implemented Interfaces
java.sql.SQLXML, java.lang.AutoCloseable
Imports
java.io.ByteArrayInputStream, .ByteArrayOutputStream, .CharArrayReader, .Closeable, .IOException, .InputStream, .InputStreamReader, .OutputStream, .OutputStreamWriter, .Reader, .StringReader, .Writer, java.lang.reflect.Constructor, .InvocationTargetException, java.nio.charset.Charset, .StandardCharsets, java.sql.SQLException, .SQLFeatureNotSupportedException, .SQLXML, java.util.ArrayList, .List, java.util.concurrent.ArrayBlockingQueue, .ExecutorService, .ThreadPoolExecutor, .TimeUnit, java.util.zip.GZIPInputStream, .GZIPOutputStream, javax.xml.parsers.DocumentBuilder, .DocumentBuilderFactory, .FactoryConfigurationError, .ParserConfigurationException, javax.xml.stream.XMLEventReader, .XMLInputFactory, .XMLOutputFactory, .XMLStreamException, .XMLStreamWriter, javax.xml.transform.Result, .Source, .Transformer, .TransformerConfigurationException, .TransformerException, .TransformerFactory, .TransformerFactoryConfigurationError, javax.xml.transform.dom.DOMResult, .DOMSource, javax.xml.transform.sax.SAXResult, .SAXSource, javax.xml.transform.stax.StAXResult, .StAXSource, javax.xml.transform.stream.StreamResult, .StreamSource, org.hsqldb.error.ErrorCode, org.hsqldb.lib.ClosableByteArrayOutputStream, .StringConverter, org.w3c.dom.DOMException, .DOMImplementation, .Document, .DocumentType, .Element, .EntityReference, .Node, .ProcessingInstruction, .Text, org.w3c.dom.bootstrap.DOMImplementationRegistry, org.xml.sax.Attributes, .ContentHandler, .InputSource, .Locator, .SAXException

The mapping in the JavaTM programming language for the SQL XML type. XML is a built-in type that stores an XML value as a column value in a row of a database table. By default, drivers implement an SQLXML object as a logical pointer to the XML data rather than the data itself. An SQLXML object is valid for the duration of the transaction in which it was created.

The SQLXML interface provides methods for accessing the XML value as a String, a Reader or Writer, or as a Stream. The XML value may also be accessed through a Source or set as a Result, which are used with XML Parser APIs such as DOM, SAX, and StAX, as well as with XSLT transforms and XPath evaluations.

Methods in the interfaces ResultSet, CallableStatement, and PreparedStatement, such as getSQLXML allow a programmer to access an XML value. In addition, this interface has methods for updating an XML value.

The XML value of the SQLXML instance may be obtained as a BinaryStream using

  SQLXML sqlxml = resultSet.getSQLXML(column);
  InputStream binaryStream = sqlxml.getBinaryStream();
For example, to parse an XML value with a DOM parser:
  DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  Document result = parser.parse(binaryStream);
or to parse an XML value with a SAX parser to your handler:
  SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
  parser.parse(binaryStream, myHandler);
or to parse an XML value with a StAX parser:
  XMLInputFactory factory = XMLInputFactory.newInstance();
  XMLStreamReader streamReader = factory.createXMLStreamReader(binaryStream);

Because databases may use an optimized representation for the XML, accessing the value through getSource() and setResult() can lead to improved processing performance without serializing to a stream representation and parsing the XML.

For example, to obtain a DOM Document Node:

  DOMSource domSource = sqlxml.getSource(DOMSource.class);
  Document document = (Document) domSource.getNode();
or to set the value to a DOM Document Node to myNode:
  DOMResult domResult = sqlxml.setResult(DOMResult.class);
  domResult.setNode(myNode);
or, to send SAX events to your handler:
  SAXSource saxSource = sqlxml.getSource(SAXSource.class);
  XMLReader xmlReader = saxSource.getXMLReader();
  xmlReader.setContentHandler(myHandler);
  xmlReader.parse(saxSource.getInputSource());
or, to set the result value from SAX events:
  SAXResult saxResult = sqlxml.setResult(SAXResult.class);
  ContentHandler contentHandler = saxResult.getXMLReader().getContentHandler();
  contentHandler.startDocument();
  // set the XML elements and attributes into the result
  contentHandler.endDocument();
or, to obtain StAX events:
  StAXSource staxSource = sqlxml.getSource(StAXSource.class);
  XMLStreamReader streamReader = staxSource.getXMLStreamReader();
or, to set the result value from StAX events:
  StAXResult staxResult = sqlxml.setResult(StAXResult.class);
  XMLStreamWriter streamWriter = staxResult.getXMLStreamWriter();
or, to perform XSLT transformations on the XML value using the XSLT in xsltFile output to file resultFile:
  File xsltFile = new File("a.xslt");
  File myFile = new File("result.xml");
  Transformer xslt = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFile));
  Source source = sqlxml.getSource(null);
  Result result = new StreamResult(myFile);
  xslt.transform(source, result);
or, to evaluate an XPath expression on the XML value:
  XPath xpath = XPathFactory.newInstance().newXPath();
  DOMSource domSource = sqlxml.getSource(DOMSource.class);
  Document document = (Document) domSource.getNode();
  String expression = "/foo/@bar";
  String barValue = xpath.evaluate(expression, document);
To set the XML value to be the result of an XSLT transform:
  File sourceFile = new File("source.xml");
  Transformer xslt = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFile));
  Source streamSource = new StreamSource(sourceFile);
  Result result = sqlxml.setResult(null);
  xslt.transform(streamSource, result);
Any Source can be transformed to a Result using the identity transform specified by calling newTransformer():
  Transformer identity = TransformerFactory.newInstance().newTransformer();
  Source source = sqlxml.getSource(null);
  File myFile = new File("result.xml");
  Result result = new StreamResult(myFile);
  identity.transform(source, result);
To write the contents of a Source to standard output:
  Transformer identity = TransformerFactory.newInstance().newTransformer();
  Source source = sqlxml.getSource(null);
  Result result = new StreamResult(System.out);
  identity.transform(source, result);
To create a DOMSource from a DOMResult:
   DOMSource domSource = new DOMSource(domResult.getNode());

Incomplete or invalid XML values may cause an SQLException when set or the exception may occur when execute() occurs. All streams must be closed before execute() occurs or an SQLException will be thrown.

Reading and writing XML values to or from an SQLXML object can happen at most once. The conceptual states of readable and not readable determine if one of the reading APIs will return a value or throw an exception. The conceptual states of writable and not writable determine if one of the writing APIs will set a value or throw an exception.

The state moves from readable to not readable once free() or any of the reading APIs are called: getBinaryStream(), getCharacterStream(), getSource(), and getString(). Implementations may also change the state to not writable when this occurs.

The state moves from writable to not writable once free() or any of the writing APIs are called: setBinaryStream(), setCharacterStream(), setResult(), and setString(). Implementations may also change the state to not readable when this occurs.

All methods on the SQLXML interface must be fully implemented if the JDBC driver supports the data type.

HSQLDB-Specific Information:

Starting with HSQLDB 2.0, a rudimentary client-side SQLXML interface implementation (this class) is supported for local use when the product is built and run under JDK 1.6+ and the SQLXML instance is constructed as the result of calling JDBCConnection.createSQLXML().

SQLXML instances retrieved in such a fashion are initially write-only, with the lifecycle of read and write availability constrained in accordance with the documentation of the interface methods.

When build and run under JDK 1.6+, it is also possible to retrieve read-only SQLXML instances from JDBCResultSet.getSQLXML(...), given that the underlying data can be converted to an XML Document Object Model (DOM).

However, at the time of this writing (2007-06-12) it is not yet possible to store SQLXML objects directly into an HSQLDB database or to use them directly for HSQLDB statement parameterization purposes. This is because the SQLXML data type is not yet natively supported by the HSQLDB engine. Instead, a JDBCSQLXML instance must first be read as a string, binary input stream, character input stream and so on, which can then be used for such purposes.

Here is the current read/write availability lifecycle for JDBCSQLXML:

Lifecycle
Origin Initially After 1st Write After 1st Read After 1st Free
org.hsqldb.jdbc.JDBCConnection.createSQLXML() Write-only Read-only Not readable or writable Not readable or writable
org.hsqldb.jdbc.JDBCResultSet.getSQLXML(...) Read-only N/A Not readable or writable Not readable or writable
Author
Campbell Burnet (campbell-burnet@users dot sourceforge.net)
Since
JDK 1.6, HSQLDB 2.0
See Also
javax.xml.parsers, javax.xml.stream, javax.xml.transform, javax.xml.xpath

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected static class
JDBCSQLXML.Exceptions

Basically just a namespace to isolate SQLXML exception generation

protected static class
JDBCSQLXML.SAX2DOMBuilder

Builds a DOM from SAX events.

public static class
JDBCSQLXML.SAX2XMLStreamWriter

Writes to a XMLStreamWriter from SAX events.

Field Summary

Modifier and TypeField and Description
private JDBCSQLXML.SAX2DOMBuilder
builder

When non-null, the SAX ContentHandler currently in use to build this object's SQLXML value from a SAX event sequence.

private boolean
closed

Whether this object is closed.

private static String
private static DOMImplementation
private static DOMImplementationRegistry
private DOMResult
domResult

When non-null, the DOMResult currently in use to build this object's SQLXML value.

private static ThreadPoolExecutor
private volatile byte[]
gzdata

This object's SQLXML value as a GZIPed byte array

private static Transformer
private InputStream
inputStream

When non-null, the stream currently in use to read this object's SQLXML value as an octet sequence.

private ClosableByteArrayOutputStream
outputStream

When non-null, the stream currently in use to write this object's SQLXML value from an octet sequence.

private String
publicId

This object's public id

private boolean
readable

Whether it is possible to read this object's SQLXML value.

private String
systemId

This object's system id

private static TransformerFactory
private static final Charset
utf8Charset

Precomputed Charset to reduce octet to character sequence conversion charset lookup overhead.

private static ArrayBlockingQueue<Runnable>
private boolean
writable

Whether it is possible to write this object's SQLXML value.

Constructor Summary

AccessConstructor and Description
protected
JDBCSQLXML()

Constructs a new, initially write-only JDBCSQLXML object.

protected
JDBCSQLXML(byte[]
the octet sequence representing the SQLXML value
bytes
)

Constructs a new read-only JDBCSQLXML object from the given octet sequence.

protected
JDBCSQLXML(char[]
the character sequence representing the SQLXML value
chars
)

Constructs a new read-only JDBCSQLXML object from the given character sequence.

protected
JDBCSQLXML(Document
the Document representing the SQLXML value
document
)

Constructs a new JDBCSQLXML object from the given Document.

protected
JDBCSQLXML(InputStream
an octet stream representing an SQLXML value
inputStream
)

Constructs a new read-only JDBCSQLXML object from the given octet sequence.

protected
JDBCSQLXML(Reader
a character stream representing an SQLXML value
reader
)

Constructs a new read-only JDBCSQLXML object from the given character sequence.

public
JDBCSQLXML(Source
a Source representing an SQLXML value
source
)

Constructs a new read-only JDBCSQLXML object from the given Source object.

protected
JDBCSQLXML(String
a character sequence representing an SQLXML value
string
)

Constructs a new read-only JDBCSQLXML object from the given character sequence.

protected
JDBCSQLXML(byte[]
the octet sequence representing the SQLXML value
bytes
,
String
must be a String that conforms to the URI syntax; allows relative URIs to be processed.
systemId
)

Constructs a new read-only JDBCSQLXML object from the given octet sequence.

protected
JDBCSQLXML(char[] chars, String systemId)

protected
JDBCSQLXML(InputStream
an octet stream representing an SQLXML value
inputStream
,
String
a String that conforms to the URI syntax, indicating the URI from which the XML data is being read, so that relative URI references can be resolved
systemId
)

Constructs a new read-only JDBCSQLXML object from the given octet sequence.

protected
JDBCSQLXML(Reader
a character stream representing an SQLXML value;
reader
,
String
a String that conforms to the URI syntax, indicating the URI from which the XML data is being read, so that relative URI references can be resolved
systemId
)

Constructs a new read-only JDBCSQLXML object from the given character sequence.

protected
JDBCSQLXML(String
a character sequence representing an SQLXML value
string
,
String
a String that conforms to the URI syntax, indicating the URI from which the XML data is being read, so that relative URI references can be resolved
systemId
)

Constructs a new read-only JDBCSQLXML object from the given character sequence.

protected
JDBCSQLXML(byte[]
the gzipped octet sequence representing the SQLXML value
bytes
,
boolean
whether to clone the given gzipped octet sequence
clone
,
String
id
systemId
,
String
id
publicId
)

Constructs a new read-only JDBCSQLXML object from the given gzipped octet sequence.

protected
JDBCSQLXML(char[] chars, int offset, int length, String systemId)

Method Summary

Modifier and TypeMethod and Description
protected synchronized void
checkClosed()

Checks whether this object is closed (has been freed).

protected synchronized void
checkReadable()

Checks whether this object is readable.

protected synchronized void
checkWritable()

Checks whether this object is writable.

protected synchronized void
close()

Overrides default java.sql.SQLXML.close.

closes this object and releases the resources that it holds.

protected static Document
createDocument(String
of the document element to create or null.
namespaceURI
,
String
of the document element to be created or null.
qualifiedName
,
DocumentType
of document to be created or null. When doctype is not null, its Node.ownerDocument attribute is set to the document being created.
docType
)

Returns: with its document element.

protected static Document
createDocument()

Returns: that is empty with no document element.

protected <T extends Result> T

Returns:

for setting the XML value designated by this SQLXML instance.
createDOMResult
(Class<T>
The class of the result, or null.
resultClass
)

Retrieves a new DOMResult for setting the XML value designated by this SQLXML instance.

protected <T extends Source> T

Returns:

a new DOMSource for reading the XML value designated by this SQLXML instance
createDOMSource
(Class<T>
The class of the source
sourceClass
)

Retrieves a new DOMSource for reading the XML value designated by this SQLXML instance.

protected <T extends Result> T

Returns:

for setting the XML value designated by this SQLXML instance.
createResult
(Class<T>
The class of the result, or null.
resultClass
)

Retrieves a new Result for setting the XML value designated by this SQLXML instance.

protected <T extends Result> T

Returns:

for setting the XML value designated by this SQLXML instance.
createSAXResult
(Class<T>
The class of the result, or null.
resultClass
)

Retrieves a new SAXResult for setting the XML value designated by this SQLXML instance.

protected <T extends Source> T

Returns:

a new SAXSource for reading the XML value designated by this SQLXML instance
createSAXSource
(Class<T>
The class of the source
sourceClass
)

Retrieves a new SAXSource for reading the XML value designated by this SQLXML instance.

protected <T extends Result> T

Returns:

for setting the XML value designated by this SQLXML instance.
createStAXResult
(Class<T>
The class of the result, or null.
resultClass
)

Retrieves a new DOMResult for setting the XML value designated by this SQLXML instance.

protected <T extends Source> T

Returns:

a new StAXSource for reading the XML value designated by this SQLXML instance
createStAXSource
(Class<T>
The class of the source
sourceClass
)

Retrieves a new StAXSource for reading the XML value designated by this SQLXML instance.

protected <T extends Result> T

Returns:

for setting the XML value designated by this SQLXML instance.
createStreamResult
(Class<T>
The class of the result, or null.
resultClass
)

Retrieves a new StreamResult for setting the XML value designated by this SQLXML instance.

protected <T extends Source> T

Returns:

a new StreamSource for reading the XML value designated by this SQLXML instance
createStreamSource
(Class<T>
The class of the source
sourceClass
)

Retrieves a new StreamSource for reading the XML value designated by this SQLXML instance.

public void
free()

Implements java.sql.SQLXML.free.

This method closes this object and releases the resources that it held.

protected void
protected void
freeInputStream()

Closes the input stream, if any, currently in use to read this object's SQLXML value and nullifies the stream reference to make it eligible for subsequent garbage collection.

protected void
freeOutputStream()

Closes the output stream, if any, currently in use to write this object's SQLXML value and nullifies the stream reference to make it eligible for subsequent garbage collection.

public synchronized InputStream

Returns:

a stream containing the XML data.
getBinaryStream
()

Implements java.sql.SQLXML.getBinaryStream.

Retrieves the XML value designated by this SQLXML instance as a stream.

protected InputStream

Returns:

a stream containing the XML data.
getBinaryStreamImpl
()

Retrieves a stream representing the XML value designated by this SQLXML instance.

public synchronized Reader

Returns:

a stream containing the XML data.
getCharacterStream
()

Implements java.sql.SQLXML.getCharacterStream.

Retrieves the XML value designated by this SQLXML instance as a java.io.Reader object.

protected Reader

Returns:

a reader containing the XML data.
getCharacterStreamImpl
()

Retrieves a reader representing the XML value designated by this SQLXML instance.

protected static DOMImplementation
getDOMImplementation()

Returns: with which to create document instances.

protected static DOMImplementationRegistry
getDOMImplementationRegistry()

Returns: with which to construct DOM implementation instances.

protected static ExecutorService
getExecutorService()

Returns: that may be used to perform processing asynchronously.

protected byte[]

Returns:

this object's SQLXML value
getGZipData
()

Retrieves this object's SQLXML value as a gzipped array of bytes, possibly by terminating any in-progress write operations and converting accumulated intermediate data.

protected static Transformer
getIdentityTransformer()

Returns: used to perform identity transforms

public synchronized <T extends Source> T

Returns:

a Source for reading the XML value.
getSource
(Class<T>
The class of the source, or null. If the class is null, a vendor specific Source implementation will be returned. The following classes are supported at a minimum:
  javax.xml.transform.dom.DOMSource - returns a DOMSource
  javax.xml.transform.sax.SAXSource - returns a SAXSource
  javax.xml.transform.stax.StAXSource - returns a StAXSource
  javax.xml.transform.stream.StreamSource - returns a StreamSource
sourceClass
)

Implements java.sql.SQLXML.getSource.

Returns a Source for reading the XML value designated by this SQLXML instance.

protected <T extends Source> T

Returns:

a Source for reading the XML value.
getSourceImpl
(Class<T>
The class of the source, or null. If null, then a DOMSource is returned.
sourceClass
)

Returns a Source for reading the XML value designated by this SQLXML instance.

public synchronized String

Returns:

a string representation of the XML value designated by this SQLXML instance.
getString
()

Implements java.sql.SQLXML.getString.

Returns a string representation of the XML value designated by this SQLXML instance.

protected String

Returns:

a string containing the XML data.
getStringImpl
()

Retrieves a string representing the XML value designated by this SQLXML instance.

protected static TransformerFactory
getTransformerFactory()

Returns: with which to obtain xml transformer instances.

protected byte[]

Returns:

this object's SQLMXL value as a gzipped byte array
gZipData
()

Directly retrieves this object's present SQLMXL value as a gzipped array of bytes.

protected void
init(Source
the Source representing the SQLXML value
source
)

Initializes this object's SQLXML value from the given Source object.

public synchronized boolean

Returns:

if true, then readable; else not readable
isReadable
()

Retrieves the object's readability status.

public synchronized boolean

Returns:

if true, then writable; else not writable
isWritable
()

Retrieves the object's readability status.

public synchronized OutputStream

Returns:

a stream to which data can be written.
setBinaryStream
()

Implements java.sql.SQLXML.setBinaryStream.

Retrieves a stream that can be used to write the XML value that this SQLXML instance represents.

protected OutputStream

Returns:

a stream to which the data can be written.
setBinaryStreamImpl
()

Retrieves a stream to completely write the XML value this SQLXML instance represents.

public synchronized Writer

Returns:

a stream to which data can be written.
setCharacterStream
()

Implements java.sql.SQLXML.setCharacterStream.

Retrieves a stream to be used to write the XML value that this SQLXML instance represents.

protected Writer

Returns:

a writer to which the data can be written.
setCharacterStreamImpl
()

Retrieves a writer to completely write the XML value this SQLXML instance represents.

protected void
setGZipData(byte[]
the SQLXML value
data
)

Assigns this object's SQLXML value from the designated gzipped array of bytes.

protected synchronized void
setReadable(boolean
if true, then readable; else not readable
readable
)

Assigns this object's readability status.

public synchronized <T extends Result> T

Returns:

Returns a Result for setting the XML value.
setResult
(Class<T>
The class of the result, or null. If resultClass is null, a vendor specific Result implementation will be returned. The following classes are supported at a minimum:
  javax.xml.transform.dom.DOMResult - returns a DOMResult
  javax.xml.transform.sax.SAXResult - returns a SAXResult
  javax.xml.transform.stax.StAXResult - returns a StAXResult
  javax.xml.transform.stream.StreamResult - returns a StreamResult
resultClass
)

Implements java.sql.SQLXML.setResult.

Returns a Result for setting the XML value designated by this SQLXML instance.

public synchronized void
setString(String
the XML value
value
)

Implements java.sql.SQLXML.setString.

Sets the XML value designated by this SQLXML instance to the given String representation.

protected void
setStringImpl(String
the XML value
value
)

Sets the XML value designated by this SQLXML instance using the given String representation.

protected synchronized void
setWritable(boolean
if true, then writable; else not writable
writable
)

Assigns this object's writability status.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait