The result works quite fine and seem unaffected by the error, but I would love to find out why the error is showing, and a possible way to fix/avoid it.
The warning is:
- Code: Select all
2016-06-01 15:29:38.928 WARN 48801 --- [nio-8080-exec-2] o.d.jaxb.JaxbValidationEventHandler : [ERROR] : unexpected element (uri:"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing", local:"sizeRelH"). Expect
2016-06-01 15:29:38.928 INFO 48801 --- [nio-8080-exec-2] o.d.jaxb.JaxbValidationEventHandler : continuing (with possible element/attribute loss)
2016-06-01 15:29:38.928 INFO 48801 --- [nio-8080-exec-2] o.d.o.parts.JaxbXmlPartXPathAware : encountered unexpected content in /word/header3.xml; pre-processing
2016-06-01 15:29:38.929 INFO 48801 --- [nio-8080-exec-2] org.docx4j.XmlUtils : Using org.apache.xalan.transformer.TransformerImpl
2016-06-01 15:29:38.933 WARN 48801 --- [nio-8080-exec-2] org.docx4j.utils.XSLTUtils : Found some mc:AlternateContent
2016-06-01 15:29:38.933 WARN 48801 --- [nio-8080-exec-2] org.docx4j.utils.XSLTUtils : Selecting w:pict
2016-06-01 15:29:38.940 WARN 48801 --- [nio-8080-exec-2] o.d.jaxb.JaxbValidationEventHandler : [ERROR] : unexpected element (uri:"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing", local:"sizeRelH"). Expect
2016-06-01 15:29:38.940 INFO 48801 --- [nio-8080-exec-2] o.d.jaxb.JaxbValidationEventHandler : continuing (with possible element/attribute loss)
2016-06-01 15:29:38.941 INFO 48801 --- [nio-8080-exec-2] o.d.o.parts.JaxbXmlPartXPathAware : encountered unexpected content in /word/footer3.xml; pre-processing
2016-06-01 15:29:38.941 INFO 48801 --- [nio-8080-exec-2] org.docx4j.XmlUtils : Using org.apache.xalan.transformer.TransformerImpl
2016-06-01 15:29:38.945 WARN 48801 --- [nio-8080-exec-2] o.d.jaxb.JaxbValidationEventHandler : [ERROR] : unexpected element (uri:"http://schemas.openxmlformats.org/markup-compatibility/2006", local:"AlternateContent"). Expect
2016-06-01 15:29:38.945 INFO 48801 --- [nio-8080-exec-2] o.d.jaxb.JaxbValidationEventHandler : continuing (with possible element/attribute loss)
2016-06-01 15:29:38.945 INFO 48801 --- [nio-8080-exec-2] o.d.o.parts.JaxbXmlPartXPathAware : encountered unexpected content in /word/header2.xml; pre-processing
2016-06-01 15:29:38.946 INFO 48801 --- [nio-8080-exec-2] org.docx4j.XmlUtils : Using org.apache.xalan.transformer.TransformerImpl
2016-06-01 15:29:38.947 WARN 48801 --- [nio-8080-exec-2] org.docx4j.utils.XSLTUtils : Found some mc:AlternateContent
2016-06-01 15:29:38.947 WARN 48801 --- [nio-8080-exec-2] org.docx4j.utils.XSLTUtils : Selecting w:pict
2016-06-01 15:29:38.950 WARN 48801 --- [nio-8080-exec-2] o.d.jaxb.JaxbValidationEventHandler : [WARNING] Message is Errors limit exceeded. To receive all errors set com.sun.xml.internal.bind logger to FINEST level.
2016-06-01 15:29:38.950 WARN 48801 --- [nio-8080-exec-2] o.d.jaxb.JaxbValidationEventHandler : Resetting error counter to work around https://github.com/gf-metro/jaxb/issues/22
2016-06-01 15:29:38.950 WARN 48801 --- [nio-8080-exec-2] o.d.jaxb.JaxbValidationEventHandler : .. reset successful
2016-06-01 15:29:38.950 INFO 48801 --- [nio-8080-exec-2] o.d.jaxb.JaxbValidationEventHandler : continuing (with possible element/attribute loss)
2016-06-01 15:29:38.950 INFO 48801 --- [nio-8080-exec-2] o.d.o.parts.JaxbXmlPartXPathAware : encountered unexpected content in /word/footer2.xml; pre-processing
2016-06-01 15:29:38.951 INFO 48801 --- [nio-8080-exec-2] org.docx4j.XmlUtils : Using org.apache.xalan.transformer.TransformerImpl
I have the code in the main method:
- Code: Select all
WordprocessingMLPackage template = getTemplate("Template.docx");
...
findAndReplaceHeaderFooter(template, "HEADER_address", "Some Address");
...
File f = new File("newFile.docx");
template.save(f);
And following methods:
- Code: Select all
private List<Object> getAllElementFromObject(Object obj, Class<?> toSearch) {
List<Object> result = new ArrayList<Object>();
if (obj instanceof JAXBElement)
obj = ((JAXBElement<?>) obj).getValue();
if (obj.getClass().equals(toSearch))
result.add(obj);
else if (obj instanceof ContentAccessor) {
// Gives error(unexpected element) on header and footer
List<?> children = ((ContentAccessor) obj).getContent();
for (Object child : children) {
result.addAll(getAllElementFromObject(child, toSearch));
}
}
return result;
}
private void replaceText(List<Object> paragraphs, String toFind, String replacer) {
for (Object par : paragraphs) {
P p = (P) par;
List<Object> texts = getAllElementFromObject(p, Text.class);
for (Object text : texts) {
Text t = (Text) text;
if (t.getValue().contains(toFind)) {
t.setValue(t.getValue().replace(toFind, replacer));
}
}
}
}
public void findAndReplaceHeaderFooter(WordprocessingMLPackage doc, String toFind, String replacer) {
List<SectionWrapper> sectionWrappers = doc.getDocumentModel().getSections();
for (SectionWrapper sw : sectionWrappers) {
HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
// Replacing in the header for the first page
if (hfp.getFirstHeader() != null) {
replaceText(getAllElementFromObject(hfp.getFirstHeader(), P.class), toFind, replacer);
}
// Replacing in the footer for the first page
if (hfp.getFirstFooter() != null) {
replaceText(getAllElementFromObject(hfp.getFirstFooter(), P.class), toFind, replacer);
}
// Replacing in the default header
if (hfp.getDefaultHeader() != null) {
replaceText(getAllElementFromObject(hfp.getDefaultHeader(), P.class), toFind, replacer);
}
// Replacing in the default header
if (hfp.getDefaultFooter() != null) {
replaceText(getAllElementFromObject(hfp.getDefaultFooter(), P.class), toFind, replacer);
}
}
}
The error occurs when calling "((ContentAccessor) obj).getContent();", there is no error when I do the same in the MainDocumentPart, only the first time that I do it in either HeaderPart or FooterPart.
I have read a bit about it might be while unmashalling that it happens.
I am using:
Maven 4.0.0
docx4j 3.3.0 (without ImportXHTML, and excluding the loggers)
Java 8 Update 91 / javaSE 1.8.0_66
Mac OS x El Capitan - 10.11.5
Word 2016 for mac
If it might help, I get following the first time I launch the application (not between reloads)
- Code: Select all
2016-06-01 16:06:13.397 INFO 54195 --- [nio-8080-exec-4] org.docx4j.jaxb.Context : java.vendor=Oracle Corporation
2016-06-01 16:06:13.398 INFO 54195 --- [nio-8080-exec-4] org.docx4j.jaxb.Context : java.version=1.8.0_66
2016-06-01 16:06:13.433 INFO 54195 --- [nio-8080-exec-4] org.docx4j.jaxb.Context : No MOXy JAXB config found; assume not intended..
2016-06-01 16:06:13.524 INFO 54195 --- [nio-8080-exec-4] o.d.jaxb.NamespacePrefixMapperUtils : Using NamespacePrefixMapperSunInternal, which is suitable for Java 6
2016-06-01 16:06:13.524 INFO 54195 --- [nio-8080-exec-4] org.docx4j.jaxb.Context : Using Java 6/7 JAXB implementation
2016-06-01 16:06:15.542 INFO 54195 --- [nio-8080-exec-4] org.docx4j.jaxb.Context : Not using MOXy; using com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl
2016-06-01 16:06:15.756 WARN 54195 --- [nio-8080-exec-4] org.docx4j.utils.ResourceUtils : Couldn't get resource: docx4j.properties
2016-06-01 16:06:15.756 WARN 54195 --- [nio-8080-exec-4] org.docx4j.Docx4jProperties : Couldn't find/read docx4j.properties; docx4j.properties not found via classloader.
2016-06-01 16:06:15.756 INFO 54195 --- [nio-8080-exec-4] org.docx4j.XmlUtils : Using com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl
2016-06-01 16:06:15.756 INFO 54195 --- [nio-8080-exec-4] org.docx4j.XmlUtils : Using com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
2016-06-01 16:06:15.773 INFO 54195 --- [nio-8080-exec-4] o.d.o.contenttype.ContentTypeManager : Detected WordProcessingML package
2016-06-01 16:06:15.775 INFO 54195 --- [nio-8080-exec-4] org.docx4j.openpackaging.io3.Load3 : Instantiated package of type org.docx4j.openpackaging.packages.WordprocessingMLPackage
2016-06-01 16:06:15.792 INFO 54195 --- [nio-8080-exec-4] org.docx4j.utils.XPathFactoryUtil : xpath implementation: org.apache.xpath.jaxp.XPathFactoryImpl
2016-06-01 16:06:15.802 INFO 54195 --- [nio-8080-exec-4] org.docx4j.openpackaging.io3.Load3 : package read; elapsed time: 2437 ms