Page 1 of 1

How to create a document that use MSOffice vml Object

PostPosted: Sat Apr 25, 2009 4:05 am
by btjandra
I would like to create a watermark document. I generate the following microsoft office xsd: com.microsoft.schemas.office.vml, com.microsoft.schemas.office.word,com.microsoft.schemas.office.office,com.microsoft.schemas.office.powerpoint,com.microsoft.schemas.office.office.excel using JAXB. I would like to know how can I integrate into docx4j. Somehow I need to let docx4j know about my JAXBContext before it can marshall or unmarshall. Could you please let me know how to do this? Thanks for your help.

Here's my code:
.......
javax.xml.namespace.QName qname7 = new javax.xml.namespace.QName(
"urn:schemas-microsoft-com:vml", "shapetype");
JAXBElement jeShapeType = this.getJAXBElement(qname7, CTShapetype.class, shapeType);

pict.getAnyAndAny().add(jeShapeType);

javax.xml.namespace.QName qname8 = new javax.xml.namespace.QName(
"urn:schemas-microsoft-com:vml", "shape");
JAXBElement jeShape = this.getJAXBElement(qname8, CTShape.class, shape);
pict.getAnyAndAny().add(jeShape);

but I get an error when save the doucment "wp.save(filename)", here is my error:

javax.xml.bind.MarshalException
- with linked exception:
[javax.xml.bind.JAXBException: com.microsoft.schemas.vml.CTShapetype is not known to this context]
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:304)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:230)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:75)

Re: How to create a document that use MSOffice vml Object

PostPosted: Sat Apr 25, 2009 7:48 am
by jason
See https://dev.plutext.org/trac/docx4j/bro ... ntext.java

Then, as you instantiate your jaxb parts, they'll need to be set to use your context (using the appropriate constructor, or the setJAXBContext method) before you can unmarshal.

Re: How to create a document that use MSOffice vml Object

PostPosted: Sun Apr 26, 2009 9:00 pm
by btjandra
Hi Jason,
I am having problem to do the setJAXBContext, is that possible for you to show me some code snippet so I can see if I did it right.
Here's what I did:

private JAXBContext getMicrosoftJAXBContext()
{
JAXBContext jc=null;
try {
jc = JAXBContext.newInstance("com.microsoft.schemas.vml:com.microsoft.schemas.office.office:com.microsoft.schemas.office.powerpoint:com.microsoft.schemas.office.word:com.miscrosoft.schemeas.excel");
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jc;

}
public Part getHeaderPartForWaterMark(String name) throws Exception
{
PartName partName = new PartName(name);
HeaderPart headerPart = new HeaderPart(partName);
headerPart.setJAXBContext(getMicrosoftJAXBContext());
headerPart.setContentType(
new org.docx4j.openpackaging.contenttype.ContentType(
org.docx4j.openpackaging.contenttype.ContentTypes.WORDPROCESSINGML_HEADER));
Hdr hdr = getWaterMark();
headerPart.setJaxbElement(hdr);

return headerPart;
}

When I run my code I got the following error:

javax.xml.bind.JAXBException: "com.miscrosoft.schemeas.excel" doesnt contain ObjectFactory.class or jaxb.index
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:127)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:132)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:285)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:372)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:337)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:244)
at com.gdais.emtk.word.services.TestScratchDoc.getMicrosoftJAXBContext(TestScratchDoc.java:593)
at com.gdais.emtk.word.services.TestScratchDoc.getHeaderPartForWaterMark(TestScratchDoc.java:605)
at com.gdais.emtk.word.services.TestScratchDoc.main(TestScratchDoc.java:2297)
26.04.2009 12:39:57 *INFO * Part: Loading part /word/header2.xml (RelationshipsPart.java, line 261)


Did I do something wrong for getting the JAXBContext instance?
I am not very good with JAXB. Any tips will be appreciated.


Thanks for you help!
Betty

Re: How to create a document that use MSOffice vml Object

PostPosted: Mon Apr 27, 2009 12:54 am
by jason
Looks OK to me. Does you "com.miscrosoft.schemeas.excel" package contain ObjectFactory.java?

It should have been generated by xjc. Have a look in org.docx4j.wml, and you'll see an example.

Try removing (temporarily) that package from your newInstance statement, and see whether the error still occurs.

Re: How to create a document that use MSOffice vml Object

PostPosted: Mon Apr 27, 2009 4:18 am
by btjandra
Hi Jason,

Yes, the ObjectFactory is in all the packages. If I took out the excel package then it complaint the word package. If I took out the word then it complaint the powerpoint package on and on, if I only have the vml it complaint the vml.

I create a project just to generate the microsoft package and pack it as a jar and included in the application.
Do you see any problem in doing this way?

Thanks!
Betty

Re: How to create a document that use MSOffice vml Object

PostPosted: Mon Apr 27, 2009 6:32 pm
by btjandra
Hi Jason,

I am able to fix the previous problem, but now I am getting another error:

com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 379 counts of IllegalAnnotationExceptions
Two classes have the same XML type name "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}CT_TwipsMeasure". Use @XmlType.name and @XmlType.namespace to assign different names to them.
this problem is related to the following location:
at org.docx4j.wml.CTTwipsMeasure
at protected org.docx4j.wml.CTTwipsMeasure org.docx4j.wml.CTFramesetSplitbar.w
at org.docx4j.wml.CTFramesetSplitbar
at protected org.docx4j.wml.CTFramesetSplitbar org.docx4j.wml.CTFrameset.framesetSplitbar
at org.docx4j.wml.CTFrameset
at protected org.docx4j.wml.CTFrameset org.docx4j.wml.CTWebSettings.frameset
at org.docx4j.wml.CTWebSettings
at public javax.xml.bind.JAXBElement org.docx4j.wml.ObjectFactory.createWebSettings(org.docx4j.wml.CTWebSettings)
at org.docx4j.wml.ObjectFactory
this problem is related to the following location:
at org.openxmlformats.schemas.wordprocessingml._2006.main.CTTwipsMeasure
at protected org.openxmlformats.schemas.wordprocessingml._2006.main.CTTwipsMeasure org.openxmlformats.schemas.wordprocessingml._2006.main.CTFramesetSplitbar.w
at org.openxmlformats.schemas.wordprocessingml._2006.main.CTFramesetSplitbar
at protected org.openxmlformats.schemas.wordprocessingml._2006.main.CTFramesetSplitbar org.openxmlformats.schemas.wordprocessingml._2006.main.CTFrameset.framesetSplitbar
at org.openxmlformats.schemas.wordprocessingml._2006.main.CTFrameset
at protected org.openxmlformats.schemas.wordprocessingml._2006.main.CTFrameset org.openxmlformats.schemas.wordprocessingml._2006.main.CTWebSettings.frameset
at org.openxmlformats.schemas.wordprocessingml._2006.main.CTWebSettings
at public javax.xml.bind.JAXBElement org.openxmlformats.schemas.wordprocessingml._2006.main.ObjectFactory.createWebSettings(org.openxmlformats.schemas.wordprocessingml._2006.main.CTWebSettings)
at org.openxmlformats.schemas.wordprocessingml._2006.main.ObjectFactory
at protected java.util.List org.openxmlformats.schemas.wordprocessingml._2006.main.CTTxbxContent.egBlockLevelElts
at org.openxmlformats.schemas.wordprocessingml._2006.main.CTTxbxContent
at protected org.openxmlformats.schemas.wordprocessingml._2006.main.CTTxbxContent com.microsoft.schemas.vml.CTTextbox.txbxContent
at com.microsoft.schemas.vml.CTTextbox
at public com.microsoft.schemas.vml.CTTextbox com.microsoft.schemas.vml.ObjectFactory.createCTTextbox()
at com.microsoft.schemas.vml.ObjectFactory
Two classes have the same XML type name "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}CT_FramesetSplitbar". Use @XmlType.name and @XmlType.namespace to assign different names to them.
this problem is related to the following location:
at org.docx4j.wml.CTFramesetSplitbar
at protected org.docx4j.wml.CTFramesetSplitbar org.docx4j.wml.CTFrameset.framesetSplitbar
at org.docx4j.wml.CTFrameset
at protected org.docx4j.wml.CTFrameset org.docx4j.wml.CTWebSettings.frameset
at org.docx4j.wml.CTWebSettings
at public javax.xml.bind.JAXBElement org.docx4j.wml.ObjectFactory.createWebSettings(org.docx4j.wml.CTWebSettings)
at org.docx4j.wml.ObjectFactory
this problem is related to the following location:
at org.openxmlformats.schemas.wordprocessingml._2006.main.CTFramesetSplitbar
at protected org.openxmlformats.schemas.wordprocessingml._2006.main.CTFramesetSplitbar org.openxmlformats.schemas.wordprocessingml._2006.main.CTFrameset.framesetSplitbar
at org.openxmlformats.schemas.wordprocessingml._2006.main.CTFrameset
at protected org.openxmlformats.schemas.wordprocessingml._2006.main.CTFrameset org.openxmlformats.schemas.wordprocessingml._2006.main.CTWebSettings.frameset
at org.openxmlformats.schemas.wordprocessingml._2006.main.CTWebSettings
at public javax.xml.bind.JAXBElement org.openxmlformats.schemas.wordprocessingml._2006.main.ObjectFactory.createWebSettings(org.openxmlformats.schemas.wordprocessingml._2006.main.CTWebSettings)
at org.openxmlformats.schemas.wordprocessingml._2006.main.ObjectFactory
at protected java.util.List org.openxmlformats.schemas.wordprocessingml._2006.main.CTTxbxContent.egBlockLevelElts
at org.openxmlformats.schemas.wordprocessingml._2006.main.CTTxbxContent
at protected org.openxmlformats.schemas.wordprocessingml._2006.main.CTTxbxContent com.microsoft.schemas.vml.CTTextbox.txbxContent
at com.microsoft.schemas.vml.CTTextbox
at public com.microsoft.schemas.vml.CTTextbox com.microsoft.schemas.vml.ObjectFactory.createCTTextbox()
at com.microsoft.schemas.vml.ObjectFactory
Two classes have the same XML type name "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}CT_FrameLayout". Use @XmlType.name and @XmlType.namespace to assign different names to them.
this problem is related to the following location:
at org.docx4j.wml.CTFrameLayout
at protected org.docx4j.wml.CTFrameLayout org.docx4j.wml.CTFrameset.frameLayout
at org.docx4j.wml.CTFrameset
at protected org.docx4j.wml.CTFrameset org.docx4j.wml.CTWebSettings.frameset
at org.docx4j.wml.CTWebSettings
at public javax.xml.bind.JAXBElement org.docx4j.wml.ObjectFactory.createWebSettings(org.docx4j.wml.CTWebSettings)
at org.docx4j.wml.ObjectFactory
this problem is related to the following location:
at org.openxmlformats.schemas.wordprocessingml._2006.main.CTFrameLayout

I think this problem come from there is a duplicate object, what happen is when I tried to run the xjc on the microsoft xsd with the xsd from docx4j I am not able to compile, I am getting this error:

ERROR] XJC while parsing schema(s) file:/D:/workspace/word-beans/src/main/resources/dml-picture.xsd[74,35]: org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'a:CT_NonVisualDrawingProps' to a(n) 'type definition' component.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:236)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:172)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:382)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaError(XSDHandler.java:2241)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getGlobalDecl(XSDHandler.java:1269)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDElementTraverser.traverseNamedElement(XSDElementTraverser.java:376)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDElementTraverser.traverseLocal(XSDElementTraverser.java:214)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.traverseLocalElements(XSDHandler.java:1781)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(XSDHandler.java:484)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(XMLSchemaLoader.java:556)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:523)
at com.sun.org.apache.xerces.internal.jaxp.validation.xs.SchemaFactoryImpl.newSchema(SchemaFactoryImpl.java:206)
at com.sun.tools.xjc.reader.xmlschema.parser.SchemaConstraintChecker.check(SchemaConstraintChecker.java:85)
at com.sun.tools.xjc.ModelLoader.loadXMLSchema(ModelLoader.java:341)
at com.sun.tools.xjc.ModelLoader.load(ModelLoader.java:151)
at com.sun.tools.xjc.ModelLoader.load(ModelLoader.java:97)
at org.jvnet.jaxb2.maven2.AbstractXJC2Mojo.runXJC(AbstractXJC2Mojo.java:457)
at org.jvnet.jaxb2.maven2.AbstractXJC2Mojo.executeImp(AbstractXJC2Mojo.java:132)
at org.jvnet.jaxb2.maven2.XJC2Mojo.execute(XJC2Mojo.java:373)
at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:420)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:539)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifecycleExecutor.java:480)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.forkProjectLifecycle(DefaultLifecycleExecutor.java:896)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.forkLifecycle(DefaultLifecycleExecutor.java:739)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:510)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:493)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:463)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:311)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:278)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:143)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:330)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:123)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:272)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
at org.codehaus.classworlds.Launcher.main(Launcher.java:375)

[ERROR] XJC while parsing schema(s) file:/D:/workspace/word-beans/src/main/resources/dml-picture.xsd[76,74]: org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'a:CT_NonVisualPictureProperties' to a(n) 'type definition' component.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:236)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:172)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:382)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaError(XSDHandler.java:2241)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getGlobalDecl(XSDHandler.java:1269)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDElementTraverser.traverseNamedElement(XSDElementTraverser.java:376)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDElementTraverser.traverseLocal(XSDElementTraverser.java:214)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.traverseLocalElements(XSDHandler.java:1781)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(XSDHandler.java:484)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(XMLSchemaLoader.java:556)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:523)


So, I download the openXML xsd to include microsoft xsd and it generated all the classes, but because your xsd forces it generate the package to use org.docx4j.wml, and it cause this problem I think. Could you please help me out as to how I can use your xsd to generate and include microsoft xsd.

When I look at the the docx4j project on the xsd directory, there is an error in dml-graphicalobject.xsd, I think there is a circular dependencies.
Please help!!!

Thanks!
Betty

Re: How to create a document that use MSOffice vml Object

PostPosted: Tue Apr 28, 2009 1:08 am
by jason
What if you generate classes from the xsd's you downloaded, but instruct it to use org.docx4j.wml and org.docx4j.dml package names? see inside the wml xsd for how to do this ... jaxb:package annotation.

Then replace org.docx4j.wml.* with the classes you generated.

You will still have an issue, which is that my version of the wml schema modifies some of the generated class names eg so we have PPr.java, not CT_PPr.java.

So either you will need to change any references in docx4j to use your class names, or stick with the docx4j names, and modify any references in your pptx, xslx packages. If there is just a few, this could be easy.

Or, as you say, you could use the xsd included in docx4j. This might be easier. I've never seen the error you mention (but I generate the wml and dml classes using separate runs of xjc .. running xjc on wml.xsd generates a subset of the dml stuff (ie the subset that wml.xsd imports) - see comments in dml-stylesheet.xsd .. its a little messy. In the comments at the top of wml.xsd, you can see which version of xjc I use to do the build.

ERROR] XJC while parsing schema(s) file:/D:/workspace/word-beans/src/main/resources/dml-picture.xsd[74,35]: org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'a:CT_NonVisualDrawingProps' to a(n) 'type definition' component.


This looks like an xsd:include|import pointing at an incorrect file path. If you can ensure your downloaded pptx, vml, and xslx point at the existing docx4j schemas everything should just work.

What version of JAXB are you using, and where did you download the schemas from?

Re: How to create a document that use MSOffice vml Object

PostPosted: Tue Apr 28, 2009 3:58 am
by btjandra
Hi Jason,
Thanks for your help. I am good now things are working for what I want to do. I am able to generate the Document with watermark.

What I did was I added <jaxb:package name="org.docx4j.wml" to several of my xsd. That get rid off the openxml package and this take care of my problems.

Thanks!
Betty