1 23 24 package org.enhydra.xml.lazydom; 25 26 import java.io.File ; 27 import java.io.FileWriter ; 28 import java.io.PrintWriter ; 29 import java.lang.reflect.Method ; 30 31 import org.enhydra.xml.dom.DOMAccess; 32 import org.enhydra.xml.dom.DOMInfo; 33 import org.enhydra.xml.dom.DOMStats; 34 import org.enhydra.xml.driver.TestCaseBase; 35 import org.enhydra.xml.driver.TestException; 36 import org.enhydra.xml.driver.TestFileOps; 37 import org.w3c.dom.Document ; 38 import org.w3c.dom.Element ; 39 import org.w3c.dom.Node ; 40 41 44 public class LazyDOMTestCaseBase extends TestCaseBase { 45 46 protected LazyDOMTestCaseBase(Method method) { 47 super(method); 48 } 49 50 53 protected void printDiffDom(String msg, 54 Node node, 55 String ext) { 56 File outFile = getResultFile(ext); 57 File expectFile = getExpectedFile(ext); 58 TestFileOps.ensureFileDir(outFile); 59 60 try { 61 PrintWriter out = new PrintWriter (new FileWriter (outFile)); 62 try { 63 DOMInfo.printTree(msg, node, 64 DOMInfo.PRINT_DEFAULT|DOMInfo.PRINT_ATTR_DETAILS, 65 out); 66 DOMStats.printStats(msg, node, 0, out); 67 } finally { 68 out.close(); 69 } 70 } catch (Exception except) { 71 throw new TestException(except); 72 } 73 getDiffer().diff(expectFile, outFile); 74 } 75 76 79 private Element accessElementByTagName(DOMAccess accessor, 80 Node root, 81 String tagName) { 82 if ((root instanceof Element ) 83 && (((Element )root).getTagName().equals(tagName))) { 84 return (Element )root; 85 } else { 86 Element foundNode = null; 87 for (Node child = accessor.accessFirstChild(root); 88 (child != null) && (foundNode == null); 89 child = accessor.accessNextSibling(child)) { 90 foundNode = accessElementByTagName(accessor, child, tagName); 91 } 92 return foundNode; 93 } 94 } 95 96 100 public Element getElementByTagName(Document doc, 101 Node root, 102 String tagName) { 103 DOMAccess accessor = new DOMAccess (doc); 104 Element element = accessElementByTagName(accessor, root, tagName); 105 if (element != null) { 106 element = accessor.getExpandedElement(element); 107 } 108 return element; 109 } 110 } 111 | Popular Tags |