1 16 package com.ibatis.sqlmap.engine.mapping.statement; 17 18 import com.ibatis.sqlmap.client.event.RowHandler; 19 import com.ibatis.sqlmap.engine.mapping.result.ResultMap; 20 import com.ibatis.sqlmap.engine.scope.RequestScope; 21 import com.ibatis.sqlmap.engine.type.XmlTypeMarker; 22 import com.ibatis.common.exception.NestedRuntimeException; 23 import org.w3c.dom.Document ; 24 25 import javax.xml.transform.*; 26 import javax.xml.transform.dom.DOMSource ; 27 import javax.xml.transform.stream.StreamResult ; 28 import java.io.StringWriter ; 29 import java.sql.ResultSet ; 30 import java.sql.SQLException ; 31 32 35 public class RowHandlerCallback { 36 37 private RowHandler rowHandler; 38 private ResultMap resultMap; 39 private Object resultObject; 40 41 48 public RowHandlerCallback(ResultMap resultMap, Object resultObject, RowHandler rowHandler) { 49 this.rowHandler = rowHandler; 50 this.resultMap = resultMap; 51 this.resultObject = resultObject; 52 } 53 54 60 public void handleResultObject(RequestScope request, Object [] results, ResultSet rs) throws SQLException { 61 Object object; 62 63 object = resultMap.resolveSubMap(request, rs).setResultObjectValues(request, resultObject, results); 64 65 if (object != ResultMap.NO_VALUE) { 66 int stackDepth = request.getSession().getRequestStackDepth(); 68 if (stackDepth == 1) { 69 Class targetType = request.getResultMap().getResultClass(); 70 if (XmlTypeMarker.class.isAssignableFrom(targetType) 71 && object instanceof Document ) { 72 object = documentToString((Document ) object); 73 } 74 } 75 76 rowHandler.handleRow(object); 77 } 78 } 79 80 private String documentToString(Document document) { 81 String s = null; 82 83 try { 84 TransformerFactory tFactory = TransformerFactory.newInstance(); 85 Transformer transformer = tFactory.newTransformer(); 86 87 DOMSource source = new DOMSource (document); 88 StringWriter writer = new StringWriter (); 89 StreamResult result = new StreamResult (writer); 90 transformer.transform(source, result); 91 s = writer.getBuffer().toString(); 92 93 } catch (TransformerException e) { 94 throw new NestedRuntimeException("Error occurred. Cause: " + e, e); 95 } 96 97 return s; 98 } 99 100 101 } 102 | Popular Tags |