1 57 58 package org.apache.soap.util.xml; 59 60 import org.w3c.dom.*; 61 62 66 public class DOMUtils { 67 70 private static String NS_URI_XMLNS = "http://www.w3.org/2000/xmlns/"; 71 72 81 static public String getAttribute (Element el, String attrName) { 82 String sRet = null; 83 Attr attr = el.getAttributeNode(attrName); 84 85 if (attr != null) { 86 sRet = attr.getValue(); 87 } 88 return sRet; 89 } 90 91 101 static public String getAttributeNS (Element el, 102 String namespaceURI, 103 String localPart) { 104 String sRet = null; 105 Attr attr = el.getAttributeNodeNS (namespaceURI, localPart); 106 107 if (attr != null) { 108 sRet = attr.getValue (); 109 } 110 111 return sRet; 112 } 113 114 122 static public String getChildCharacterData (Element parentEl) { 123 if (parentEl == null) { 124 return null; 125 } 126 Node tempNode = parentEl.getFirstChild(); 127 StringBuffer strBuf = new StringBuffer (); 128 CharacterData charData; 129 130 while (tempNode != null) { 131 switch (tempNode.getNodeType()) { 132 case Node.TEXT_NODE : 133 case Node.CDATA_SECTION_NODE : charData = (CharacterData)tempNode; 134 strBuf.append(charData.getData()); 135 break; 136 } 137 tempNode = tempNode.getNextSibling(); 138 } 139 return strBuf.toString(); 140 } 141 142 149 public static Element getFirstChildElement (Element elem) { 150 for (Node n = elem.getFirstChild (); n != null; n = n.getNextSibling ()) { 151 if (n.getNodeType () == Node.ELEMENT_NODE) { 152 return (Element) n; 153 } 154 } 155 return null; 156 } 157 158 165 public static Element getNextSiblingElement (Element elem) { 166 for (Node n = elem.getNextSibling (); n != null; n = n.getNextSibling ()) { 167 if (n.getNodeType () == Node.ELEMENT_NODE) { 168 return (Element) n; 169 } 170 } 171 return null; 172 } 173 174 184 public static Element findChildElementWithAttribute (Element elem, 185 String attrName, 186 String attrValue) { 187 for (Node n = elem.getFirstChild (); n != null; n = n.getNextSibling ()) { 188 if (n.getNodeType () == Node.ELEMENT_NODE) { 189 if (attrValue.equals (DOMUtils.getAttribute ((Element) n, attrName))) { 190 return (Element) n; 191 } 192 } 193 } 194 return null; 195 } 196 197 204 public static int countKids (Element elem, short nodeType) { 205 int nkids = 0; 206 for (Node n = elem.getFirstChild (); n != null; n = n.getNextSibling ()) { 207 if (n.getNodeType () == nodeType) { 208 nkids++; 209 } 210 } 211 return nkids; 212 } 213 214 226 public static String getNamespaceURIFromPrefix (Node context, 227 String prefix) { 228 short nodeType = context.getNodeType (); 229 Node tempNode = null; 230 231 switch (nodeType) 232 { 233 case Node.ATTRIBUTE_NODE : 234 { 235 tempNode = ((Attr) context).getOwnerElement (); 236 break; 237 } 238 case Node.ELEMENT_NODE : 239 { 240 tempNode = context; 241 break; 242 } 243 default : 244 { 245 tempNode = context.getParentNode (); 246 break; 247 } 248 } 249 250 while (tempNode != null && tempNode.getNodeType () == Node.ELEMENT_NODE) 251 { 252 Element tempEl = (Element) tempNode; 253 String namespaceURI = (prefix == null) 254 ? getAttribute (tempEl, "xmlns") 255 : getAttributeNS (tempEl, NS_URI_XMLNS, prefix); 256 257 if (namespaceURI != null) 258 { 259 return namespaceURI; 260 } 261 else 262 { 263 tempNode = tempEl.getParentNode (); 264 } 265 } 266 267 return null; 268 } 269 270 public static Element getElementByID(Element el, String id) 271 { 272 if (el == null) 273 return null; 274 String thisId = el.getAttribute("id"); 275 if (id.equals(thisId)) 276 return el; 277 278 NodeList list = el.getChildNodes(); 279 for (int i = 0; i < list.getLength(); i++) { 280 Node node = list.item(i); 281 if (node instanceof Element) { 282 Element ret = getElementByID((Element)node, id); 283 if (ret != null) 284 return ret; 285 } 286 } 287 288 return null; 289 } 290 291 public static QName getQualifiedAttributeValue(Element el, 292 String attrName) 293 throws IllegalArgumentException 294 { 295 String attrValue = DOMUtils.getAttribute(el, attrName); 296 297 if (attrValue != null) 298 { 299 int index = attrValue.indexOf(':'); 300 String attrValuePrefix = (index != -1) 301 ? attrValue.substring(0, index) 302 : null; 303 String attrValueLocalPart = attrValue.substring(index + 1); 304 String attrValueNamespaceURI = 305 DOMUtils.getNamespaceURIFromPrefix(el, attrValuePrefix); 306 307 if (attrValueNamespaceURI != null) 308 { 309 return new QName(attrValueNamespaceURI, attrValueLocalPart); 310 } 311 else 312 { 313 throw new IllegalArgumentException ("Unable to determine " + 314 "namespace of '" + 315 (attrValuePrefix != null 316 ? attrValuePrefix + ":" 317 : "") + attrValueLocalPart + 318 "'."); 319 } 320 } 321 else 322 { 323 return null; 324 } 325 } 326 } 327 | Popular Tags |