1 18 19 package org.apache.tools.ant.util; 20 21 import org.w3c.dom.Document ; 22 import org.w3c.dom.DocumentFragment ; 23 import org.w3c.dom.Element ; 24 import org.w3c.dom.Node ; 25 import org.w3c.dom.Text ; 26 27 import org.apache.tools.ant.DynamicElementNS; 28 import org.apache.tools.ant.ProjectComponent; 29 import org.apache.tools.ant.DynamicConfiguratorNS; 30 31 42 public class XMLFragment extends ProjectComponent implements DynamicElementNS { 43 44 private Document doc; 45 private DocumentFragment fragment; 46 47 50 public XMLFragment() { 51 doc = JAXPUtils.getDocumentBuilder().newDocument(); 52 fragment = doc.createDocumentFragment(); 53 } 54 55 59 public DocumentFragment getFragment() { 60 return fragment; 61 } 62 63 67 public void addText(String s) { 68 addText(fragment, s); 69 } 70 71 78 public Object createDynamicElement(String uri, String name, String qName) { 79 Element e = null; 80 if (uri.equals("")) { 81 e = doc.createElement(name); 82 } else { 83 e = doc.createElementNS(uri, qName); 84 } 85 fragment.appendChild(e); 86 return new Child(e); 87 } 88 89 94 private void addText(Node n, String s) { 95 s = getProject().replaceProperties(s); 96 if (s != null && !s.trim().equals("")) { 98 Text t = doc.createTextNode(s.trim()); 99 n.appendChild(t); 100 } 101 } 102 103 106 public class Child implements DynamicConfiguratorNS { 107 private Element e; 108 109 Child(Element e) { 110 this.e = e; 111 } 112 113 117 public void addText(String s) { 118 XMLFragment.this.addText(e, s); 119 } 120 121 128 public void setDynamicAttribute( 129 String uri, String name, String qName, String value) { 130 if (uri.equals("")) { 131 e.setAttribute(name, value); 132 } else { 133 e.setAttributeNS(uri, qName, value); 134 } 135 } 136 137 144 public Object createDynamicElement(String uri, String name, String qName) { 145 Element e2 = null; 146 if (uri.equals("")) { 147 e2 = doc.createElement(name); 148 } else { 149 e2 = doc.createElementNS(uri, qName); 150 } 151 e.appendChild(e2); 152 return new Child(e2); 153 } 154 } 155 156 } 157 | Popular Tags |