Changeset 1610 for trunk/docx4j/src/main/java
- Timestamp:
- 07/10/11 07:00:04 (11 months ago)
- Location:
- trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts
- Files:
-
- 2 edited
-
JaxbXmlPart.java (modified) (4 diffs)
-
WordprocessingML/NumberingDefinitionsPart.java (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/JaxbXmlPart.java
r1608 r1610 26 26 import javax.xml.bind.JAXBException; 27 27 import javax.xml.bind.Marshaller; 28 import javax.xml.bind.UnmarshalException; 28 29 import javax.xml.bind.Unmarshaller; 29 30 import javax.xml.bind.util.JAXBResult; 31 import javax.xml.transform.Templates; 32 import javax.xml.transform.stream.StreamSource; 30 33 31 34 import org.apache.log4j.Logger; 32 35 import org.docx4j.XmlUtils; 33 36 import org.docx4j.jaxb.Context; 37 import org.docx4j.jaxb.JaxbValidationEventHandler; 34 38 import org.docx4j.jaxb.NamespacePrefixMapperUtils; 35 39 import org.docx4j.openpackaging.exceptions.Docx4JException; 36 40 import org.docx4j.openpackaging.exceptions.InvalidFormatException; 41 import org.docx4j.wml.Numbering; 37 42 38 43 /** OPC Parts are either XML, or binary (or text) documents. … … 217 222 218 223 try { 219 220 // if (jc==null) { 221 // setJAXBContext(Context.jc); 222 // } 223 224 224 225 Unmarshaller u = jc.createUnmarshaller(); 225 226 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 232 265 233 266 } catch (JAXBException e ) { … … 239 272 240 273 } 241 274 242 275 public E unmarshal(org.w3c.dom.Element el) throws JAXBException { 243 276 … … 245 278 246 279 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 } 253 310 return jaxbElement; 254 311 255 312 } catch (JAXBException e) { 256 // e.printStackTrace();257 313 log.error(e); 258 314 throw e; 259 315 } 260 } 316 } 317 261 318 262 319 public boolean isContentEqual(Part other) throws Docx4JException { -
trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/NumberingDefinitionsPart.java
r1609 r1610 305 305 } 306 306 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 405 308 }
Note: See TracChangeset
for help on using the changeset viewer.
