Changeset 1609 for trunk/docx4j/src/main/java
- Timestamp:
- 07/10/11 04:43:04 (11 months ago)
- Location:
- trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML
- Files:
-
- 2 edited
-
MainDocumentPart.java (modified) (3 diffs)
-
NumberingDefinitionsPart.java (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/MainDocumentPart.java
r1605 r1609 282 282 jaxbElement = (org.docx4j.wml.Document) binder.unmarshal( doc ); 283 283 } catch (UnmarshalException ue) { 284 285 // mimic docx4j 2.7.0 and earlier behaviour 284 log.info("encountered unexpected content; pre-processing"); 285 /* Always try our preprocessor, since if what is first encountered is 286 * eg: 287 * 288 <w14:glow w14:rad="101600"> ... 289 * 290 * the error would be: 291 * 292 * unexpected element (uri:"http://schemas.microsoft.com/office/word/2010/wordml", local:"glow") 293 * 294 * but there could well be mc:AlternateContent somewhere 295 * further down in the document. 296 */ 297 298 // mimic docx4j 2.7.0 and earlier behaviour; this will 299 // drop w14:glow etc; the preprocessor doesn't need to 300 // do that 286 301 eventHandler.setContinue(true); 287 302 288 if (ue.getMessage().contains(JaxbValidationEventHandler.UNEXPECTED_MC_ALTERNATE_CONTENT)) { 289 // Try our preprocessor 290 log.info("encountered mc:AlternateContent; pre-processing"); 303 // There is no JAXBResult(binder), 304 // so use a 305 DOMResult result = new DOMResult(); 306 307 Templates mcPreprocessorXslt = JaxbValidationEventHandler.getMcPreprocessor(); 308 XmlUtils.transform(doc, mcPreprocessorXslt, null, result); 309 310 doc = (org.w3c.dom.Document)result.getNode(); 291 311 292 // There is no JAXBResult(binder),293 // so use a294 DOMResult result = new DOMResult();295 296 Templates mcPreprocessorXslt = JaxbValidationEventHandler.getMcPreprocessor();297 XmlUtils.transform(doc, mcPreprocessorXslt, null, result);298 299 doc = (org.w3c.dom.Document)result.getNode();300 301 } else {302 log.error(ue);303 log.info("trying again; likely attribute/element loss");304 }305 312 jaxbElement = (org.docx4j.wml.Document) binder.unmarshal( doc ); 306 313 … … 308 315 309 316 return jaxbElement; 310 /*311 Unmarshaller u = jc.createUnmarshaller();312 313 //u.setSchema(org.docx4j.jaxb.WmlSchema.schema);314 u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler());315 316 // JAXBElement<?> root = (JAXBElement<?>)u.unmarshal( is );317 // jaxbElement = (org.docx4j.wml.Document)root.getValue();318 319 jaxbElement = (org.docx4j.wml.Document) u.unmarshal( is );320 return jaxbElement;321 322 //System.out.println("\n\n" + this.getClass().getName() + " unmarshalled \n\n" );323 */324 317 325 318 } catch (Exception e ) { … … 334 327 335 328 binder = jc.createBinder(); 336 binder.setEventHandler( 337 new org.docx4j.jaxb.JaxbValidationEventHandler()); 338 jaxbElement = (org.docx4j.wml.Document) binder.unmarshal( el ); 339 329 JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler(); 330 eventHandler.setContinue(false); 331 binder.setEventHandler(eventHandler); 332 333 try { 334 jaxbElement = (org.docx4j.wml.Document) binder.unmarshal( el ); 335 } catch (UnmarshalException ue) { 336 log.info("encountered unexpected content; pre-processing"); 337 try { 338 org.w3c.dom.Document doc; 339 if (el instanceof org.w3c.dom.Document) { 340 doc = (org.w3c.dom.Document) el; 341 } else { 342 // Hope for the best. Dodgy though; what if this is 343 // being used on something deep in the tree? 344 // TODO: revisit 345 doc = el.getOwnerDocument(); 346 } 347 eventHandler.setContinue(true); 348 DOMResult result = new DOMResult(); 349 Templates mcPreprocessorXslt = JaxbValidationEventHandler 350 .getMcPreprocessor(); 351 XmlUtils.transform(doc, mcPreprocessorXslt, null, result); 352 doc = (org.w3c.dom.Document) result.getNode(); 353 jaxbElement = (org.docx4j.wml.Document) binder 354 .unmarshal(doc); 355 } catch (Exception e) { 356 throw new JAXBException("Preprocessing exception", e); 357 } 358 } 340 359 return jaxbElement; 341 360 342 361 } catch (JAXBException e) { 343 // e.printStackTrace();344 362 log.error(e); 345 363 throw e; -
trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/NumberingDefinitionsPart.java
r1382 r1609 22 22 23 23 24 import java.io.IOException; 24 25 import java.math.BigInteger; 25 26 import java.util.HashMap; 26 27 27 28 import javax.xml.bind.JAXBException; 29 import javax.xml.bind.UnmarshalException; 28 30 import javax.xml.bind.Unmarshaller; 29 31 import javax.xml.bind.util.JAXBResult; 32 import javax.xml.transform.Templates; 33 import javax.xml.transform.dom.DOMResult; 34 import javax.xml.transform.stream.StreamSource; 35 36 import org.docx4j.XmlUtils; 30 37 import org.docx4j.jaxb.Context; 38 import org.docx4j.jaxb.JaxbValidationEventHandler; 31 39 import org.docx4j.model.PropertyResolver; 32 40 import org.docx4j.model.listnumbering.AbstractListNumberingDefinition; … … 34 42 import org.docx4j.model.listnumbering.ListLevel; 35 43 import org.docx4j.model.listnumbering.ListNumberingDefinition; 44 import org.docx4j.openpackaging.exceptions.Docx4JException; 36 45 import org.docx4j.openpackaging.exceptions.InvalidFormatException; 37 46 import org.docx4j.openpackaging.exceptions.InvalidOperationException; … … 296 305 } 297 306 298 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 } 299 405 }
Note: See TracChangeset
for help on using the changeset viewer.
