Page 1 of 1

wp14:sizeRelH within wp:anchor - supported in 2.7.1?

PostPosted: Mon Nov 21, 2011 10:43 pm
by rkamke
Hello,

I have a XML part of a DOCX which in fact is a w:hdr. It contains a w:drawing with a wp:anchor. Nested in this anchor element I have several subelements: wp:simplePo, wp:positionH, wp:positionV, wp:extent, wp:effectExtent, wp:wrapTight, wp:docPr, wp:cNvGraphicFramePr, a:graphic (so far everything is fine), wp14:sizeRelH and wp14:sizeRelV.
The "Office Drawing Extensions to Office Open XML Structure Sepcification" - I found a version dated June 8, 2011 - suggests this structure being valid. Older sources (http://www.schemacentral.com/sc/ooxml/e-wp_anchor.html) suggest an error here.

docx4j 2.7.1 complains when parsing the XML part (text reformatted):
WARN (JaxbValidationEventHandler.java:80) - [ERROR] : unexpected element (uri:"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing", local:"sizeRelH").
Expected elements are
<{http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing}wrapTopAndBottom>,
<{http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing}docPr>,
<{http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing}wrapNone>,
<{http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing}wrapTight>,
<{http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing}simplePos>,
<{http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing}wrapThrough>,
<{http://schemas.openxmlformats.org/drawingml/2006/main}graphic>,
<{http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing}wrapSquare>,
<{http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing}extent>,
<{http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing}effectExtent>,
<{http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing}positionH>,
<{http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing}positionV>,
<{http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing}cNvGraphicFramePr>

I wonder whether docx4j 2.7.1 supports wp14:sizeRelH and wp14:sizeRelV after a:graphic within a wp:anchor. Can you give me some piece of advice how to cope with this situation?

Thank you,
Roland

Re: wp14:sizeRelH within wp:anchor - supported in 2.7.1?

PostPosted: Mon Nov 21, 2011 11:50 pm
by jason
Hello Roland

HeaderPart in 2.7.1 includes:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
        @Override
    public Hdr unmarshal( java.io.InputStream is ) throws JAXBException {
                try {
                       
                        log.info("For MDP, unmarshall via binder");
                        // InputStream to Document
                        javax.xml.parsers.DocumentBuilderFactory dbf
                                = DocumentBuilderFactory.newInstance();
                        dbf.setNamespaceAware(true);
                        org.w3c.dom.Document doc = dbf.newDocumentBuilder().parse(is);

                        //
                        binder = jc.createBinder();
                       
                        JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler();
                        eventHandler.setContinue(false);
                        binder.setEventHandler(eventHandler);
                       
                        try {
                                jaxbElement =  (Hdr) binder.unmarshal( doc );
                        } catch (UnmarshalException ue) {
                                log.info("encountered unexpected content; pre-processing");
                                eventHandler.setContinue(true);
                                DOMResult result = new DOMResult();
                                Templates mcPreprocessorXslt = JaxbValidationEventHandler.getMcPreprocessor();
                                XmlUtils.transform(doc, mcPreprocessorXslt, null, result);
                                doc = (org.w3c.dom.Document)result.getNode();
                                jaxbElement =  (Hdr) binder.unmarshal( doc );                                  
                        }
                       
                        return jaxbElement;
                       
                } catch (Exception e ) {
                        e.printStackTrace();
                        return null;
                }
    }

    /**
     * @since 2.7.1
     */
       
        @Override
    public Hdr unmarshal(org.w3c.dom.Element el) throws JAXBException {

                try {

                        binder = jc.createBinder();
                        JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler();
                        eventHandler.setContinue(false);
                        binder.setEventHandler(eventHandler);
                       
                        try {
                                jaxbElement =  (Hdr) binder.unmarshal( el );
                        } catch (UnmarshalException ue) {
                                log.info("encountered unexpected content; pre-processing");
                                try {
                                        org.w3c.dom.Document doc;
                                        if (el instanceof org.w3c.dom.Document) {
                                                doc = (org.w3c.dom.Document) el;
                                        } else {
                                                // Hope for the best. Dodgy though; what if this is
                                                // being used on something deep in the tree?
                                                // TODO: revisit
                                                doc = el.getOwnerDocument();
                                        }
                                        eventHandler.setContinue(true);
                                        DOMResult result = new DOMResult();
                                        Templates mcPreprocessorXslt = JaxbValidationEventHandler
                                                        .getMcPreprocessor();
                                        XmlUtils.transform(doc, mcPreprocessorXslt, null, result);
                                        doc = (org.w3c.dom.Document) result.getNode();
                                        jaxbElement = (Hdr) binder
                                                        .unmarshal(doc);
                                } catch (Exception e) {
                                        throw new JAXBException("Preprocessing exception", e);
                                }
                        }
                        return jaxbElement;
                       
                } catch (JAXBException e) {
                        log.error(e);
                        throw e;
                }
        }

 
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


which traps UnmarshalException, and then preprocesses, effectively choosing the 2007 mc:AlternateContent presumed to be present (and dropping the wp14 stuff).

Perhaps you could include the relevant XML so we can see whether it ought to be handled by this logic.

Is the JaxbValidationEventHandler message you are seeing arising when you open the docx, or in the course of some other processing?

cheers .. Jason