Ignore:
Timestamp:
07/10/11 07:00:04 (11 months ago)
Author:
jharrop
Message:

Move the markup compatibility preprocessing stuff from NumberingDefinitionsPart? to JaxbXmlPart?.

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

Legend:

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

    r1608 r1610  
    2626import javax.xml.bind.JAXBException; 
    2727import javax.xml.bind.Marshaller; 
     28import javax.xml.bind.UnmarshalException; 
    2829import javax.xml.bind.Unmarshaller; 
    2930import javax.xml.bind.util.JAXBResult; 
     31import javax.xml.transform.Templates; 
     32import javax.xml.transform.stream.StreamSource; 
    3033 
    3134import org.apache.log4j.Logger; 
    3235import org.docx4j.XmlUtils; 
    3336import org.docx4j.jaxb.Context; 
     37import org.docx4j.jaxb.JaxbValidationEventHandler; 
    3438import org.docx4j.jaxb.NamespacePrefixMapperUtils; 
    3539import org.docx4j.openpackaging.exceptions.Docx4JException; 
    3640import org.docx4j.openpackaging.exceptions.InvalidFormatException; 
     41import org.docx4j.wml.Numbering; 
    3742 
    3843/** OPC Parts are either XML, or binary (or text) documents. 
     
    217222         
    218223                try { 
    219                          
    220 //                      if (jc==null) { 
    221 //                              setJAXBContext(Context.jc);                              
    222 //                      } 
    223                                      
     224                     
    224225                        Unmarshaller u = jc.createUnmarshaller(); 
    225226                         
    226                         //u.setSchema(org.docx4j.jaxb.WmlSchema.schema); 
    227                         u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler()); 
    228  
    229                         log.info("unmarshalling " + this.getClass().getName() );                                                                         
    230                         jaxbElement = (E) XmlUtils.unwrap( 
    231                                         u.unmarshal( is ));                                              
     227                        JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler(); 
     228                        if (is.markSupported()) { 
     229                                // Only fail hard if we know we can restart 
     230                                eventHandler.setContinue(false); 
     231                        } 
     232                        u.setEventHandler(eventHandler); 
     233                         
     234                        try { 
     235                                jaxbElement = (E) XmlUtils.unwrap( 
     236                                                u.unmarshal( is ));                                              
     237                        } catch (UnmarshalException ue) { 
     238                                 
     239                                if (is.markSupported() ) { 
     240                                        // When reading from zip, we use a ByteArrayInputStream, 
     241                                        // which does support this. 
     242                                 
     243                                        log.info("encountered unexpected content; pre-processing"); 
     244                                        eventHandler.setContinue(true); 
     245                                                                                 
     246                                        try { 
     247                                                Templates mcPreprocessorXslt = JaxbValidationEventHandler.getMcPreprocessor(); 
     248                                                is.reset(); 
     249                                                JAXBResult result = XmlUtils.prepareJAXBResult(Context.jc); 
     250                                                XmlUtils.transform(new StreamSource(is),  
     251                                                                mcPreprocessorXslt, null, result); 
     252                                                jaxbElement = (E) XmlUtils.unwrap( 
     253                                                                result.getResult() );    
     254                                        } catch (Exception e) { 
     255                                                throw new JAXBException("Preprocessing exception", e); 
     256                                        } 
     257                                                                                         
     258                                } else { 
     259                                        log.error(ue); 
     260                                        log.error(".. and mark not supported"); 
     261                                        throw ue; 
     262                                } 
     263                        } 
     264                         
    232265 
    233266                } catch (JAXBException e ) { 
     
    239272         
    240273    }    
    241  
     274     
    242275    public E unmarshal(org.w3c.dom.Element el) throws JAXBException { 
    243276 
     
    245278 
    246279                        Unmarshaller u = jc.createUnmarshaller(); 
    247                                                  
    248                         u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler()); 
    249  
    250                         jaxbElement = (E) XmlUtils.unwrap( 
    251                                         u.unmarshal( el ) ); 
    252  
     280                        JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler(); 
     281                        eventHandler.setContinue(false); 
     282                        u.setEventHandler(eventHandler); 
     283                         
     284                        try { 
     285                                jaxbElement = (E) XmlUtils.unwrap( 
     286                                                u.unmarshal( el ) ); 
     287                        } catch (UnmarshalException ue) { 
     288                                log.info("encountered unexpected content; pre-processing"); 
     289                                try { 
     290                                        org.w3c.dom.Document doc; 
     291                                        if (el instanceof org.w3c.dom.Document) { 
     292                                                doc = (org.w3c.dom.Document) el; 
     293                                        } else { 
     294                                                // Hope for the best. Dodgy though; what if this is 
     295                                                // being used on something deep in the tree? 
     296                                                // TODO: revisit 
     297                                                doc = el.getOwnerDocument(); 
     298                                        } 
     299                                        eventHandler.setContinue(true); 
     300                                        JAXBResult result = XmlUtils.prepareJAXBResult(Context.jc); 
     301                                        Templates mcPreprocessorXslt = JaxbValidationEventHandler 
     302                                                        .getMcPreprocessor(); 
     303                                        XmlUtils.transform(doc, mcPreprocessorXslt, null, result); 
     304                                        jaxbElement = (E) XmlUtils.unwrap( 
     305                                                        result.getResult() );    
     306                                } catch (Exception e) { 
     307                                        throw new JAXBException("Preprocessing exception", e); 
     308                                } 
     309                        } 
    253310                        return jaxbElement; 
    254311                         
    255312                } catch (JAXBException e) { 
    256 //                      e.printStackTrace(); 
    257313                        log.error(e); 
    258314                        throw e; 
    259315                } 
    260         } 
     316        }        
     317     
    261318 
    262319    public boolean isContentEqual(Part other) throws Docx4JException { 
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/NumberingDefinitionsPart.java

    r1609 r1610  
    305305        } 
    306306         
    307         @Override 
    308     public Numbering unmarshal( java.io.InputStream is ) throws JAXBException { 
    309          
    310                 try { 
    311                                                              
    312                         Unmarshaller u = jc.createUnmarshaller(); 
    313                          
    314                         JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler(); 
    315                         if (is.markSupported()) { 
    316                                 // Only fail hard if we know we can restart 
    317                                 eventHandler.setContinue(false); 
    318                         } 
    319                         u.setEventHandler(eventHandler); 
    320                          
    321                         try { 
    322                                 jaxbElement = (Numbering) XmlUtils.unwrap( 
    323                                                 u.unmarshal( is ));      
    324                         } catch (UnmarshalException ue) { 
    325                                  
    326                                 if (is.markSupported() ) { 
    327                                         // When reading from zip, we use a ByteArrayInputStream, 
    328                                         // which does support this. 
    329                                  
    330                                         log.info("encountered unexpected content; pre-processing"); 
    331                                         eventHandler.setContinue(true); 
    332                                                                                  
    333                                         try { 
    334                                                 Templates mcPreprocessorXslt = JaxbValidationEventHandler.getMcPreprocessor(); 
    335                                                 is.reset(); 
    336                                                 JAXBResult result = XmlUtils.prepareJAXBResult(Context.jc); 
    337                                                 XmlUtils.transform(new StreamSource(is),  
    338                                                                 mcPreprocessorXslt, null, result); 
    339                                                 jaxbElement = (Numbering) XmlUtils.unwrap( 
    340                                                                 result.getResult() );    
    341                                         } catch (Exception e) { 
    342                                                 throw new JAXBException("Preprocessing exception", e); 
    343                                         } 
    344                                                                                          
    345                                 } else { 
    346                                         log.error(ue); 
    347                                         log.error(".. and mark not supported"); 
    348                                         throw ue; 
    349                                 } 
    350                         } 
    351                          
    352  
    353                 } catch (JAXBException e ) { 
    354                         log.error(e); 
    355                         throw e; 
    356                 } 
    357          
    358                 return jaxbElement; 
    359          
    360     }    
    361  
    362         @Override 
    363     public Numbering unmarshal(org.w3c.dom.Element el) throws JAXBException { 
    364  
    365                 try { 
    366  
    367                         Unmarshaller u = jc.createUnmarshaller(); 
    368                         JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler(); 
    369                         eventHandler.setContinue(false); 
    370                         u.setEventHandler(eventHandler); 
    371                          
    372                         try { 
    373                                 jaxbElement = (Numbering) XmlUtils.unwrap( 
    374                                                 u.unmarshal( el ) ); 
    375                         } catch (UnmarshalException ue) { 
    376                                 log.info("encountered unexpected content; pre-processing"); 
    377                                 try { 
    378                                         org.w3c.dom.Document doc; 
    379                                         if (el instanceof org.w3c.dom.Document) { 
    380                                                 doc = (org.w3c.dom.Document) el; 
    381                                         } else { 
    382                                                 // Hope for the best. Dodgy though; what if this is 
    383                                                 // being used on something deep in the tree? 
    384                                                 // TODO: revisit 
    385                                                 doc = el.getOwnerDocument(); 
    386                                         } 
    387                                         eventHandler.setContinue(true); 
    388                                         JAXBResult result = XmlUtils.prepareJAXBResult(Context.jc); 
    389                                         Templates mcPreprocessorXslt = JaxbValidationEventHandler 
    390                                                         .getMcPreprocessor(); 
    391                                         XmlUtils.transform(doc, mcPreprocessorXslt, null, result); 
    392                                         jaxbElement = (Numbering) XmlUtils.unwrap( 
    393                                                         result.getResult() );    
    394                                 } catch (Exception e) { 
    395                                         throw new JAXBException("Preprocessing exception", e); 
    396                                 } 
    397                         } 
    398                         return jaxbElement; 
    399                          
    400                 } catch (JAXBException e) { 
    401                         log.error(e); 
    402                         throw e; 
    403                 } 
    404         }        
     307 
    405308} 
Note: See TracChangeset for help on using the changeset viewer.