|
8 | 8 | import java.io.CharArrayWriter;
|
9 | 9 | import java.io.IOException;
|
10 | 10 |
|
11 |
| -import org.junit.Test; |
| 11 | +import javax.xml.parsers.DocumentBuilderFactory; |
12 | 12 |
|
| 13 | +import org.junit.Test; |
| 14 | +import org.jdom2.Attribute; |
13 | 15 | import org.jdom2.DefaultJDOMFactory;
|
14 | 16 | import org.jdom2.DocType;
|
15 | 17 | import org.jdom2.Document;
|
16 | 18 | import org.jdom2.Element;
|
| 19 | +import org.jdom2.Namespace; |
17 | 20 | import org.jdom2.input.DOMBuilder;
|
18 | 21 | import org.jdom2.input.SAXBuilder;
|
19 | 22 | import org.jdom2.input.sax.XMLReaders;
|
@@ -71,6 +74,32 @@ public void testXSDDocument() {
|
71 | 74 | checkDOM("/xsdcomplex/input.xml", true);
|
72 | 75 | }
|
73 | 76 |
|
| 77 | + @Test |
| 78 | + public void testNoNamespaceDOM() throws Exception { |
| 79 | + // https://github.com/hunterhacker/jdom/issues/138 |
| 80 | + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); |
| 81 | + org.w3c.dom.Document doc = dbFactory.newDocumentBuilder().newDocument(); |
| 82 | + doc.setXmlVersion("1.0"); |
| 83 | + |
| 84 | + org.w3c.dom.Element root = doc.createElement("Document"); |
| 85 | + |
| 86 | + root.setAttribute("xmlns", "urn:iso:foo"); |
| 87 | + root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); |
| 88 | + root.setAttribute("xsi:schemaLocation", "urn:iso:foo bar.xsd"); |
| 89 | + doc.appendChild(root); |
| 90 | + |
| 91 | + // The above is a badly-formed DOM document without the correct |
| 92 | + // namespaceing. The second attribute should use root.setAttributeNS |
| 93 | + DOMBuilder dbuilder = new DOMBuilder(); |
| 94 | + Document jdoc = dbuilder.build(doc); |
| 95 | + |
| 96 | + Namespace xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); |
| 97 | + Attribute att = jdoc.getRootElement().getAttribute("schemaLocation", xsi); |
| 98 | + assertTrue(att != null); |
| 99 | + assertTrue("xsi".equals(att.getNamespacePrefix())); |
| 100 | + |
| 101 | + } |
| 102 | + |
74 | 103 | private void checkDOM(String resname, boolean xsdvalidate) {
|
75 | 104 | try {
|
76 | 105 | org.w3c.dom.Document domdoc = HelpTestDOMBuilder.getDocument(resname, xsdvalidate);
|
|
0 commit comments