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