From 351bfef0ad5c5e5328758981797f80beba1d017d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Jirs=C3=A1k?= Date: Sun, 1 Jul 2018 15:01:05 +0200 Subject: [PATCH] #39 XMLWriter.writeOpen(Element) writes namespaces declared directly on element. --- src/main/java/org/dom4j/io/XMLWriter.java | 14 ++++++ src/test/java/org/dom4j/XMLWriterTest.java | 54 ++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/src/main/java/org/dom4j/io/XMLWriter.java b/src/main/java/org/dom4j/io/XMLWriter.java index 56742d39..6dbb46ac 100644 --- a/src/main/java/org/dom4j/io/XMLWriter.java +++ b/src/main/java/org/dom4j/io/XMLWriter.java @@ -574,6 +574,7 @@ public void write(Object object) throws IOException { public void writeOpen(Element element) throws IOException { writer.write("<"); writer.write(element.getQualifiedName()); + writeNamespaces(element); writeAttributes(element); writer.write(">"); } @@ -1206,6 +1207,19 @@ protected void writeNamespace(String prefix, String uri) writer.write("\""); } + /** + * Writes all namespaces declared directly on element. + * + * @throws IOException + */ + protected void writeNamespaces(Element element) throws IOException { + assert element != null; + for (Namespace ns : element.declaredNamespaces()) { + writeNamespace(ns); + namespaceStack.push(ns); + } + } + protected void writeProcessingInstruction(ProcessingInstruction pi) throws IOException { // indent(); diff --git a/src/test/java/org/dom4j/XMLWriterTest.java b/src/test/java/org/dom4j/XMLWriterTest.java index ecf0d34c..cee1380e 100644 --- a/src/test/java/org/dom4j/XMLWriterTest.java +++ b/src/test/java/org/dom4j/XMLWriterTest.java @@ -14,6 +14,7 @@ import org.dom4j.io.XMLWriter; import org.dom4j.tree.BaseElement; import org.dom4j.tree.DefaultDocument; +import org.testng.Assert; import org.xml.sax.ContentHandler; import org.xml.sax.SAXException; import org.xml.sax.helpers.AttributesImpl; @@ -577,6 +578,59 @@ public void testGitHubIssue26_case7() throws IOException { new XMLWriter(new CharArrayWriter(128), format).write(element); } + public void testElementNamespaceWriteDocument() throws IOException { + Document document = DocumentHelper.createDocument(); + Element root = document.addElement("rss") + .addNamespace("g", "http://base.google.com/ns/1.0") + .addNamespace("c", "http://base.google.com/cns/1.0"); + + OutputFormat outputFormat = OutputFormat.createCompactFormat(); + outputFormat.setSuppressDeclaration(true); + + StringWriter stringWriter = new StringWriter(); + XMLWriter writer = new XMLWriter(stringWriter, outputFormat); + writer.write(document); + writer.close(); + + Assert.assertEquals(stringWriter.toString(), ""); + } + + public void testElementNamespaceWriteOpen() throws IOException { + Document document = DocumentHelper.createDocument(); + Element root = document.addElement("rss") + .addNamespace("g", "http://base.google.com/ns/1.0") + .addNamespace("c", "http://base.google.com/cns/1.0"); + + OutputFormat outputFormat = OutputFormat.createCompactFormat(); + outputFormat.setSuppressDeclaration(true); + + StringWriter stringWriter = new StringWriter(); + XMLWriter writer = new XMLWriter(stringWriter, outputFormat); + writer.writeOpen(root); + writer.close(); + + Assert.assertEquals(stringWriter.toString(), ""); + } + + public void testElementNamespaceAttributesWriteOpen() throws IOException { + Document document = DocumentHelper.createDocument(); + Element root = document.addElement("rss") + .addNamespace("g", "http://base.google.com/ns/1.0") + .addNamespace("c", "http://base.google.com/cns/1.0"); + root.addAttribute("nons", "value"); + root.addAttribute(QName.get("g:ns"), "value"); + + OutputFormat outputFormat = OutputFormat.createCompactFormat(); + outputFormat.setSuppressDeclaration(true); + + StringWriter stringWriter = new StringWriter(); + XMLWriter writer = new XMLWriter(stringWriter, outputFormat); + writer.writeOpen(root); + writer.close(); + + Assert.assertEquals(stringWriter.toString(), ""); + } + protected void generateXML(ContentHandler handler) throws SAXException { handler.startDocument();