1 18 package org.apache.tools.ant.taskdefs.optional.junit; 19 20 import java.util.Vector ; 21 import org.w3c.dom.Attr ; 22 import org.w3c.dom.CDATASection ; 23 import org.w3c.dom.Comment ; 24 import org.w3c.dom.DOMException ; 25 import org.w3c.dom.Document ; 26 import org.w3c.dom.Element ; 27 import org.w3c.dom.NamedNodeMap ; 28 import org.w3c.dom.Node ; 29 import org.w3c.dom.NodeList ; 30 import org.w3c.dom.ProcessingInstruction ; 31 import org.w3c.dom.Text ; 32 33 37 public final class DOMUtil { 38 39 40 private DOMUtil() { 41 } 42 43 47 public interface NodeFilter { 48 53 boolean accept(Node node); 54 } 55 56 65 public static NodeList listChildNodes(Node parent, NodeFilter filter, boolean recurse) { 66 NodeListImpl matches = new NodeListImpl(); 67 NodeList children = parent.getChildNodes(); 68 if (children != null) { 69 final int len = children.getLength(); 70 for (int i = 0; i < len; i++) { 71 Node child = children.item(i); 72 if (filter.accept(child)) { 73 matches.addElement(child); 74 } 75 if (recurse) { 76 NodeList recmatches = listChildNodes(child, filter, recurse); 77 final int reclength = recmatches.getLength(); 78 for (int j = 0; j < reclength; j++) { 79 matches.addElement(recmatches.item(i)); 80 } 81 } 82 } 83 } 84 return matches; 85 } 86 87 88 public static class NodeListImpl extends Vector implements NodeList { 89 93 public int getLength() { 94 return size(); 95 } 96 101 public Node item(int i) { 102 try { 103 return (Node ) elementAt(i); 104 } catch (ArrayIndexOutOfBoundsException e) { 105 return null; } 107 } 108 } 109 110 117 public static String getNodeAttribute(Node node, String name) { 118 if (node instanceof Element ) { 119 Element element = (Element ) node; 120 return element.getAttribute(name); 121 } 122 return null; 123 } 124 125 126 135 public static Element getChildByTagName (Node parent, String tagname) { 136 if (parent == null) { 137 return null; 138 } 139 NodeList childList = parent.getChildNodes(); 140 final int len = childList.getLength(); 141 for (int i = 0; i < len; i++) { 142 Node child = childList.item(i); 143 if (child != null && child.getNodeType() == Node.ELEMENT_NODE 144 && child.getNodeName().equals(tagname)) { 145 return (Element ) child; 146 } 147 } 148 return null; 149 } 150 151 163 public static Node importNode(Node parent, Node child) { 164 Node copy = null; 165 final Document doc = parent.getOwnerDocument(); 166 167 switch (child.getNodeType()) { 168 case Node.CDATA_SECTION_NODE: 169 copy = doc.createCDATASection(((CDATASection ) child).getData()); 170 break; 171 case Node.COMMENT_NODE: 172 copy = doc.createComment(((Comment ) child).getData()); 173 break; 174 case Node.DOCUMENT_FRAGMENT_NODE: 175 copy = doc.createDocumentFragment(); 176 break; 177 case Node.ELEMENT_NODE: 178 final Element elem = doc.createElement(((Element ) child).getTagName()); 179 copy = elem; 180 final NamedNodeMap attributes = child.getAttributes(); 181 if (attributes != null) { 182 final int size = attributes.getLength(); 183 for (int i = 0; i < size; i++) { 184 final Attr attr = (Attr ) attributes.item(i); 185 elem.setAttribute(attr.getName(), attr.getValue()); 186 } 187 } 188 break; 189 case Node.ENTITY_REFERENCE_NODE: 190 copy = doc.createEntityReference(child.getNodeName()); 191 break; 192 case Node.PROCESSING_INSTRUCTION_NODE: 193 final ProcessingInstruction pi = (ProcessingInstruction ) child; 194 copy = doc.createProcessingInstruction(pi.getTarget(), pi.getData()); 195 break; 196 case Node.TEXT_NODE: 197 copy = doc.createTextNode(((Text ) child).getData()); 198 break; 199 default: 200 throw new IllegalStateException ("Invalid node type: " + child.getNodeType()); 202 } 203 204 try { 207 final NodeList children = child.getChildNodes(); 208 if (children != null) { 209 final int size = children.getLength(); 210 for (int i = 0; i < size; i++) { 211 final Node newChild = children.item(i); 212 if (newChild != null) { 213 importNode(copy, newChild); 214 } 215 } 216 } 217 } catch (DOMException ignored) { 218 } 220 221 parent.appendChild(copy); 223 return copy; 224 } 225 } 226 | Popular Tags |