1 package org.ejen; 22 23 import org.ejen.util.DOMUtil; 24 import java.util.Properties ; 25 import javax.xml.transform.dom.DOMSource ; 26 import org.w3c.dom.Node ; 27 import org.w3c.dom.NodeList ; 28 import org.apache.xpath.XPathAPI; 29 import org.w3c.dom.Document ; 30 31 72 public class EjenMergeNode extends EjenChildNode { 73 protected String _file = null; 74 protected String _select = null; 75 protected String _to = null; 76 77 81 public String nodeName() { 82 return "source"; 83 } 84 85 89 public Properties getAttributes() { 90 Properties attrs = super.getAttributes(); 91 92 if (_file != null) { 93 attrs.setProperty("file", _file); 94 } 95 if (_select != null) { 96 attrs.setProperty("select", _select); 97 } 98 if (_to != null) { 99 attrs.setProperty("to", _to); 100 } 101 return attrs; 102 } 103 104 109 public void setFile(String file) { 110 _file = file; 111 } 112 113 118 public void setSelect(String select) { 119 _select = select; 120 } 121 122 127 public void setTo(String to) { 128 _to = to; 129 } 130 131 135 public void check() { 136 super.check(); 137 if (_file == null) { 138 throw new EjenException(this, "No 'file' attribute"); 139 } 140 } 141 142 146 public void process() { 147 super.process(); 148 DOMSource src = null; 149 150 try { 151 src = (DOMSource ) (getFromGlobalContext(CTX_DOM_SOURCE)); 152 } catch (Exception e) { 153 throw new EjenException(this, null, e); 154 } 155 if (src == null) { 156 throw new EjenException(this, 157 "no '" + CTX_DOM_SOURCE + "' in global context"); 158 } 159 try { 160 Document srcDoc = (Document ) (src.getNode()); 161 Node to = srcDoc.getDocumentElement(); 162 163 if (_to != null) { 164 to = XPathAPI.selectSingleNode(srcDoc, evaluateAVT(_to)); 165 } 166 Document mergeDoc = DOMUtil.parseXMLFile(evaluateAVT(_file)); 167 168 if (_select == null) { 169 to.appendChild(srcDoc.importNode(mergeDoc.getDocumentElement().cloneNode(true), 170 true)); 171 } else { 172 NodeList nl = XPathAPI.selectNodeList(mergeDoc, 173 evaluateAVT(_select)); 174 175 for (int i = 0; i < nl.getLength(); i++) { 176 to.appendChild(srcDoc.importNode(nl.item(i), true)); 177 } 178 } 179 } catch (EjenException e) { 180 throw e; 181 } catch (Exception e) { 182 throw new EjenException(this, "merge error", e); 183 } 184 } 185 } 186 | Popular Tags |