1 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 ; 28 29 import javax.xml.parsers.DocumentBuilderFactory ; 30 import javax.xml.parsers.ParserConfigurationException ; 31 import java.util.Map ; 32 33 36 public class DomDataExchange extends BaseDataExchange implements DataExchange { 37 38 42 public DomDataExchange(DataExchangeFactory dataExchangeFactory) { 43 super(dataExchangeFactory); 44 } 45 46 public void initialize(Map properties) { 47 } 48 49 public Object [] getData(RequestScope request, ParameterMap parameterMap, Object parameterObject) { 50 Probe probe = ProbeFactory.getProbe(parameterObject); 51 52 ParameterMapping[] mappings = parameterMap.getParameterMappings(); 53 Object [] values = new Object [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 setData(RequestScope request, ResultMap resultMap, Object resultObject, Object [] values) { 63 64 String name = ((BasicResultMap) resultMap).getXmlName(); 65 if (name == null) { 66 name = "result"; 67 } 68 69 if (resultObject == null) { 70 try { 71 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 72 doc.appendChild(doc.createElement(name)); 73 resultObject = doc; 74 } catch (ParserConfigurationException 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 setData(RequestScope request, ParameterMap parameterMap, Object parameterObject, Object [] 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 |