1 22 package org.jboss.util.xml; 23 24 26 import java.io.ByteArrayInputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.util.ArrayList ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 34 import javax.xml.namespace.QName ; 35 import javax.xml.parsers.DocumentBuilder ; 36 import javax.xml.parsers.DocumentBuilderFactory ; 37 import javax.xml.parsers.ParserConfigurationException ; 38 39 import org.jboss.logging.Logger; 40 import org.w3c.dom.Attr ; 41 import org.w3c.dom.Document ; 42 import org.w3c.dom.Element ; 43 import org.w3c.dom.NamedNodeMap ; 44 import org.w3c.dom.Node ; 45 import org.w3c.dom.NodeList ; 46 import org.w3c.dom.Text ; 47 import org.xml.sax.InputSource ; 48 import org.xml.sax.SAXException ; 49 50 56 public final class DOMUtils 57 { 58 private static Logger log = Logger.getLogger(DOMUtils.class); 59 60 private static ThreadLocal documentThreadLocal = new ThreadLocal (); 62 private static ThreadLocal builderThreadLocal = new ThreadLocal () { 63 protected Object initialValue() { 64 try 65 { 66 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 67 factory.setValidating(false); 68 factory.setNamespaceAware(true); 69 DocumentBuilder builder = factory.newDocumentBuilder(); 70 builder.setEntityResolver(new JBossEntityResolver()); 71 return builder; 72 } 73 catch (ParserConfigurationException e) 74 { 75 throw new RuntimeException ("Failed to create DocumentBuilder", e); 76 } 77 } 78 }; 79 80 private DOMUtils() 82 { 83 } 84 85 87 public static DocumentBuilder getDocumentBuilder() 88 { 89 DocumentBuilder builder = (DocumentBuilder )builderThreadLocal.get(); 90 return builder; 91 } 92 93 95 public static Element parse(String xmlString) throws IOException 96 { 97 try 98 { 99 return parse(new ByteArrayInputStream (xmlString.getBytes("UTF-8"))); 100 } 101 catch (IOException e) 102 { 103 log.error("Cannot parse: " + xmlString); 104 throw e; 105 } 106 } 107 108 110 public static Element parse(InputStream xmlStream) throws IOException 111 { 112 try 113 { 114 Document doc = getDocumentBuilder().parse(xmlStream); 115 Element root = doc.getDocumentElement(); 116 return root; 117 } 118 catch (SAXException e) 119 { 120 throw new IOException (e.toString()); 121 } 122 } 123 124 126 public static Element parse(InputSource source) throws IOException 127 { 128 try 129 { 130 Document doc = getDocumentBuilder().parse(source); 131 Element root = doc.getDocumentElement(); 132 return root; 133 } 134 catch (SAXException e) 135 { 136 throw new IOException (e.toString()); 137 } 138 } 139 140 142 public static Element createElement(String localPart) 143 { 144 Document doc = getOwnerDocument(); 145 log.trace("createElement {}" + localPart); 146 return doc.createElement(localPart); 147 } 148 149 151 public static Element createElement(String localPart, String prefix) 152 { 153 Document doc = getOwnerDocument(); 154 log.trace("createElement {}" + prefix + ":" + localPart); 155 return doc.createElement(prefix + ":" + localPart); 156 } 157 158 160 public static Element createElement(String localPart, String prefix, String uri) 161 { 162 Document doc = getOwnerDocument(); 163 if (prefix == null || prefix.length() == 0) 164 { 165 log.trace("createElement {" + uri + "}" + localPart); 166 return doc.createElementNS(uri, localPart); 167 } 168 else 169 { 170 log.trace("createElement {" + uri + "}" + prefix + ":" + localPart); 171 return doc.createElementNS(uri, prefix + ":" + localPart); 172 } 173 } 174 175 177 public static Element createElement(QName qname) 178 { 179 return createElement(qname.getLocalPart(), qname.getPrefix(), qname.getNamespaceURI()); 180 } 181 182 184 public static Text createTextNode(String value) 185 { 186 Document doc = getOwnerDocument(); 187 return doc.createTextNode(value); 188 } 189 190 192 public static QName getElementQName(Element el) 193 { 194 String qualifiedName = el.getNodeName(); 195 return resolveQName(el, qualifiedName); 196 } 197 198 200 public static QName resolveQName(Element el, String qualifiedName) 201 { 202 QName qname; 203 String prefix = ""; 204 String namespaceURI = ""; 205 String localPart = qualifiedName; 206 207 int colIndex = qualifiedName.indexOf(":"); 208 if (colIndex > 0) 209 { 210 prefix = qualifiedName.substring(0, colIndex); 211 localPart = qualifiedName.substring(colIndex + 1); 212 213 if ("xmlns".equals(prefix)) 214 { 215 namespaceURI = "URI:XML_PREDEFINED_NAMESPACE"; 216 } 217 else 218 { 219 Element nsElement = el; 220 while (namespaceURI.equals("") && nsElement != null) 221 { 222 namespaceURI = nsElement.getAttribute("xmlns:" + prefix); 223 if (namespaceURI.equals("")) 224 nsElement = getParentElement(nsElement); 225 } 226 } 227 228 if (namespaceURI.equals("")) 229 throw new IllegalArgumentException ("Cannot find namespace uri for: " + qualifiedName); 230 } 231 232 qname = new QName (namespaceURI, localPart, prefix); 233 return qname; 234 } 235 236 240 public static String getAttributeValue(Element el, String attrName) 241 { 242 return getAttributeValue(el, new QName (attrName)); 243 } 244 245 249 public static String getAttributeValue(Element el, QName attrName) 250 { 251 String attr = null; 252 if ("".equals(attrName.getNamespaceURI())) 253 attr = el.getAttribute(attrName.getLocalPart()); 254 else attr = el.getAttributeNS(attrName.getNamespaceURI(), attrName.getLocalPart()); 255 256 if ("".equals(attr)) 257 attr = null; 258 259 return attr; 260 } 261 262 264 public static QName getAttributeValueAsQName(Element el, String attrName) 265 { 266 return getAttributeValueAsQName(el, new QName (attrName)); 267 268 } 269 270 272 public static QName getAttributeValueAsQName(Element el, QName attrName) 273 { 274 QName qname = null; 275 276 String qualifiedName = getAttributeValue(el, attrName); 277 if (qualifiedName != null) 278 { 279 qname = resolveQName(el, qualifiedName); 280 } 281 282 return qname; 283 } 284 285 287 public static boolean getAttributeValueAsBoolean(Element el, String attrName) 288 { 289 return getAttributeValueAsBoolean(el, new QName (attrName)); 290 } 291 292 294 public static boolean getAttributeValueAsBoolean(Element el, QName attrName) 295 { 296 String attrVal = getAttributeValue(el, attrName); 297 boolean ret = "true".equalsIgnoreCase(attrVal) || "1".equalsIgnoreCase(attrVal); 298 return ret; 299 } 300 301 303 public static Integer getAttributeValueAsInteger(Element el, String attrName) 304 { 305 return getAttributeValueAsInteger(el, new QName (attrName)); 306 } 307 308 310 public static Integer getAttributeValueAsInteger(Element el, QName attrName) 311 { 312 String attrVal = getAttributeValue(el, attrName); 313 return (attrVal != null ? new Integer (attrVal) : null); 314 } 315 316 318 public static Map getAttributes(Element el) 319 { 320 Map attmap = new HashMap (); 321 NamedNodeMap attribs = el.getAttributes(); 322 for (int i = 0; i < attribs.getLength(); i++) 323 { 324 Attr attr = (Attr )attribs.item(i); 325 String name = attr.getName(); 326 QName qname = resolveQName(el, name); 327 String value = attr.getNodeValue(); 328 attmap.put(qname, value); 329 } 330 return attmap; 331 } 332 333 335 public static void copyAttributes(Element destElement, Element srcElement) 336 { 337 NamedNodeMap attribs = srcElement.getAttributes(); 338 for (int i = 0; i < attribs.getLength(); i++) 339 { 340 Attr attr = (Attr )attribs.item(i); 341 String uri = attr.getNamespaceURI(); 342 String qname = attr.getName(); 343 String value = attr.getNodeValue(); 344 345 if (uri == null && qname.startsWith("xmlns")) 348 { 349 log.trace("Ignore attribute: [uri=" + uri + ",qname=" + qname + ",value=" + value + "]"); 350 } 351 else 352 { 353 destElement.setAttributeNS(uri, qname, value); 354 } 355 } 356 } 357 358 360 public static boolean hasChildElements(Node node) 361 { 362 NodeList nlist = node.getChildNodes(); 363 for (int i = 0; i < nlist.getLength(); i++) 364 { 365 Node child = nlist.item(i); 366 if (child.getNodeType() == Node.ELEMENT_NODE) 367 return true; 368 } 369 return false; 370 } 371 372 374 public static Iterator getChildElements(Node node) 375 { 376 ArrayList list = new ArrayList (); 377 NodeList nlist = node.getChildNodes(); 378 for (int i = 0; i < nlist.getLength(); i++) 379 { 380 Node child = nlist.item(i); 381 if (child.getNodeType() == Node.ELEMENT_NODE) 382 list.add(child); 383 } 384 return list.iterator(); 385 } 386 387 389 public static String getTextContent(Node node) 390 { 391 boolean hasTextContent = false; 392 StringBuffer buffer = new StringBuffer (); 393 NodeList nlist = node.getChildNodes(); 394 for (int i = 0; i < nlist.getLength(); i++) 395 { 396 Node child = nlist.item(i); 397 if (child.getNodeType() == Node.TEXT_NODE) 398 { 399 buffer.append(child.getNodeValue()); 400 hasTextContent = true; 401 } 402 } 403 return (hasTextContent ? buffer.toString() : null); 404 } 405 406 408 public static Element getFirstChildElement(Node node) 409 { 410 return getFirstChildElementIntern(node, null); 411 } 412 413 415 public static Element getFirstChildElement(Node node, String nodeName) 416 { 417 return getFirstChildElementIntern(node, new QName (nodeName)); 418 } 419 420 422 public static Element getFirstChildElement(Node node, QName nodeName) 423 { 424 return getFirstChildElementIntern(node, nodeName); 425 } 426 427 private static Element getFirstChildElementIntern(Node node, QName nodeName) 428 { 429 Element childElement = null; 430 Iterator it = getChildElementsIntern(node, nodeName); 431 if (it.hasNext()) 432 { 433 childElement = (Element )it.next(); 434 } 435 return childElement; 436 } 437 438 440 public static Iterator getChildElements(Node node, String nodeName) 441 { 442 return getChildElementsIntern(node, new QName (nodeName)); 443 } 444 445 447 public static Iterator getChildElements(Node node, QName nodeName) 448 { 449 return getChildElementsIntern(node, nodeName); 450 } 451 452 private static Iterator getChildElementsIntern(Node node, QName nodeName) 453 { 454 ArrayList list = new ArrayList (); 455 NodeList nlist = node.getChildNodes(); 456 for (int i = 0; i < nlist.getLength(); i++) 457 { 458 Node child = nlist.item(i); 459 if (child.getNodeType() == Node.ELEMENT_NODE) 460 { 461 if (nodeName == null) 462 { 463 list.add(child); 464 } 465 else 466 { 467 QName qname; 468 if (nodeName.getNamespaceURI().length() > 0) 469 { 470 qname = new QName (child.getNamespaceURI(), child.getLocalName()); 471 } 472 else 473 { 474 qname = new QName (child.getLocalName()); 475 } 476 if (qname.equals(nodeName)) 477 { 478 list.add(child); 479 } 480 } 481 } 482 } 483 return list.iterator(); 484 } 485 486 488 public static Element getParentElement(Node node) 489 { 490 Node parent = node.getParentNode(); 491 return (parent instanceof Element ? (Element )parent : null); 492 } 493 494 495 public static Document getOwnerDocument() 496 { 497 Document doc = (Document )documentThreadLocal.get(); 498 if (doc == null) 499 { 500 doc = getDocumentBuilder().newDocument(); 501 documentThreadLocal.set(doc); 502 } 503 return doc; 504 } 505 } 506 | Popular Tags |