Path: blob/master/src/java.sql/share/classes/java/sql/SQLXML.java
41153 views
/*1* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.sql;2627import java.io.InputStream;28import java.io.OutputStream;29import java.io.Reader;30import java.io.Writer;3132import javax.xml.transform.Result;33import javax.xml.transform.Source;3435/**36* The mapping in the JavaTM programming language for the SQL XML type.37* XML is a built-in type that stores an XML value38* as a column value in a row of a database table.39* By default drivers implement an SQLXML object as40* a logical pointer to the XML data41* rather than the data itself.42* An SQLXML object is valid for the duration of the transaction in which it was created.43* <p>44* The SQLXML interface provides methods for accessing the XML value45* as a String, a Reader or Writer, or as a Stream. The XML value46* may also be accessed through a Source or set as a Result, which47* are used with XML Parser APIs such as DOM, SAX, and StAX, as48* well as with XSLT transforms and XPath evaluations.49* <p>50* Methods in the interfaces ResultSet, CallableStatement, and PreparedStatement,51* such as getSQLXML allow a programmer to access an XML value.52* In addition, this interface has methods for updating an XML value.53* <p>54* The XML value of the SQLXML instance may be obtained as a BinaryStream using55* <pre>56* SQLXML sqlxml = resultSet.getSQLXML(column);57* InputStream binaryStream = sqlxml.getBinaryStream();58* </pre>59* For example, to parse an XML value with a DOM parser:60* <pre>61* DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();62* Document result = parser.parse(binaryStream);63* </pre>64* or to parse an XML value with a SAX parser to your handler:65* <pre>66* SAXParser parser = SAXParserFactory.newInstance().newSAXParser();67* parser.parse(binaryStream, myHandler);68* </pre>69* or to parse an XML value with a StAX parser:70* <pre>71* XMLInputFactory factory = XMLInputFactory.newInstance();72* XMLStreamReader streamReader = factory.createXMLStreamReader(binaryStream);73* </pre>74* <p>75* Because databases may use an optimized representation for the XML,76* accessing the value through getSource() and77* setResult() can lead to improved processing performance78* without serializing to a stream representation and parsing the XML.79* <p>80* For example, to obtain a DOM Document Node:81* <pre>82* DOMSource domSource = sqlxml.getSource(DOMSource.class);83* Document document = (Document) domSource.getNode();84* </pre>85* or to set the value to a DOM Document Node to myNode:86* <pre>87* DOMResult domResult = sqlxml.setResult(DOMResult.class);88* domResult.setNode(myNode);89* </pre>90* or, to send SAX events to your handler:91* <pre>92* SAXSource saxSource = sqlxml.getSource(SAXSource.class);93* XMLReader xmlReader = saxSource.getXMLReader();94* xmlReader.setContentHandler(myHandler);95* xmlReader.parse(saxSource.getInputSource());96* </pre>97* or, to set the result value from SAX events:98* <pre>99* SAXResult saxResult = sqlxml.setResult(SAXResult.class);100* ContentHandler contentHandler = saxResult.getHandler();101* contentHandler.startDocument();102* // set the XML elements and attributes into the result103* contentHandler.endDocument();104* </pre>105* or, to obtain StAX events:106* <pre>107* StAXSource staxSource = sqlxml.getSource(StAXSource.class);108* XMLStreamReader streamReader = staxSource.getXMLStreamReader();109* </pre>110* or, to set the result value from StAX events:111* <pre>112* StAXResult staxResult = sqlxml.setResult(StAXResult.class);113* XMLStreamWriter streamWriter = staxResult.getXMLStreamWriter();114* </pre>115* or, to perform XSLT transformations on the XML value using the XSLT in xsltFile116* output to file resultFile:117* <pre>118* File xsltFile = new File("a.xslt");119* File myFile = new File("result.xml");120* Transformer xslt = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFile));121* Source source = sqlxml.getSource(null);122* Result result = new StreamResult(myFile);123* xslt.transform(source, result);124* </pre>125* or, to evaluate an XPath expression on the XML value:126* <pre>127* XPath xpath = XPathFactory.newInstance().newXPath();128* DOMSource domSource = sqlxml.getSource(DOMSource.class);129* Document document = (Document) domSource.getNode();130* String expression = "/foo/@bar";131* String barValue = xpath.evaluate(expression, document);132* </pre>133* To set the XML value to be the result of an XSLT transform:134* <pre>135* File sourceFile = new File("source.xml");136* Transformer xslt = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFile));137* Source streamSource = new StreamSource(sourceFile);138* Result result = sqlxml.setResult(null);139* xslt.transform(streamSource, result);140* </pre>141* Any Source can be transformed to a Result using the identity transform142* specified by calling newTransformer():143* <pre>144* Transformer identity = TransformerFactory.newInstance().newTransformer();145* Source source = sqlxml.getSource(null);146* File myFile = new File("result.xml");147* Result result = new StreamResult(myFile);148* identity.transform(source, result);149* </pre>150* To write the contents of a Source to standard output:151* <pre>152* Transformer identity = TransformerFactory.newInstance().newTransformer();153* Source source = sqlxml.getSource(null);154* Result result = new StreamResult(System.out);155* identity.transform(source, result);156* </pre>157* To create a DOMSource from a DOMResult:158* <pre>159* DOMSource domSource = new DOMSource(domResult.getNode());160* </pre>161* <p>162* Incomplete or invalid XML values may cause an SQLException when163* set or the exception may occur when execute() occurs. All streams164* must be closed before execute() occurs or an SQLException will be thrown.165* <p>166* Reading and writing XML values to or from an SQLXML object can happen at most once.167* The conceptual states of readable and not readable determine if one168* of the reading APIs will return a value or throw an exception.169* The conceptual states of writable and not writable determine if one170* of the writing APIs will set a value or throw an exception.171* <p>172* The state moves from readable to not readable once free() or any of the173* reading APIs are called: getBinaryStream(), getCharacterStream(), getSource(), and getString().174* Implementations may also change the state to not writable when this occurs.175* <p>176* The state moves from writable to not writable once free() or any of the177* writing APIs are called: setBinaryStream(), setCharacterStream(), setResult(), and setString().178* Implementations may also change the state to not readable when this occurs.179*180* <p>181* All methods on the {@code SQLXML} interface must be fully implemented if the182* JDBC driver supports the data type.183*184* @see javax.xml.parsers185* @see javax.xml.stream186* @see javax.xml.transform187* @see javax.xml.xpath188* @since 1.6189*/190public interface SQLXML191{192/**193* This method closes this object and releases the resources that it held.194* The SQL XML object becomes invalid and neither readable or writable195* when this method is called.196*197* After {@code free} has been called, any attempt to invoke a198* method other than {@code free} will result in a {@code SQLException}199* being thrown. If {@code free} is called multiple times, the subsequent200* calls to {@code free} are treated as a no-op.201* @throws SQLException if there is an error freeing the XML value.202* @throws SQLFeatureNotSupportedException if the JDBC driver does not support203* this method204* @since 1.6205*/206void free() throws SQLException;207208/**209* Retrieves the XML value designated by this SQLXML instance as a stream.210* The bytes of the input stream are interpreted according to appendix F of the XML 1.0 specification.211* The behavior of this method is the same as ResultSet.getBinaryStream()212* when the designated column of the ResultSet has a type java.sql.Types of SQLXML.213* <p>214* The SQL XML object becomes not readable when this method is called and215* may also become not writable depending on implementation.216*217* @return a stream containing the XML data.218* @throws SQLException if there is an error processing the XML value.219* An exception is thrown if the state is not readable.220* @throws SQLFeatureNotSupportedException if the JDBC driver does not support221* this method222* @since 1.6223*/224InputStream getBinaryStream() throws SQLException;225226/**227* Retrieves a stream that can be used to write the XML value that this SQLXML instance represents.228* The stream begins at position 0.229* The bytes of the stream are interpreted according to appendix F of the XML 1.0 specification230* The behavior of this method is the same as ResultSet.updateBinaryStream()231* when the designated column of the ResultSet has a type java.sql.Types of SQLXML.232* <p>233* The SQL XML object becomes not writable when this method is called and234* may also become not readable depending on implementation.235*236* @return a stream to which data can be written.237* @throws SQLException if there is an error processing the XML value.238* An exception is thrown if the state is not writable.239* @throws SQLFeatureNotSupportedException if the JDBC driver does not support240* this method241* @since 1.6242*/243OutputStream setBinaryStream() throws SQLException;244245/**246* Retrieves the XML value designated by this SQLXML instance as a java.io.Reader object.247* The format of this stream is defined by org.xml.sax.InputSource,248* where the characters in the stream represent the unicode code points for249* XML according to section 2 and appendix B of the XML 1.0 specification.250* Although an encoding declaration other than unicode may be present,251* the encoding of the stream is unicode.252* The behavior of this method is the same as ResultSet.getCharacterStream()253* when the designated column of the ResultSet has a type java.sql.Types of SQLXML.254* <p>255* The SQL XML object becomes not readable when this method is called and256* may also become not writable depending on implementation.257*258* @return a stream containing the XML data.259* @throws SQLException if there is an error processing the XML value.260* The getCause() method of the exception may provide a more detailed exception, for example,261* if the stream does not contain valid characters.262* An exception is thrown if the state is not readable.263* @throws SQLFeatureNotSupportedException if the JDBC driver does not support264* this method265* @since 1.6266*/267Reader getCharacterStream() throws SQLException;268269/**270* Retrieves a stream to be used to write the XML value that this SQLXML instance represents.271* The format of this stream is defined by org.xml.sax.InputSource,272* where the characters in the stream represent the unicode code points for273* XML according to section 2 and appendix B of the XML 1.0 specification.274* Although an encoding declaration other than unicode may be present,275* the encoding of the stream is unicode.276* The behavior of this method is the same as ResultSet.updateCharacterStream()277* when the designated column of the ResultSet has a type java.sql.Types of SQLXML.278* <p>279* The SQL XML object becomes not writable when this method is called and280* may also become not readable depending on implementation.281*282* @return a stream to which data can be written.283* @throws SQLException if there is an error processing the XML value.284* The getCause() method of the exception may provide a more detailed exception, for example,285* if the stream does not contain valid characters.286* An exception is thrown if the state is not writable.287* @throws SQLFeatureNotSupportedException if the JDBC driver does not support288* this method289* @since 1.6290*/291Writer setCharacterStream() throws SQLException;292293/**294* Returns a string representation of the XML value designated by this SQLXML instance.295* The format of this String is defined by org.xml.sax.InputSource,296* where the characters in the stream represent the unicode code points for297* XML according to section 2 and appendix B of the XML 1.0 specification.298* Although an encoding declaration other than unicode may be present,299* the encoding of the String is unicode.300* The behavior of this method is the same as ResultSet.getString()301* when the designated column of the ResultSet has a type java.sql.Types of SQLXML.302* <p>303* The SQL XML object becomes not readable when this method is called and304* may also become not writable depending on implementation.305*306* @return a string representation of the XML value designated by this SQLXML instance.307* @throws SQLException if there is an error processing the XML value.308* The getCause() method of the exception may provide a more detailed exception, for example,309* if the stream does not contain valid characters.310* An exception is thrown if the state is not readable.311* @throws SQLFeatureNotSupportedException if the JDBC driver does not support312* this method313* @since 1.6314*/315String getString() throws SQLException;316317/**318* Sets the XML value designated by this SQLXML instance to the given String representation.319* The format of this String is defined by org.xml.sax.InputSource,320* where the characters in the stream represent the unicode code points for321* XML according to section 2 and appendix B of the XML 1.0 specification.322* Although an encoding declaration other than unicode may be present,323* the encoding of the String is unicode.324* The behavior of this method is the same as ResultSet.updateString()325* when the designated column of the ResultSet has a type java.sql.Types of SQLXML.326* <p>327* The SQL XML object becomes not writable when this method is called and328* may also become not readable depending on implementation.329*330* @param value the XML value331* @throws SQLException if there is an error processing the XML value.332* The getCause() method of the exception may provide a more detailed exception, for example,333* if the stream does not contain valid characters.334* An exception is thrown if the state is not writable.335* @throws SQLFeatureNotSupportedException if the JDBC driver does not support336* this method337* @since 1.6338*/339void setString(String value) throws SQLException;340341/**342* Returns a Source for reading the XML value designated by this SQLXML instance.343* Sources are used as inputs to XML parsers and XSLT transformers.344* <p>345* Sources for XML parsers will have namespace processing on by default.346* The systemID of the Source is implementation dependent.347* <p>348* The SQL XML object becomes not readable when this method is called and349* may also become not writable depending on implementation.350* <p>351* Note that SAX is a callback architecture, so a returned352* SAXSource should then be set with a content handler that will353* receive the SAX events from parsing. The content handler354* will receive callbacks based on the contents of the XML.355* <pre>356* SAXSource saxSource = sqlxml.getSource(SAXSource.class);357* XMLReader xmlReader = saxSource.getXMLReader();358* xmlReader.setContentHandler(myHandler);359* xmlReader.parse(saxSource.getInputSource());360* </pre>361*362* @param <T> the type of the class modeled by this Class object363* @param sourceClass The class of the source, or null.364* If the class is null, a vendor specific Source implementation will be returned.365* The following classes are supported at a minimum:366* <pre>367* javax.xml.transform.dom.DOMSource - returns a DOMSource368* javax.xml.transform.sax.SAXSource - returns a SAXSource369* javax.xml.transform.stax.StAXSource - returns a StAXSource370* javax.xml.transform.stream.StreamSource - returns a StreamSource371* </pre>372* @return a Source for reading the XML value.373* @throws SQLException if there is an error processing the XML value374* or if this feature is not supported.375* The getCause() method of the exception may provide a more detailed exception, for example,376* if an XML parser exception occurs.377* An exception is thrown if the state is not readable.378* @throws SQLFeatureNotSupportedException if the JDBC driver does not support379* this method380* @since 1.6381*/382<T extends Source> T getSource(Class<T> sourceClass) throws SQLException;383384/**385* Returns a Result for setting the XML value designated by this SQLXML instance.386* <p>387* The systemID of the Result is implementation dependent.388* <p>389* The SQL XML object becomes not writable when this method is called and390* may also become not readable depending on implementation.391* <p>392* Note that SAX is a callback architecture and the returned393* SAXResult has a content handler assigned that will receive the394* SAX events based on the contents of the XML. Call the content395* handler with the contents of the XML document to assign the values.396* <pre>397* SAXResult saxResult = sqlxml.setResult(SAXResult.class);398* ContentHandler contentHandler = saxResult.getXMLReader().getContentHandler();399* contentHandler.startDocument();400* // set the XML elements and attributes into the result401* contentHandler.endDocument();402* </pre>403*404* @param <T> the type of the class modeled by this Class object405* @param resultClass The class of the result, or null.406* If resultClass is null, a vendor specific Result implementation will be returned.407* The following classes are supported at a minimum:408* <pre>409* javax.xml.transform.dom.DOMResult - returns a DOMResult410* javax.xml.transform.sax.SAXResult - returns a SAXResult411* javax.xml.transform.stax.StAXResult - returns a StAXResult412* javax.xml.transform.stream.StreamResult - returns a StreamResult413* </pre>414* @return Returns a Result for setting the XML value.415* @throws SQLException if there is an error processing the XML value416* or if this feature is not supported.417* The getCause() method of the exception may provide a more detailed exception, for example,418* if an XML parser exception occurs.419* An exception is thrown if the state is not writable.420* @throws SQLFeatureNotSupportedException if the JDBC driver does not support421* this method422* @since 1.6423*/424<T extends Result> T setResult(Class<T> resultClass) throws SQLException;425426}427428429