KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > exchange > DomDataExchange


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.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.BasicResultMap;
24 import com.ibatis.sqlmap.engine.mapping.result.ResultMap;
25 import com.ibatis.sqlmap.engine.mapping.result.ResultMapping;
26 import com.ibatis.sqlmap.engine.scope.RequestScope;
27 import org.w3c.dom.Document JavaDoc;
28
29 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
30 import javax.xml.parsers.ParserConfigurationException JavaDoc;
31 import java.util.Map JavaDoc;
32
33 /**
34  * A DataExchange implemtation for working with DOM objects
35  */

36 public class DomDataExchange extends BaseDataExchange implements DataExchange {
37
38   /**
39    * Constructor for the factory
40    * @param dataExchangeFactory - the factory
41    */

42   public DomDataExchange(DataExchangeFactory dataExchangeFactory) {
43     super(dataExchangeFactory);
44   }
45
46   public void initialize(Map properties) {
47   }
48
49   public Object JavaDoc[] getData(RequestScope request, ParameterMap parameterMap, Object JavaDoc parameterObject) {
50     Probe probe = ProbeFactory.getProbe(parameterObject);
51
52     ParameterMapping[] mappings = parameterMap.getParameterMappings();
53     Object JavaDoc[] values = new Object JavaDoc[mappings.length];
54
55     for (int i = 0; i < mappings.length; i++) {
56       values[i] = probe.getObject(parameterObject, mappings[i].getPropertyName());
57     }
58
59     return values;
60   }
61
62   public Object JavaDoc setData(RequestScope request, ResultMap resultMap, Object JavaDoc resultObject, Object JavaDoc[] values) {
63
64     String JavaDoc name = ((BasicResultMap) resultMap).getXmlName();
65     if (name == null) {
66       name = "result";
67     }
68
69     if (resultObject == null) {
70       try {
71         Document JavaDoc doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
72         doc.appendChild(doc.createElement(name));
73         resultObject = doc;
74       } catch (ParserConfigurationException JavaDoc e) {
75         throw new SqlMapException("Error creating new Document for DOM result. Cause: " + e, e);
76       }
77     }
78
79     Probe probe = ProbeFactory.getProbe(resultObject);
80
81     ResultMapping[] mappings = resultMap.getResultMappings();
82
83     for (int i = 0; i < mappings.length; i++) {
84       if (values[i] != null) {
85         probe.setObject(resultObject, mappings[i].getPropertyName(), values[i]);
86       }
87     }
88
89     return resultObject;
90   }
91
92   public Object JavaDoc setData(RequestScope request, ParameterMap parameterMap, Object JavaDoc parameterObject, Object JavaDoc[] values) {
93     Probe probe = ProbeFactory.getProbe(parameterObject);
94
95     ParameterMapping[] mappings = parameterMap.getParameterMappings();
96
97     for (int i = 0; i < mappings.length; i++) {
98       if (values[i] != null) {
99         if (mappings[i].isOutputAllowed()) {
100           probe.setObject(parameterObject, mappings[i].getPropertyName(), values[i]);
101         }
102       }
103     }
104
105     return parameterObject;
106   }
107
108
109 }
110
Popular Tags