Skip to content

Commit

Permalink
When marshalling in common XMLStreamWriter case, setMcIgnorable (MOXy…
Browse files Browse the repository at this point in the history
… needs this).

XMLStreamWriterWrapperIndenting
  • Loading branch information
plutext committed Sep 13, 2018
1 parent 9b8417b commit 2fbcf26
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/main/java/org/docx4j/openpackaging/parts/JaxbXmlPart.java
Expand Up @@ -71,6 +71,7 @@
import org.docx4j.org.apache.xml.security.Init;
import org.docx4j.org.apache.xml.security.c14n.Canonicalizer;
import org.docx4j.utils.XMLStreamWriterWrapper;
import org.docx4j.utils.XMLStreamWriterWrapperIndenting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
Expand Down Expand Up @@ -827,14 +828,39 @@ public void marshal(java.io.OutputStream os, Object namespacePrefixMapper) throw
IOUtils.write(bytes, os);

} else {

XMLOutputFactory xof = XMLOutputFactory.newFactory();
XMLStreamWriter xsw = xof.createXMLStreamWriter(os, "UTF-8");

// get rid of xmlns="" which com.sun.xml.internal.stream.writers.XMLStreamWriterImpl writes
XMLStreamWriterWrapper xsww = new XMLStreamWriterWrapper(this, xsw);
XMLStreamWriterWrapper xsww = null;
if (Docx4jProperties.getProperty("docx4j.jaxb.formatted.output", true)) {
xsww = new XMLStreamWriterWrapperIndenting(this, xsw);
} else {
xsww = new XMLStreamWriterWrapper(this, xsw);
}

marshaller.setListener(new Docx4jMarshallerListener(xsww));

// Word requires certain namespaces to be written at the document level;
// these are the ones used in the top level ignorable attribute, plus any
// used in attribute values in the mc elements themselves.
// The JAXB reference implementation does this (courtesy of namespacePrefixMapper
// or automatically??), but MOXy needs some help:-

String mceIgnorable = "";
if (this.getMceIgnorable()!=null) {
mceIgnorable = this.getMceIgnorable();
}
((McIgnorableNamespaceDeclarator) namespacePrefixMapper).setMcIgnorable(mceIgnorable + getMcChoiceNamespaces());
// If necessary, we could do it in our streamwriter; there is some
// code in git history to do that
// xsww.setIgnorableNamespaces(mceIgnorable + getMcChoiceNamespaces());

marshaller.marshal(jaxbElement, xsww);
// JAXB sends empty elements as start, end :-(
// for a way around this, see https://stackoverflow.com/questions/27158723/write-empty-tag-with-jaxb-and-xmlstreamwriter

xsww.close();
xsw.close();

Expand Down

0 comments on commit 2fbcf26

Please sign in to comment.