1 /* 2 * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.sql; 27 28 import java.io.InputStream; 29 import java.io.OutputStream; 30 import java.io.Reader; 31 import java.io.Writer; 32 33 import javax.xml.transform.Result; 34 import javax.xml.transform.Source; 35 36 /** 37 * The mapping in the JavaTM programming language for the SQL XML type. 38 * XML is a built-in type that stores an XML value 39 * as a column value in a row of a database table. 40 * By default drivers implement an SQLXML object as 41 * a logical pointer to the XML data 42 * rather than the data itself. 43 * An SQLXML object is valid for the duration of the transaction in which it was created. 44 * <p> 45 * The SQLXML interface provides methods for accessing the XML value 46 * as a String, a Reader or Writer, or as a Stream. The XML value 47 * may also be accessed through a Source or set as a Result, which 48 * are used with XML Parser APIs such as DOM, SAX, and StAX, as 49 * well as with XSLT transforms and XPath evaluations. 50 * <p> 51 * Methods in the interfaces ResultSet, CallableStatement, and PreparedStatement, 52 * such as getSQLXML allow a programmer to access an XML value. 53 * In addition, this interface has methods for updating an XML value. 54 * <p> 55 * The XML value of the SQLXML instance may be obtained as a BinaryStream using 56 * <pre> 57 * SQLXML sqlxml = resultSet.getSQLXML(column); 58 * InputStream binaryStream = sqlxml.getBinaryStream(); 59 * </pre> 60 * For example, to parse an XML value with a DOM parser: 61 * <pre> 62 * DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 63 * Document result = parser.parse(binaryStream); 64 * </pre> 65 * or to parse an XML value with a SAX parser to your handler: 66 * <pre> 67 * SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); 68 * parser.parse(binaryStream, myHandler); 69 * </pre> 70 * or to parse an XML value with a StAX parser: 71 * <pre> 72 * XMLInputFactory factory = XMLInputFactory.newInstance(); 73 * XMLStreamReader streamReader = factory.createXMLStreamReader(binaryStream); 74 * </pre> 75 * <p> 76 * Because databases may use an optimized representation for the XML, 77 * accessing the value through getSource() and 78 * setResult() can lead to improved processing performance 79 * without serializing to a stream representation and parsing the XML. 80 * <p> 81 * For example, to obtain a DOM Document Node: 82 * <pre> 83 * DOMSource domSource = sqlxml.getSource(DOMSource.class); 84 * Document document = (Document) domSource.getNode(); 85 * </pre> 86 * or to set the value to a DOM Document Node to myNode: 87 * <pre> 88 * DOMResult domResult = sqlxml.setResult(DOMResult.class); 89 * domResult.setNode(myNode); 90 * </pre> 91 * or, to send SAX events to your handler: 92 * <pre> 93 * SAXSource saxSource = sqlxml.getSource(SAXSource.class); 94 * XMLReader xmlReader = saxSource.getXMLReader(); 95 * xmlReader.setContentHandler(myHandler); 96 * xmlReader.parse(saxSource.getInputSource()); 97 * </pre> 98 * or, to set the result value from SAX events: 99 * <pre> 100 * SAXResult saxResult = sqlxml.setResult(SAXResult.class); 101 * ContentHandler contentHandler = saxResult.getXMLReader().getContentHandler(); 102 * contentHandler.startDocument(); 103 * // set the XML elements and attributes into the result 104 * contentHandler.endDocument(); 105 * </pre> 106 * or, to obtain StAX events: 107 * <pre> 108 * StAXSource staxSource = sqlxml.getSource(StAXSource.class); 109 * XMLStreamReader streamReader = staxSource.getXMLStreamReader(); 110 * </pre> 111 * or, to set the result value from StAX events: 112 * <pre> 113 * StAXResult staxResult = sqlxml.setResult(StAXResult.class); 114 * XMLStreamWriter streamWriter = staxResult.getXMLStreamWriter(); 115 * </pre> 116 * or, to perform XSLT transformations on the XML value using the XSLT in xsltFile 117 * output to file resultFile: 118 * <pre> 119 * File xsltFile = new File("a.xslt"); 120 * File myFile = new File("result.xml"); 121 * Transformer xslt = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFile)); 122 * Source source = sqlxml.getSource(null); 123 * Result result = new StreamResult(myFile); 124 * xslt.transform(source, result); 125 * </pre> 126 * or, to evaluate an XPath expression on the XML value: 127 * <pre> 128 * XPath xpath = XPathFactory.newInstance().newXPath(); 129 * DOMSource domSource = sqlxml.getSource(DOMSource.class); 130 * Document document = (Document) domSource.getNode(); 131 * String expression = "/foo/@bar"; 132 * String barValue = xpath.evaluate(expression, document); 133 * </pre> 134 * To set the XML value to be the result of an XSLT transform: 135 * <pre> 136 * File sourceFile = new File("source.xml"); 137 * Transformer xslt = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFile)); 138 * Source streamSource = new StreamSource(sourceFile); 139 * Result result = sqlxml.setResult(null); 140 * xslt.transform(streamSource, result); 141 * </pre> 142 * Any Source can be transformed to a Result using the identity transform 143 * specified by calling newTransformer(): 144 * <pre> 145 * Transformer identity = TransformerFactory.newInstance().newTransformer(); 146 * Source source = sqlxml.getSource(null); 147 * File myFile = new File("result.xml"); 148 * Result result = new StreamResult(myFile); 149 * identity.transform(source, result); 150 * </pre> 151 * To write the contents of a Source to standard output: 152 * <pre> 153 * Transformer identity = TransformerFactory.newInstance().newTransformer(); 154 * Source source = sqlxml.getSource(null); 155 * Result result = new StreamResult(System.out); 156 * identity.transform(source, result); 157 * </pre> 158 * To create a DOMSource from a DOMResult: 159 * <pre> 160 * DOMSource domSource = new DOMSource(domResult.getNode()); 161 * </pre> 162 * <p> 163 * Incomplete or invalid XML values may cause an SQLException when 164 * set or the exception may occur when execute() occurs. All streams 165 * must be closed before execute() occurs or an SQLException will be thrown. 166 * <p> 167 * Reading and writing XML values to or from an SQLXML object can happen at most once. 168 * The conceptual states of readable and not readable determine if one 169 * of the reading APIs will return a value or throw an exception. 170 * The conceptual states of writable and not writable determine if one 171 * of the writing APIs will set a value or throw an exception. 172 * <p> 173 * The state moves from readable to not readable once free() or any of the 174 * reading APIs are called: getBinaryStream(), getCharacterStream(), getSource(), and getString(). 175 * Implementations may also change the state to not writable when this occurs. 176 * <p> 177 * The state moves from writable to not writeable once free() or any of the 178 * writing APIs are called: setBinaryStream(), setCharacterStream(), setResult(), and setString(). 179 * Implementations may also change the state to not readable when this occurs. 180 * <p> 181 * <p> 182 * All methods on the <code>SQLXML</code> interface must be fully implemented if the 183 * JDBC driver supports the data type. 184 * 185 * @see javax.xml.parsers 186 * @see javax.xml.stream 187 * @see javax.xml.transform 188 * @see javax.xml.xpath 189 * @since 1.6 190 */ 191 public interface SQLXML 192 { 193 /** 194 * This method closes this object and releases the resources that it held. 195 * The SQL XML object becomes invalid and neither readable or writeable 196 * when this method is called. 197 * 198 * After <code>free</code> has been called, any attempt to invoke a 199 * method other than <code>free</code> will result in a <code>SQLException</code> 200 * being thrown. If <code>free</code> is called multiple times, the subsequent 201 * calls to <code>free</code> are treated as a no-op. 202 * @throws SQLException if there is an error freeing the XML value. 203 * @exception SQLFeatureNotSupportedException if the JDBC driver does not support 204 * this method 205 * @since 1.6 206 */ free()207 void free() throws SQLException; 208 209 /** 210 * Retrieves the XML value designated by this SQLXML instance as a stream. 211 * The bytes of the input stream are interpreted according to appendix F of the XML 1.0 specification. 212 * The behavior of this method is the same as ResultSet.getBinaryStream() 213 * when the designated column of the ResultSet has a type java.sql.Types of SQLXML. 214 * <p> 215 * The SQL XML object becomes not readable when this method is called and 216 * may also become not writable depending on implementation. 217 * 218 * @return a stream containing the XML data. 219 * @throws SQLException if there is an error processing the XML value. 220 * An exception is thrown if the state is not readable. 221 * @exception SQLFeatureNotSupportedException if the JDBC driver does not support 222 * this method 223 * @since 1.6 224 */ getBinaryStream()225 InputStream getBinaryStream() throws SQLException; 226 227 /** 228 * Retrieves a stream that can be used to write the XML value that this SQLXML instance represents. 229 * The stream begins at position 0. 230 * The bytes of the stream are interpreted according to appendix F of the XML 1.0 specification 231 * The behavior of this method is the same as ResultSet.updateBinaryStream() 232 * when the designated column of the ResultSet has a type java.sql.Types of SQLXML. 233 * <p> 234 * The SQL XML object becomes not writeable when this method is called and 235 * may also become not readable depending on implementation. 236 * 237 * @return a stream to which data can be written. 238 * @throws SQLException if there is an error processing the XML value. 239 * An exception is thrown if the state is not writable. 240 * @exception SQLFeatureNotSupportedException if the JDBC driver does not support 241 * this method 242 * @since 1.6 243 */ setBinaryStream()244 OutputStream setBinaryStream() throws SQLException; 245 246 /** 247 * Retrieves the XML value designated by this SQLXML instance as a java.io.Reader object. 248 * The format of this stream is defined by org.xml.sax.InputSource, 249 * where the characters in the stream represent the unicode code points for 250 * XML according to section 2 and appendix B of the XML 1.0 specification. 251 * Although an encoding declaration other than unicode may be present, 252 * the encoding of the stream is unicode. 253 * The behavior of this method is the same as ResultSet.getCharacterStream() 254 * when the designated column of the ResultSet has a type java.sql.Types of SQLXML. 255 * <p> 256 * The SQL XML object becomes not readable when this method is called and 257 * may also become not writable depending on implementation. 258 * 259 * @return a stream containing the XML data. 260 * @throws SQLException if there is an error processing the XML value. 261 * The getCause() method of the exception may provide a more detailed exception, for example, 262 * if the stream does not contain valid characters. 263 * An exception is thrown if the state is not readable. 264 * @exception SQLFeatureNotSupportedException if the JDBC driver does not support 265 * this method 266 * @since 1.6 267 */ getCharacterStream()268 Reader getCharacterStream() throws SQLException; 269 270 /** 271 * Retrieves a stream to be used to write the XML value that this SQLXML instance represents. 272 * The format of this stream is defined by org.xml.sax.InputSource, 273 * where the characters in the stream represent the unicode code points for 274 * XML according to section 2 and appendix B of the XML 1.0 specification. 275 * Although an encoding declaration other than unicode may be present, 276 * the encoding of the stream is unicode. 277 * The behavior of this method is the same as ResultSet.updateCharacterStream() 278 * when the designated column of the ResultSet has a type java.sql.Types of SQLXML. 279 * <p> 280 * The SQL XML object becomes not writeable when this method is called and 281 * may also become not readable depending on implementation. 282 * 283 * @return a stream to which data can be written. 284 * @throws SQLException if there is an error processing the XML value. 285 * The getCause() method of the exception may provide a more detailed exception, for example, 286 * if the stream does not contain valid characters. 287 * An exception is thrown if the state is not writable. 288 * @exception SQLFeatureNotSupportedException if the JDBC driver does not support 289 * this method 290 * @since 1.6 291 */ setCharacterStream()292 Writer setCharacterStream() throws SQLException; 293 294 /** 295 * Returns a string representation of the XML value designated by this SQLXML instance. 296 * The format of this String is defined by org.xml.sax.InputSource, 297 * where the characters in the stream represent the unicode code points for 298 * XML according to section 2 and appendix B of the XML 1.0 specification. 299 * Although an encoding declaration other than unicode may be present, 300 * the encoding of the String is unicode. 301 * The behavior of this method is the same as ResultSet.getString() 302 * when the designated column of the ResultSet has a type java.sql.Types of SQLXML. 303 * <p> 304 * The SQL XML object becomes not readable when this method is called and 305 * may also become not writable depending on implementation. 306 * 307 * @return a string representation of the XML value designated by this SQLXML instance. 308 * @throws SQLException if there is an error processing the XML value. 309 * The getCause() method of the exception may provide a more detailed exception, for example, 310 * if the stream does not contain valid characters. 311 * An exception is thrown if the state is not readable. 312 * @exception SQLFeatureNotSupportedException if the JDBC driver does not support 313 * this method 314 * @since 1.6 315 */ getString()316 String getString() throws SQLException; 317 318 /** 319 * Sets the XML value designated by this SQLXML instance to the given String representation. 320 * The format of this String is defined by org.xml.sax.InputSource, 321 * where the characters in the stream represent the unicode code points for 322 * XML according to section 2 and appendix B of the XML 1.0 specification. 323 * Although an encoding declaration other than unicode may be present, 324 * the encoding of the String is unicode. 325 * The behavior of this method is the same as ResultSet.updateString() 326 * when the designated column of the ResultSet has a type java.sql.Types of SQLXML. 327 * <p> 328 * The SQL XML object becomes not writeable when this method is called and 329 * may also become not readable depending on implementation. 330 * 331 * @param value the XML value 332 * @throws SQLException if there is an error processing the XML value. 333 * The getCause() method of the exception may provide a more detailed exception, for example, 334 * if the stream does not contain valid characters. 335 * An exception is thrown if the state is not writable. 336 * @exception SQLFeatureNotSupportedException if the JDBC driver does not support 337 * this method 338 * @since 1.6 339 */ setString(String value)340 void setString(String value) throws SQLException; 341 342 /** 343 * Returns a Source for reading the XML value designated by this SQLXML instance. 344 * Sources are used as inputs to XML parsers and XSLT transformers. 345 * <p> 346 * Sources for XML parsers will have namespace processing on by default. 347 * The systemID of the Source is implementation dependent. 348 * <p> 349 * The SQL XML object becomes not readable when this method is called and 350 * may also become not writable depending on implementation. 351 * <p> 352 * Note that SAX is a callback architecture, so a returned 353 * SAXSource should then be set with a content handler that will 354 * receive the SAX events from parsing. The content handler 355 * will receive callbacks based on the contents of the XML. 356 * <pre> 357 * SAXSource saxSource = sqlxml.getSource(SAXSource.class); 358 * XMLReader xmlReader = saxSource.getXMLReader(); 359 * xmlReader.setContentHandler(myHandler); 360 * xmlReader.parse(saxSource.getInputSource()); 361 * </pre> 362 * 363 * @param sourceClass The class of the source, or null. 364 * If the class is null, a vendor specifc Source implementation will be returned. 365 * The following classes are supported at a minimum: 366 * <pre> 367 * javax.xml.transform.dom.DOMSource - returns a DOMSource 368 * javax.xml.transform.sax.SAXSource - returns a SAXSource 369 * javax.xml.transform.stax.StAXSource - returns a StAXSource 370 * javax.xml.transform.stream.StreamSource - returns a StreamSource 371 * </pre> 372 * @return a Source for reading the XML value. 373 * @throws SQLException if there is an error processing the XML value 374 * 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 * @exception SQLFeatureNotSupportedException if the JDBC driver does not support 379 * this method 380 * @since 1.6 381 */ getSource(Class<T> sourceClass)382 <T extends Source> T getSource(Class<T> sourceClass) throws SQLException; 383 384 /** 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 writeable when this method is called and 390 * may also become not readable depending on implementation. 391 * <p> 392 * Note that SAX is a callback architecture and the returned 393 * SAXResult has a content handler assigned that will receive the 394 * SAX events based on the contents of the XML. Call the content 395 * 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 result 401 * contentHandler.endDocument(); 402 * </pre> 403 * 404 * @param resultClass The class of the result, or null. 405 * If resultClass is null, a vendor specific Result implementation will be returned. 406 * The following classes are supported at a minimum: 407 * <pre> 408 * javax.xml.transform.dom.DOMResult - returns a DOMResult 409 * javax.xml.transform.sax.SAXResult - returns a SAXResult 410 * javax.xml.transform.stax.StAXResult - returns a StAXResult 411 * javax.xml.transform.stream.StreamResult - returns a StreamResult 412 * </pre> 413 * @return Returns a Result for setting the XML value. 414 * @throws SQLException if there is an error processing the XML value 415 * or if this feature is not supported. 416 * The getCause() method of the exception may provide a more detailed exception, for example, 417 * if an XML parser exception occurs. 418 * An exception is thrown if the state is not writable. 419 * @exception SQLFeatureNotSupportedException if the JDBC driver does not support 420 * this method 421 * @since 1.6 422 */ setResult(Class<T> resultClass)423 <T extends Result> T setResult(Class<T> resultClass) throws SQLException; 424 425 } 426