-
Notifications
You must be signed in to change notification settings - Fork 0
Generate eCore and GenModel from XSD
Donatas Čiukšys edited this page Jan 31, 2018
·
1 revision
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.codegen.ecore.genmodel.GenJDKLevel;
import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
import org.eclipse.emf.codegen.ecore.genmodel.GenModelFactory;
import org.eclipse.emf.codegen.ecore.genmodel.GenPackage;
import org.eclipse.emf.codegen.ecore.genmodel.impl.GenModelFactoryImpl;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.plugin.EcorePlugin;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl;
import org.eclipse.xsd.ecore.XSDEcoreBuilder;
public class Main {
public static void main(String[] args) throws Exception {
URI schemaURI = URI.createFileURI(new File("schema.xsd").getAbsolutePath());
String eCorePath = new File("schema.ecore").getAbsolutePath();
Collection<Resource> ecoreResources = new XSDEcoreBuilder().generateResources(schemaURI);
for (Resource ecoreResource : ecoreResources) {
System.out.println(ecoreResource.getClass().getName() + " " + (ecoreResource instanceof XMIResourceImpl));
if (ecoreResource instanceof XMIResourceImpl) {
XMIResourceImpl generatedPackage = (XMIResourceImpl) ecoreResource;
ecoreResource.save(new FileOutputStream(eCorePath), null);
// createGenModel(generatedPackage, eCorePath, new File("schema.genmodel").getAbsolutePath());
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put
("ecore", new EcoreResourceFactoryImpl());
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put
("genmodel", new EcoreResourceFactoryImpl());
IPath ecorePath = new Path(eCorePath);
// generator.basePackage = arguments[2];
String prefix = "KazkoksPrefix";
ResourceSet resourceSet = new ResourceSetImpl();
// resourceSet.getURIConverter().getURIMap().putAll(EcorePlugin.computePlatformURIMap());
URI ecoreURI = URI.createFileURI(ecorePath.toString());
Resource resource = resourceSet.getResource(ecoreURI, true);
EPackage ePackage = (EPackage) resource.getContents().get(0);
IPath genModelPath = ecorePath.removeFileExtension().addFileExtension("genmodel");
URI genModelURI = URI.createFileURI(genModelPath.toString());
Resource genModelResource = Resource.Factory.Registry.INSTANCE.getFactory(genModelURI).createResource(genModelURI);
GenModel genModel = GenModelFactory.eINSTANCE.createGenModel();
genModelResource.getContents().add(genModel);
resourceSet.getResources().add(genModelResource);
genModel.setModelDirectory("/arTikraiCiaGeneruojam");
genModel.getForeignModel().add(ecorePath.toString());
genModel.initialize(Collections.singleton(ePackage));
GenPackage genPackage = genModel.getGenPackages().get(0);
genModel.setModelName(genModelURI.trimFileExtension().lastSegment());
genPackage.setPrefix(prefix);
genPackage.setBasePackage("com.nasdaq");
genModelResource.save(Collections.EMPTY_MAP);
}
}
}
private static void createGenModel(final XMIResourceImpl resource, final String ecoreLocation,
final String genModelLocation) {
GenModel genModel = GenModelFactory.eINSTANCE.createGenModel();
// genModel.setComplianceLevel(GenJDKLevel.JDK80_LITERAL);
// genModel.setModelDirectory(new File(ecoreLocation).getParent());
// genModel.getForeignModel().add(new Path(ecoreLocation).lastSegment());
// genModel.setModelName("XXXXXXXXXXXXXXXXXXXXXXXXXX");
// genModel.setRootExtendsInterface(Constants.GEN_MODEL_EXTENDS_INTERFACE.getValue());
genModel.initialize(Collections.singleton((EPackage)(resource.getContents().get(0))));
// GenPackage genPackage = (GenPackage)genModel.getGenPackages().get(0);
// genPackage.setPrefix("Schema");
try {
URI genModelURI = URI.createFileURI(genModelLocation);
final XMIResourceImpl genModelResource = new XMIResourceImpl(genModelURI);
genModelResource.getDefaultSaveOptions().put(XMLResource.OPTION_ENCODING,
"UTF-8");
genModelResource.getContents().add(genModel);
genModelResource.save(Collections.EMPTY_MAP);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}