-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathXmlHandler.java
156 lines (143 loc) · 5.44 KB
/
XmlHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.test.handler;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OraclePreparedStatement;
import oracle.jdbc.OracleResultSet;
import oracle.sql.OPAQUE;
import oracle.xdb.XMLType;
import oracle.xml.parser.v2.XMLParseException;
public class XmlHandler implements TypeHandler<Object>{
// final static Logger logger = LoggerFactory.getLogger(XMLTypeHandlerCallback.class);
@Override
public Object getResult(ResultSet resultSet, String columnName) throws SQLException
{
if (resultSet instanceof OracleResultSet)
{
OracleResultSet oracleResultSet = (OracleResultSet) resultSet;
OPAQUE opaqueValue = oracleResultSet.getOPAQUE(columnName);
if (opaqueValue != null)
{
XMLType xmlResult = XMLType.createXML(opaqueValue);
return xmlResult.getDocument();
}
else
{
return (Document) null;
}
}
else
{
throw new UnsupportedOperationException("XMLType mapping only supported for Oracle RDBMS LOL");
}
}
@Override
public Object getResult(CallableStatement callableStatement, int columnIndex) throws SQLException
{
if (callableStatement instanceof OracleCallableStatement)
{
OracleCallableStatement oracleCallableStatement = (OracleCallableStatement) callableStatement;
OPAQUE opaqueValue = oracleCallableStatement.getOPAQUE(columnIndex);
if (opaqueValue != null)
{
XMLType xmlResult = XMLType.createXML(opaqueValue);
return xmlResult.getDocument();
}
else
{
return (Document) null;
}
}
else
{
throw new UnsupportedOperationException("XMLType mapping only supported for Oracle RDBMS");
}
}
@Override
public void setParameter(PreparedStatement preparedStatement, int i, Object parameter, JdbcType jdbcType) throws SQLException
{
if (preparedStatement instanceof OraclePreparedStatement)
{
OraclePreparedStatement oraclePreparedStatement = (OraclePreparedStatement) preparedStatement;
if (parameter == null)
{
// oraclePreparedStatement.setNull(i, oracle.jdbc.OracleTypes.OPAQUE);
oraclePreparedStatement.setNull(i, oracle.jdbc.OracleTypes.OPAQUE, "SYS.XMLTYPE");
// oraclePreparedStatement.setObject(i, null);
// XMLType poXML = XMLType.createXML(preparedStatement.getConnection(), "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><NULL></NULL>");
// oraclePreparedStatement.setOPAQUE(i, poXML);
// try {
// DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
// DocumentBuilder docBuilder;
// docBuilder = docFactory.newDocumentBuilder();
// // root elements
// Document doc = docBuilder.newDocument();
// doc.appendChild(doc.createElement("null"));
// XMLType poXML=XMLType.createXML(preparedStatement.getConnection(), doc);
// oraclePreparedStatement.setObject(i, poXML);
// } catch (ParserConfigurationException e) {
// e.printStackTrace();
// }
}
else
{
XMLType xmlInput = XMLType.createXML(oraclePreparedStatement.getConnection(), (Document) parameter);
oraclePreparedStatement.setObject(i, 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);
}
}
}
@Override
public Object getResult(ResultSet resultSet, int columnIndex) throws SQLException {
if (resultSet instanceof OracleResultSet)
{
OracleResultSet oracleResultSet = (OracleResultSet) resultSet;
OPAQUE opaqueValue = oracleResultSet.getOPAQUE(columnIndex);
if (opaqueValue != null)
{
XMLType xmlResult = XMLType.createXML(opaqueValue);
return xmlResult.getDocument();
}
else
{
return (Document) null;
}
}
else
{
throw new UnsupportedOperationException("XMLType mapping only supported for Oracle RDBMS LOL");
}
}
}