1 16 17 package org.jboss.axis.utils; 18 19 import org.jboss.logging.Logger; 20 import org.w3c.dom.Document ; 21 import org.w3c.dom.Element ; 22 import org.w3c.dom.Node ; 23 import org.w3c.dom.Text ; 24 25 import javax.xml.parsers.DocumentBuilder ; 26 import javax.xml.parsers.DocumentBuilderFactory ; 27 import javax.xml.parsers.ParserConfigurationException ; 28 import javax.xml.soap.Name ; 29 30 35 public final class DOM2Utils 36 { 37 38 private static Logger log = Logger.getLogger(DOM2Utils.class.getName()); 39 40 private static DocumentBuilder builder = getDocumentBuilder(); 42 43 private DOM2Utils() 45 { 46 } 47 48 51 public static DocumentBuilder getDocumentBuilder() 52 { 53 54 if (builder == null) 55 { 56 try 57 { 58 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 59 factory.setValidating(false); 60 factory.setNamespaceAware(true); 61 builder = factory.newDocumentBuilder(); 62 } 63 catch (ParserConfigurationException e) 64 { 65 log.error(e); 66 } 67 } 68 69 return builder; 70 } 71 72 77 public static Element createElement(Element parent, String localPart) 78 { 79 Document doc = getOwnerDocument(parent); 80 log.debug("createElement {}" + localPart); 81 return doc.createElement(localPart); 82 } 83 84 89 public static Element createElement(Element parent, String localPart, String prefix) 90 { 91 Document doc = getOwnerDocument(parent); 92 log.debug("createElement {}" + prefix + ":" + localPart); 93 return doc.createElement(prefix + ":" + localPart); 94 } 95 96 101 public static Element createElement(Element parent, String uri, String prefix, String localPart) 102 { 103 Document doc = getOwnerDocument(parent); 104 if (prefix == null || prefix.length() == 0) 105 { 106 log.debug("createElement {" + uri + "}" + localPart); 107 return doc.createElementNS(uri, localPart); 108 } 109 else 110 { 111 log.debug("createElement {" + uri + "}" + prefix + ":" + localPart); 112 return doc.createElementNS(uri, prefix + ":" + localPart); 113 } 114 } 115 116 121 public static Element createElement(Element parent, Name name) 122 { 123 return createElement(parent, name.getURI(), name.getPrefix(), name.getLocalName()); 124 } 125 126 129 public static Text createTextNode(String value) 130 { 131 Document doc = builder.newDocument(); 132 return doc.createTextNode(value); 133 } 134 135 private static Document getOwnerDocument(Node parent) 136 { 137 Document doc = null; 138 if (parent != null) 139 doc = parent.getOwnerDocument(); 140 if (doc == null) 141 doc = builder.newDocument(); 142 return doc; 143 } 144 } 145 | Popular Tags |