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