• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / ibatis-2 / #341

08 Sep 2023 11:16PM UTC coverage: 64.938% (+0.03%) from 64.913%
#341

push

github

web-flow
Merge pull request #183 from hazendaz/master

fixes #174, update GHA, maven wrapper, fix EOL markers, do not use star imports

5047 of 7772 relevant lines covered (64.94%)

0.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.86
/src/main/java/com/ibatis/sqlmap/engine/mapping/statement/RowHandlerCallback.java
1
/*
2
 * Copyright 2004-2023 the original author or authors.
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
 *    https://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.StatementScope;
21
import com.ibatis.sqlmap.engine.type.XmlTypeMarker;
22

23
import java.io.StringWriter;
24
import java.sql.ResultSet;
25
import java.sql.SQLException;
26

27
import javax.xml.transform.Transformer;
28
import javax.xml.transform.TransformerException;
29
import javax.xml.transform.TransformerFactory;
30
import javax.xml.transform.dom.DOMSource;
31
import javax.xml.transform.stream.StreamResult;
32

33
import org.w3c.dom.Document;
34

35
/**
36
 * Class to manager row handler access.
37
 */
38
public class RowHandlerCallback {
39

40
  /** The row handler. */
41
  private RowHandler rowHandler;
42

43
  /** The result map. */
44
  private ResultMap resultMap;
45

46
  /** The result object. */
47
  private Object resultObject;
48

49
  /**
50
   * Constructor.
51
   *
52
   * @param resultMap
53
   *          - the result map
54
   * @param resultObject
55
   *          - the result object
56
   * @param rowHandler
57
   *          - the row handler object
58
   */
59
  public RowHandlerCallback(ResultMap resultMap, Object resultObject, RowHandler rowHandler) {
1✔
60
    this.rowHandler = rowHandler;
1✔
61
    this.resultMap = resultMap;
1✔
62
    this.resultObject = resultObject;
1✔
63
  }
1✔
64

65
  /**
66
   * Prepares the row object, and passes it to the row handler.
67
   *
68
   * @param statementScope
69
   *          - the request scope
70
   * @param results
71
   *          - the result data
72
   * @param rs
73
   *          the rs
74
   *
75
   * @throws SQLException
76
   *           the SQL exception
77
   */
78
  public void handleResultObject(StatementScope statementScope, Object[] results, ResultSet rs) throws SQLException {
79
    Object object;
80

81
    statementScope.setCurrentNestedKey(null);
1✔
82
    object = resultMap.resolveSubMap(statementScope, rs).setResultObjectValues(statementScope, resultObject, results);
1✔
83

84
    if (object != ResultMap.NO_VALUE) {
1✔
85
      // XML Only special processing. (converts elements to string for easy insertion).
86
      int stackDepth = statementScope.getSession().getRequestStackDepth();
1✔
87
      if (stackDepth == 1) {
1✔
88
        Class targetType = statementScope.getResultMap().getResultClass();
1✔
89
        if (XmlTypeMarker.class.isAssignableFrom(targetType) && object instanceof Document) {
1✔
90
          object = documentToString((Document) object);
1✔
91
        }
92
      }
93

94
      rowHandler.handleRow(object);
1✔
95
    }
96
  }
1✔
97

98
  /**
99
   * Document to string.
100
   *
101
   * @param document
102
   *          the document
103
   *
104
   * @return the string
105
   */
106
  private String documentToString(Document document) {
107
    String s = null;
1✔
108

109
    try {
110
      TransformerFactory tFactory = TransformerFactory.newInstance();
1✔
111
      Transformer transformer = tFactory.newTransformer();
1✔
112

113
      DOMSource source = new DOMSource(document);
1✔
114
      StringWriter writer = new StringWriter();
1✔
115
      StreamResult result = new StreamResult(writer);
1✔
116
      transformer.transform(source, result);
1✔
117
      s = writer.getBuffer().toString();
1✔
118

119
    } catch (TransformerException e) {
×
120
      throw new RuntimeException("Error occurred.  Cause: " + e, e);
×
121
    }
1✔
122

123
    return s;
1✔
124
  }
125

126
  /**
127
   * Gets the row handler.
128
   *
129
   * @return the row handler
130
   */
131
  public RowHandler getRowHandler() {
132
    return rowHandler;
1✔
133
  }
134

135
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc