001: /*
002: * Copyright 2004 Clinton Begin
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.ibatis.sqlmap.engine.mapping.statement;
017:
018: import com.ibatis.sqlmap.client.event.RowHandler;
019: import com.ibatis.sqlmap.engine.mapping.result.ResultMap;
020: import com.ibatis.sqlmap.engine.scope.RequestScope;
021: import com.ibatis.sqlmap.engine.type.XmlTypeMarker;
022:
023: import org.w3c.dom.Document;
024:
025: import javax.xml.transform.*;
026: import javax.xml.transform.dom.DOMSource;
027: import javax.xml.transform.stream.StreamResult;
028: import java.io.StringWriter;
029: import java.sql.ResultSet;
030: import java.sql.SQLException;
031:
032: /**
033: * Class to manager row handler access
034: */
035: public class RowHandlerCallback {
036:
037: private RowHandler rowHandler;
038: private ResultMap resultMap;
039: private Object resultObject;
040:
041: /**
042: * Constructor
043: *
044: * @param resultMap - the result map
045: * @param resultObject - the result object
046: * @param rowHandler - the row handler object
047: */
048: public RowHandlerCallback(ResultMap resultMap, Object resultObject,
049: RowHandler rowHandler) {
050: this .rowHandler = rowHandler;
051: this .resultMap = resultMap;
052: this .resultObject = resultObject;
053: }
054:
055: /**
056: * Prepares the row object, and passes it to the row handler
057: *
058: * @param request - the request scope
059: * @param results - the result data
060: */
061: public void handleResultObject(RequestScope request,
062: Object[] results, ResultSet rs) throws SQLException {
063: Object object;
064:
065: request.setCurrentNestedKey(null);
066: object = resultMap.resolveSubMap(request, rs)
067: .setResultObjectValues(request, resultObject, results);
068:
069: if (object != ResultMap.NO_VALUE) {
070: // XML Only special processing. (converts elements to string for easy insertion).
071: int stackDepth = request.getSession()
072: .getRequestStackDepth();
073: if (stackDepth == 1) {
074: Class targetType = request.getResultMap()
075: .getResultClass();
076: if (XmlTypeMarker.class.isAssignableFrom(targetType)
077: && object instanceof Document) {
078: object = documentToString((Document) object);
079: }
080: }
081:
082: rowHandler.handleRow(object);
083: }
084: }
085:
086: private String documentToString(Document document) {
087: String s = null;
088:
089: try {
090: TransformerFactory tFactory = TransformerFactory
091: .newInstance();
092: Transformer transformer = tFactory.newTransformer();
093:
094: DOMSource source = new DOMSource(document);
095: StringWriter writer = new StringWriter();
096: StreamResult result = new StreamResult(writer);
097: transformer.transform(source, result);
098: s = writer.getBuffer().toString();
099:
100: } catch (TransformerException e) {
101: throw new RuntimeException("Error occurred. Cause: " + e,
102: e);
103: }
104:
105: return s;
106: }
107:
108: public RowHandler getRowHandler() {
109: return rowHandler;
110: }
111:
112: }
|