1 56 57 58 package org.jdom.output; 59 60 import java.util.*; 61 62 import org.jdom.*; 63 import org.jdom.adapters.*; 64 65 66 78 public class DOMOutputter { 79 80 private static final String CVS_ID = 81 "@(#) $RCSfile: DOMOutputter.java,v $ $Revision: 1.41 $ $Date: 2004/09/03 06:03:42 $ $Name: $"; 82 83 84 private static final String DEFAULT_ADAPTER_CLASS = 85 "org.jdom.adapters.XercesDOMAdapter"; 86 87 88 private String adapterClass; 89 90 95 public DOMOutputter() { 96 } 98 99 106 public DOMOutputter(String adapterClass) { 107 this.adapterClass = adapterClass; 108 } 109 110 111 119 public org.w3c.dom.Document output(Document document) 120 throws JDOMException { 121 NamespaceStack namespaces = new NamespaceStack(); 122 123 org.w3c.dom.Document domDoc = null; 124 try { 125 DocType dt = document.getDocType(); 127 domDoc = createDOMDocument(dt); 128 129 Iterator itr = document.getContent().iterator(); 131 while (itr.hasNext()) { 132 Object node = itr.next(); 133 134 if (node instanceof Element) { 135 Element element = (Element) node; 136 org.w3c.dom.Element domElement = 137 output(element, domDoc, namespaces); 138 org.w3c.dom.Element root = domDoc.getDocumentElement(); 140 if (root == null) { 141 domDoc.appendChild(domElement); } 144 else { 145 domDoc.replaceChild(domElement, root); 148 } 149 } 150 else if (node instanceof Comment) { 151 Comment comment = (Comment) node; 152 org.w3c.dom.Comment domComment = 153 domDoc.createComment(comment.getText()); 154 domDoc.appendChild(domComment); 155 } 156 else if (node instanceof ProcessingInstruction) { 157 ProcessingInstruction pi = 158 (ProcessingInstruction) node; 159 org.w3c.dom.ProcessingInstruction domPI = 160 domDoc.createProcessingInstruction( 161 pi.getTarget(), pi.getData()); 162 domDoc.appendChild(domPI); 163 } 164 else if (node instanceof DocType) { 165 } 167 else { 168 throw new JDOMException( 169 "Document contained top-level content with type:" + 170 node.getClass().getName()); 171 } 172 } 173 } 174 catch (Throwable e) { 175 throw new JDOMException("Exception outputting Document", e); 176 } 177 178 return domDoc; 179 } 180 181 private org.w3c.dom.Document createDOMDocument(DocType dt) 182 throws JDOMException { 183 if (adapterClass != null) { 184 try { 186 DOMAdapter adapter = 187 (DOMAdapter)Class.forName(adapterClass).newInstance(); 188 return adapter.createDocument(dt); 190 } 191 catch (ClassNotFoundException e) { 192 } 194 catch (IllegalAccessException e) { 195 } 197 catch (InstantiationException e) { 198 } 200 } 201 else { 202 try { 204 DOMAdapter adapter = 205 (DOMAdapter)Class.forName( 206 "org.jdom.adapters.JAXPDOMAdapter").newInstance(); 207 return adapter.createDocument(dt); 209 } 210 catch (ClassNotFoundException e) { 211 } 213 catch (IllegalAccessException e) { 214 } 216 catch (InstantiationException e) { 217 } 219 } 220 221 try { 223 DOMAdapter adapter = (DOMAdapter) 224 Class.forName(DEFAULT_ADAPTER_CLASS).newInstance(); 225 return adapter.createDocument(dt); 226 } 229 catch (ClassNotFoundException e) { 230 } 232 catch (IllegalAccessException e) { 233 } 235 catch (InstantiationException e) { 236 } 238 239 throw new JDOMException("No JAXP or default parser available"); 240 241 } 242 243 private org.w3c.dom.Element output(Element element, 244 org.w3c.dom.Document domDoc, 245 NamespaceStack namespaces) 246 throws JDOMException { 247 try { 248 int previouslyDeclaredNamespaces = namespaces.size(); 249 250 org.w3c.dom.Element domElement = null; 251 if (element.getNamespace() == Namespace.NO_NAMESPACE) { 252 domElement = domDoc.createElement(element.getQualifiedName()); 254 } 255 else { 256 domElement = domDoc.createElementNS( 257 element.getNamespaceURI(), 258 element.getQualifiedName()); 259 } 260 261 Namespace ns = element.getNamespace(); 267 if (ns != Namespace.XML_NAMESPACE && 268 !(ns == Namespace.NO_NAMESPACE && 269 namespaces.getURI("") == null)) { 270 String prefix = ns.getPrefix(); 271 String uri = namespaces.getURI(prefix); 272 if (!ns.getURI().equals(uri)) { namespaces.push(ns); 274 String attrName = getXmlnsTagFor(ns); 275 domElement.setAttribute(attrName, ns.getURI()); 276 } 277 } 278 279 Iterator itr = element.getAdditionalNamespaces().iterator(); 281 while (itr.hasNext()) { 282 Namespace additional = (Namespace)itr.next(); 283 String prefix = additional.getPrefix(); 284 String uri = namespaces.getURI(prefix); 285 if (!additional.getURI().equals(uri)) { 286 String attrName = getXmlnsTagFor(additional); 287 domElement.setAttribute(attrName, additional.getURI()); 288 namespaces.push(additional); 289 } 290 } 291 292 itr = element.getAttributes().iterator(); 294 while (itr.hasNext()) { 295 Attribute attribute = (Attribute) itr.next(); 296 domElement.setAttributeNode(output(attribute, domDoc)); 297 Namespace ns1 = attribute.getNamespace(); 298 if ((ns1 != Namespace.NO_NAMESPACE) && 299 (ns1 != Namespace.XML_NAMESPACE)) { 300 String prefix = ns1.getPrefix(); 301 String uri = namespaces.getURI(prefix); 302 if (!ns1.getURI().equals(uri)) { String attrName = getXmlnsTagFor(ns1); 304 domElement.setAttribute(attrName, ns1.getURI()); 305 namespaces.push(ns1); 306 } 307 } 308 if (attribute.getNamespace() == Namespace.NO_NAMESPACE) { 310 domElement.setAttribute(attribute.getQualifiedName(), 312 attribute.getValue()); 313 } 314 else { 315 domElement.setAttributeNS(attribute.getNamespaceURI(), 316 attribute.getQualifiedName(), 317 attribute.getValue()); 318 } 319 } 320 321 itr = element.getContent().iterator(); 323 while (itr.hasNext()) { 324 Object node = itr.next(); 325 326 if (node instanceof Element) { 327 Element e = (Element) node; 328 org.w3c.dom.Element domElt = output(e, domDoc, namespaces); 329 domElement.appendChild(domElt); 330 } 331 else if (node instanceof String ) { 332 String str = (String ) node; 333 org.w3c.dom.Text domText = domDoc.createTextNode(str); 334 domElement.appendChild(domText); 335 } 336 else if (node instanceof CDATA) { 337 CDATA cdata = (CDATA) node; 338 org.w3c.dom.CDATASection domCdata = 339 domDoc.createCDATASection(cdata.getText()); 340 domElement.appendChild(domCdata); 341 } 342 else if (node instanceof Text) { 343 Text text = (Text) node; 344 org.w3c.dom.Text domText = 345 domDoc.createTextNode(text.getText()); 346 domElement.appendChild(domText); 347 } 348 else if (node instanceof Comment) { 349 Comment comment = (Comment) node; 350 org.w3c.dom.Comment domComment = 351 domDoc.createComment(comment.getText()); 352 domElement.appendChild(domComment); 353 } 354 else if (node instanceof ProcessingInstruction) { 355 ProcessingInstruction pi = 356 (ProcessingInstruction) node; 357 org.w3c.dom.ProcessingInstruction domPI = 358 domDoc.createProcessingInstruction( 359 pi.getTarget(), pi.getData()); 360 domElement.appendChild(domPI); 361 } 362 else if (node instanceof EntityRef) { 363 EntityRef entity = (EntityRef) node; 364 org.w3c.dom.EntityReference domEntity = 365 domDoc.createEntityReference(entity.getName()); 366 domElement.appendChild(domEntity); 367 } 368 else { 369 throw new JDOMException( 370 "Element contained content with type:" + 371 node.getClass().getName()); 372 } 373 } 374 375 while (namespaces.size() > previouslyDeclaredNamespaces) { 377 namespaces.pop(); 378 } 379 380 return domElement; 381 } 382 catch (Exception e) { 383 throw new JDOMException("Exception outputting Element " + 384 element.getQualifiedName(), e); 385 } 386 } 387 388 private org.w3c.dom.Attr output(Attribute attribute, 389 org.w3c.dom.Document domDoc) 390 throws JDOMException { 391 org.w3c.dom.Attr domAttr = null; 392 try { 393 if (attribute.getNamespace() == Namespace.NO_NAMESPACE) { 394 domAttr = domDoc.createAttribute(attribute.getQualifiedName()); 396 } 397 else { 398 domAttr = domDoc.createAttributeNS(attribute.getNamespaceURI(), 399 attribute.getQualifiedName()); 400 } 401 domAttr.setValue(attribute.getValue()); 402 } catch (Exception e) { 403 throw new JDOMException("Exception outputting Attribute " + 404 attribute.getQualifiedName(), e); 405 } 406 return domAttr; 407 } 408 409 415 private static String getXmlnsTagFor(Namespace ns) { 416 String attrName = "xmlns"; 417 if (!ns.getPrefix().equals("")) { 418 attrName += ":"; 419 attrName += ns.getPrefix(); 420 } 421 return attrName; 422 } 423 } 424 | Popular Tags |