1 54 55 package com.mullassery.act.util; 56 57 import java.io.BufferedWriter ; 58 import java.io.File ; 59 import java.io.FileWriter ; 60 import java.io.InputStream ; 61 import java.util.HashMap ; 62 import java.util.Iterator ; 63 import java.util.StringTokenizer ; 64 import java.util.Vector ; 65 66 import javax.xml.parsers.DocumentBuilder ; 67 import javax.xml.parsers.DocumentBuilderFactory ; 68 69 import org.apache.tools.ant.util.DOMElementWriter; 70 import org.w3c.dom.Document ; 71 import org.w3c.dom.Element ; 72 import org.w3c.dom.NamedNodeMap ; 73 import org.w3c.dom.Node ; 74 import org.w3c.dom.NodeList ; 75 76 import com.mullassery.act.ACTException; 77 import com.mullassery.act.TaskMaster; 78 79 84 public class XMLUtil { 85 86 87 private XMLUtil() { 88 } 89 90 public static void cleanText(Node node) { 91 try { 92 NodeList childNodes = node.getChildNodes(); 93 int noChildren = childNodes.getLength(); 94 Node n = null; 95 short type = 0; 96 Vector rem = new Vector (); 97 for (int i = 0; i < noChildren; i++) { 98 n = childNodes.item(i); 99 type = n.getNodeType(); 100 if (type == Node.TEXT_NODE) { 101 rem.add(n); 102 } else if (type == Node.ELEMENT_NODE) { 103 cleanText(n); 104 } 105 } 106 for (int i = 0; i < rem.size(); i++) { 107 node.removeChild((Node ) rem.get(i)); 108 } 109 } catch (Exception e) { 110 } 112 } 113 114 public static boolean compareElements(Element e1, Element e2) { 115 if (e1.equals(e2)) 116 return true; 117 Document doc = e1.getOwnerDocument(); 118 e2 = (Element ) doc.importNode(e2, true); 119 return e1.equals(e2); 120 } 121 122 public static Element findElementNode( 123 Node root, 124 String elementName, 125 boolean deep) { 126 return (Element ) findNode(root, elementName, deep, true); 127 128 } 129 130 public static Node findNode(Node root, String elementName, boolean deep) { 131 return findNode(root, elementName, deep, false); 132 } 133 134 public static Node findNode( 135 Node root, 136 String elementName, 137 boolean deep, 138 boolean elementsOnly) { 139 if (!(root.hasChildNodes())) 141 return null; 142 143 Node matchingNode = null; 145 String nodeName = null; 146 Node child = null; 147 148 NodeList childNodes = root.getChildNodes(); 149 int noChildren = childNodes.getLength(); 150 for (int i = 0; i < noChildren; i++) { 151 if (matchingNode == null) { 152 child = childNodes.item(i); 153 nodeName = child.getNodeName(); 154 if ((nodeName != null) & (nodeName.equals(elementName))) 155 return child; 156 if (deep) 157 matchingNode = 158 findNode(child, elementName, deep, elementsOnly); 159 } else 160 break; 161 } 162 163 if (!elementsOnly) { 164 NamedNodeMap childAttrs = root.getAttributes(); 165 noChildren = childAttrs.getLength(); 166 for (int i = 0; i < noChildren; i++) { 167 if (matchingNode == null) { 168 child = childAttrs.item(i); 169 nodeName = child.getNodeName(); 170 if ((nodeName != null) & (nodeName.equals(elementName))) 171 return child; 172 } else 173 break; 174 } 175 } 176 return matchingNode; 177 } 178 179 public static Element getChildElement(Element parent, String childName) 180 throws ACTException { 181 Node n = XMLUtil.findElementNode(parent, childName, false); 182 if (n == null || n.getNodeType() != Node.ELEMENT_NODE) { 183 return (Element ) parent.appendChild( 184 parent.getOwnerDocument().createElement(childName)); 185 } else { 186 return (Element ) n; 187 } 188 } 189 190 196 public static DocumentBuilder getDocumentBuilder() throws ACTException { 197 try { 198 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 199 dbf.setIgnoringComments(true); 200 dbf.setCoalescing(true); 201 dbf.setIgnoringElementContentWhitespace(true); 202 dbf.setValidating(false); 203 return dbf.newDocumentBuilder(); 204 } catch (Exception exc) { 205 throw new ACTException(exc.getMessage()); 206 } 207 } 208 209 public static Element getDocumentElement(File fileName) 210 throws ACTException { 211 try { 212 Document doc = getDocumentBuilder().parse(fileName); 213 return doc.getDocumentElement(); 214 } catch (Exception se) { 215 return null; 216 } 217 } 218 219 226 public static boolean isSame(Node n1, Node n2, boolean deep) { 227 if (n1 == null && n2 != null) 228 return false; 229 if (n1 != null && n2 == null) 230 return false; 231 if (!(n1.getNodeName().equals(n2.getNodeName()))) 232 return false; 233 234 short s1 = n1.getNodeType(); 235 short s2 = n2.getNodeType(); 236 if (s1 != s2) 237 return false; 238 if (!(s1 == Node.ATTRIBUTE_NODE || s1 == Node.ELEMENT_NODE)) 240 return true; 241 if (s1 == Node.ATTRIBUTE_NODE) { 242 String v1 = n1.getNodeValue(); 243 String v2 = n2.getNodeValue(); 244 if (v1 != null && v2 != null && !v1.equals(v2)) 245 return false; 246 else 247 return true; 248 } 249 250 Node t1, t2 = null; 251 NamedNodeMap nm1 = n1.getAttributes(); 253 NamedNodeMap nm2 = n2.getAttributes(); 254 if (nm1 != null && nm2 != null && nm1.getLength() != nm2.getLength()) 255 return false; 256 for (int i = 0; i < nm1.getLength(); i++) { 257 t1 = nm1.item(i); 258 t2 = findNode(n2, t1.getNodeName(), false); 259 if (!isSame(t1, t2, false)) 260 return false; 261 } 262 if (!deep) 263 return true; 264 265 NodeList nl1 = n1.getChildNodes(); 267 NodeList nl2 = n2.getChildNodes(); 268 if (nl1.getLength() != nl2.getLength()) 269 return false; 270 for (int i = 0; i < nl1.getLength(); i++) { 271 t1 = nl1.item(i); 272 t2 = findNode(n2, t1.getNodeName(), false); 273 if (!isSame(t1, t2, true)) 274 return false; 275 } 276 return true; 277 } 278 279 public static Element loadDocument(File f, String defElement) 280 throws ACTException { 281 Document doc = null; 282 try { 283 doc = getDocumentBuilder().parse(f); 284 return doc.getDocumentElement(); 285 } catch (Exception se) { 286 if (defElement != null) 287 return getDocumentBuilder().newDocument().createElement( 288 defElement); 289 throw new ACTException(se.getMessage()); 290 } 291 } 292 293 public static Element loadDocument(InputStream in, String defElement) 294 throws ACTException { 295 Document doc = null; 296 try { 297 doc = getDocumentBuilder().parse(in); 298 return doc.getDocumentElement(); 299 } catch (Exception se) { 300 if (defElement != null) 301 return getDocumentBuilder().newDocument().createElement( 302 defElement); 303 throw new ACTException(se.getMessage()); 304 } 305 } 306 307 335 private static String match(Iterator en, String find) throws ACTException { 336 String obj, match = null; 337 while (en.hasNext()) { 338 obj = en.next().toString(); 339 if (obj.startsWith(find)) { 340 if (obj.equals(find)) { 341 match = obj; 342 break; } 344 if (match != null) 345 throw new ACTException( 346 "Parameter \"" 347 + find 348 + "\" is ambigious since it matches with \"" 349 + match 350 + "\" and \"" 351 + obj 352 + "\"!!"); 353 match = obj; 354 } 355 } 356 if (match == null) 357 throw new ACTException( 358 "Parameter " + find + " does not match any element!"); 359 return match; 360 } 361 362 public static void printTree(Node doc) { 363 if (doc == null) { 364 DebugUtil.debug("Nothing to print!!"); 365 return; 366 } 367 try { 368 DebugUtil.debug(doc.getNodeName() + " " + doc.getNodeValue()); 369 NamedNodeMap cl = doc.getAttributes(); 370 for (int x = 0; x < cl.getLength(); x++) { 371 Node node = cl.item(x); 372 DebugUtil.debug( 373 "\t" + node.getNodeName() + " ->" + node.getNodeValue()); 374 } 375 NodeList nl = doc.getChildNodes(); 376 for (int x = 0; x < nl.getLength(); x++) { 377 Node node = nl.item(x); 378 printTree(node); 379 } 380 } catch (Throwable e) { 381 DebugUtil.debug("Cannot print!! " + e.getMessage()); 382 } 383 } 384 385 public static void saveElement(Element doc, File sFile, boolean append) 386 throws ACTException { 387 if (doc == null) 388 return; 389 BufferedWriter bw = null; 390 try { 391 if (!sFile.exists()) 392 sFile.createNewFile(); 393 bw = 394 new BufferedWriter ( 395 new FileWriter (sFile.getAbsolutePath(), append)); 396 cleanText(doc); 397 doc.normalize(); 398 new DOMElementWriter().write(doc, bw, 0, "\t"); 399 } catch (Exception ex) { 400 throw new ACTException("Error writing to file. " + ex.getMessage()); 401 } finally { 402 try { 403 bw.flush(); 404 bw.close(); 405 } catch (Exception e) { 406 } 407 } 408 } 409 410 public static void setProperty( 411 HashMap cm, 412 Element ep, 413 String str, 414 String value) 415 throws ACTException { 416 417 420 StringTokenizer st = new StringTokenizer (str, "."); 421 String match; 422 do { 423 if (st.countTokens() == 1) { match = match(cm.keySet().iterator(), st.nextToken()); 425 } else { 426 cm = (HashMap ) cm.get(TaskMaster.CHILDREN); 427 match = match(cm.keySet().iterator(), st.nextToken()); 428 ep = XMLUtil.getChildElement(ep, match); 429 cm = TaskMaster.getElementInfo((Class ) cm.get(match)); 430 } 431 } while (st.hasMoreTokens()); 432 ep.setAttribute(match, value); 433 } 434 435 } 436 | Popular Tags |