1 22 package org.objectweb.petals.component.common.util; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.InputStream ; 26 import java.io.StringWriter ; 27 28 import javax.xml.parsers.DocumentBuilder ; 29 import javax.xml.parsers.DocumentBuilderFactory ; 30 import javax.xml.transform.OutputKeys ; 31 import javax.xml.transform.Result ; 32 import javax.xml.transform.Source ; 33 import javax.xml.transform.Transformer ; 34 import javax.xml.transform.TransformerException ; 35 import javax.xml.transform.TransformerFactory ; 36 import javax.xml.transform.dom.DOMResult ; 37 import javax.xml.transform.dom.DOMSource ; 38 import javax.xml.transform.stream.StreamResult ; 39 40 import org.w3c.dom.Document ; 41 import org.w3c.dom.NamedNodeMap ; 42 import org.w3c.dom.Node ; 43 import org.w3c.dom.NodeList ; 44 45 import com.sun.org.apache.xpath.internal.NodeSet; 46 47 53 public class XMLHelper { 54 55 58 59 protected XMLHelper() { 60 } 61 62 69 public static Document createDocumentFromString(String source) { 70 Document doc = null; 71 try { 72 DocumentBuilderFactory factory = DocumentBuilderFactory 73 .newInstance(); 74 factory.setNamespaceAware(true); 75 DocumentBuilder builder = factory.newDocumentBuilder(); 76 InputStream is = new ByteArrayInputStream (source.getBytes()); 77 doc = builder.parse(is); 78 doc.normalize(); 79 } catch (Exception e) { 80 e.printStackTrace(); 81 } 82 return doc; 83 } 84 85 93 public static String createStringFromDOMDocument(Document document) 94 throws TransformerException { 95 document.normalize(); 96 Source source = new DOMSource (document); 97 StringWriter out = new StringWriter (); 98 Result resultStream = new StreamResult (out); 99 TransformerFactory tFactory = TransformerFactory.newInstance(); 100 Transformer transformer; 101 transformer = tFactory.newTransformer(); 102 transformer.transform(source, resultStream); 103 String result = out.toString(); 104 105 return result; 106 } 107 108 116 public static String createStringFromDOMNode(Node node) 117 throws TransformerException { 118 Source source = new DOMSource (node); 120 StringWriter out = new StringWriter (); 121 Result resultStream = new StreamResult (out); 122 TransformerFactory tFactory = TransformerFactory.newInstance(); 123 Transformer transformer; 124 transformer = tFactory.newTransformer(); 125 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 126 transformer.transform(source, resultStream); 127 String result = out.toString(); 128 return result; 129 } 130 131 139 public static Node createDOMNodeFromSource(Source source) 140 throws TransformerException { 141 DOMResult resultDom = new DOMResult (); 142 TransformerFactory tFactory = TransformerFactory.newInstance(); 143 Transformer transformer; 144 transformer = tFactory.newTransformer(); 145 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 146 transformer.transform(source, resultDom); 147 Node resultNode = resultDom.getNode(); 148 return resultNode; 149 } 150 151 164 public static Node findChild(Node parent, String nodeName, boolean recursive) { 165 parent.normalize(); 166 Node result = null; 167 if (parent != null && nodeName != null) { 168 NodeList nl = parent.getChildNodes(); 169 for (int i = 0; i < nl.getLength() && result == null; i++) { 170 if (nodeName.equals(nl.item(i).getNodeName())) { 171 result = nl.item(i); 172 } 173 } 174 if (result == null && recursive) { 176 for (int i = 0; i < nl.getLength() && result == null; i++) { 177 result = findChild(nl.item(i), nodeName, true); 178 } 179 } 180 } 181 return result; 182 } 183 184 199 public static Node findChild(Node parent, String namespaceURI, 200 String nodeName, boolean recursive) { 201 parent.normalize(); 202 String prefix = getPrefixForNamespaceURI(parent, namespaceURI, false); 203 return (findChild(parent, prefix + nodeName, recursive)); 204 } 205 206 219 public static NodeList findChildren(Node parent, String nodeName, 220 boolean recursive) { 221 parent.normalize(); 222 NodeSet nodeList = new NodeSet(); 223 if (parent != null && nodeName != null) { 224 NodeList nl = parent.getChildNodes(); 225 for (int i = 0; i < nl.getLength(); i++) { 226 if (nodeName.equals(nl.item(i).getNodeName())) { 227 nodeList.addElement(nl.item(i)); 228 } 229 } 230 if (recursive) { 232 for (int i = 0; i < nl.getLength(); i++) { 233 nodeList = findChildren(nl.item(i), nodeName, nodeList); 234 } 235 } 236 } 237 return nodeList; 238 } 239 240 249 public static String getAttributeValue(Node n, String attName) { 250 NamedNodeMap atts = n.getAttributes(); 251 Node att = atts.getNamedItem(attName); 252 if (att != null) { 253 return att.getNodeValue(); 254 } 255 return null; 256 } 257 258 264 public static Node getFirstChild(Node node) { 265 node.normalize(); 266 Node result = node.getFirstChild(); 267 while (result != null && result.getNodeType() == Node.TEXT_NODE) { 268 result = result.getNextSibling(); 269 } 270 return result; 271 } 272 273 279 public static Node getNextSibling(Node node) { 280 node.normalize(); 281 Node result = node.getNextSibling(); 282 while (result != null && result.getNodeType() == Node.TEXT_NODE) { 283 result = result.getNextSibling(); 284 } 285 return result; 286 } 287 288 300 public static String getPrefixForNamespaceURI(Node node, 301 String namespaceURI, boolean deep) { 302 String result = ""; 303 if (!deep) { 304 if (namespaceURI != null && !"".equals(namespaceURI)) { 305 while (node.getParentNode() != null) { 306 node = node.getParentNode(); 307 } 308 NamedNodeMap attributes = node.getAttributes(); 310 int i = 0; 311 if (attributes != null) { 312 while (i < attributes.getLength()) { 313 Node attr = attributes.item(i++); 314 if (namespaceURI.equals(attr.getNodeValue())) { 315 String nodeName = attr.getNodeName(); 316 if (nodeName.startsWith("xmlns:")) { 317 result = nodeName.replaceFirst("xmlns:", "") 318 + ":"; 319 return result; 320 } else if (nodeName.startsWith("xmlns")) { 321 return result; 322 } 323 } 324 } 325 } 326 i = 0; 328 NodeList nl = node.getChildNodes(); 329 while (i < nl.getLength()) { 330 Node tmpNode = nl.item(i++); 331 String prefix = getPrefixForNamespaceURI(tmpNode, 332 namespaceURI, true); 333 if (prefix != null) { 334 return prefix; 335 } 336 } 337 } 338 } else { 339 NamedNodeMap attributes = node.getAttributes(); 341 int i = 0; 342 if (attributes != null) { 343 while (i < attributes.getLength()) { 344 Node attr = attributes.item(i++); 345 if (namespaceURI.equals(attr.getNodeValue())) { 346 String nodeName = attr.getNodeName(); 347 if (nodeName.startsWith("xmlns:")) { 348 result = nodeName.replaceFirst("xmlns:", "") + ":"; 349 return result; 350 } else if (nodeName.startsWith("xmlns")) { 351 return result; 352 } 353 } 354 } 355 } 356 i = 0; 358 NodeList nl = node.getChildNodes(); 359 while (i < nl.getLength()) { 360 Node tmpNode = nl.item(i++); 361 String prefix = getPrefixForNamespaceURI(tmpNode, namespaceURI, 362 true); 363 if (prefix != null) { 364 return prefix; 365 } 366 } 367 return null; 368 } 369 return result; 370 } 371 372 384 public static String getNamespaceURIForPrefix(Node node, String pref, 385 boolean deep) { 386 String result = ""; 387 if (!deep) { 388 if (pref != null && !"".equals(pref)) { 389 while (node.getParentNode() != null) { 390 node = node.getParentNode(); 391 } 392 NamedNodeMap attributes = node.getAttributes(); 394 int i = 0; 395 if (attributes != null) { 396 while (i < attributes.getLength()) { 397 Node attr = attributes.item(i++); 398 if (attr.getNodeName().startsWith("xmlns:" + pref)) { 399 result = attr.getNodeValue(); 400 return result; 401 } 402 } 403 } 404 i = 0; 406 NodeList nl = node.getChildNodes(); 407 while (i < nl.getLength()) { 408 Node tmpNode = nl.item(i++); 409 String uri = getNamespaceURIForPrefix(tmpNode, pref, true); 410 if (uri != null) { 411 return uri; 412 } 413 } 414 } 415 } else { 416 NamedNodeMap attributes = node.getAttributes(); 418 int i = 0; 419 if (attributes != null) { 420 while (i < attributes.getLength()) { 421 Node attr = attributes.item(i++); 422 if (attr.getNodeName().startsWith("xmlns:" + pref)) { 423 result = attr.getNodeValue(); 424 return result; 425 } 426 } 427 } 428 i = 0; 430 NodeList nl = node.getChildNodes(); 431 while (i < nl.getLength()) { 432 Node tmpNode = nl.item(i++); 433 String uri = getNamespaceURIForPrefix(tmpNode, pref, true); 434 if (uri != null) { 435 return uri; 436 } 437 } 438 return null; 439 } 440 return result; 441 } 442 443 449 public static String getTextContent(Node node) { 450 node.normalize(); 451 NodeList list = node.getChildNodes(); 452 for (int i = 0; i < list.getLength(); i++) { 453 if (list.item(i).getNodeType() == Node.TEXT_NODE) { 454 return list.item(i).getNodeValue(); 455 } 456 } 457 return null; 458 } 459 460 469 public static String parseToString(Node node) throws TransformerException { 470 String result = null; 471 if (node != null) { 472 node.normalize(); 473 Transformer transformer = TransformerFactory.newInstance() 474 .newTransformer(); 475 StringWriter stringWriter = new StringWriter (128); 476 transformer.transform(new DOMSource (node), new StreamResult ( 477 stringWriter)); 478 StringBuffer buffer = stringWriter.getBuffer(); 479 result = buffer.toString(); 480 } 481 return result; 482 } 483 484 495 private static NodeSet findChildren(Node parent, String nodeName, 496 NodeSet nodeList) { 497 if (parent != null && nodeName != null) { 498 NodeList nl = parent.getChildNodes(); 499 for (int i = 0; i < nl.getLength(); i++) { 500 if (nodeName.equals(nl.item(i).getNodeName())) { 501 nodeList.addElement(nl.item(i)); 502 } 503 } 504 for (int i = 0; i < nl.getLength(); i++) { 505 nodeList = findChildren(nl.item(i), nodeName, nodeList); 506 } 507 } 508 return nodeList; 509 } 510 511 } 512 | Popular Tags |