Skip to content

Commit

Permalink
#39 XMLWriter.writeOpen(Element) writes namespaces declared directly …
Browse files Browse the repository at this point in the history
…on element.
  • Loading branch information
FilipJirsak committed Jul 1, 2018
1 parent 53f923a commit 351bfef
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/main/java/org/dom4j/io/XMLWriter.java
Expand Up @@ -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(">");
}
Expand Down Expand Up @@ -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();
Expand Down
54 changes: 54 additions & 0 deletions src/test/java/org/dom4j/XMLWriterTest.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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(), "<rss xmlns:g=\"http://base.google.com/ns/1.0\" xmlns:c=\"http://base.google.com/cns/1.0\"></rss>");
}

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(), "<rss xmlns:g=\"http://base.google.com/ns/1.0\" xmlns:c=\"http://base.google.com/cns/1.0\">");
}

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(), "<rss xmlns:g=\"http://base.google.com/ns/1.0\" xmlns:c=\"http://base.google.com/cns/1.0\" nons=\"value\" g:ns=\"value\">");
}

protected void generateXML(ContentHandler handler) throws SAXException {
handler.startDocument();

Expand Down

0 comments on commit 351bfef

Please sign in to comment.