1 22 23 package org.objectweb.petals.tools.jbicommon.util; 24 25 import java.io.ByteArrayInputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.StringWriter ; 29 import java.util.ArrayList ; 30 import java.util.List ; 31 import java.util.logging.Level ; 32 import java.util.logging.Logger ; 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 import javax.xml.transform.OutputKeys ; 39 import javax.xml.transform.Result ; 40 import javax.xml.transform.Source ; 41 import javax.xml.transform.Transformer ; 42 import javax.xml.transform.TransformerException ; 43 import javax.xml.transform.TransformerFactory ; 44 import javax.xml.transform.dom.DOMSource ; 45 import javax.xml.transform.stream.StreamResult ; 46 47 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptorException; 48 import org.w3c.dom.Document ; 49 import org.w3c.dom.NamedNodeMap ; 50 import org.w3c.dom.Node ; 51 import org.w3c.dom.NodeList ; 52 import org.xml.sax.InputSource ; 53 import org.xml.sax.SAXException ; 54 55 import com.sun.org.apache.xpath.internal.XPathAPI; 56 57 66 public final class XMLUtil { 67 68 private static final Logger LOGGER = Logger.getLogger(XMLUtil.class 69 .getName()); 70 71 private XMLUtil() { 72 } 74 75 86 public static Node createAttribute(final Document document, 87 final String att, final String value) { 88 Node element = null; 89 if (document != null) { 90 element = document.createAttribute(att); 91 element.setNodeValue(value); 92 } 93 return element; 94 } 95 96 109 public static Node createNode(final Document document, 110 final String nodeName, final String ... attVal) { 111 Node element = null; 112 if (document != null && attVal.length % 2 == 0) { 113 element = document.createElement(nodeName); 114 for (int i = 0; i < attVal.length; i = i + 2) { 115 element.getAttributes().setNamedItem( 116 createAttribute(document, attVal[i], attVal[i + 1])); 117 } 118 } 119 return element; 120 } 121 122 135 public static Node findChild(final Node node, final String nodeName, 136 final boolean recursive) { 137 node.normalize(); 138 Node result = null; 139 NodeList nl = node.getChildNodes(); 140 if (node != null && nodeName != null) { 141 result = lookupNodeInNodeList(nodeName, nl); 142 if (result == null && recursive) { 144 for (int i = 0; i < nl.getLength() && result == null; i++) { 145 result = findChild(nl.item(i), nodeName, true); 146 } 147 } 148 } 149 return result; 150 } 151 152 private static Node lookupNodeInNodeList(final String nodeName, 153 final NodeList nl) { 154 Node result = null; 155 for (int i = 0; i < nl.getLength() && result == null; i++) { 156 if (nodeName.equals(nl.item(i).getNodeName())) { 157 result = nl.item(i); 158 } 159 } 160 return result; 161 } 162 163 178 public static Node findChild(final Node node, final String namespaceURI, 179 final String nodeName, final boolean recursive) { 180 node.normalize(); 181 String prefix = node.lookupPrefix(namespaceURI); 182 return findChild(node, prefix + nodeName, recursive); 183 } 184 185 194 public static String parseToString(final Node node) 195 throws TransformerException { 196 String result = null; 197 if (node != null) { 198 node.normalize(); 199 Transformer transformer = TransformerFactory.newInstance() 200 .newTransformer(); 201 StringWriter stringWriter = new StringWriter (); 202 transformer.transform(new DOMSource (node), new StreamResult ( 203 stringWriter)); 204 StringBuffer buffer = stringWriter.getBuffer(); 205 result = buffer.toString(); 206 } 207 return result; 208 } 209 210 219 public static String getAttributeValue(final Node n, final String attName) { 220 String ret = null; 221 if (n != null) { 222 NamedNodeMap atts = n.getAttributes(); 223 Node att = atts.getNamedItem(attName); 224 if (att != null) { 225 ret = att.getNodeValue(); 226 } 227 } 228 return ret; 229 } 230 231 243 public static String getRequiredAttributeValue(final Node n, 244 final String attName) throws JBIDescriptorException { 245 String ret = null; 246 if (n != null) { 247 NamedNodeMap atts = n.getAttributes(); 248 Node att = atts.getNamedItem(attName); 249 if (att != null) { 250 String attValue = att.getNodeValue(); 251 if (!StringHelper.isNullOrEmpty(attName)) { 252 ret = attValue; 253 } else { 254 throw new JBIDescriptorException( 255 "Required attribute can't be empty: " + attName); 256 } 257 } else { 258 throw new JBIDescriptorException( 259 "Missing a required attribute: " + attName); 260 } 261 } 262 return ret; 263 } 264 265 271 public static Node getFirstChild(final Node node) { 272 Node result = node.getFirstChild(); 273 while (result != null && result.getNodeType() == Node.TEXT_NODE) { 274 result = result.getNextSibling(); 275 } 276 return result; 277 } 278 279 285 public static Node getNextSibling(final Node node) { 286 Node result = node.getNextSibling(); 287 if (result != null) { 288 while (result.getNodeType() == Node.TEXT_NODE) { 289 result = result.getNextSibling(); 290 } 291 } 292 return result; 293 } 294 295 315 321 public static String getTextContent(final Node node) { 322 String result = null; 323 NodeList list = node.getChildNodes(); 324 for (int i = 0; i < list.getLength(); i++) { 325 if (list.item(i).getNodeType() == Node.TEXT_NODE) { 326 result = list.item(i).getNodeValue(); 327 result = result.replace('\t', ' ').replace('\n', ' ').trim(); 328 break; 329 } 330 } 331 return result; 332 } 333 334 342 public static String createStringFromDOMDocument(final Node document) 343 throws TransformerException { 344 return createStringFromDOMNode(document, false); 345 } 346 347 355 public static String createStringFromDOMNode(final Node node) 356 throws TransformerException { 357 return createStringFromDOMNode(node, true); 358 } 359 360 368 public static String createStringFromDOMNode(final Node node, 369 final boolean omitDeclaration) throws TransformerException { 370 node.normalize(); 371 Source source = new DOMSource (node); 372 StringWriter out = new StringWriter (); 373 Result resultStream = new StreamResult (out); 374 TransformerFactory tFactory = TransformerFactory.newInstance(); 375 Transformer transformer; 376 transformer = tFactory.newTransformer(); 377 if (omitDeclaration) { 378 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, 379 "yes"); 380 } 381 transformer.transform(source, resultStream); 382 return out.toString(); 383 } 384 385 393 public static Document createDocumentFromString(final String xml) { 394 Document doc = null; 395 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 396 factory.setNamespaceAware(true); 397 DocumentBuilder builder; 398 try { 399 builder = factory.newDocumentBuilder(); 400 InputStream in = new ByteArrayInputStream (xml.getBytes()); 401 InputSource inputSource = new InputSource (in); 402 doc = builder.parse(inputSource); 403 doc.normalize(); 404 } catch (ParserConfigurationException e) { 405 LOGGER.log(Level.SEVERE, "Bad XML parser configuration", e); 406 } catch (SAXException e) { 407 LOGGER.log(Level.SEVERE, 408 "Bad XML fragment can't be transformed to a DOM tree.", e); 409 } catch (IOException e) { 410 LOGGER.log(Level.SEVERE, "Unexpected Error", e); 411 } 412 413 return doc; 414 } 415 416 423 public static List <String > getTextContents(final NodeList list) { 424 List <String > result = null; 425 if (list != null) { 426 result = new ArrayList <String >(); 427 for (int i = 0; i < list.getLength(); i++) { 428 Node pathElement = list.item(i); 429 if (pathElement.getNodeType() != Node.TEXT_NODE) { 430 result.add(getTextContent(pathElement)); 431 } 432 } 433 } 434 return result; 435 } 436 437 442 public static QName extractXmlAttributeQName(final Node node, 443 final String attrName) { 444 String attr = XMLUtil.getAttributeValue(node, attrName); 445 446 QName qName = null; 447 448 if ((attr.indexOf(':') > -1) && (attr.charAt(0) != '{')) { 450 String ns = attr.substring(0, attr.indexOf(':')); 451 452 String namespace = node.lookupNamespaceURI(ns); 453 457 458 qName = new QName (namespace, attr.substring(attr.indexOf(':') + 1), 459 ns); 460 } else { 461 qName = QName.valueOf(attr); 463 } 464 return qName; 465 } 466 467 473 public static QName extractRequiredXmlAttributeQName(final Node node, 474 final String attrName) throws JBIDescriptorException { 475 String attr = XMLUtil.getRequiredAttributeValue(node, attrName); 476 477 QName qName = null; 478 479 if ((attr.indexOf(':') > -1) && (attr.charAt(0) != '{')) { 481 String ns = attr.substring(0, attr.indexOf(':')); 482 483 String namespace = node.lookupNamespaceURI(ns); 484 488 489 qName = new QName (namespace, attr.substring(attr.indexOf(':') + 1), 490 ns); 491 } else { 492 qName = QName.valueOf(attr); 494 } 495 return qName; 496 } 497 498 507 public static List <Node > getNodeChildren(final Node node) { 508 List <Node > children = new ArrayList <Node >(); 509 if (node != null) { 510 NodeList childrenList = node.getChildNodes(); 511 for (int i = 0; i < childrenList.getLength(); i++) { 512 Node child = childrenList.item(i); 513 if (child.getNodeType() == Node.ELEMENT_NODE) { 514 children.add(child); 515 } 516 } 517 } 518 519 return children; 520 } 521 522 531 public static Node getNode(Document document, String path) { 532 Node result = null; 533 if (document != null && path != null) { 534 try { 535 result = XPathAPI.selectSingleNode(document, path); 536 } catch (TransformerException e) { 537 return null; 538 } 539 } 540 return result; 541 } 542 543 } 544 | Popular Tags |