Dashboard > iBATIS DataMapper > Home > Type Handler Callbacks > XMLTypeHandlerCallback.java
XMLTypeHandlerCallback.java
Added by Jan Vissers, last edited by Jan Vissers on Sep 18, 2006  (view change)
Labels: 
(None)


iBATIS DataMapper version 2.2.0.638

This TypeHandlerCallback class was build and tested using iBATIS DataMapper (Java) 2.2.0.638
and requires the following Oracle specific libraries

  • ojdbc14.jar
  • xdb.jar
  • xmlparserv2.jar
com.yourpackage.XMLTypeHandlerCallback
package com.yourpackage;
    
    import java.io.StringReader;
    import java.sql.SQLException;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    
    import org.w3c.dom.Document;
    import org.xml.sax.InputSource;
    
    import oracle.jdbc.OraclePreparedStatement;
    import oracle.jdbc.OracleResultSet;
    import oracle.sql.OPAQUE;
    import oracle.xdb.XMLType;
    import oracle.xml.parser.v2.XMLParseException;
    
    import com.ibatis.sqlmap.client.extensions.ParameterSetter;
    import com.ibatis.sqlmap.client.extensions.ResultGetter;
    import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;
    
    /**
    * Custom type handler to map <code>oracle.xdb.XMLType</code> to
    * <code>org.w3c.dom.Document</code>. More specifically it allows us to map
    * an Oracle XMLType column onto a XML Document.
    */
    public class XMLTypeHandlerCallback implements TypeHandlerCallback {
    
    public Object getResult(ResultGetter getter) throws SQLException {
    if (getter.getResultSet() instanceof OracleResultSet) {
    OPAQUE opaqueValue = getOpaqueValue(getter);
    if (opaqueValue != null) {
    XMLType xmlResult = XMLType.createXML(opaqueValue);
    return xmlResult.getDOM();
    } else {
    return (Document)null;
    }
    } else {
    throw new UnsupportedOperationException("XMLType mapping only supported for Oracle RDBMS");
    }
    }
    
    public void setParameter(ParameterSetter setter, Object parameter) throws SQLException {
    if (setter.getPreparedStatement() instanceof OraclePreparedStatement) {
    OraclePreparedStatement ops = (OraclePreparedStatement) setter.getPreparedStatement();
    if (parameter == null) {
    ops.setNull(setter.getParameterIndex(), oracle.jdbc.OracleTypes.OPAQUE, "SYS.XMLTYPE");
    } else {
    XMLType xmlInput = XMLType.createXML(ops.getConnection(), (Document) parameter);
    ops.setObject(setter.getParameterIndex(), xmlInput);
    }
    } else {
    throw new UnsupportedOperationException("XMLType mapping only supported for Oracle RDBMS");
    }
    }
    
    public Object valueOf(String s) {
    try {
    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    return db.parse(new InputSource(new StringReader(s)));
    } catch (Exception e) {
    if (e instanceof XMLParseException) {
    throw new IllegalArgumentException("Argument for valueOf() doesn't describe a XML Document");
    } else {
    throw new RuntimeException("Error creating XML document.  Cause: " + e);
    }
    }
    }
    
    private OPAQUE getOpaqueValue(ResultGetter getter) throws SQLException {
    OracleResultSet ors = (OracleResultSet) getter.getResultSet();
    OPAQUE op = null;
    if (getter.getColumnName() != null) {
    op = ors.getOPAQUE(getter.getColumnName());
    } else {
    op = ors.getOPAQUE(getter.getColumnIndex());
    }
    return op;
    }
    }

Site running on a free Atlassian Confluence Open Source Project License granted to OSS. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.5 Build:#811 Jul 25, 2007) - Bug/feature request - Contact Administrators