1 56 57 package org.jdom.input; 58 59 import org.jdom.*; 60 import org.jdom.Document; 61 import org.jdom.Element; 62 import org.w3c.dom.*; 63 64 78 public class DOMBuilder { 79 80 private static final String CVS_ID = 81 "@(#) $RCSfile: DOMBuilder.java,v $ $Revision: 1.59 $ $Date: 2004/09/03 06:03:41 $ $Name: $"; 82 83 84 private String adapterClass; 85 86 87 private JDOMFactory factory = new DefaultJDOMFactory(); 88 89 94 public DOMBuilder() { 95 } 96 97 105 public DOMBuilder(String adapterClass) { 106 this.adapterClass = adapterClass; 107 } 108 109 115 public void setFactory(JDOMFactory factory) { 116 this.factory = factory; 117 } 118 119 123 public JDOMFactory getFactory() { 124 return factory; 125 } 126 127 133 public Document build(org.w3c.dom.Document domDocument) { 134 Document doc = factory.document(null); 135 buildTree(domDocument, doc, null, true); 136 return doc; 137 } 138 139 145 public org.jdom.Element build(org.w3c.dom.Element domElement) { 146 Document doc = factory.document(null); 147 buildTree(domElement, doc, null, true); 148 return doc.getRootElement(); 149 } 150 151 161 private void buildTree(Node node, 162 Document doc, 163 Element current, 164 boolean atRoot) { 165 switch (node.getNodeType()) { 167 case Node.DOCUMENT_NODE: 168 NodeList nodes = node.getChildNodes(); 169 for (int i=0, size=nodes.getLength(); i<size; i++) { 170 buildTree(nodes.item(i), doc, current, true); 171 } 172 break; 173 174 case Node.ELEMENT_NODE: 175 String nodeName = node.getNodeName(); 176 String prefix = ""; 177 String localName = nodeName; 178 int colon = nodeName.indexOf(':'); 179 if (colon >= 0) { 180 prefix = nodeName.substring(0, colon); 181 localName = nodeName.substring(colon + 1); 182 } 183 184 Namespace ns = null; 186 String uri = node.getNamespaceURI(); 187 if (uri == null) { 188 ns = (current == null) ? Namespace.NO_NAMESPACE 189 : current.getNamespace(prefix); 190 } 191 else { 192 ns = Namespace.getNamespace(prefix, uri); 193 } 194 195 Element element = factory.element(localName, ns); 196 197 if (atRoot) { 198 doc.setRootElement(element); } else { 201 factory.addContent(current, element); 203 } 204 205 NamedNodeMap attributeList = node.getAttributes(); 207 int attsize = attributeList.getLength(); 208 209 for (int i = 0; i < attsize; i++) { 210 Attr att = (Attr) attributeList.item(i); 211 212 String attname = att.getName(); 213 if (attname.startsWith("xmlns")) { 214 String attPrefix = ""; 215 colon = attname.indexOf(':'); 216 if (colon >= 0) { 217 attPrefix = attname.substring(colon + 1); 218 } 219 220 String attvalue = att.getValue(); 221 222 Namespace declaredNS = 223 Namespace.getNamespace(attPrefix, attvalue); 224 225 if (prefix.equals(attPrefix)) { 231 element.setNamespace(declaredNS); 232 } 233 else { 234 factory.addNamespaceDeclaration(element, declaredNS); 235 } 236 } 237 } 238 239 for (int i = 0; i < attsize; i++) { 241 Attr att = (Attr) attributeList.item(i); 242 243 String attname = att.getName(); 244 245 if ( !attname.startsWith("xmlns")) { 246 String attPrefix = ""; 247 String attLocalName = attname; 248 colon = attname.indexOf(':'); 249 if (colon >= 0) { 250 attPrefix = attname.substring(0, colon); 251 attLocalName = attname.substring(colon + 1); 252 } 253 254 String attvalue = att.getValue(); 255 256 Namespace attns = null; 258 if ("".equals(attPrefix)) { 259 attns = Namespace.NO_NAMESPACE; 260 } 261 else { 262 attns = element.getNamespace(attPrefix); 263 } 264 265 Attribute attribute = 266 factory.attribute(attLocalName, attvalue, attns); 267 factory.setAttribute(element, attribute); 268 } 269 } 270 271 NodeList children = node.getChildNodes(); 275 if (children != null) { 276 int size = children.getLength(); 277 for (int i = 0; i < size; i++) { 278 Node item = children.item(i); 279 if (item != null) { 280 buildTree(item, doc, element, false); 281 } 282 } 283 } 284 break; 285 286 case Node.TEXT_NODE: 287 String data = node.getNodeValue(); 288 factory.addContent(current, factory.text(data)); 289 break; 290 291 case Node.CDATA_SECTION_NODE: 292 String cdata = node.getNodeValue(); 293 factory.addContent(current, factory.cdata(cdata)); 294 break; 295 296 297 case Node.PROCESSING_INSTRUCTION_NODE: 298 if (atRoot) { 299 factory.addContent(doc, 300 factory.processingInstruction(node.getNodeName(), 301 node.getNodeValue())); 302 } else { 303 factory.addContent(current, 304 factory.processingInstruction(node.getNodeName(), 305 node.getNodeValue())); 306 } 307 break; 308 309 case Node.COMMENT_NODE: 310 if (atRoot) { 311 factory.addContent(doc, factory.comment(node.getNodeValue())); 312 } else { 313 factory.addContent(current, factory.comment(node.getNodeValue())); 314 } 315 break; 316 317 case Node.ENTITY_REFERENCE_NODE: 318 EntityRef entity = factory.entityRef(node.getNodeName()); 319 factory.addContent(current, entity); 320 break; 321 322 case Node.ENTITY_NODE: 323 break; 325 326 case Node.DOCUMENT_TYPE_NODE: 327 DocumentType domDocType = (DocumentType)node; 328 String publicID = domDocType.getPublicId(); 329 String systemID = domDocType.getSystemId(); 330 String internalDTD = domDocType.getInternalSubset(); 331 332 DocType docType = factory.docType(domDocType.getName()); 333 docType.setPublicID(publicID); 334 docType.setSystemID(systemID); 335 docType.setInternalSubset(internalDTD); 336 337 factory.addContent(doc, docType); 338 break; 339 } 340 } 341 } 342 | Popular Tags |