View Javadoc
1   /*
2    * Copyright 2004-2025 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    @Override
50    public void initialize(Map properties) {
51    }
52  
53    @Override
54    public Object[] getData(StatementScope statementScope, ParameterMap parameterMap, Object parameterObject) {
55      Probe probe = ProbeFactory.getProbe(parameterObject);
56  
57      ParameterMapping[] mappings = parameterMap.getParameterMappings();
58      Object[] values = new Object[mappings.length];
59  
60      for (int i = 0; i < mappings.length; i++) {
61        values[i] = probe.getObject(parameterObject, mappings[i].getPropertyName());
62      }
63  
64      return values;
65    }
66  
67    @Override
68    public Object setData(StatementScope statementScope, ResultMap resultMap, Object resultObject, Object[] values) {
69  
70      String name = resultMap.getXmlName();
71      if (name == null) {
72        name = "result";
73      }
74  
75      if (resultObject == null) {
76        try {
77          Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
78          doc.appendChild(doc.createElement(name));
79          resultObject = doc;
80        } catch (ParserConfigurationException e) {
81          throw new SqlMapException("Error creating new Document for DOM result.  Cause: " + e, e);
82        }
83      }
84  
85      Probe probe = ProbeFactory.getProbe(resultObject);
86  
87      ResultMapping[] mappings = resultMap.getResultMappings();
88  
89      for (int i = 0; i < mappings.length; i++) {
90        if (values[i] != null) {
91          probe.setObject(resultObject, mappings[i].getPropertyName(), values[i]);
92        }
93      }
94  
95      return resultObject;
96    }
97  
98    @Override
99    public Object setData(StatementScope statementScope, ParameterMap parameterMap, Object parameterObject,
100       Object[] values) {
101     Probe probe = ProbeFactory.getProbe(parameterObject);
102 
103     ParameterMapping[] mappings = parameterMap.getParameterMappings();
104 
105     for (int i = 0; i < mappings.length; i++) {
106       if (values[i] != null && mappings[i].isOutputAllowed()) {
107         probe.setObject(parameterObject, mappings[i].getPropertyName(), values[i]);
108       }
109     }
110 
111     return parameterObject;
112   }
113 
114 }