KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jxpath > ri > model > dom > DOMModelTest


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.jxpath.ri.model.dom;
17
18 import junit.framework.Test;
19 import junit.framework.TestSuite;
20
21 import org.apache.commons.jxpath.AbstractFactory;
22 import org.apache.commons.jxpath.ri.model.XMLModelTestCase;
23 import org.apache.commons.jxpath.xml.DocumentContainer;
24 import org.w3c.dom.Attr JavaDoc;
25 import org.w3c.dom.Document JavaDoc;
26 import org.w3c.dom.Element JavaDoc;
27 import org.w3c.dom.Node JavaDoc;
28 import org.w3c.dom.NodeList JavaDoc;
29
30 /**
31  * Tests JXPath with DOM
32  *
33  * @author Dmitri Plotnikov
34  * @version $Revision: 1.13 $ $Date: 2004/03/02 01:32:20 $
35  */

36
37 public class DOMModelTest extends XMLModelTestCase {
38     
39     /**
40      * Construct a new instance of this test case.
41      *
42      * @param name Name of the test case
43      */

44     public DOMModelTest(String JavaDoc name) {
45         super(name);
46     }
47
48     /**
49      * Return the tests included in this test suite.
50      */

51     public static Test suite() {
52         return (new TestSuite(DOMModelTest.class));
53     }
54
55     protected String JavaDoc getModel() {
56         return DocumentContainer.MODEL_DOM;
57     }
58
59     protected AbstractFactory getAbstractFactory() {
60         return new TestDOMFactory();
61     }
62     
63     public void testGetNode() {
64         assertXPathNodeType(context, "/", Document JavaDoc.class);
65         assertXPathNodeType(context, "/vendor/location", Element JavaDoc.class);
66         assertXPathNodeType(context, "//location/@name", Attr JavaDoc.class);
67     }
68     
69     protected String JavaDoc getXMLSignature(
70         Object JavaDoc node,
71         boolean elements,
72         boolean attributes,
73         boolean text,
74         boolean pi)
75     {
76         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
77         appendXMLSignature(buffer, node, elements, attributes, text, pi);
78         return buffer.toString();
79     }
80
81     private void appendXMLSignature(
82         StringBuffer JavaDoc buffer,
83         Object JavaDoc object,
84         boolean elements,
85         boolean attributes,
86         boolean text,
87         boolean pi)
88     {
89         Node JavaDoc node = (Node JavaDoc) object;
90         int type = node.getNodeType();
91         switch (type) {
92             case Node.DOCUMENT_NODE :
93                 buffer.append("<D>");
94                 appendXMLSignature(
95                     buffer,
96                     node.getChildNodes(),
97                     elements,
98                     attributes,
99                     text,
100                     pi);
101                 buffer.append("</D");
102                 break;
103
104             case Node.ELEMENT_NODE :
105                 String JavaDoc tag = elements ? ((Element JavaDoc) node).getTagName() : "E";
106                 buffer.append("<");
107                 buffer.append(tag);
108                 buffer.append(">");
109                 appendXMLSignature(
110                     buffer,
111                     node.getChildNodes(),
112                     elements,
113                     attributes,
114                     text,
115                     pi);
116                 buffer.append("</");
117                 buffer.append(tag);
118                 buffer.append(">");
119                 break;
120
121             case Node.TEXT_NODE :
122             case Node.CDATA_SECTION_NODE :
123                 if (text) {
124                     String JavaDoc string = node.getNodeValue();
125                     string = string.replace('\n', '=');
126                     buffer.append(string);
127                 }
128                 break;
129         }
130     }
131
132     private void appendXMLSignature(
133         StringBuffer JavaDoc buffer,
134         NodeList JavaDoc children,
135         boolean elements,
136         boolean attributes,
137         boolean text,
138         boolean pi)
139     {
140         for (int i = 0; i < children.getLength(); i++) {
141             appendXMLSignature(
142                 buffer,
143                 children.item(i),
144                 elements,
145                 attributes,
146                 text,
147                 pi);
148         }
149     }
150 }
Popular Tags