views:

1325

answers:

1

I have been trying to do this as the last stage in a standalone application to convert from the file format used by a modeling program to an EMF model. I am able to convert the original format to XSD, which I can manually convert to an EMF model using the Eclipse importer, but I do not know how to do this programmatically to automate the process. Java commands would work fine, as would any command-line statement to do the same, since I could just execute the statement from within Java. I have spent a while looking for how to do this, trying http://wiki.eclipse.org/Generating_Dynamic_Ecore_from_XML_Schema and a variety of other potential solutions, but nothing seems to work. If anyone might be able to provide some sample code as to how to generate the .ecore and(/or?) .genmodel files from an XSD file, I'd very much appreciate it, but even some guidance as to how I can proceed would help very much.

Thank you.

A: 

Take a look at the class org.eclipse.xsd.ecore.XSDEcoreBuilder and the way it's used by the Eclipse importer wizard.
Seems to be fairly straightforward to use, you simply call one of its generate methods and you get back either a Collection<Resource> or a Collection<EObject>.

(Edit: answering additional questions in comments)
The EPackage class is the Ecore equivalent of xs:schema, which contains the EClasses, which are in turn the Ecore equivalents of xs:complexTypes.

The following code snippet should create and save a foo.ecore file into the same folder as the source XSD. If foo.xsd has additional imported XSDs, they will be coverted into separate .ecore files, hence the return type Collection<Resource>.

URI schemaURI = URI.createFileURI("foo.xsd");
Collection<Resource> ecoreResources = XSDEcoreBuilder.generateResources(schemaURI);
for (Resource ecoreResource : ecoreResources) {
    ecoreResource.save(null);
}
Zsolt Török
Looking at the Eclipse source code is often the best way to find answers to questions like this. Stuff you find in the wiki or the Eclipse newsgroups is often out of date.
Stephen C
I tried looking through the Eclipse XSD importer to see how it uses XSDEcoreBuilder. It seems it does something similar, producing an EPackage list. The Collection in http://wiki.eclipse.org/Generating_Dynamic_Ecore_from_XML_Schema is also an EPackage list, I believe, because of the code following in which it iterates through the collection to get each individual EPackage. However, I don't understand, what do you do with this EPackage to get it in, say, one of these .ecore or .genmodel formats, or how to read it at all. That is, what do I do with this EPackage? Sorry about being this clueless.
Trying to call generateResources gives a runtime error:Exception in thread "main" java.lang.NullPointerException at org.eclipse.xsd.ecore.XSDEcoreBuilder.generateResources(XSDEcoreBuilder.java:2765) at org.eclipse.xsd.ecore.XSDEcoreBuilder.generateResources(XSDEcoreBuilder.java:2742) at ...Calling generate instead of generateResources seems to work; it generates an EPackage instead of a Resource, though, but I don't know how to save that. I tried looking in the generateResources code and emulating that in my code, but I can't access all methods. Thanks for your help though; looks closer.
As an update, I believe that the exact problem is that "Resource ecoreResource = resourceSet.createResource(URI.createURI("*.ecore"));" in generateResources ends with ecoreResource still being null. At least, that is what I determined from trying to recreate the method in my code by looking at the Eclipse source. It may just be a problem in my code emulation or a misunderstanding, though; I really do not know at this point. I guess it would be easier if it is possible to just convert an EPackage to ecore, but I have no clue if that is possible, either. Or if the problem can be determined here.