1 7 8 package org.jboss.webservice.util; 10 11 13 import org.jboss.logging.Logger; 14 import org.w3c.dom.Element ; 15 import org.w3c.dom.Node ; 16 import org.w3c.dom.NodeList ; 17 18 import javax.xml.namespace.QName ; 19 20 26 public final class DOMUtils 27 { 28 private static final Logger log = Logger.getLogger(DOMUtils.class); 30 31 private DOMUtils() 33 { 34 } 35 36 41 public static String getAttributeValue(Element el, String attrName) 42 { 43 String attr = el.getAttribute(attrName); 44 45 if ("".equals(attr)) 46 attr = null; 47 48 return attr; 49 } 50 51 54 public static QName getAttributeValueAsQName(Element el, String attrName) 55 { 56 QName qname = null; 57 58 String attr = getAttributeValue(el, attrName); 59 if (attr != null) 60 { 61 String prefix = ""; 62 String localPart = attr; 63 String namespaceURI = ""; 64 65 int colonIndex = attr.indexOf(":"); 66 if (colonIndex > 0) 67 { 68 prefix = attr.substring(0, colonIndex); 69 localPart = attr.substring(colonIndex + 1); 70 71 Element nsElement = el; 72 while (namespaceURI.equals("") && nsElement != null) 73 { 74 namespaceURI = nsElement.getAttribute("xmlns:" + prefix); 75 if (namespaceURI.equals("")) 76 nsElement = getParentElement(nsElement); 77 } 78 79 if (namespaceURI.equals("")) 80 throw new IllegalArgumentException ("Cannot find namespace uri for: " + attr); 81 } 82 83 qname = new QName (namespaceURI, localPart, prefix); 84 } 85 86 return qname; 87 } 88 89 92 public static boolean getAttributeValueAsBoolean(Element el, String attrName) 93 { 94 String attrVal = getAttributeValue(el, attrName); 95 boolean ret = "true".equalsIgnoreCase(attrVal) || "1".equalsIgnoreCase(attrVal); 96 return ret; 97 } 98 99 102 public static Integer getAttributeValueAsInteger(Element el, String attrName) 103 { 104 Integer retVal = null; 105 String attrVal = getAttributeValue(el, attrName); 106 if (attrVal != null) 107 { 108 retVal = new Integer (attrVal); 109 } 110 return retVal; 111 } 112 113 116 public static Element getFirstChildElement(Element el) 117 { 118 return getFirstChildElement(el, null); 119 } 120 121 124 public static Element getFirstChildElement(Element el, String tagName) 125 { 126 Element childElement = null; 127 NodeList nlist = el.getChildNodes(); 128 for (int i = 0; childElement == null && i < nlist.getLength(); i++) 129 { 130 Node node = nlist.item(i); 131 if (node.getNodeType() == Node.ELEMENT_NODE) 132 { 133 if (tagName == null || tagName.equals(node.getLocalName())) 134 childElement = (Element )node; 135 } 136 } 137 return childElement; 138 } 139 140 143 public static Element getParentElement(Element el) 144 { 145 Node parent = el.getParentNode(); 146 return (parent instanceof Element ? (Element )parent : null); 147 } 148 } | Popular Tags |