1 17 package org.apache.servicemix.jbi.util; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.w3c.dom.Attr ; 22 import org.w3c.dom.Document ; 23 import org.w3c.dom.Element ; 24 import org.w3c.dom.NamedNodeMap ; 25 import org.w3c.dom.Node ; 26 import org.w3c.dom.NodeList ; 27 28 import javax.xml.namespace.QName ; 29 import javax.xml.transform.OutputKeys ; 30 import javax.xml.transform.Transformer ; 31 import javax.xml.transform.TransformerException ; 32 import javax.xml.transform.TransformerFactory ; 33 import javax.xml.transform.dom.DOMSource ; 34 import javax.xml.transform.stream.StreamResult ; 35 36 import java.io.StringWriter ; 37 38 43 public class DOMUtil { 44 private static final Log log = LogFactory.getLog(DOMUtil.class); 45 46 49 public static String getElementText(Element element) { 50 StringBuffer buffer = new StringBuffer (); 51 NodeList nodeList = element.getChildNodes(); 52 for (int i = 0, size = nodeList.getLength(); i < size; i++) { 53 Node node = nodeList.item(i); 54 if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) { 55 buffer.append(node.getNodeValue()); 56 } 57 } 58 return buffer.toString(); 59 } 60 61 64 public static void moveContent(Element from, Element to) { 65 NodeList childNodes = from.getChildNodes(); 67 while (childNodes.getLength() > 0) { 68 Node node = childNodes.item(0); 69 from.removeChild(node); 70 to.appendChild(node); 71 } 72 } 73 74 77 public static void copyAttributes(Element from, Element to) { 78 NamedNodeMap attributes = from.getAttributes(); 80 for (int i = 0, size = attributes.getLength(); i < size; i++) { 81 Attr node = (Attr ) attributes.item(i); 82 to.setAttributeNS(node.getNamespaceURI(), node.getName(), node.getValue()); 83 } 84 } 85 86 89 public static String asXML(Node node) throws TransformerException { 90 Transformer transformer = TransformerFactory.newInstance().newTransformer(); 91 StringWriter buffer = new StringWriter (); 92 transformer.transform(new DOMSource (node), new StreamResult (buffer)); 93 return buffer.toString(); 94 } 95 96 99 public static String asIndentedXML(Node node) throws TransformerException { 100 Transformer transformer = TransformerFactory.newInstance().newTransformer(); 101 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 102 StringWriter buffer = new StringWriter (); 103 transformer.transform(new DOMSource (node), new StreamResult (buffer)); 104 return buffer.toString(); 105 } 106 107 110 public static void addChildElement(Element element, String name, Object textValue) { 111 Document document = element.getOwnerDocument(); 112 Element child = document.createElement(name); 113 element.appendChild(child); 114 if (textValue != null) { 115 String text = textValue.toString(); 116 child.appendChild(document.createTextNode(text)); 117 } 118 } 119 120 127 public static QName createQName(Element element, String qualifiedName) { 128 int index = qualifiedName.indexOf(':'); 129 if (index >= 0) { 130 String prefix = qualifiedName.substring(0, index); 131 String localName = qualifiedName.substring(index + 1); 132 String uri = recursiveGetAttributeValue(element, "xmlns:" + prefix); 133 return new QName (uri, localName, prefix); 134 } 135 else { 136 String uri = recursiveGetAttributeValue(element, "xmlns"); 137 if (uri != null) { 138 return new QName (uri, qualifiedName); 139 } 140 return new QName (qualifiedName); 141 } 142 } 143 144 147 public static String recursiveGetAttributeValue(Element element, String attributeName) { 148 String answer = null; 149 try { 150 answer = element.getAttribute(attributeName); 151 } 152 catch (Exception e) { 153 if (log.isTraceEnabled()) { 154 log.trace("Caught exception looking up attribute: " + attributeName + " on element: " + element + ". Cause: " + e, e); 155 } 156 } 157 if (answer == null || answer.length() == 0) { 158 Node parentNode = element.getParentNode(); 159 if (parentNode instanceof Element ) { 160 return recursiveGetAttributeValue((Element ) parentNode, attributeName); 161 } 162 } 163 return answer; 164 } 165 166 171 public static Element getFirstChildElement(Node parent) { 172 NodeList childs = parent.getChildNodes(); 173 for (int i = 0; i < childs.getLength(); i++) { 174 Node child = childs.item(i); 175 if (child instanceof Element ) { 176 return (Element ) child; 177 } 178 } 179 return null; 180 } 181 182 187 public static Element getNextSiblingElement(Element el) { 188 for (Node n = el.getNextSibling(); n != null; n = n.getNextSibling()) { 189 if (n instanceof Element ) { 190 return (Element ) n; 191 } 192 } 193 return null; 194 } 195 196 201 public static QName getQName(Element el) { 202 if (el == null) { 203 return null; 204 } else if (el.getPrefix() != null) { 205 return new QName (el.getNamespaceURI(), el.getLocalName(), el.getPrefix()); 206 } else { 207 return new QName (el.getNamespaceURI(), el.getLocalName()); 208 } 209 } 210 211 } 212 | Popular Tags |