The above-mentioned code works here using the following setup:
Version: Luna Service Release 2 (4.4.2)
Build id: 20150219-0600
Plugins:
- ATL SDK - ATLAS Transformation Language SDK 3.5.0.v201405260755 org.eclipse.m2m.atl.sdk.feature.group
- Eclipse Modeling Project Eclipse Modeling Tools 4.4.2.20150219-0708 epp.package.modeling
- Kermeta MDK for Ecore 1.4.0 fr.irisa.triskell.kermeta.ecore.feature.group IRISA/INRIA
- OCL Examples and Editors SDK 3.4.4.v20150213-2254 org.eclipse.ocl.examples.feature.group
- Eclipse Modeling Project
- Xtext Complete SDK 2.7.3.v201411190455 org.eclipse.xtext.sdk.feature.group Eclipse Modeling Project
However, the initial XSD file is overwritten by the Ecore content. Also, the save
operation is called twice (once for the XSDResourceImpl
and once for EcoreResourceFactoryImpl
). We need it only for EcoreResourceFactoryImpl
. To fix this, here the sample code:
URI schemaURI = URI.createFileURI("library3.xsd");
File outputFile = new File("library3.ecore");
XSDEcoreBuilder xsdEcoreBuilder = new XSDEcoreBuilder();
Collection<Resource> ecoreResources = xsdEcoreBuilder.generateResources(schemaURI);
// for every resource found (includes eventually referenced XSDs)
for (Resource ecoreResource : ecoreResources) {
try {
if (ecoreResource.getClass().getName().contains("EcoreResourceFactoryImpl")) {
ecoreResource.save(new FileOutputStream(outputFile), null);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}