Changeset 1613 for trunk/docx4j/src/main


Ignore:
Timestamp:
07/10/11 08:11:33 (11 months ago)
Author:
jharrop
Message:

HeaderPart?: binder when unmarshalling from w3c dom Element.
FooterPart?: binder.
Both use preprocessor.

Location:
trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/FooterPart.java

    r1505 r1613  
    2424import java.util.List; 
    2525 
     26import javax.xml.bind.Binder; 
    2627import javax.xml.bind.JAXBException; 
     28import javax.xml.bind.UnmarshalException; 
    2729import javax.xml.bind.Unmarshaller; 
     30import javax.xml.parsers.DocumentBuilderFactory; 
     31import javax.xml.transform.Templates; 
     32import javax.xml.transform.dom.DOMResult; 
    2833 
    2934import org.apache.log4j.Logger; 
     35import org.docx4j.XmlUtils; 
     36import org.docx4j.jaxb.JaxbValidationEventHandler; 
    3037import org.docx4j.openpackaging.exceptions.InvalidFormatException; 
    3138import org.docx4j.openpackaging.parts.JaxbXmlPart; 
     
    3441import org.docx4j.wml.ContentAccessor; 
    3542import org.docx4j.wml.Ftr; 
     43import org.docx4j.wml.Hdr; 
     44import org.w3c.dom.Node; 
    3645 
    3746 
     
    6978    }    
    7079                 
     80        private Binder<Node> binder; 
     81         
     82         
     83        /** 
     84         * Enables synchronization between XML infoset nodes and JAXB objects  
     85         * representing same XML document. 
     86         *  
     87         * An instance of this class maintains the association between XML nodes 
     88         * of an infoset preserving view and a JAXB representation of an XML document.  
     89         * Navigation between the two views is provided by the methods  
     90         * getXMLNode(Object) and getJAXBNode(Object) . 
     91         *  
     92         * In theory, modifications can be made to either the infoset preserving view or  
     93         * the JAXB representation of the document while the other view remains 
     94         * unmodified. The binder ought to be able to synchronize the changes made in 
     95         * the modified view back into the other view using the appropriate 
     96         * Binder update methods, #updateXML(Object, Object) or #updateJAXB(Object). 
     97         *  
     98         * But JAXB doesn't currently work as advertised .. access to this 
     99         * object is offered for advanced users on an experimental basis only. 
     100     * @since 2.7.1 
     101         */ 
     102        public Binder<Node> getBinder() { 
     103                 
     104                return binder; 
     105        } 
     106         
     107        /** 
     108         * Fetch JAXB Nodes matching an XPath (for example "//w:p"). 
     109         *  
     110         * If you have modified your JAXB objects (eg added or changed a  
     111         * w:p paragraph), you need to update the association. The problem 
     112         * is that this can only be done ONCE, owing to a bug in JAXB: 
     113         * see https://jaxb.dev.java.net/issues/show_bug.cgi?id=459 
     114         *  
     115         * So this is left for you to choose to do via the refreshXmlFirst parameter.    
     116         *  
     117         * @param xpathExpr 
     118         * @param refreshXmlFirst 
     119         * @return 
     120         * @throws JAXBException 
     121     * @since 2.7.1 
     122         */      
     123        public List<Object> getJAXBNodesViaXPath(String xpathExpr, boolean refreshXmlFirst)  
     124                        throws JAXBException { 
     125                 
     126                return XmlUtils.getJAXBNodesViaXPath(binder, jaxbElement, xpathExpr, refreshXmlFirst); 
     127        }        
     128 
     129        /** 
     130         * Fetch JAXB Nodes matching an XPath (for example ".//w:p" - note the dot, 
     131         * which is necessary for this sort of relative path). 
     132         *  
     133         * If you have modified your JAXB objects (eg added or changed a  
     134         * w:p paragraph), you need to update the association. The problem 
     135         * is that this can only be done ONCE, owing to a bug in JAXB: 
     136         * see https://jaxb.dev.java.net/issues/show_bug.cgi?id=459 
     137         *  
     138         * So this is left for you to choose to do via the refreshXmlFirst parameter.    
     139 
     140         * @param xpathExpr 
     141         * @param someJaxbElement 
     142         * @param refreshXmlFirst 
     143         * @return 
     144         * @throws JAXBException 
     145     * @since 2.7.1 
     146         */ 
     147        public List<Object> getJAXBNodesViaXPath(String xpathExpr, Object someJaxbElement, boolean refreshXmlFirst)  
     148                throws JAXBException { 
     149 
     150                return XmlUtils.getJAXBNodesViaXPath(binder, someJaxbElement, xpathExpr, refreshXmlFirst); 
     151        }        
     152         
     153         
     154    /** 
     155     * Unmarshal XML data from the specified InputStream and return the  
     156     * resulting content tree.  Validation event location information may 
     157     * be incomplete when using this form of the unmarshal API. 
     158     * 
     159     * <p> 
     160     * Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>. 
     161     *  
     162     * @param is the InputStream to unmarshal XML data from 
     163     * @return the newly created root object of the java content tree  
     164     * 
     165     * @throws JAXBException  
     166     *     If any unexpected errors occur while unmarshalling 
     167     * @since 2.7.1 
     168     */ 
     169        @Override 
     170    public Ftr unmarshal( java.io.InputStream is ) throws JAXBException { 
     171                try { 
     172                         
     173                        log.info("For MDP, unmarshall via binder"); 
     174                        // InputStream to Document 
     175                        javax.xml.parsers.DocumentBuilderFactory dbf  
     176                                = DocumentBuilderFactory.newInstance(); 
     177                        dbf.setNamespaceAware(true); 
     178                        org.w3c.dom.Document doc = dbf.newDocumentBuilder().parse(is); 
     179 
     180                        //  
     181                        binder = jc.createBinder(); 
     182                         
     183                        JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler(); 
     184                        eventHandler.setContinue(false); 
     185                        binder.setEventHandler(eventHandler); 
     186                         
     187                        try { 
     188                                jaxbElement =  (Ftr) binder.unmarshal( doc ); 
     189                        } catch (UnmarshalException ue) { 
     190                                log.info("encountered unexpected content; pre-processing"); 
     191                                eventHandler.setContinue(true); 
     192                                DOMResult result = new DOMResult(); 
     193                                Templates mcPreprocessorXslt = JaxbValidationEventHandler.getMcPreprocessor(); 
     194                                XmlUtils.transform(doc, mcPreprocessorXslt, null, result); 
     195                                doc = (org.w3c.dom.Document)result.getNode(); 
     196                                jaxbElement =  (Ftr) binder.unmarshal( doc );                                    
     197                        } 
     198                         
     199                        return jaxbElement; 
     200                         
     201                } catch (Exception e ) { 
     202                        e.printStackTrace(); 
     203                        return null; 
     204                } 
     205    } 
     206 
     207    /** 
     208     * @since 2.7.1 
     209     */  
     210        @Override 
     211    public Ftr unmarshal(org.w3c.dom.Element el) throws JAXBException { 
     212 
     213                try { 
     214 
     215                        binder = jc.createBinder(); 
     216                        JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler(); 
     217                        eventHandler.setContinue(false); 
     218                        binder.setEventHandler(eventHandler); 
     219                         
     220                        try { 
     221                                jaxbElement =  (Ftr) binder.unmarshal( el ); 
     222                        } catch (UnmarshalException ue) { 
     223                                log.info("encountered unexpected content; pre-processing"); 
     224                                try { 
     225                                        org.w3c.dom.Document doc; 
     226                                        if (el instanceof org.w3c.dom.Document) { 
     227                                                doc = (org.w3c.dom.Document) el; 
     228                                        } else { 
     229                                                // Hope for the best. Dodgy though; what if this is 
     230                                                // being used on something deep in the tree? 
     231                                                // TODO: revisit 
     232                                                doc = el.getOwnerDocument(); 
     233                                        } 
     234                                        eventHandler.setContinue(true); 
     235                                        DOMResult result = new DOMResult(); 
     236                                        Templates mcPreprocessorXslt = JaxbValidationEventHandler 
     237                                                        .getMcPreprocessor(); 
     238                                        XmlUtils.transform(doc, mcPreprocessorXslt, null, result); 
     239                                        doc = (org.w3c.dom.Document) result.getNode(); 
     240                                        jaxbElement = (Ftr) binder 
     241                                                        .unmarshal(doc); 
     242                                } catch (Exception e) { 
     243                                        throw new JAXBException("Preprocessing exception", e); 
     244                                } 
     245                        } 
     246                        return jaxbElement; 
     247                         
     248                } catch (JAXBException e) { 
     249                        log.error(e); 
     250                        throw e; 
     251                } 
     252        } 
     253         
     254         
     255         
    71256} 
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/HeaderPart.java

    r1505 r1613  
    2626import javax.xml.bind.Binder; 
    2727import javax.xml.bind.JAXBException; 
     28import javax.xml.bind.UnmarshalException; 
    2829import javax.xml.bind.Unmarshaller; 
    2930import javax.xml.parsers.DocumentBuilderFactory; 
     31import javax.xml.transform.Templates; 
     32import javax.xml.transform.dom.DOMResult; 
    3033 
    3134import org.apache.log4j.Logger; 
    3235import org.docx4j.XmlUtils; 
     36import org.docx4j.jaxb.JaxbValidationEventHandler; 
    3337import org.docx4j.openpackaging.exceptions.InvalidFormatException; 
    3438import org.docx4j.openpackaging.parts.JaxbXmlPart; 
     
    160164        @Override 
    161165    public Hdr unmarshal( java.io.InputStream is ) throws JAXBException { 
    162          
    163166                try { 
    164167                         
     168                        log.info("For MDP, unmarshall via binder"); 
    165169                        // InputStream to Document 
    166170                        javax.xml.parsers.DocumentBuilderFactory dbf  
     
    172176                        binder = jc.createBinder(); 
    173177                         
    174                         binder.setEventHandler( 
    175                                         new org.docx4j.jaxb.JaxbValidationEventHandler()); 
    176                          
    177                         jaxbElement =  (Hdr) binder.unmarshal( doc ); 
    178                          
    179 ////                    if (jc==null) { 
    180 ////                            setJAXBContext(Context.jc);                              
    181 ////                    } 
    182 //                                   
    183 //                      Unmarshaller u = jc.createUnmarshaller(); 
    184 //                       
    185 //                      //u.setSchema(org.docx4j.jaxb.WmlSchema.schema); 
    186 //                      u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler()); 
    187 // 
    188 //                      log.info("unmarshalling " + this.getClass().getName() + " \n\n" );                                                                       
    189 //                                               
    190 //                      jaxbElement = (Hdr) u.unmarshal( is ); 
    191 //                                                       
    192 //                      log.info("\n\n" + this.getClass().getName() + " unmarshalled \n\n" );                                                                    
    193  
     178                        JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler(); 
     179                        eventHandler.setContinue(false); 
     180                        binder.setEventHandler(eventHandler); 
     181                         
     182                        try { 
     183                                jaxbElement =  (Hdr) binder.unmarshal( doc ); 
     184                        } catch (UnmarshalException ue) { 
     185                                log.info("encountered unexpected content; pre-processing"); 
     186                                eventHandler.setContinue(true); 
     187                                DOMResult result = new DOMResult(); 
     188                                Templates mcPreprocessorXslt = JaxbValidationEventHandler.getMcPreprocessor(); 
     189                                XmlUtils.transform(doc, mcPreprocessorXslt, null, result); 
     190                                doc = (org.w3c.dom.Document)result.getNode(); 
     191                                jaxbElement =  (Hdr) binder.unmarshal( doc );                                    
     192                        } 
     193                         
     194                        return jaxbElement; 
     195                         
    194196                } catch (Exception e ) { 
    195197                        e.printStackTrace(); 
     198                        return null; 
    196199                } 
    197          
    198                 return jaxbElement; 
    199          
    200200    } 
     201 
     202    /** 
     203     * @since 2.7.1 
     204     */          
     205        @Override 
     206    public Hdr unmarshal(org.w3c.dom.Element el) throws JAXBException { 
     207 
     208                try { 
     209 
     210                        binder = jc.createBinder(); 
     211                        JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler(); 
     212                        eventHandler.setContinue(false); 
     213                        binder.setEventHandler(eventHandler); 
     214                         
     215                        try { 
     216                                jaxbElement =  (Hdr) binder.unmarshal( el ); 
     217                        } catch (UnmarshalException ue) { 
     218                                log.info("encountered unexpected content; pre-processing"); 
     219                                try { 
     220                                        org.w3c.dom.Document doc; 
     221                                        if (el instanceof org.w3c.dom.Document) { 
     222                                                doc = (org.w3c.dom.Document) el; 
     223                                        } else { 
     224                                                // Hope for the best. Dodgy though; what if this is 
     225                                                // being used on something deep in the tree? 
     226                                                // TODO: revisit 
     227                                                doc = el.getOwnerDocument(); 
     228                                        } 
     229                                        eventHandler.setContinue(true); 
     230                                        DOMResult result = new DOMResult(); 
     231                                        Templates mcPreprocessorXslt = JaxbValidationEventHandler 
     232                                                        .getMcPreprocessor(); 
     233                                        XmlUtils.transform(doc, mcPreprocessorXslt, null, result); 
     234                                        doc = (org.w3c.dom.Document) result.getNode(); 
     235                                        jaxbElement = (Hdr) binder 
     236                                                        .unmarshal(doc); 
     237                                } catch (Exception e) { 
     238                                        throw new JAXBException("Preprocessing exception", e); 
     239                                } 
     240                        } 
     241                        return jaxbElement; 
     242                         
     243                } catch (JAXBException e) { 
     244                        log.error(e); 
     245                        throw e; 
     246                } 
     247        } 
     248         
     249         
    201250         
    202251} 
Note: See TracChangeset for help on using the changeset viewer.