xmlns of models created in EMF standalone [message #644335] |
Fri, 10 December 2010 15:27 |
Syd Messages: 3 Registered: December 2010 |
Junior Member |
|
|
Hi folks!
I have a small program (code below) that creates models that are the instance of a simple ecore meta model:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore">
<ecore:EPackage name="MyPackage">
<eClassifiers xsi:type="ecore:EClass" name="MyClass">
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="MyClassyClass">
</eClassifiers>
</ecore:EPackage>
</xmi:XMI>
instances are not in a container package/element so come out like this:
<?xml version="1.0" encoding="ASCII"?>
<xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI">
<MyClass/>
<MyClassyClass/>
</xmi:XMI>
however the xmlns is missing, the first line of the instance should be:
<xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns="MyPackage">
how can I fix this so the xmlns is created in the instances? I'm using EMF jars with version 2.5.0
Thanks in advance for your replies,
Syd
p.s. the code is as follow
EPackage metamodelpackage = EPackage.Registry.INSTANCE.getEPackage("./data/Test.ecore");
//if the package is not cached, load the resource
if(metamodelpackage == null){
//load the package
URI mmuri = URI.createFileURI("./data/Test.ecore");
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ecore", new XMIResourceFactoryImpl());
Resource resource = resourceSet.getResource(mmuri, true);
metamodelpackage = (EPackage) resource.getContents().get(0);
//register the metamodel with central
EPackage.Registry.INSTANCE.put("./data/Test.ecore", metamodelpackage);
EPackage.Registry.INSTANCE.put("MyPackage", metamodelpackage);
}
EFactory factory = metamodelpackage.getEFactoryInstance();
EClass cls=(EClass)metamodelpackage.getEClassifier("MyClass");
EObject data1 = factory.create(cls);
cls=(EClass)metamodelpackage.getEClassifier("MyClassyClass");
EObject data2 = factory.create(cls);
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", new XMIResourceFactoryImpl());
//create the model result, where instances will be stored
Resource resource = resourceSet.createResource( URI.createFileURI("./data/test_instance.xmi") , "MyPackage");
resource.getContents().add(data1);
resource.getContents().add(data2);
try {
resource.save(null);
}
catch (java.io.IOException e){
System.err.println("could not write generated file to directory");
e.printStackTrace();
System.exit(-1);
}
|
|
|
Re: xmlns of models created in EMF standalone [message #644341 is a reply to message #644335] |
Fri, 10 December 2010 15:47 |
Ed Merks Messages: 33141 Registered: July 2009 |
Senior Member |
|
|
Syd,
Comments below.
Syd wrote:
> Hi folks!
>
> I have a small program (code below) that creates models that are the
> instance of a simple ecore meta model:
>
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore">
> <ecore:EPackage name="MyPackage">
> <eClassifiers xsi:type="ecore:EClass" name="MyClass">
> </eClassifiers>
> <eClassifiers xsi:type="ecore:EClass" name="MyClassyClass">
> </eClassifiers>
> </ecore:EPackage>
> </xmi:XMI>
>
> instances are not in a container package/element so come out like this:
>
> <?xml version="1.0" encoding="ASCII"?>
> <xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI">
> <MyClass/>
> <MyClassyClass/>
> </xmi:XMI>
Your EPackage doesn't have an nsURI. Best you invoke
Diagnostician.INSTANCE.validate on your EPackage to ensure it's valid.
>
> however the xmlns is missing, the first line of the instance should be:
>
> <xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
> xmlns="MyPackage">
>
> how can I fix this so the xmlns is created in the instances? I'm using
> EMF jars with version 2.5.0
>
> Thanks in advance for your replies,
>
> Syd
>
> p.s. the code is as follow
> EPackage metamodelpackage =
> EPackage.Registry.INSTANCE.getEPackage("./data/Test.ecore");
>
> //if the package is not cached, load the resource
> if(metamodelpackage == null){
> //load the package
> URI mmuri = URI.createFileURI("./data/Test.ecore");
You'll see in the history that I am always telling people to use
absolute paths...
> ResourceSet resourceSet = new ResourceSetImpl();
>
> resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put( "ecore",
> new XMIResourceFactoryImpl());
>
> Resource resource = resourceSet.getResource(mmuri, true);
> metamodelpackage = (EPackage) resource.getContents().get(0);
>
> //register the metamodel with central
> EPackage.Registry.INSTANCE.put("./data/Test.ecore",
> metamodelpackage);
> EPackage.Registry.INSTANCE.put("MyPackage",
> metamodelpackage);
> }
>
> EFactory factory = metamodelpackage.getEFactoryInstance();
>
> EClass cls=(EClass)metamodelpackage.getEClassifier("MyClass");
> EObject data1 = factory.create(cls);
> cls=(EClass)metamodelpackage.getEClassifier("MyClassyClass");
> EObject data2 = factory.create(cls);
>
> ResourceSet resourceSet = new ResourceSetImpl();
>
> resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put( "xmi",
> new XMIResourceFactoryImpl());
>
> //create the model result, where instances will be stored
> Resource resource = resourceSet.createResource(
> URI.createFileURI("./data/test_instance.xmi") , "MyPackage");
>
> resource.getContents().add(data1);
> resource.getContents().add(data2);
>
> try {
> resource.save(null);
> }
> catch (java.io.IOException e){
> System.err.println("could not write generated file to
> directory");
> e.printStackTrace();
> System.exit(-1);
> }
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03121 seconds