View Javadoc
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.exchange;
17  
18  import com.ibatis.common.beans.Probe;
19  import com.ibatis.common.beans.ProbeFactory;
20  import com.ibatis.sqlmap.client.SqlMapException;
21  import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap;
22  import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMapping;
23  import com.ibatis.sqlmap.engine.mapping.result.ResultMap;
24  import com.ibatis.sqlmap.engine.mapping.result.ResultMapping;
25  import com.ibatis.sqlmap.engine.scope.StatementScope;
26  
27  import java.util.Map;
28  
29  import javax.xml.parsers.DocumentBuilderFactory;
30  import javax.xml.parsers.ParserConfigurationException;
31  
32  import org.w3c.dom.Document;
33  
34  /**
35   * A DataExchange implemtation for working with DOM objects.
36   */
37  public class DomDataExchange extends BaseDataExchange implements DataExchange {
38  
39    /**
40     * Constructor for the factory.
41     *
42     * @param dataExchangeFactory
43     *          - the factory
44     */
45    public DomDataExchange(DataExchangeFactory dataExchangeFactory) {
46      super(dataExchangeFactory);
47    }
48  
49    public void initialize(Map properties) {
50    }
51  
52    public Object[] getData(StatementScope statementScope, ParameterMap parameterMap, Object parameterObject) {
53      Probe probe = ProbeFactory.getProbe(parameterObject);
54  
55      ParameterMapping[] mappings = parameterMap.getParameterMappings();
56      Object[] values = new Object[mappings.length];
57  
58      for (int i = 0; i < mappings.length; i++) {
59        values[i] = probe.getObject(parameterObject, mappings[i].getPropertyName());
60      }
61  
62      return values;
63    }
64  
65    public Object setData(StatementScope statementScope, ResultMap resultMap, Object resultObject, Object[] values) {
66  
67      String name = ((ResultMap) resultMap).getXmlName();
68      if (name == null) {
69        name = "result";
70      }
71  
72      if (resultObject == null) {
73        try {
74          Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
75          doc.appendChild(doc.createElement(name));
76          resultObject = doc;
77        } catch (ParserConfigurationException e) {
78          throw new SqlMapException("Error creating new Document for DOM result.  Cause: " + e, e);
79        }
80      }
81  
82      Probe probe = ProbeFactory.getProbe(resultObject);
83  
84      ResultMapping[] mappings = resultMap.getResultMappings();
85  
86      for (int i = 0; i < mappings.length; i++) {
87        if (values[i] != null) {
88          probe.setObject(resultObject, mappings[i].getPropertyName(), values[i]);
89        }
90      }
91  
92      return resultObject;
93    }
94  
95    public Object setData(StatementScope statementScope, ParameterMap parameterMap, Object parameterObject,
96        Object[] values) {
97      Probe probe = ProbeFactory.getProbe(parameterObject);
98  
99      ParameterMapping[] mappings = parameterMap.getParameterMappings();
100 
101     for (int i = 0; i < mappings.length; i++) {
102       if (values[i] != null) {
103         if (mappings[i].isOutputAllowed()) {
104           probe.setObject(parameterObject, mappings[i].getPropertyName(), values[i]);
105         }
106       }
107     }
108 
109     return parameterObject;
110   }
111 
112 }