Page 1 of 1

Error when getting content from HeaderPart or FooterPart

PostPosted: Thu Jun 02, 2016 1:13 am
by Larvalis
Hey I have been trying out docx4j in my spring project and encountered an error which I can't quite find the reason for.
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

Re: Error when getting content from HeaderPart or FooterPart

PostPosted: Sun Jun 05, 2016 7:49 pm
by jason

Re: Error when getting content from HeaderPart or FooterPart

PostPosted: Mon Jun 06, 2016 11:22 pm
by Larvalis
Hey Jason

I can see you added one of the elements that made the error: "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing", and it seems to be working as I do not get an error regarding the wordprocessingDrawing anymore.

However there were another error I got as well for markup-compatibility "http://schemas.openxmlformats.org/markup-compatibility/2006", which I still get (for both headers):
Code: Select all
2016-06-06 14:00:53.862  WARN 63887 --- [nio-8080-exec-9] o.d.jaxb.JaxbValidationEventHandler      : [ERROR] : unexpected element (uri:"http://schemas.openxmlformats.org/markup-compatibility/2006", local:"AlternateContent"). Expect
2016-06-06 14:00:53.862  INFO 63887 --- [nio-8080-exec-9] o.d.jaxb.JaxbValidationEventHandler      : continuing (with possible element/attribute loss)
2016-06-06 14:00:53.862  INFO 63887 --- [nio-8080-exec-9] o.d.o.parts.JaxbXmlPartXPathAware        : encountered unexpected content in /word/header2.xml; pre-processing
2016-06-06 14:00:53.865  INFO 63887 --- [nio-8080-exec-9] org.docx4j.XmlUtils                      : Using org.apache.xalan.transformer.TransformerImpl
2016-06-06 14:00:53.888  WARN 63887 --- [nio-8080-exec-9] org.docx4j.utils.XSLTUtils               : Found some mc:AlternateContent
2016-06-06 14:00:53.888  WARN 63887 --- [nio-8080-exec-9] org.docx4j.utils.XSLTUtils               : Selecting w:pict



I also tried to make a new version of my template.docx with Word 2016 for Mac, as I got the original template from a windows computer with office 2010, but no change in the warning/error.

Re: Error when getting content from HeaderPart or FooterPart

PostPosted: Mon Jun 06, 2016 11:34 pm
by jason
That's expected.

Constructs of the form:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
      <mc:AlternateContent>
        <mc:Choice Requires="[support for some specified namespace]">
         
         
        </mc:Choice>
        <mc:Fallback>
     
         
        </mc:Fallback>
      </mc:AlternateContent>
 
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


result in something like "encountered unexpected content in /word/header2.xml; pre-processing"

Docx4j "pre-processes" such cases using https://github.com/plutext/docx4j/blob/ ... essor.xslt

Re: Error when getting content from HeaderPart or FooterPart

PostPosted: Tue Jun 07, 2016 6:02 pm
by Larvalis
I see, in my case the error comes from having a figure of the textBox/textArea type in the header, which I believe isn't all that cooperative with with the way I use to modify the document.
From what I can see it is the method "getJaxbElement()" used in "getContent()" that initiate the JaxbValidationEventHandler.

Thanks for you help with the wordprocessingDrawing error and for pointing me in the right direction in understanding the other error.

Re: Error when getting content from HeaderPart or FooterPart

PostPosted: Wed Dec 12, 2018 8:09 pm
by Shreyansh
what is the actual solution of the other error?

I am getting same error (unexpected element (uri:"http://schemas.openxmlformats.org/markup-compatibility/2006", local:"AlternateContent")) when reading header using getJaxbElement() and header containing shape.

what can i do to solve this problem?

Re: Error when getting content from HeaderPart or FooterPart

PostPosted: Wed Dec 12, 2018 9:10 pm
by jason
Try https://www.docx4java.org/docx4j/docx4j ... didate.jar

This thread is obsolete if you use that, so locking it now.