KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > mapping > statement > RowHandlerCallback


1 /*
2  * Copyright 2004 Clinton Begin
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
24
25 import javax.xml.transform.*;
26 import javax.xml.transform.dom.DOMSource JavaDoc;
27 import javax.xml.transform.stream.StreamResult JavaDoc;
28 import java.io.StringWriter JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.sql.SQLException JavaDoc;
31
32 /**
33  * Class to manager row handler access
34  */

35 public class RowHandlerCallback {
36
37   private RowHandler rowHandler;
38   private ResultMap resultMap;
39   private Object JavaDoc resultObject;
40
41   /**
42    * Constructor
43    *
44    * @param resultMap - the result map
45    * @param resultObject - the result object
46    * @param rowHandler - the row handler object
47    */

48   public RowHandlerCallback(ResultMap resultMap, Object JavaDoc resultObject, RowHandler rowHandler) {
49     this.rowHandler = rowHandler;
50     this.resultMap = resultMap;
51     this.resultObject = resultObject;
52   }
53
54   /**
55    * Prepares the row object, and passes it to the row handler
56    *
57    * @param request - the request scope
58    * @param results - the result data
59    */

60   public void handleResultObject(RequestScope request, Object JavaDoc[] results, ResultSet JavaDoc rs) throws SQLException JavaDoc {
61     Object JavaDoc object;
62
63     object = resultMap.resolveSubMap(request, rs).setResultObjectValues(request, resultObject, results);
64
65     if (object != ResultMap.NO_VALUE) {
66       // XML Only special processing. (converts elements to string for easy insertion).
67
int stackDepth = request.getSession().getRequestStackDepth();
68       if (stackDepth == 1) {
69         Class JavaDoc targetType = request.getResultMap().getResultClass();
70         if (XmlTypeMarker.class.isAssignableFrom(targetType)
71             && object instanceof Document JavaDoc) {
72           object = documentToString((Document JavaDoc) object);
73         }
74       }
75
76       rowHandler.handleRow(object);
77     }
78   }
79
80   private String JavaDoc documentToString(Document JavaDoc document) {
81     String JavaDoc s = null;
82
83     try {
84       TransformerFactory tFactory = TransformerFactory.newInstance();
85       Transformer transformer = tFactory.newTransformer();
86
87       DOMSource JavaDoc source = new DOMSource JavaDoc(document);
88       StringWriter JavaDoc writer = new StringWriter JavaDoc();
89       StreamResult JavaDoc result = new StreamResult JavaDoc(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