1 19 20 package org.netbeans.modules.project.ant; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import org.openide.ErrorManager; 25 import org.w3c.dom.Element ; 26 import org.w3c.dom.Node ; 27 import org.w3c.dom.NodeList ; 28 import org.w3c.dom.Text ; 29 import org.xml.sax.ErrorHandler ; 30 import org.xml.sax.SAXException ; 31 import org.xml.sax.SAXParseException ; 32 33 37 public class Util { 38 39 private Util() {} 40 41 51 public static Element findElement(Element parent, String name, String namespace) { 52 Element result = null; 53 NodeList l = parent.getChildNodes(); 54 int len = l.getLength(); 55 for (int i = 0; i < len; i++) { 56 if (l.item(i).getNodeType() == Node.ELEMENT_NODE) { 57 Element el = (Element )l.item(i); 58 if (name.equals(el.getLocalName()) && namespace.equals(el.getNamespaceURI())) { 59 if (result == null) { 60 result = el; 61 } else { 62 return null; 63 } 64 } 65 } 66 } 67 return result; 68 } 69 70 76 public static String findText(Element parent) { 77 NodeList l = parent.getChildNodes(); 78 for (int i = 0; i < l.getLength(); i++) { 79 if (l.item(i).getNodeType() == Node.TEXT_NODE) { 80 Text text = (Text )l.item(i); 81 return text.getNodeValue(); 82 } 83 } 84 return null; 85 } 86 87 97 public static List <Element > findSubElements(Element parent) throws IllegalArgumentException { 98 NodeList l = parent.getChildNodes(); 99 List <Element > elements = new ArrayList <Element >(l.getLength()); 100 for (int i = 0; i < l.getLength(); i++) { 101 Node n = l.item(i); 102 if (n.getNodeType() == Node.ELEMENT_NODE) { 103 elements.add((Element )n); 104 } else if (n.getNodeType() == Node.TEXT_NODE) { 105 String text = ((Text )n).getNodeValue(); 106 if (text.trim().length() > 0) { 107 throw new IllegalArgumentException ("non-ws text encountered in " + parent + ": " + text); } 109 } else if (n.getNodeType() == Node.COMMENT_NODE) { 110 } else { 112 throw new IllegalArgumentException ("unexpected non-element child of " + parent + ": " + n); } 114 } 115 return elements; 116 } 117 118 122 public static ErrorHandler defaultErrorHandler() { 123 return new ErrHandler(); 124 } 125 126 private static final class ErrHandler implements ErrorHandler { 127 128 public ErrHandler() {} 129 130 private void annotate(SAXParseException exception) throws SAXException { 131 ErrorManager.getDefault().annotate(exception, ErrorManager.UNKNOWN, 132 "Occurred at: " + exception.getSystemId() + ":" + exception.getLineNumber(), null, null, null); 134 } 135 136 public void fatalError(SAXParseException exception) throws SAXException { 137 annotate(exception); 138 throw exception; 139 } 140 141 public void error(SAXParseException exception) throws SAXException { 142 annotate(exception); 143 throw exception; 144 } 145 146 public void warning(SAXParseException exception) throws SAXException { 147 annotate(exception); 148 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exception); 149 } 150 151 } 152 153 } 154 | Popular Tags |