Malformed xml with 3.2.2
Posted: Wed Jul 08, 2015 12:38 am
I just started to upgrade to 3.2.2 and I noticed that we had a patch that we had to bring back, so might as well bring this to your attention. With the code as is I am getting the following exception:
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Exception executing transformer: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 40; Content is not allowed in prolog.
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:215)
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:158)
at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:140)
at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:1)
at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:82)
... 5 more
Caused by: javax.xml.transform.TransformerException: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 40; Content is not allowed in prolog.
at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:502)
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:211)
... 9 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 40; Content is not allowed in prolog.
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1239)
at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:485)
... 10 more
and here is the document in error:
<?xml version="1.0" encoding="utf-8"?>
DOCPROPERTY Title \*
MERGEFORMAT Business PlanningBusiness Architecture
DOCPROPERTY Subject \*
MERGEFORMAT July 07, 2015 Business PlanningThis...
What we had to add is in AbstractXsltExporterDelegate
Not sure exactly why this is happening or if the patch is the most correct way to fix the issue, but it does work
Cheers,
Alain
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Exception executing transformer: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 40; Content is not allowed in prolog.
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:215)
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:158)
at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:140)
at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:1)
at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:82)
... 5 more
Caused by: javax.xml.transform.TransformerException: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 40; Content is not allowed in prolog.
at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:502)
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:211)
... 9 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 40; Content is not allowed in prolog.
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1239)
at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:485)
... 10 more
and here is the document in error:
<?xml version="1.0" encoding="utf-8"?>
DOCPROPERTY Title \*
MERGEFORMAT Business PlanningBusiness Architecture
DOCPROPERTY Subject \*
MERGEFORMAT July 07, 2015 Business PlanningThis...
What we had to add is in AbstractXsltExporterDelegate
- Code: Select all
protected static final String XSLT_RESOURCE_ROOT = "org/docx4j/convert/out/fo/";
protected static final URIResolver RESOURCES_URI_RESOLVER = new OutFoURIResolver();
protected static class OutFoURIResolver implements URIResolver {
@Override
public Source resolve(String href, String base) throws TransformerException {
try {
return new StreamSource(org.docx4j.utils.ResourceUtils.getResource(XSLT_RESOURCE_ROOT + href));
}
catch (IOException e) {
throw new TransformerException(e);
}
}
}
protected Templates loadDefaultTemplates() throws Docx4JException {
Templates ret = null;
Source xsltSource = null;
//do a lazy loading in case the user has a custom template
try {
XmlUtils.getTransformerFactory().setURIResolver(RESOURCES_URI_RESOLVER); //<==added Castortech
xsltSource = new StreamSource(ResourceUtils.getResource(defaultTemplatesResource));
ret = XmlUtils.getTransformerTemplate(xsltSource);
} catch (IOException e) {
throw new Docx4JException("Exception loading default template \"" + defaultTemplatesResource + "\", " + e.getMessage(), e);
} catch (TransformerConfigurationException e) {
throw new Docx4JException("Exception loading default template \"" + defaultTemplatesResource + "\", " + e.getMessage(), e);
}
return ret;
}
Not sure exactly why this is happening or if the patch is the most correct way to fix the issue, but it does work
Cheers,
Alain