KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > lazydom > LazyDOMTestCaseBase


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: LazyDOMTestCaseBase.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.lazydom;
25
26 import java.io.File JavaDoc;
27 import java.io.FileWriter JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
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 JavaDoc;
38 import org.w3c.dom.Element JavaDoc;
39 import org.w3c.dom.Node JavaDoc;
40
41 /**
42  * Base class for lazy dom test cases. Contains common code.
43  */

44 public class LazyDOMTestCaseBase extends TestCaseBase {
45     /** Constructor. */
46     protected LazyDOMTestCaseBase(Method JavaDoc method) {
47         super(method);
48     }
49
50     /**
51      * Print DOM to a file, starting at a node and diff with expected
52      */

53     protected void printDiffDom(String JavaDoc msg,
54                                 Node JavaDoc node,
55                                 String JavaDoc ext) {
56         File JavaDoc outFile = getResultFile(ext);
57         File JavaDoc expectFile = getExpectedFile(ext);
58         TestFileOps.ensureFileDir(outFile);
59
60         try {
61             PrintWriter JavaDoc out = new PrintWriter JavaDoc(new FileWriter JavaDoc(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 JavaDoc except) {
71             throw new TestException(except);
72         }
73         getDiffer().diff(expectFile, outFile);
74     }
75
76     /**
77      * Recursively find an element-by-tag-name without expanding.
78      */

79     private Element JavaDoc accessElementByTagName(DOMAccess accessor,
80                                            Node JavaDoc root,
81                                            String JavaDoc tagName) {
82         if ((root instanceof Element JavaDoc)
83             && (((Element JavaDoc)root).getTagName().equals(tagName))) {
84             return (Element JavaDoc)root;
85         } else {
86             Element JavaDoc foundNode = null;
87             for (Node JavaDoc 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     /**
97      * Find the first element of the specified tag, but do no expansion except
98      * for the returned node.
99      */

100     public Element JavaDoc getElementByTagName(Document JavaDoc doc,
101                                        Node JavaDoc root,
102                                        String JavaDoc tagName) {
103         DOMAccess accessor = new DOMAccess (doc);
104         Element JavaDoc element = accessElementByTagName(accessor, root, tagName);
105         if (element != null) {
106             element = accessor.getExpandedElement(element);
107         }
108         return element;
109     }
110 }
111
Popular Tags