Page 1 of 1

docx4j uses ri/jdk even if MOXY enabled?

PostPosted: Tue May 16, 2017 2:48 am
by csekol
HI,

I am bit confused with MOXy and need some help.
We are using docx4j for converting xhtml to docx.

While I was playing around with xhtml import, I got this exception javax.xml.bind.JAXBException: JAXB: neither Reference Implementation nor Java 6 implementation present?
I am using java 7 and 8, so I have no idea how it could happen that jaxb from jre not found. Probably some class loading issue. Our application runs as an osgi bundle. (I am not an osgi expert, just scratching the surface.)

So I decided to give MOXy a try and added the dependencies to pom.xml. My understanding is that if I change to MOXy, It should not look for RI, or jre jaxb implementations at all.

Now I am still getting this exception but from different calls. I could localize 2 call chains:
one is when converting the html to wml objcets. (Called mainly for logging)
Here is the stack trace:
Code: Select all
   at org.docx4j.XmlUtils.marshaltoString(XmlUtils.java:783)
   at org.docx4j.XmlUtils.marshaltoString(XmlUtils.java:666)
   at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.addParagraphProperties(XHTMLImporterImpl.java:2257)
   at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.populatePPr(XHTMLImporterImpl.java:1501)
   at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.getPPr(XHTMLImporterImpl.java:1452)
   at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.traverse(XHTMLImporterImpl.java:1204)
   at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.traverse(XHTMLImporterImpl.java:800)
   at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.convert(XHTMLImporterImpl.java:483)


The other is when saving the docx file calling wordMLPackage.save(file) (After commenting out the previous logging statements).
stack trace:
Code: Select all
   at org.docx4j.jaxb.NamespacePrefixMapperUtils.getPrefixMapper(NamespacePrefixMapperUtils.java:46)
   at org.docx4j.openpackaging.contenttype.ContentTypeManager.marshal(ContentTypeManager.java:829)
   at org.docx4j.openpackaging.io3.stores.ZipPartStore.saveContentTypes(ZipPartStore.java:213)
   at org.docx4j.openpackaging.io3.Save.save(Save.java:176)
   at org.docx4j.openpackaging.packages.OpcPackage.save(OpcPackage.java:693)
   at org.docx4j.openpackaging.packages.OpcPackage.save(OpcPackage.java:582)
   at org.docx4j.openpackaging.packages.OpcPackage.save(OpcPackage.java:565)
   at org.docx4j.openpackaging.packages.OpcPackage.save(OpcPackage.java:554)


I assume if using MOXy, NamespacePrefixMapperUtils.getPrefixMapper() should not be called, or handle MOXy as well. Is it correct?

Could you please help if I misconfigured something?
Do you have any idea how to work around this issue?

Thanks in advance,
László

Re: docx4j uses ri/jdk even if MOXY enabled?

PostPosted: Tue May 16, 2017 11:15 pm
by jason
Enable INFO level logging on org.docx4j.jaxb.Context.

If MOXy is in use, you should see: "MOXy JAXB implementation is in use!"

Re: docx4j uses ri/jdk even if MOXY enabled?

PostPosted: Wed May 17, 2017 10:31 pm
by csekol
It says MOXy is in use:
2017-05-17 13:31:10,657 INFO [pool-18-thread-1] [org.docx4j.jaxb.Context] <clinit> MOXy JAXB implementation is in use!

Re: docx4j uses ri/jdk even if MOXY enabled?

PostPosted: Thu May 18, 2017 11:22 am
by jason
csekol wrote:It says MOXy is in use


OK, so everything should be good. Do you still think there is a problem?

Re: docx4j uses ri/jdk even if MOXY enabled?

PostPosted: Thu May 18, 2017 10:38 pm
by csekol
Yes, I think there is a problem.

I am expecting if I use MOXy, I dont need neither jaxb RI, nor the jre jaxb.
Still when MOXy is enabled, docx4j trying to use them and I am getting the error "JAXB: neither Reference Implementation nor Java 6 implementation present?"

Checking the code in org.docx4j.Context I can see that NamespacePrefixMapperUtils.getPrefixMapper() is called only if MOXy not enabled:
In org.docx4j.Contextstatic initializer:
Code: Select all
      // Diagnostics regarding JAXB implementation
      InputStream jaxbPropsIS=null;
      try {
         // Is MOXy configured?
         jaxbPropsIS = ResourceUtils.getResource("org/docx4j/wml/jaxb.properties");
         log.info("MOXy JAXB implementation intended..");
         jaxbImplementation = JAXBImplementation.ECLIPSELINK_MOXy;
      } catch (Exception e3) {
         log.info("No MOXy JAXB config found; assume not intended..");
         log.debug(e3.getMessage());
      }
      if (jaxbPropsIS==null) {
         // Only probe for other implementations if no MOXy
         try {
            Object namespacePrefixMapper = NamespacePrefixMapperUtils.getPrefixMapper();
            if ( namespacePrefixMapper.getClass().getName().equals("org.docx4j.jaxb.NamespacePrefixMapperSunInternal") ) {
               // Java 6
               log.info("Using Java 6/7 JAXB implementation");
               jaxbImplementation = JAXBImplementation.ORACLE_JRE;

            } else {
               log.info("Using JAXB Reference Implementation");
               jaxbImplementation = JAXBImplementation.REFERENCE;

            }
            
         } catch (JAXBException e) {
            log.error("PANIC! No suitable JAXB implementation available");
            log.error(e.getMessage(), e);
            e.printStackTrace();
         }
      }


From the call stack below you can see it is called also when using MOXy. I think it should not be called. Am I correct?

Cheers,
László

Re: docx4j uses ri/jdk even if MOXY enabled?

PostPosted: Fri May 19, 2017 9:52 am
by jason
NamespacePrefixMapperUtils.getPrefixMapper() is called from elsewhere; see https://github.com/plutext/docx4j/blob/ ... .java#L661

Since you are using MOXy, it is safe to ignore the message. To avoid confusion, I guess it ought to be suppressed

Re: docx4j uses ri/jdk even if MOXY enabled?

PostPosted: Sat May 20, 2017 2:25 am
by csekol
Actually my problem is not the message itself, but the fact it is trying to call that method and that throws an exception.
If you check the 2nd stack trace in my first post, you can see it is called also during save(). In my case since jaxb is not visible, it throws an exception and cannot save the docx.

As I can see, it should not be called at all if MOXy is in use. Or did I misunderstood something?