Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package test.oracle.xml;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.IOException;
  5. import java.io.Serializable;
  6. import java.io.StringWriter;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10.  
  11. import javax.xml.parsers.DocumentBuilder;
  12. import javax.xml.parsers.DocumentBuilderFactory;
  13. import javax.xml.parsers.ParserConfigurationException;
  14. import javax.xml.transform.OutputKeys;
  15. import javax.xml.transform.Transformer;
  16. import javax.xml.transform.TransformerException;
  17. import javax.xml.transform.TransformerFactory;
  18. import javax.xml.transform.dom.DOMSource;
  19. import javax.xml.transform.stream.StreamResult;
  20.  
  21. import oracle.jdbc.OracleResultSet;
  22. import oracle.jdbc.driver.OraclePreparedStatement;
  23. import oracle.sql.OPAQUE;
  24. import oracle.xdb.XMLType;
  25.  
  26. import org.hibernate.HibernateException;
  27. import org.hibernate.usertype.UserType;
  28. import org.w3c.dom.Document;
  29. import org.xml.sax.SAXException;
  30.  
  31. public class OracleXMLType implements UserType, Serializable {
  32.     // all other methods are ommitted at present for brevity
  33.  
  34.     private static final long serialVersionUID = 2308230823023l;
  35.     private static final Class returnedClass = Document.class;
  36.     private static final int[] SQL_TYPES = new int[] { oracle.xdb.XMLType._SQL_TYPECODE };
  37.  
  38.     public int[] sqlTypes() {
  39.         return SQL_TYPES;
  40.     }
  41.  
  42.     public Class returnedClass() {
  43.         return returnedClass;
  44.     }
  45.  
  46.     public int hashCode(Object _obj) {
  47.         return _obj.hashCode();
  48.     }
  49.  
  50.     public Object assemble(Serializable _cached, Object _owner)
  51.             throws HibernateException {
  52.         try {
  53.             return OracleXMLType.stringToDom((String) _cached);
  54.         } catch (Exception e) {
  55.             throw new HibernateException(
  56.                     "Could not assemble String to Document", e);
  57.         }
  58.     }
  59.  
  60.     public Serializable disassemble(Object _obj) throws HibernateException {
  61.         try {
  62.             return OracleXMLType.domToString((Document) _obj);
  63.         } catch (Exception e) {
  64.             throw new HibernateException(
  65.                     "Could not disassemble Document to Serializable", e);
  66.         }
  67.     }
  68.  
  69.     public Object replace(Object _orig, Object _tar, Object _owner) {
  70.         return deepCopy(_orig);
  71.     }
  72.  
  73.     public boolean equals(Object arg0, Object arg1) throws HibernateException {
  74.         if (arg0 == null && arg1 == null)
  75.             return true;
  76.         else if (arg0 == null && arg1 != null)
  77.             return false;
  78.         else
  79.             return arg0.equals(arg1);
  80.     }
  81.  
  82.     public Object nullSafeGet(ResultSet rs, String[] names, Object arg2)
  83.             throws HibernateException, SQLException {
  84.         XMLType xmlType = null;
  85.         Document doc = null;
  86.         try {
  87.             OPAQUE op = null;
  88.             OracleResultSet ors = null;
  89.             if (rs instanceof OracleResultSet) {
  90.                 ors = (OracleResultSet) rs;
  91.             } else {
  92.                 throw new UnsupportedOperationException(
  93.                         "ResultSet needs to be of type OracleResultSet");
  94.             }
  95.             op = ors.getOPAQUE(names[0]);
  96.             if (op != null) {
  97.                 xmlType = XMLType.createXML(op);
  98.             }
  99.             doc = xmlType.getDOM();
  100.         } finally {
  101.             if (null != xmlType) {
  102.                 xmlType.close();
  103.             }
  104.         }
  105.         return doc;
  106.     }
  107.  
  108.     public void nullSafeSet(PreparedStatement st, Object value, int index)
  109.             throws HibernateException, SQLException {
  110.         try {
  111.             XMLType xmlType = null;
  112.             if (value != null) {
  113.                 xmlType = XMLType.createXML(st.getConnection(),
  114.                         OracleXMLType.domToString((Document) value));
  115.             } else {
  116.                 xmlType = XMLType.createXML(st.getConnection(), "");
  117.             }
  118.  
  119.             ((OraclePreparedStatement) st).setObject(index, xmlType);
  120.         } catch (Exception e) {
  121.             throw new SQLException(
  122.                     "Could not convert Document to String for storage");
  123.         }
  124.     }
  125.  
  126.     public Object deepCopy(Object value) throws HibernateException {
  127.         if (value == null)
  128.             return null;
  129.  
  130.         return (Document) ((Document) value).cloneNode(true);
  131.     }
  132.  
  133.     public boolean isMutable() {
  134.         return false;
  135.     }
  136.  
  137.     protected static String domToString(Document _document)
  138.             throws TransformerException {
  139.         TransformerFactory tFactory = TransformerFactory.newInstance();
  140.         Transformer transformer = tFactory.newTransformer();
  141.         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  142.         DOMSource source = new DOMSource(_document);
  143.         StringWriter sw = new StringWriter();
  144.         StreamResult result = new StreamResult(sw);
  145.         transformer.transform(source, result);
  146.         return sw.toString();
  147.     }
  148.  
  149.     protected static Document stringToDom(String xmlSource)
  150.             throws SAXException, ParserConfigurationException, IOException {
  151.         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  152.         DocumentBuilder builder = factory.newDocumentBuilder();
  153.         ByteArrayInputStream inputStream = new ByteArrayInputStream(
  154.                 xmlSource.getBytes("UTF-8"));
  155.         Document document = builder.parse(inputStream);
  156.         return document;
  157.     }
  158. }