@Override @Nullable public String getXmlAsString(ResultSet rs, String columnName) throws SQLException { SQLXML xmlObject = rs.getSQLXML(columnName); return (xmlObject != null ? xmlObject.getString() : null); }
@Override @Nullable public InputStream getXmlAsBinaryStream(ResultSet rs, String columnName) throws SQLException { SQLXML xmlObject = rs.getSQLXML(columnName); return (xmlObject != null ? xmlObject.getBinaryStream() : null); }
@Override public void cleanup() { if (this.xmlObject != null) { try { this.xmlObject.free(); } catch (SQLException ex) { throw new DataAccessResourceFailureException("Could not free SQLXML object", ex); } } }
public static String prettyPrint(SQLXML xml) throws SQLException { try { TransformerFactory transFactory = TransformerFactory.newInstance(); transFactory.setAttribute("indent-number", new Integer(2)); //$NON-NLS-1$ Transformer tf = transFactory.newTransformer(); tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); //$NON-NLS-1$ tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");//$NON-NLS-1$ tf.setOutputProperty(OutputKeys.INDENT, "yes");//$NON-NLS-1$ tf.setOutputProperty(OutputKeys.METHOD, "xml");//$NON-NLS-1$ tf.setOutputProperty(OutputKeys.STANDALONE, "yes");//$NON-NLS-1$ tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); //$NON-NLS-1$ //$NON-NLS-2$ ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamResult xmlOut = new StreamResult(new BufferedOutputStream(out)); tf.transform(xml.getSource(StreamSource.class), xmlOut); return out.toString(); } catch (Exception e) { return xml.getString(); } } }
@Override @Nullable public Reader getXmlAsCharacterStream(ResultSet rs, String columnName) throws SQLException { SQLXML xmlObject = rs.getSQLXML(columnName); return (xmlObject != null ? xmlObject.getCharacterStream() : null); }
@Override protected void provideXml(SQLXML xmlObject) throws SQLException, IOException { xmlObject.setString(value); } };
@Override @Nullable public Source getXmlAsSource(ResultSet rs, String columnName, @Nullable Class<? extends Source> sourceClass) throws SQLException { SQLXML xmlObject = rs.getSQLXML(columnName); if (xmlObject == null) { return null; } return (sourceClass != null ? xmlObject.getSource(sourceClass) : xmlObject.getSource(DOMSource.class)); }
@Override public <X> X unwrap(SQLXML value, Class<X> valueClass, Connection connection) throws SQLException { if (value == null) { return null; } else if (valueClass.isAssignableFrom(SQLXML.class)) { return (X) value; } else if (valueClass.isAssignableFrom(String.class)) { try { return (X) IOUtils.toString(value.getCharacterStream()); } catch (IOException exception) { throw new JdbcTypeException(exception); } } else if (valueClass.isAssignableFrom(InputStream.class)) { try { return (X) IOUtils.toString(value.getBinaryStream()); } catch (IOException exception) { throw new JdbcTypeException(exception); } } else if (valueClass.isAssignableFrom(Reader.class)) { return (X) value.getCharacterStream(); } else { throw newUnwrapFailure(valueClass); } } }
@Override public <X> SQLXML wrap(X value, Connection connection) throws SQLException { if (value == null) { return null; } SQLXML sqlXml; if (String.class.isInstance(value)) { sqlXml = connection.createSQLXML(); sqlXml.setString((String) value); } else if (InputStream.class.isInstance(value)) { sqlXml = connection.createSQLXML(); try { IOUtils.copy((InputStream) value, sqlXml.setBinaryStream()); } catch (IOException exception) { throw new JdbcTypeException(exception); } } else if (Reader.class.isInstance(value)) { sqlXml = connection.createSQLXML(); try { IOUtils.copy((Reader) value, sqlXml.setCharacterStream()); } catch (IOException exception) { throw new JdbcTypeException(exception); } } else { throw newWrapFailure(value); } return sqlXml; }
@Override public void set(DatabasePlatform platform, PreparedStatement statement, int index, AbstractSession session) throws SQLException { if (this.obj instanceof String) { //Bug#5200836, unwrap the connection prior to using. Connection con = session.getServerPlatform().unwrapConnection(statement.getConnection()); SQLXML sqlxml = con.createSQLXML(); sqlxml.setString((String) obj); this.obj = sqlxml; } else if (this.obj instanceof Document) { //Bug#5200836, unwrap the connection prior to using. Connection con = session.getServerPlatform().unwrapConnection(statement.getConnection()); SQLXML sqlxml = con.createSQLXML(); DOMResult result = sqlxml.setResult(DOMResult.class); result.setNode((Document) obj); this.obj = sqlxml; } super.set(platform, statement, index, session); } }
public static String prettyPrint( SQLXML xml ) throws SQLException { try { TransformerFactory transFactory = TransformerFactory.newInstance(); transFactory.setAttribute("indent-number", new Integer(2)); //$NON-NLS-1$ Transformer tf = transFactory.newTransformer(); tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); //$NON-NLS-1$ tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");//$NON-NLS-1$ tf.setOutputProperty(OutputKeys.INDENT, "yes");//$NON-NLS-1$ tf.setOutputProperty(OutputKeys.METHOD, "xml");//$NON-NLS-1$ tf.setOutputProperty(OutputKeys.STANDALONE, "yes");//$NON-NLS-1$ tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); //$NON-NLS-1$ //$NON-NLS-2$ ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamResult xmlOut = new StreamResult(new BufferedOutputStream(out)); tf.transform(xml.getSource(StreamSource.class), xmlOut); return out.toString(); } catch (Exception e) { return xml.getString(); } } }
@Override @Nullable public Reader getXmlAsCharacterStream(ResultSet rs, int columnIndex) throws SQLException { SQLXML xmlObject = rs.getSQLXML(columnIndex); return (xmlObject != null ? xmlObject.getCharacterStream() : null); }
@Override protected void provideXml(SQLXML xmlObject) throws SQLException, IOException { xmlObject.setString(value); } };
@Override @Nullable public Source getXmlAsSource(ResultSet rs, int columnIndex, @Nullable Class<? extends Source> sourceClass) throws SQLException { SQLXML xmlObject = rs.getSQLXML(columnIndex); if (xmlObject == null) { return null; } return (sourceClass != null ? xmlObject.getSource(sourceClass) : xmlObject.getSource(DOMSource.class)); }
@Override @Nullable public String getXmlAsString(ResultSet rs, int columnIndex) throws SQLException { SQLXML xmlObject = rs.getSQLXML(columnIndex); return (xmlObject != null ? xmlObject.getString() : null); }
@Override @Nullable public InputStream getXmlAsBinaryStream(ResultSet rs, int columnIndex) throws SQLException { SQLXML xmlObject = rs.getSQLXML(columnIndex); return (xmlObject != null ? xmlObject.getBinaryStream() : null); }