001: /*
002: * Copyright 2004 Clinton Begin
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.ibatis.sqlmap.engine.exchange;
017:
018: import com.ibatis.common.beans.Probe;
019: import com.ibatis.common.beans.ProbeFactory;
020: import com.ibatis.sqlmap.client.SqlMapException;
021: import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap;
022: import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMapping;
023: import com.ibatis.sqlmap.engine.mapping.result.BasicResultMap;
024: import com.ibatis.sqlmap.engine.mapping.result.ResultMap;
025: import com.ibatis.sqlmap.engine.mapping.result.ResultMapping;
026: import com.ibatis.sqlmap.engine.scope.RequestScope;
027: import org.w3c.dom.Document;
028:
029: import javax.xml.parsers.DocumentBuilderFactory;
030: import javax.xml.parsers.ParserConfigurationException;
031: import java.util.Map;
032:
033: /**
034: * A DataExchange implemtation for working with DOM objects
035: */
036: public class DomDataExchange extends BaseDataExchange implements
037: DataExchange {
038:
039: /**
040: * Constructor for the factory
041: * @param dataExchangeFactory - the factory
042: */
043: public DomDataExchange(DataExchangeFactory dataExchangeFactory) {
044: super (dataExchangeFactory);
045: }
046:
047: public void initialize(Map properties) {
048: }
049:
050: public Object[] getData(RequestScope request,
051: ParameterMap parameterMap, Object parameterObject) {
052: Probe probe = ProbeFactory.getProbe(parameterObject);
053:
054: ParameterMapping[] mappings = parameterMap
055: .getParameterMappings();
056: Object[] values = new Object[mappings.length];
057:
058: for (int i = 0; i < mappings.length; i++) {
059: values[i] = probe.getObject(parameterObject, mappings[i]
060: .getPropertyName());
061: }
062:
063: return values;
064: }
065:
066: public Object setData(RequestScope request, ResultMap resultMap,
067: Object resultObject, Object[] values) {
068:
069: String name = ((BasicResultMap) resultMap).getXmlName();
070: if (name == null) {
071: name = "result";
072: }
073:
074: if (resultObject == null) {
075: try {
076: Document doc = DocumentBuilderFactory.newInstance()
077: .newDocumentBuilder().newDocument();
078: doc.appendChild(doc.createElement(name));
079: resultObject = doc;
080: } catch (ParserConfigurationException e) {
081: throw new SqlMapException(
082: "Error creating new Document for DOM result. Cause: "
083: + e, e);
084: }
085: }
086:
087: Probe probe = ProbeFactory.getProbe(resultObject);
088:
089: ResultMapping[] mappings = resultMap.getResultMappings();
090:
091: for (int i = 0; i < mappings.length; i++) {
092: if (values[i] != null) {
093: probe.setObject(resultObject, mappings[i]
094: .getPropertyName(), values[i]);
095: }
096: }
097:
098: return resultObject;
099: }
100:
101: public Object setData(RequestScope request,
102: ParameterMap parameterMap, Object parameterObject,
103: Object[] values) {
104: Probe probe = ProbeFactory.getProbe(parameterObject);
105:
106: ParameterMapping[] mappings = parameterMap
107: .getParameterMappings();
108:
109: for (int i = 0; i < mappings.length; i++) {
110: if (values[i] != null) {
111: if (mappings[i].isOutputAllowed()) {
112: probe.setObject(parameterObject, mappings[i]
113: .getPropertyName(), values[i]);
114: }
115: }
116: }
117:
118: return parameterObject;
119: }
120:
121: }
|