1 19 20 package org.netbeans.modules.projectimport.jbuilder.parsing; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import org.w3c.dom.Element ; 25 import org.w3c.dom.Node ; 26 import org.w3c.dom.NodeList ; 27 import org.w3c.dom.Text ; 28 29 32 class Util { 33 34 private Util() {} 35 36 37 41 51 public static Element findElement(Element parent, String name, String namespace) { 52 Element result = null; 53 NodeList l = parent.getChildNodes(); 54 for (int i = 0; i < l.getLength(); i++) { 55 if (l.item(i).getNodeType() == Node.ELEMENT_NODE) { 56 Element el = (Element )l.item(i); 57 if ((namespace == null && name.equals(el.getTagName())) || 58 (namespace != null && name.equals(el.getLocalName()) && 59 namespace.equals(el.getNamespaceURI()))) { 60 if (result == null) { 61 result = el; 62 } else { 63 return null; 64 } 65 } 66 } 67 } 68 return result; 69 } 70 71 77 public static String findText(Element parent) { 78 NodeList l = parent.getChildNodes(); 79 for (int i = 0; i < l.getLength(); i++) { 80 if (l.item(i).getNodeType() == Node.TEXT_NODE) { 81 Text text = (Text )l.item(i); 82 return text.getNodeValue(); 83 } 84 } 85 return null; 86 } 87 88 98 public static List findSubElements(Element parent) throws IllegalArgumentException { 99 NodeList l = parent.getChildNodes(); 100 List elements = new ArrayList (l.getLength()); 101 for (int i = 0; i < l.getLength(); i++) { 102 Node n = l.item(i); 103 if (n.getNodeType() == Node.ELEMENT_NODE) { 104 elements.add((Element )n); 105 } else if (n.getNodeType() == Node.TEXT_NODE) { 106 String text = ((Text )n).getNodeValue(); 107 if (text.trim().length() > 0) { 108 throw new IllegalArgumentException ("non-ws text encountered in " + parent + ": " + text); } 110 } else if (n.getNodeType() == Node.COMMENT_NODE) { 111 } else { 113 throw new IllegalArgumentException ("unexpected non-element child of " + parent + ": " + n); } 115 } 116 return elements; 117 } 118 119 } 120 | Popular Tags |