001: /*
002: * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package java.sql;
027:
028: import java.io.InputStream;
029: import java.io.OutputStream;
030: import java.io.Reader;
031: import java.io.Writer;
032:
033: import javax.xml.transform.Result;
034: import javax.xml.transform.Source;
035:
036: /**
037: * The mapping in the JavaTM programming language for the SQL XML type.
038: * XML is a built-in type that stores an XML value
039: * as a column value in a row of a database table.
040: * By default drivers implement an SQLXML object as
041: * a logical pointer to the XML data
042: * rather than the data itself.
043: * An SQLXML object is valid for the duration of the transaction in which it was created.
044: * <p>
045: * The SQLXML interface provides methods for accessing the XML value
046: * as a String, a Reader or Writer, or as a Stream. The XML value
047: * may also be accessed through a Source or set as a Result, which
048: * are used with XML Parser APIs such as DOM, SAX, and StAX, as
049: * well as with XSLT transforms and XPath evaluations.
050: * <p>
051: * Methods in the interfaces ResultSet, CallableStatement, and PreparedStatement,
052: * such as getSQLXML allow a programmer to access an XML value.
053: * In addition, this interface has methods for updating an XML value.
054: * <p>
055: * The XML value of the SQLXML instance may be obtained as a BinaryStream using
056: * <pre>
057: * SQLXML sqlxml = resultSet.getSQLXML(column);
058: * InputStream binaryStream = sqlxml.getBinaryStream();
059: * </pre>
060: * For example, to parse an XML value with a DOM parser:
061: * <pre>
062: * DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
063: * Document result = parser.parse(binaryStream);
064: * </pre>
065: * or to parse an XML value with a SAX parser to your handler:
066: * <pre>
067: * SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
068: * parser.parse(binaryStream, myHandler);
069: * </pre>
070: * or to parse an XML value with a StAX parser:
071: * <pre>
072: * XMLInputFactory factory = XMLInputFactory.newInstance();
073: * XMLStreamReader streamReader = factory.createXMLStreamReader(binaryStream);
074: * </pre>
075: * <p>
076: * Because databases may use an optimized representation for the XML,
077: * accessing the value through getSource() and
078: * setResult() can lead to improved processing performance
079: * without serializing to a stream representation and parsing the XML.
080: * <p>
081: * For example, to obtain a DOM Document Node:
082: * <pre>
083: * DOMSource domSource = sqlxml.getSource(DOMSource.class);
084: * Document document = (Document) domSource.getNode();
085: * </pre>
086: * or to set the value to a DOM Document Node to myNode:
087: * <pre>
088: * DOMResult domResult = sqlxml.setResult(DOMResult.class);
089: * domResult.setNode(myNode);
090: * </pre>
091: * or, to send SAX events to your handler:
092: * <pre>
093: * SAXSource saxSource = sqlxml.getSource(SAXSource.class);
094: * XMLReader xmlReader = saxSource.getXMLReader();
095: * xmlReader.setContentHandler(myHandler);
096: * xmlReader.parse(saxSource.getInputSource());
097: * </pre>
098: * or, to set the result value from SAX events:
099: * <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: * This method closes this object and releases the resources that it held.
194: * The SQL XML object becomes invalid and neither readable or writeable
195: * when this method is called.
196: *
197: * After <code>free</code> has been called, any attempt to invoke a
198: * method other than <code>free</code> will result in a <code>SQLException</code>
199: * being thrown. If <code>free</code> is called multiple times, the subsequent
200: * calls to <code>free</code> are treated as a no-op.
201: * @throws SQLException if there is an error freeing the XML value.
202: * @exception SQLFeatureNotSupportedException if the JDBC driver does not support
203: * this method
204: * @since 1.6
205: */
206: void free() throws SQLException;
207:
208: /**
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 and
215: * 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: * @exception SQLFeatureNotSupportedException if the JDBC driver does not support
221: * this method
222: * @since 1.6
223: */
224: InputStream getBinaryStream() throws SQLException;
225:
226: /**
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 specification
230: * 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 writeable when this method is called and
234: * 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: * @exception SQLFeatureNotSupportedException if the JDBC driver does not support
240: * this method
241: * @since 1.6
242: */
243: OutputStream setBinaryStream() throws SQLException;
244:
245: /**
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 for
249: * 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 and
256: * 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: * @exception SQLFeatureNotSupportedException if the JDBC driver does not support
264: * this method
265: * @since 1.6
266: */
267: Reader getCharacterStream() throws SQLException;
268:
269: /**
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 for
273: * 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 writeable when this method is called and
280: * 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: * @exception SQLFeatureNotSupportedException if the JDBC driver does not support
288: * this method
289: * @since 1.6
290: */
291: Writer setCharacterStream() throws SQLException;
292:
293: /**
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 for
297: * 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 and
304: * 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: * @exception SQLFeatureNotSupportedException if the JDBC driver does not support
312: * this method
313: * @since 1.6
314: */
315: String getString() throws SQLException;
316:
317: /**
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 for
321: * 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 writeable when this method is called and
328: * may also become not readable depending on implementation.
329: *
330: * @param value the XML value
331: * @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: * @exception SQLFeatureNotSupportedException if the JDBC driver does not support
336: * this method
337: * @since 1.6
338: */
339: void setString(String value) throws SQLException;
340:
341: /**
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 and
349: * may also become not writable depending on implementation.
350: * <p>
351: * Note that SAX is a callback architecture, so a returned
352: * SAXSource should then be set with a content handler that will
353: * receive the SAX events from parsing. The content handler
354: * 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 sourceClass The class of the source, or null.
363: * If the class is null, a vendor specifc Source implementation will be returned.
364: * The following classes are supported at a minimum:
365: * <pre>
366: * javax.xml.transform.dom.DOMSource - returns a DOMSource
367: * javax.xml.transform.sax.SAXSource - returns a SAXSource
368: * javax.xml.transform.stax.StAXSource - returns a StAXSource
369: * javax.xml.transform.stream.StreamSource - returns a StreamSource
370: * </pre>
371: * @return a Source for reading the XML value.
372: * @throws SQLException if there is an error processing the XML value
373: * or if this feature is not supported.
374: * The getCause() method of the exception may provide a more detailed exception, for example,
375: * if an XML parser exception occurs.
376: * An exception is thrown if the state is not readable.
377: * @exception SQLFeatureNotSupportedException if the JDBC driver does not support
378: * this method
379: * @since 1.6
380: */
381: <T extends Source> T getSource(Class<T> sourceClass)
382: 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: */
423: <T extends Result> T setResult(Class<T> resultClass)
424: throws SQLException;
425:
426: }
|