KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > NamespaceTest


1 /*
2  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
6  */

7
8 package org.dom4j;
9
10 import junit.textui.TestRunner;
11
12 import java.util.HashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.dom4j.io.SAXReader;
18
19 /**
20  * A test harness to test the use of Namespaces.
21  *
22  * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan </a>
23  * @version $Revision: 1.4 $
24  */

25 public class NamespaceTest extends AbstractTestCase {
26     /** Input XML file to read */
27     private static final String JavaDoc INPUT_XML_FILE = "/xml/namespaces.xml";
28
29     /** Namespace to use in tests */
30     private static final Namespace XSL_NAMESPACE = Namespace.get("xsl",
31             "http://www.w3.org/1999/XSL/Transform");
32
33     private static final QName XSL_TEMPLATE = QName.get("template",
34             XSL_NAMESPACE);
35
36     public static void main(String JavaDoc[] args) {
37         TestRunner.run(NamespaceTest.class);
38     }
39
40     // Test case(s)
41
// -------------------------------------------------------------------------
42
public void debugShowNamespaces() throws Exception JavaDoc {
43         Element root = getRootElement();
44
45         for (Iterator JavaDoc iter = root.elementIterator(); iter.hasNext();) {
46             Element element = (Element) iter.next();
47
48             log("Found element: " + element);
49             log("Namespace: " + element.getNamespace());
50             log("Namespace prefix: " + element.getNamespacePrefix());
51             log("Namespace URI: " + element.getNamespaceURI());
52         }
53     }
54
55     public void testGetElement() throws Exception JavaDoc {
56         Element root = getRootElement();
57
58         Element firstTemplate = root.element(XSL_TEMPLATE);
59         assertTrue(
60                 "Root element contains at least one <xsl:template/> element",
61                 firstTemplate != null);
62
63         log("Found element: " + firstTemplate);
64     }
65
66     public void testGetElements() throws Exception JavaDoc {
67         Element root = getRootElement();
68
69         List JavaDoc list = root.elements(XSL_TEMPLATE);
70         assertTrue(
71                 "Root element contains at least one <xsl:template/> element",
72                 list.size() > 0);
73
74         log("Found elements: " + list);
75     }
76
77     public void testElementIterator() throws Exception JavaDoc {
78         Element root = getRootElement();
79         Iterator JavaDoc iter = root.elementIterator(XSL_TEMPLATE);
80         assertTrue(
81                 "Root element contains at least one <xsl:template/> element",
82                 iter.hasNext());
83
84         do {
85             Element element = (Element) iter.next();
86             log("Found element: " + element);
87         } while (iter.hasNext());
88     }
89
90     /**
91      * Tests the use of namespace URI Mapping associated with a DocumentFactory
92      *
93      * @throws Exception
94      * DOCUMENT ME!
95      */

96     public void testNamespaceUriMap() throws Exception JavaDoc {
97         // register namespace prefix->uri mappings with factory
98
Map JavaDoc uris = new HashMap JavaDoc();
99         uris.put("x", "fooNamespace");
100         uris.put("y", "barNamespace");
101
102         DocumentFactory factory = new DocumentFactory();
103         factory.setXPathNamespaceURIs(uris);
104
105         // parse or create a document
106
SAXReader reader = new SAXReader();
107         reader.setDocumentFactory(factory);
108
109         Document doc = getDocument("/xml/test/nestedNamespaces.xml", reader);
110
111         // evaluate XPath using registered namespace prefixes
112
// which do not appear in the document (though the URIs do!)
113
String JavaDoc value = doc.valueOf("/x:pizza/y:cheese/x:pepper");
114
115         log("Found value: " + value);
116
117         assertEquals("XPath used default namesapce URIS", "works", value);
118     }
119
120     // Implementation methods
121
// -------------------------------------------------------------------------
122
protected void setUp() throws Exception JavaDoc {
123         super.setUp();
124         document = getDocument(INPUT_XML_FILE);
125     }
126
127     /**
128      * DOCUMENT ME!
129      *
130      * @return the root element of the document
131      */

132     protected Element getRootElement() {
133         Element root = document.getRootElement();
134         assertTrue("Document has root element", root != null);
135
136         return root;
137     }
138 }
139
140 /*
141  * Redistribution and use of this software and associated documentation
142  * ("Software"), with or without modification, are permitted provided that the
143  * following conditions are met:
144  *
145  * 1. Redistributions of source code must retain copyright statements and
146  * notices. Redistributions must also contain a copy of this document.
147  *
148  * 2. Redistributions in binary form must reproduce the above copyright notice,
149  * this list of conditions and the following disclaimer in the documentation
150  * and/or other materials provided with the distribution.
151  *
152  * 3. The name "DOM4J" must not be used to endorse or promote products derived
153  * from this Software without prior written permission of MetaStuff, Ltd. For
154  * written permission, please contact dom4j-info@metastuff.com.
155  *
156  * 4. Products derived from this Software may not be called "DOM4J" nor may
157  * "DOM4J" appear in their names without prior written permission of MetaStuff,
158  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
159  *
160  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
161  *
162  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
163  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
164  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
165  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
166  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
167  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
168  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
169  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
170  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
171  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
172  * POSSIBILITY OF SUCH DAMAGE.
173  *
174  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
175  */

176
Popular Tags