Page 1 of 1

ClassCastException with JAXB binder unmarshal

PostPosted: Fri Dec 30, 2011 8:56 am
by desau
Hi all,

Some background:
I have a project which reads docx files, makes changes to the doc (changes/adds text, images, etc), then writes the changes out to a new docx file.
I've been using docx4j 2.5.0 for a little over a year now, it's been working great, very impressed with how well this library works and how easy it is to use.

Moving forward -- I need to start to support Word 2010 content, in particular mc:AlternateContent. I think using the mc:Fallback content is sufficient, which I see was added into 2.7.1. So far, so good.

So now I'm trying to migrate from 2.5.0 to 2.7.1.

A couple questions:
1) for 2.5.0, I had to have parent-pointer-plugin in my classpath .. I don't see that as part of the required libraries of docx4j anymore -- can I rip that out?

2) I did rip it out, and I'm not getting any ClassNotFoundExceptions (yet). However -- when I read a docx in, I'm getting the following exception.

I've got the docx content saved into a database in its original zipped format, so to read it, I use this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
    LoadFromZipNG loader = new LoadFromZipNG();
    return (WordprocessingMLPackage)loader.get(new ByteArrayInputStream(docx));
 
Parsed in 0.014 seconds, using GeSHi 1.0.8.4


When I call loader.get(), I'm getting the following ClassCastException in the log. However, that exception isn't being thrown, nor is any exception being thrown -- it's just being logged and I'm getting a WordprocessingMLPackage returned. So -- I'm not sure if this exception is a problem or not. I'm having a subsequent problem, but I'm not sure if it's related to this, or a problem with my code. I'll attach the full log of the docx load in case it's of any use.

Code: Select all
java.lang.ClassCastException: org.docx4j.wml.PPr cannot be cast to javax.xml.bind.JAXBElement
   at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl$IntercepterLoader.intercept(ElementBeanInfoImpl.java:199)
   at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.endElement(UnmarshallingContext.java:437)
   at com.sun.xml.bind.v2.runtime.unmarshaller.InterningXmlVisitor.endElement(InterningXmlVisitor.java:41)
   at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.endElement(SAXConnector.java:109)
   at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:235)
   at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:261)
   at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:230)
   at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:261)
   at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:230)
   at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:261)
   at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:230)
   at com.sun.xml.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:107)
   at com.sun.xml.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:90)
   at com.sun.xml.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:83)
   at com.sun.xml.bind.v2.runtime.BinderImpl.associativeUnmarshal(BinderImpl.java:140)
   at com.sun.xml.bind.v2.runtime.BinderImpl.unmarshal(BinderImpl.java:111)
   at org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart.unmarshal(HeaderPart.java:197)
   at org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart.unmarshal(HeaderPart.java:48)
   at org.docx4j.openpackaging.io.LoadFromZipNG.getRawPart(LoadFromZipNG.java:551)
   at org.docx4j.openpackaging.io.LoadFromZipNG.getPart(LoadFromZipNG.java:427)
   at org.docx4j.openpackaging.io.LoadFromZipNG.addPartsFromRelationships(LoadFromZipNG.java:350)
   at org.docx4j.openpackaging.io.LoadFromZipNG.getPart(LoadFromZipNG.java:449)
   at org.docx4j.openpackaging.io.LoadFromZipNG.addPartsFromRelationships(LoadFromZipNG.java:350)
   at org.docx4j.openpackaging.io.LoadFromZipNG.process(LoadFromZipNG.java:243)
   at org.docx4j.openpackaging.io.LoadFromZipNG.get(LoadFromZipNG.java:193)


Thanks for any advice!

Re: ClassCastException reading docx from Byte Array

PostPosted: Fri Dec 30, 2011 9:09 am
by jason
Hi, thanks for the kind words.

You don't need the parent-pointer-plugin, since it is now in the source tree.

That class cast exception shouldn't be happening. As you can see from the stack trace, it is occurring when one of your headers is being unmarshalled.

Code: Select all
11:51:05,292 WARN  [Thread-50] [JaxbValidationEventHandler] [ERROR] : unexpected element (uri:"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing", local:"sizeRelH"). Expect
11:51:05,292 INFO  [Thread-50] [JaxbValidationEventHandler] continuing (with possible element/attribute loss)
11:51:05,293 INFO  [Thread-50] [HeaderPart] encountered unexpected content; pre-processing
11:51:05,375 WARN  [Thread-50] [XSLTUtils] Found some mc:AlternateContent
11:51:05,375 WARN  [Thread-50] [XSLTUtils] Selecting w:pict
java.lang.ClassCastException: org.docx4j.wml.PPr cannot be cast to javax.xml.bind.JAXBElement


Could you please attach a document which exhibits this issue?

Re: ClassCastException reading docx from Byte Array

PostPosted: Fri Dec 30, 2011 9:27 am
by desau
Sure - here you are.

I trimmed out as much as I could while keeping the problem. Looks like it's due to an image in the header.

Re: ClassCastException reading docx from Byte Array

PostPosted: Fri Dec 30, 2011 3:44 pm
by jason
It turns out there is a workaround in place for this issue in MainDocumentPart.

I've replicated that workaround for headers and footers: http://www.docx4java.org/trac/docx4j/changeset/1736

I've also reported the underlying JAXB issue: http://java.net/jira/browse/JAXB-874

Re: ClassCastException with JAXB binder unmarshal

PostPosted: Sat Dec 31, 2011 5:53 am
by desau
Awesome! I'll grab the latest nightly and try it out.

Thanks!!

Re: ClassCastException with JAXB binder unmarshal

PostPosted: Sat Dec 31, 2011 6:34 am
by desau
Hmmm. Looks like the latest nightly is from 3 days ago. Is this built automatically every 24 hours?

Re: ClassCastException with JAXB binder unmarshal

PostPosted: Sat Dec 31, 2011 1:29 pm
by jason
Nope, its a manual process. I do a "nightly" when it makes sense to do so. I'll upload one later today.
cheers .. Jason

Re: ClassCastException with JAXB binder unmarshal

PostPosted: Sat Dec 31, 2011 1:48 pm
by desau
Great, I'll keep an eye out.

I tried building from the source but had problems (with both ant and maven builds).