2362N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * The mapping in the JavaTM programming language for the SQL XML type. 0N/A * XML is a built-in type that stores an XML value 0N/A * as a column value in a row of a database table. 0N/A * By default drivers implement an SQLXML object as 0N/A * a logical pointer to the XML data 0N/A * rather than the data itself. 0N/A * An SQLXML object is valid for the duration of the transaction in which it was created. 0N/A * The SQLXML interface provides methods for accessing the XML value 0N/A * as a String, a Reader or Writer, or as a Stream. The XML value 0N/A * may also be accessed through a Source or set as a Result, which 0N/A * are used with XML Parser APIs such as DOM, SAX, and StAX, as 0N/A * well as with XSLT transforms and XPath evaluations. 0N/A * Methods in the interfaces ResultSet, CallableStatement, and PreparedStatement, 0N/A * such as getSQLXML allow a programmer to access an XML value. 0N/A * In addition, this interface has methods for updating an XML value. 0N/A * The XML value of the SQLXML instance may be obtained as a BinaryStream using 0N/A * SQLXML sqlxml = resultSet.getSQLXML(column); 0N/A * InputStream binaryStream = sqlxml.getBinaryStream(); 0N/A * For example, to parse an XML value with a DOM parser: 0N/A * DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 0N/A * Document result = parser.parse(binaryStream); 0N/A * or to parse an XML value with a SAX parser to your handler: 0N/A * SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); 0N/A * parser.parse(binaryStream, myHandler); 0N/A * or to parse an XML value with a StAX parser: 0N/A * XMLInputFactory factory = XMLInputFactory.newInstance(); 0N/A * XMLStreamReader streamReader = factory.createXMLStreamReader(binaryStream); 0N/A * Because databases may use an optimized representation for the XML, 0N/A * accessing the value through getSource() and 0N/A * setResult() can lead to improved processing performance 0N/A * without serializing to a stream representation and parsing the XML. 0N/A * For example, to obtain a DOM Document Node: 0N/A * DOMSource domSource = sqlxml.getSource(DOMSource.class); 0N/A * Document document = (Document) domSource.getNode(); 0N/A * or to set the value to a DOM Document Node to myNode: 0N/A * DOMResult domResult = sqlxml.setResult(DOMResult.class); 0N/A * domResult.setNode(myNode); 0N/A * or, to send SAX events to your handler: 0N/A * SAXSource saxSource = sqlxml.getSource(SAXSource.class); 0N/A * XMLReader xmlReader = saxSource.getXMLReader(); 0N/A * xmlReader.setContentHandler(myHandler); 0N/A * xmlReader.parse(saxSource.getInputSource()); 0N/A * or, to set the result value from SAX events: 0N/A * SAXResult saxResult = sqlxml.setResult(SAXResult.class); 0N/A * ContentHandler contentHandler = saxResult.getXMLReader().getContentHandler(); 0N/A * contentHandler.startDocument(); 0N/A * // set the XML elements and attributes into the result 0N/A * contentHandler.endDocument(); 0N/A * or, to obtain StAX events: 0N/A * StAXSource staxSource = sqlxml.getSource(StAXSource.class); 0N/A * XMLStreamReader streamReader = staxSource.getXMLStreamReader(); 0N/A * or, to set the result value from StAX events: 0N/A * StAXResult staxResult = sqlxml.setResult(StAXResult.class); 0N/A * XMLStreamWriter streamWriter = staxResult.getXMLStreamWriter(); 0N/A * or, to perform XSLT transformations on the XML value using the XSLT in xsltFile 0N/A * output to file resultFile: 0N/A * File xsltFile = new File("a.xslt"); 0N/A * Transformer xslt = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFile)); 0N/A * Source source = sqlxml.getSource(null); 0N/A * Result result = new StreamResult(myFile); 0N/A * xslt.transform(source, result); 0N/A * or, to evaluate an XPath expression on the XML value: 0N/A * XPath xpath = XPathFactory.newInstance().newXPath(); 0N/A * DOMSource domSource = sqlxml.getSource(DOMSource.class); 0N/A * Document document = (Document) domSource.getNode(); 0N/A * String expression = "/foo/@bar"; 0N/A * String barValue = xpath.evaluate(expression, document); 0N/A * To set the XML value to be the result of an XSLT transform: 0N/A * Transformer xslt = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFile)); 0N/A * Source streamSource = new StreamSource(sourceFile); 0N/A * Result result = sqlxml.setResult(null); 0N/A * xslt.transform(streamSource, result); 0N/A * Any Source can be transformed to a Result using the identity transform 0N/A * specified by calling newTransformer(): 0N/A * Transformer identity = TransformerFactory.newInstance().newTransformer(); 0N/A * Source source = sqlxml.getSource(null); 0N/A * Result result = new StreamResult(myFile); 0N/A * identity.transform(source, result); 0N/A * To write the contents of a Source to standard output: 0N/A * Transformer identity = TransformerFactory.newInstance().newTransformer(); 0N/A * Source source = sqlxml.getSource(null); 0N/A * Result result = new StreamResult(System.out); 0N/A * identity.transform(source, result); 0N/A * To create a DOMSource from a DOMResult: 0N/A * DOMSource domSource = new DOMSource(domResult.getNode()); 0N/A * Incomplete or invalid XML values may cause an SQLException when 0N/A * set or the exception may occur when execute() occurs. All streams 0N/A * must be closed before execute() occurs or an SQLException will be thrown. 0N/A * Reading and writing XML values to or from an SQLXML object can happen at most once. 0N/A * The conceptual states of readable and not readable determine if one 0N/A * of the reading APIs will return a value or throw an exception. 0N/A * The conceptual states of writable and not writable determine if one 0N/A * of the writing APIs will set a value or throw an exception. 0N/A * The state moves from readable to not readable once free() or any of the 0N/A * reading APIs are called: getBinaryStream(), getCharacterStream(), getSource(), and getString(). 0N/A * Implementations may also change the state to not writable when this occurs. 0N/A * The state moves from writable to not writeable once free() or any of the 0N/A * writing APIs are called: setBinaryStream(), setCharacterStream(), setResult(), and setString(). 0N/A * Implementations may also change the state to not readable when this occurs. 0N/A * All methods on the <code>SQLXML</code> interface must be fully implemented if the 0N/A * JDBC driver supports the data type. 0N/A * This method closes this object and releases the resources that it held. 0N/A * The SQL XML object becomes invalid and neither readable or writeable 0N/A * when this method is called. 0N/A * After <code>free</code> has been called, any attempt to invoke a 0N/A * method other than <code>free</code> will result in a <code>SQLException</code> 0N/A * being thrown. If <code>free</code> is called multiple times, the subsequent 0N/A * calls to <code>free</code> are treated as a no-op. 0N/A * @throws SQLException if there is an error freeing the XML value. 0N/A * @exception SQLFeatureNotSupportedException if the JDBC driver does not support 0N/A * Retrieves the XML value designated by this SQLXML instance as a stream. 0N/A * The bytes of the input stream are interpreted according to appendix F of the XML 1.0 specification. 0N/A * The behavior of this method is the same as ResultSet.getBinaryStream() 0N/A * when the designated column of the ResultSet has a type java.sql.Types of SQLXML. 0N/A * The SQL XML object becomes not readable when this method is called and 0N/A * may also become not writable depending on implementation. 0N/A * @return a stream containing the XML data. 0N/A * @throws SQLException if there is an error processing the XML value. 0N/A * An exception is thrown if the state is not readable. 0N/A * @exception SQLFeatureNotSupportedException if the JDBC driver does not support 0N/A * Retrieves a stream that can be used to write the XML value that this SQLXML instance represents. 0N/A * The stream begins at position 0. 0N/A * The bytes of the stream are interpreted according to appendix F of the XML 1.0 specification 0N/A * The behavior of this method is the same as ResultSet.updateBinaryStream() 0N/A * when the designated column of the ResultSet has a type java.sql.Types of SQLXML. 0N/A * The SQL XML object becomes not writeable when this method is called and 0N/A * may also become not readable depending on implementation. 0N/A * @return a stream to which data can be written. 0N/A * @throws SQLException if there is an error processing the XML value. 0N/A * An exception is thrown if the state is not writable. 0N/A * @exception SQLFeatureNotSupportedException if the JDBC driver does not support 0N/A * Retrieves the XML value designated by this SQLXML instance as a java.io.Reader object. 0N/A * The format of this stream is defined by org.xml.sax.InputSource, 0N/A * where the characters in the stream represent the unicode code points for 0N/A * XML according to section 2 and appendix B of the XML 1.0 specification. 0N/A * Although an encoding declaration other than unicode may be present, 0N/A * the encoding of the stream is unicode. 0N/A * The behavior of this method is the same as ResultSet.getCharacterStream() 0N/A * when the designated column of the ResultSet has a type java.sql.Types of SQLXML. 0N/A * The SQL XML object becomes not readable when this method is called and 0N/A * may also become not writable depending on implementation. 0N/A * @return a stream containing the XML data. 0N/A * @throws SQLException if there is an error processing the XML value. 0N/A * The getCause() method of the exception may provide a more detailed exception, for example, 0N/A * if the stream does not contain valid characters. 0N/A * An exception is thrown if the state is not readable. 0N/A * @exception SQLFeatureNotSupportedException if the JDBC driver does not support 0N/A * Retrieves a stream to be used to write the XML value that this SQLXML instance represents. 0N/A * The format of this stream is defined by org.xml.sax.InputSource, 0N/A * where the characters in the stream represent the unicode code points for 0N/A * XML according to section 2 and appendix B of the XML 1.0 specification. 0N/A * Although an encoding declaration other than unicode may be present, 0N/A * the encoding of the stream is unicode. 0N/A * The behavior of this method is the same as ResultSet.updateCharacterStream() 0N/A * when the designated column of the ResultSet has a type java.sql.Types of SQLXML. 0N/A * The SQL XML object becomes not writeable when this method is called and 0N/A * may also become not readable depending on implementation. 0N/A * @return a stream to which data can be written. 0N/A * @throws SQLException if there is an error processing the XML value. 0N/A * The getCause() method of the exception may provide a more detailed exception, for example, 0N/A * if the stream does not contain valid characters. 0N/A * An exception is thrown if the state is not writable. 0N/A * @exception SQLFeatureNotSupportedException if the JDBC driver does not support 0N/A * Returns a string representation of the XML value designated by this SQLXML instance. 0N/A * The format of this String is defined by org.xml.sax.InputSource, 0N/A * where the characters in the stream represent the unicode code points for 0N/A * XML according to section 2 and appendix B of the XML 1.0 specification. 0N/A * Although an encoding declaration other than unicode may be present, 0N/A * the encoding of the String is unicode. 0N/A * The behavior of this method is the same as ResultSet.getString() 0N/A * when the designated column of the ResultSet has a type java.sql.Types of SQLXML. 0N/A * The SQL XML object becomes not readable when this method is called and 0N/A * may also become not writable depending on implementation. 0N/A * @return a string representation of the XML value designated by this SQLXML instance. 0N/A * @throws SQLException if there is an error processing the XML value. 0N/A * The getCause() method of the exception may provide a more detailed exception, for example, 0N/A * if the stream does not contain valid characters. 0N/A * An exception is thrown if the state is not readable. 0N/A * @exception SQLFeatureNotSupportedException if the JDBC driver does not support 0N/A * Sets the XML value designated by this SQLXML instance to the given String representation. 0N/A * The format of this String is defined by org.xml.sax.InputSource, 0N/A * where the characters in the stream represent the unicode code points for 0N/A * XML according to section 2 and appendix B of the XML 1.0 specification. 0N/A * Although an encoding declaration other than unicode may be present, 0N/A * the encoding of the String is unicode. 0N/A * The behavior of this method is the same as ResultSet.updateString() 0N/A * when the designated column of the ResultSet has a type java.sql.Types of SQLXML. 0N/A * The SQL XML object becomes not writeable when this method is called and 0N/A * may also become not readable depending on implementation. 0N/A * @param value the XML value 0N/A * @throws SQLException if there is an error processing the XML value. 0N/A * The getCause() method of the exception may provide a more detailed exception, for example, 0N/A * if the stream does not contain valid characters. 0N/A * An exception is thrown if the state is not writable. 0N/A * @exception SQLFeatureNotSupportedException if the JDBC driver does not support 0N/A * Returns a Source for reading the XML value designated by this SQLXML instance. 0N/A * Sources are used as inputs to XML parsers and XSLT transformers. 0N/A * Sources for XML parsers will have namespace processing on by default. 0N/A * The systemID of the Source is implementation dependent. 0N/A * The SQL XML object becomes not readable when this method is called and 0N/A * may also become not writable depending on implementation. 0N/A * Note that SAX is a callback architecture, so a returned 0N/A * SAXSource should then be set with a content handler that will 0N/A * receive the SAX events from parsing. The content handler 0N/A * will receive callbacks based on the contents of the XML. 0N/A * SAXSource saxSource = sqlxml.getSource(SAXSource.class); 0N/A * XMLReader xmlReader = saxSource.getXMLReader(); 0N/A * xmlReader.setContentHandler(myHandler); 0N/A * xmlReader.parse(saxSource.getInputSource()); 0N/A * @param sourceClass The class of the source, or null. 0N/A * If the class is null, a vendor specifc Source implementation will be returned. 0N/A * The following classes are supported at a minimum: 0N/A * javax.xml.transform.stax.StAXSource - returns a StAXSource 0N/A * javax.xml.transform.stream.StreamSource - returns a StreamSource 0N/A * @return a Source for reading the XML value. 0N/A * @throws SQLException if there is an error processing the XML value 0N/A * or if this feature is not supported. 0N/A * The getCause() method of the exception may provide a more detailed exception, for example, 0N/A * if an XML parser exception occurs. 0N/A * An exception is thrown if the state is not readable. 0N/A * @exception SQLFeatureNotSupportedException if the JDBC driver does not support 0N/A * Returns a Result for setting the XML value designated by this SQLXML instance. 0N/A * The systemID of the Result is implementation dependent. 0N/A * The SQL XML object becomes not writeable when this method is called and 0N/A * may also become not readable depending on implementation. 0N/A * Note that SAX is a callback architecture and the returned 0N/A * SAXResult has a content handler assigned that will receive the 0N/A * SAX events based on the contents of the XML. Call the content 0N/A * handler with the contents of the XML document to assign the values. 0N/A * SAXResult saxResult = sqlxml.setResult(SAXResult.class); 0N/A * ContentHandler contentHandler = saxResult.getXMLReader().getContentHandler(); 0N/A * contentHandler.startDocument(); 0N/A * // set the XML elements and attributes into the result 0N/A * contentHandler.endDocument(); 0N/A * @param resultClass The class of the result, or null. 0N/A * If resultClass is null, a vendor specific Result implementation will be returned. 0N/A * The following classes are supported at a minimum: 0N/A * javax.xml.transform.stax.StAXResult - returns a StAXResult 0N/A * javax.xml.transform.stream.StreamResult - returns a StreamResult 0N/A * @return Returns a Result for setting the XML value. 0N/A * @throws SQLException if there is an error processing the XML value 0N/A * or if this feature is not supported. 0N/A * The getCause() method of the exception may provide a more detailed exception, for example, 0N/A * if an XML parser exception occurs. 0N/A * An exception is thrown if the state is not writable. 0N/A * @exception SQLFeatureNotSupportedException if the JDBC driver does not support