1 17 package org.apache.servicemix.jbi.jaxp; 18 19 23 24 import java.util.HashMap ; 25 import java.util.Map ; 26 import java.util.Stack ; 27 28 import javax.xml.namespace.NamespaceContext ; 29 import javax.xml.parsers.DocumentBuilder ; 30 import javax.xml.parsers.DocumentBuilderFactory ; 31 import javax.xml.parsers.ParserConfigurationException ; 32 import javax.xml.stream.XMLStreamException; 33 34 import org.w3c.dom.Attr ; 35 import org.w3c.dom.Document ; 36 import org.w3c.dom.Element ; 37 38 public class W3CDOMStreamWriter 39 extends DOMStreamWriter 40 { 41 static final String XML_NS = "http://www.w3.org/2000/xmlns/"; 42 private Stack stack = new Stack (); 43 private Document document; 44 private Element currentNode; 45 private NamespaceContext context; 46 private Map properties = new HashMap (); 47 48 public W3CDOMStreamWriter() throws ParserConfigurationException 49 { 50 this(DocumentBuilderFactory.newInstance().newDocumentBuilder()); 51 } 52 53 public W3CDOMStreamWriter(DocumentBuilder builder) 54 { 55 document = builder.newDocument(); 56 } 57 58 public W3CDOMStreamWriter(Document document) 59 { 60 this.document = document; 61 } 62 63 public Document getDocument() 64 { 65 return document; 66 } 67 68 public void writeStartElement(String local) 69 throws XMLStreamException 70 { 71 newChild(document.createElement(local)); 72 } 73 74 private void newChild(Element element) 75 { 76 if (currentNode != null) 77 { 78 stack.push(currentNode); 79 currentNode.appendChild(element); 80 } 81 else 82 { 83 document.appendChild(element); 84 } 85 86 W3CNamespaceContext context = new W3CNamespaceContext(); 87 context.setElement(element); 88 this.context = context; 89 90 currentNode = element; 91 } 92 93 public void writeStartElement(String namespace, String local) 94 throws XMLStreamException 95 { 96 newChild(document.createElementNS(namespace, local)); 97 } 98 99 public void writeStartElement(String prefix, String local, String namespace) 100 throws XMLStreamException 101 { 102 if (prefix == null || prefix.equals("")) 103 { 104 writeStartElement(namespace, local); 105 } 106 else 107 { 108 newChild(document.createElementNS(namespace, prefix + ":" + local)); 109 } 110 } 111 112 public void writeEmptyElement(String namespace, String local) 113 throws XMLStreamException 114 { 115 writeStartElement(namespace, local); 116 } 117 118 public void writeEmptyElement(String prefix, String namespace, String local) 119 throws XMLStreamException 120 { 121 writeStartElement(prefix, namespace, local); 122 } 123 124 public void writeEmptyElement(String local) 125 throws XMLStreamException 126 { 127 writeStartElement(local); 128 } 129 130 public void writeEndElement() 131 throws XMLStreamException 132 { 133 if (stack.size() > 0) 134 currentNode = (Element ) stack.pop(); 135 else 136 currentNode = null; 137 } 138 139 public void writeEndDocument() 140 throws XMLStreamException 141 { 142 } 143 144 public void writeAttribute(String local, String value) 145 throws XMLStreamException 146 { 147 Attr a = document.createAttribute(local); 148 a.setValue(value); 149 currentNode.setAttributeNode(a); 150 } 151 152 public void writeAttribute(String prefix, String namespace, String local, String value) 153 throws XMLStreamException 154 { 155 if (prefix.length() > 0) 156 local = prefix + ":" + local; 157 158 Attr a = document.createAttributeNS(namespace, local); 159 a.setValue(value); 160 currentNode.setAttributeNodeNS(a); 161 } 162 163 public void writeAttribute(String namespace, String local, String value) 164 throws XMLStreamException 165 { 166 Attr a = document.createAttributeNS(namespace, local); 167 a.setValue(value); 168 currentNode.setAttributeNodeNS(a); 169 } 170 171 public void writeNamespace(String prefix, String namespace) 172 throws XMLStreamException 173 { 174 if (prefix.length() == 0) 175 { 176 writeDefaultNamespace(namespace); 177 } 178 else 179 { 180 currentNode.setAttributeNS(XML_NS, "xmlns:" + prefix, namespace); 181 } 182 } 183 184 public void writeDefaultNamespace(String namespace) 185 throws XMLStreamException 186 { 187 currentNode.setAttributeNS(XML_NS, "xmlns", namespace); 188 } 189 190 public void writeComment(String value) 191 throws XMLStreamException 192 { 193 currentNode.appendChild(document.createComment(value)); 194 } 195 196 public void writeProcessingInstruction(String target) 197 throws XMLStreamException 198 { 199 currentNode.appendChild(document.createProcessingInstruction(target, null)); 200 } 201 202 public void writeProcessingInstruction(String target, String data) 203 throws XMLStreamException 204 { 205 currentNode.appendChild(document.createProcessingInstruction(target, data)); 206 } 207 208 public void writeCData(String data) 209 throws XMLStreamException 210 { 211 currentNode.appendChild(document.createCDATASection(data)); 212 } 213 214 public void writeDTD(String arg0) 215 throws XMLStreamException 216 { 217 throw new UnsupportedOperationException (); 218 } 219 220 public void writeEntityRef(String ref) 221 throws XMLStreamException 222 { 223 currentNode.appendChild(document.createEntityReference(ref)); 224 } 225 226 public void writeStartDocument() 227 throws XMLStreamException 228 { 229 } 230 231 public void writeStartDocument(String version) 232 throws XMLStreamException 233 { 234 writeStartDocument(); 235 } 236 237 public void writeStartDocument(String encoding, String version) 238 throws XMLStreamException 239 { 240 writeStartDocument(); 241 } 242 243 public void writeCharacters(String text) 244 throws XMLStreamException 245 { 246 currentNode.appendChild(document.createTextNode(text)); 247 } 248 249 public void writeCharacters(char[] text, int start, int len) 250 throws XMLStreamException 251 { 252 writeCharacters(new String (text, start, len)); 253 } 254 255 public String getPrefix(String uri) 256 throws XMLStreamException 257 { 258 return context.getPrefix(uri); 259 } 260 261 public void setPrefix(String arg0, String arg1) 262 throws XMLStreamException 263 { 264 } 265 266 public void setDefaultNamespace(String arg0) 267 throws XMLStreamException 268 { 269 } 270 271 public void setNamespaceContext(NamespaceContext context) 272 throws XMLStreamException 273 { 274 this.context = context; 275 } 276 277 public NamespaceContext getNamespaceContext() 278 { 279 return context; 280 } 281 282 public Object getProperty(String prop) 283 throws IllegalArgumentException 284 { 285 return properties.get(prop); 286 } 287 } 288 | Popular Tags |