From: Juergen H. <jho...@us...> - 2008-10-21 19:55:33
|
Update of /cvsroot/springframework/spring/src/org/springframework/jdbc/support/xml In directory fdv4jf1.ch3.sourceforge.com:/tmp/cvs-serv23464/src/org/springframework/jdbc/support/xml Modified Files: SqlXmlHandler.java SqlXmlValue.java Added Files: Jdbc4SqlXmlHandler.java Log Message: provided Jdbc4SqlXmlHandler as default implementation of the SqlXmlHandler interface; added SqlValue class to "jdbc.support" package, with SqlXmlValue derived from SqlValue instead of SqlTypeValue --- NEW FILE: Jdbc4SqlXmlHandler.java --- /* * Copyright 2002-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.jdbc.support.xml; import java.io.IOException; import java.io.InputStream; import java.io.Reader; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.SQLXML; import javax.xml.transform.Source; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMSource; import org.w3c.dom.Document; import org.springframework.dao.DataAccessResourceFailureException; /** * Default implementation of the {@link SqlXmlHandler} interface. * Provides database-specific implementations for storing and * retrieving XML documents to and from fields in a database, * relying on the JDBC 4.0 <code>java.sql.SQLXML</code> facility. * * @author Thomas Risberg * @author Juergen Hoeller * @since 2.5.6 * @see java.sql.SQLXML * @see java.sql.ResultSet#getSQLXML * @see java.sql.PreparedStatement#setSQLXML */ public class Jdbc4SqlXmlHandler implements SqlXmlHandler { //------------------------------------------------------------------------- // Convenience methods for accessing XML content //------------------------------------------------------------------------- public String getXmlAsString(ResultSet rs, String columnName) throws SQLException { return rs.getSQLXML(columnName).getString(); } public String getXmlAsString(ResultSet rs, int columnIndex) throws SQLException { return rs.getSQLXML(columnIndex).getString(); } public InputStream getXmlAsBinaryStream(ResultSet rs, String columnName) throws SQLException { return rs.getSQLXML(columnName).getBinaryStream(); } public InputStream getXmlAsBinaryStream(ResultSet rs, int columnIndex) throws SQLException { return rs.getSQLXML(columnIndex).getBinaryStream(); } public Reader getXmlAsCharacterStream(ResultSet rs, String columnName) throws SQLException { return rs.getSQLXML(columnName).getCharacterStream(); } public Reader getXmlAsCharacterStream(ResultSet rs, int columnIndex) throws SQLException { return rs.getSQLXML(columnIndex).getCharacterStream(); } public Source getXmlAsSource(ResultSet rs, String columnName, Class sourceClass) throws SQLException { return rs.getSQLXML(columnName).getSource(sourceClass != null ? sourceClass : DOMSource.class); } public Source getXmlAsSource(ResultSet rs, int columnIndex, Class sourceClass) throws SQLException { return rs.getSQLXML(columnIndex).getSource(sourceClass != null ? sourceClass : DOMSource.class); } //------------------------------------------------------------------------- // Convenience methods for building XML content //------------------------------------------------------------------------- public SqlXmlValue newSqlXmlValue(final String value) { return new AbstractJdbc4SqlXmlValue() { protected void provideXml(SQLXML xmlObject) throws SQLException, IOException { xmlObject.setString(value); } }; } public SqlXmlValue newSqlXmlValue(final XmlBinaryStreamProvider provider) { return new AbstractJdbc4SqlXmlValue() { protected void provideXml(SQLXML xmlObject) throws SQLException, IOException { provider.provideXml(xmlObject.setBinaryStream()); } }; } public SqlXmlValue newSqlXmlValue(final XmlCharacterStreamProvider provider) { return new AbstractJdbc4SqlXmlValue() { protected void provideXml(SQLXML xmlObject) throws SQLException, IOException { provider.provideXml(xmlObject.setCharacterStream()); } }; } public SqlXmlValue newSqlXmlValue(final Class resultClass, final XmlResultProvider provider) { return new AbstractJdbc4SqlXmlValue() { protected void provideXml(SQLXML xmlObject) throws SQLException, IOException { provider.provideXml(xmlObject.setResult(resultClass)); } }; } public SqlXmlValue newSqlXmlValue(final Document document) { return new AbstractJdbc4SqlXmlValue() { protected void provideXml(SQLXML xmlObject) throws SQLException, IOException { ((DOMResult) xmlObject.setResult(DOMResult.class)).setNode(document); } }; } /** * Internal base class for {@link SqlXmlValue} implementations. */ private static abstract class AbstractJdbc4SqlXmlValue implements SqlXmlValue { private SQLXML xmlObject; public void setValue(PreparedStatement ps, int paramIndex) throws SQLException { this.xmlObject = ps.getConnection().createSQLXML(); try { provideXml(this.xmlObject); } catch (IOException ex) { throw new DataAccessResourceFailureException("Failure encountered while providing XML", ex); } ps.setSQLXML(paramIndex, this.xmlObject); } public void cleanup() { try { this.xmlObject.free(); } catch (SQLException ex) { throw new DataAccessResourceFailureException("Could not free SQLXML object", ex); } } protected abstract void provideXml(SQLXML xmlObject) throws SQLException, IOException; } } Index: SqlXmlValue.java =================================================================== RCS file: /cvsroot/springframework/spring/src/org/springframework/jdbc/support/xml/SqlXmlValue.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** SqlXmlValue.java 5 Jun 2008 22:35:55 -0000 1.2 --- SqlXmlValue.java 21 Oct 2008 19:55:26 -0000 1.3 *************** *** 17,24 **** package org.springframework.jdbc.support.xml; ! import org.springframework.jdbc.core.DisposableSqlTypeValue; /** ! * Subinterface of {@link org.springframework.jdbc.core.SqlTypeValue} * that supports passing in XML data to specified column and adds a * cleanup callback, to be invoked after the value has been set and --- 17,24 ---- package org.springframework.jdbc.support.xml; ! import org.springframework.jdbc.support.SqlValue; /** ! * Subinterface of {@link org.springframework.jdbc.support.SqlValue} * that supports passing in XML data to specified column and adds a * cleanup callback, to be invoked after the value has been set and *************** *** 27,33 **** * @author Thomas Risberg * @since 2.5.5 ! * @see org.springframework.jdbc.core.DisposableSqlTypeValue */ ! public interface SqlXmlValue extends DisposableSqlTypeValue { } --- 27,33 ---- * @author Thomas Risberg * @since 2.5.5 ! * @see org.springframework.jdbc.support.SqlValue */ ! public interface SqlXmlValue extends SqlValue { } Index: SqlXmlHandler.java =================================================================== RCS file: /cvsroot/springframework/spring/src/org/springframework/jdbc/support/xml/SqlXmlHandler.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** SqlXmlHandler.java 13 Jun 2008 18:46:01 -0000 1.3 --- SqlXmlHandler.java 21 Oct 2008 19:55:26 -0000 1.4 *************** *** 19,23 **** import java.io.InputStream; import java.io.Reader; - import java.io.Writer; import java.sql.ResultSet; import java.sql.SQLException; --- 19,22 ---- *************** *** 28,52 **** /** ! * Abstraction for handling XML fields in specific databases. * ! * <p>Its main purpose is to isolate database specific handling of XML stored in the ! * database. JDBC 4.0 introduces the new data type <code>java.sql.SQLXML</code> but ! * most databases and their drivers currently rely on database specific data types ! * and features. * ! * <p>Provides accessor methods for XML fields, and acts as factory for * {@link SqlXmlValue} instances. * * @author Thomas Risberg * @since 2.5.5 ! * @see java.sql.ResultSet#getSQLXML * @see java.sql.SQLXML */ public interface SqlXmlHandler { /** * Retrieve the given column as String from the given ResultSet. ! * Might simply invoke <code>ResultSet.getString</code> or work with ! * <code>SQLXML</code> or database specific classes depending on the * database and driver. * @param rs the ResultSet to retrieve the content from --- 27,57 ---- /** ! * Abstraction for handling XML fields in specific databases. Its main purpose ! * is to isolate database-specific handling of XML stored in the database. * ! * <p>JDBC 4.0 introduces the new data type <code>java.sql.SQLXML</code> ! * but most databases and their drivers currently rely on database-specific ! * data types and features. * ! * <p>Provides accessor methods for XML fields and acts as factory for * {@link SqlXmlValue} instances. * * @author Thomas Risberg * @since 2.5.5 ! * @see Jdbc4SqlXmlHandler * @see java.sql.SQLXML + * @see java.sql.ResultSet#getSQLXML + * @see java.sql.PreparedStatement#setSQLXML */ public interface SqlXmlHandler { + //------------------------------------------------------------------------- + // Convenience methods for accessing XML content + //------------------------------------------------------------------------- + /** * Retrieve the given column as String from the given ResultSet. ! * <p>Might simply invoke <code>ResultSet.getString</code> or work with ! * <code>SQLXML</code> or database-specific classes depending on the * database and driver. * @param rs the ResultSet to retrieve the content from *************** *** 61,66 **** /** * Retrieve the given column as String from the given ResultSet. ! * Might simply invoke <code>ResultSet.getString</code> or work with ! * <code>SQLXML</code> or database specific classes depending on the * database and driver. * @param rs the ResultSet to retrieve the content from --- 66,71 ---- /** * Retrieve the given column as String from the given ResultSet. ! * <p>Might simply invoke <code>ResultSet.getString</code> or work with ! * <code>SQLXML</code> or database-specific classes depending on the * database and driver. * @param rs the ResultSet to retrieve the content from *************** *** 75,80 **** /** * Retrieve the given column as binary stream from the given ResultSet. ! * Might simply invoke <code>ResultSet.getAsciiStream</code> or work with ! * <code>SQLXML</code> or database specific classes depending on the * database and driver. * @param rs the ResultSet to retrieve the content from --- 80,85 ---- /** * Retrieve the given column as binary stream from the given ResultSet. ! * <p>Might simply invoke <code>ResultSet.getAsciiStream</code> or work with ! * <code>SQLXML</code> or database-specific classes depending on the * database and driver. * @param rs the ResultSet to retrieve the content from *************** *** 89,94 **** /** * Retrieve the given column as binary stream from the given ResultSet. ! * Might simply invoke <code>ResultSet.getAsciiStream</code> or work with ! * <code>SQLXML</code> or database specific classes depending on the * database and driver. * @param rs the ResultSet to retrieve the content from --- 94,99 ---- /** * Retrieve the given column as binary stream from the given ResultSet. ! * <p>Might simply invoke <code>ResultSet.getAsciiStream</code> or work with ! * <code>SQLXML</code> or database-specific classes depending on the * database and driver. * @param rs the ResultSet to retrieve the content from *************** *** 103,108 **** /** * Retrieve the given column as character stream from the given ResultSet. ! * Might simply invoke <code>ResultSet.getCharacterStream</code> or work with ! * <code>SQLXML</code> or database specific classes depending on the * database and driver. * @param rs the ResultSet to retrieve the content from --- 108,113 ---- /** * Retrieve the given column as character stream from the given ResultSet. ! * <p>Might simply invoke <code>ResultSet.getCharacterStream</code> or work with ! * <code>SQLXML</code> or database-specific classes depending on the * database and driver. * @param rs the ResultSet to retrieve the content from *************** *** 117,122 **** /** * Retrieve the given column as character stream from the given ResultSet. ! * Might simply invoke <code>ResultSet.getCharacterStream</code> or work with ! * <code>SQLXML</code> or database specific classes depending on the * database and driver. * @param rs the ResultSet to retrieve the content from --- 122,127 ---- /** * Retrieve the given column as character stream from the given ResultSet. ! * <p>Might simply invoke <code>ResultSet.getCharacterStream</code> or work with ! * <code>SQLXML</code> or database-specific classes depending on the * database and driver. * @param rs the ResultSet to retrieve the content from *************** *** 132,137 **** * Retrieve the given column as Source implemented using the specified source class * from the given ResultSet. ! * Might work with <code>SQLXML</code> or database specific classes depending on the ! * database and driver. * @param rs the ResultSet to retrieve the content from * @param columnName the column name to use --- 137,142 ---- * Retrieve the given column as Source implemented using the specified source class * from the given ResultSet. ! * <p>Might work with <code>SQLXML</code> or database-specific classes depending ! * on the database and driver. * @param rs the ResultSet to retrieve the content from * @param columnName the column name to use *************** *** 147,152 **** * Retrieve the given column as Source implemented using the specified source class * from the given ResultSet. ! * Might work with <code>SQLXML</code> or database specific classes depending on the ! * database and driver. * @param rs the ResultSet to retrieve the content from * @param columnIndex the column index to use --- 152,157 ---- * Retrieve the given column as Source implemented using the specified source class * from the given ResultSet. ! * <p>Might work with <code>SQLXML</code> or database-specific classes depending ! * on the database and driver. * @param rs the ResultSet to retrieve the content from * @param columnIndex the column index to use *************** *** 159,165 **** Source getXmlAsSource(ResultSet rs, int columnIndex, Class sourceClass) throws SQLException; /** ! * Get an instance of an <code>SqlXmlValue</code> implementation to be used together with ! * the database specific implementation of this <code>SqlXmlHandler</code>. * @param value the XML String value providing XML data * @return the implementation specific instance --- 164,175 ---- Source getXmlAsSource(ResultSet rs, int columnIndex, Class sourceClass) throws SQLException; + + //------------------------------------------------------------------------- + // Convenience methods for building XML content + //------------------------------------------------------------------------- + /** ! * Create a <code>SqlXmlValue</code> instance for the given XML data, ! * as supported by the underlying JDBC driver. * @param value the XML String value providing XML data * @return the implementation specific instance *************** *** 170,175 **** /** ! * Get an instance of an <code>SqlXmlValue</code> implementation to be used together with ! * the database specific implementation of this <code>SqlXmlHandler</code>. * @param provider the <code>XmlBinaryStreamProvider</code> providing XML data * @return the implementation specific instance --- 180,185 ---- /** ! * Create a <code>SqlXmlValue</code> instance for the given XML data, ! * as supported by the underlying JDBC driver. * @param provider the <code>XmlBinaryStreamProvider</code> providing XML data * @return the implementation specific instance *************** *** 180,185 **** /** ! * Get an instance of an <code>SqlXmlValue</code> implementation to be used together with ! * the database specific implementation of this <code>SqlXmlHandler</code>. * @param provider the <code>XmlCharacterStreamProvider</code> providing XML data * @return the implementation specific instance --- 190,195 ---- /** ! * Create a <code>SqlXmlValue</code> instance for the given XML data, ! * as supported by the underlying JDBC driver. * @param provider the <code>XmlCharacterStreamProvider</code> providing XML data * @return the implementation specific instance *************** *** 190,195 **** /** ! * Get an instance of an <code>SqlXmlValue</code> implementation to be used together with ! * the database specific implementation of this <code>SqlXmlHandler</code>. * @param resultClass the Result implementation class to be used * @param provider the <code>XmlResultProvider</code> that will provide the XML data --- 200,205 ---- /** ! * Create a <code>SqlXmlValue</code> instance for the given XML data, ! * as supported by the underlying JDBC driver. * @param resultClass the Result implementation class to be used * @param provider the <code>XmlResultProvider</code> that will provide the XML data *************** *** 201,206 **** /** ! * Get an instance of an <code>SqlXmlValue</code> implementation to be used together with ! * the database specific implementation of this <code>SqlXmlHandler</code>. * @param doc the XML Document to be used * @return the implementation specific instance --- 211,216 ---- /** ! * Create a <code>SqlXmlValue</code> instance for the given XML data, ! * as supported by the underlying JDBC driver. * @param doc the XML Document to be used * @return the implementation specific instance |