1 13 package com.tonbeller.wcf.utils; 14 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Random ; 19 20 import org.w3c.dom.Document ; 21 import org.w3c.dom.Element ; 22 import org.w3c.dom.Node ; 23 import org.w3c.dom.NodeList ; 24 import org.w3c.dom.Text ; 25 26 public class DomUtils { 27 28 32 public static List getChildElemsByTagName(Element parent, String tagName) { 33 ArrayList retVec = new ArrayList (); 34 35 NodeList children = parent.getChildNodes(); 36 for (int i = 0; i < children.getLength(); ++i) { 37 if (children.item(i).getNodeType() == Node.ELEMENT_NODE) { 38 Element child = (Element ) children.item(i); 39 if (child.getTagName().equals(tagName)) 40 retVec.add(child); 41 } 42 } 43 return retVec; 44 } 45 46 49 public static List getChildElements(Node parent) { 50 ArrayList retVec = new ArrayList (); 51 52 NodeList children = parent.getChildNodes(); 53 for (int i = 0; i < children.getLength(); ++i) { 54 if (children.item(i).getNodeType() == Node.ELEMENT_NODE) { 55 retVec.add(children.item(i)); 56 } 57 } 58 return retVec; 59 } 60 61 65 public static List getChildNodesExceptAttributes(Node parent) { 66 ArrayList retVec = new ArrayList (); 67 NodeList children = parent.getChildNodes(); 68 for (int i = 0; i < children.getLength(); ++i) { 69 if (children.item(i).getNodeType() == Node.TEXT_NODE 70 || children.item(i).getNodeType() == Node.ELEMENT_NODE) { 71 retVec.add(children.item(i)); 72 } 73 } 74 return retVec; 75 } 76 77 80 public static void removeChildElements(Element parent) { 81 List v = getChildElements(parent); 82 Iterator en = v.iterator(); 83 while (en.hasNext()) 84 parent.removeChild((Node ) en.next()); 85 } 86 87 90 public static void removeChildNodesExceptAttributes(Element parent) { 91 List v = getChildNodesExceptAttributes(parent); 92 Iterator en = v.iterator(); 93 while (en.hasNext()) 94 parent.removeChild((Node ) en.next()); 95 } 96 97 98 public static Text setText(Element parent, String text) { 99 removeChildNodesExceptAttributes(parent); 100 Document doc = parent.getOwnerDocument(); 101 Text child = doc.createTextNode(text); 102 parent.appendChild(child); 103 return child; 104 } 105 106 109 public static Element getChildElemByTagName(Element parent, String tagName) { 110 NodeList children = parent.getChildNodes(); 111 for (int i = 0; i < children.getLength(); ++i) { 112 if (children.item(i).getNodeType() == Node.ELEMENT_NODE) { 113 Element child = (Element ) children.item(i); 114 if (child.getTagName().equals(tagName)) 115 return child; 116 } 117 } 118 return null; 119 } 120 121 124 public static Element getChildElemById(Element parent, String id) { 125 NodeList children = parent.getChildNodes(); 126 for (int i = 0; i < children.getLength(); ++i) { 127 if (children.item(i).getNodeType() == Node.ELEMENT_NODE) { 128 Element child = (Element ) children.item(i); 129 if (XoplonNS.getAttribute(child, "id").equals(id)) 130 return child; 131 } 132 } 133 return null; 134 } 135 136 140 public static Document getDocument(Node node) { 141 if (node.getNodeType() == Node.DOCUMENT_NODE) 142 return (Document ) node; 143 return node.getOwnerDocument(); 144 } 145 146 147 public static Element appendElement(Element parent, String type) { 148 Document doc = parent.getOwnerDocument(); 149 Element elem = doc.createElement(type); 150 parent.appendChild(elem); 151 return elem; 152 } 153 154 public static Text appendNbsp(Element parent) { 155 return appendText(parent, "\u00a0"); 156 } 157 158 public static Text appendText(Element parent, String text) { 159 Document doc = parent.getOwnerDocument(); 160 Text x = doc.createTextNode(text); 161 parent.appendChild(x); 162 return x; 163 } 164 165 static final Random random = new Random (); 166 167 public static synchronized String randomId() { 168 return "wcf" + Integer.toHexString(random.nextInt()); 169 } 170 171 public static synchronized void setRandomSeed(long seed) { 172 random.setSeed(seed); 173 } 174 175 181 public static void generateIds(Node root, String attrName) { 182 if (root.getNodeType() == Node.ELEMENT_NODE) { 183 Element e = (Element ) root; 184 String id = e.getAttribute(attrName); 185 if (id == null || id.length() == 0) 186 e.setAttribute(attrName, randomId()); 187 } 188 NodeList list = root.getChildNodes(); 189 int N = list.getLength(); 190 for (int i = 0; i < N; i++) 191 generateIds(list.item(i), attrName); 192 } 193 194 public static void generateIds(Node root) { 195 generateIds(root, "id"); 196 } 197 198 205 public static Element findElementWithId(String id, Element root) { 206 if (id.equals(root.getAttribute("id"))) 207 return root; 208 NodeList list = root.getChildNodes(); 209 int len = list.getLength(); 210 for (int i = 0; i < len; i++) { 211 Node n = list.item(i); 212 if (n.getNodeType() != Node.ELEMENT_NODE) 213 continue; 214 Element child = (Element ) list.item(i); 215 Element found = findElementWithId(id, child); 216 if (found != null) 217 return found; 218 } 219 return null; 220 } 221 222 227 public static void removeAttribute(Element elem, String name) { 228 try { 229 elem.removeAttribute(name); 230 } catch (Exception e) { 231 } 233 } 234 } 235 | Popular Tags |