Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » xmlns of models created in EMF standalone(how to get it to show?)
xmlns of models created in EMF standalone [message #644335] Fri, 10 December 2010 15:27 Go to next message
Syd  is currently offline Syd Friend
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 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
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/
Re: xmlns of models created in EMF standalone [message #644362 is a reply to message #644341] Fri, 10 December 2010 17:04 Go to previous messageGo to next message
Syd  is currently offline Syd Friend
Messages: 3
Registered: December 2010
Junior Member

Thanks for the quick reply Ed, your knowledge passed on is one reason why EMF is so successful!

Ed Merks wrote on Fri, 10 December 2010 10:47

Your EPackage doesn't have an nsURI. Best you invoke
Diagnostician.INSTANCE.validate on your EPackage to ensure it's valid.



Setting an nsURI in the package worked, thanks for the Diagnostician tip as well.

Ed Merks wrote on Fri, 10 December 2010 10:47

You'll see in the history that I am always telling people to use
absolute paths...



I did a quick search but couldn't find out why you prefer absolute paths? (please send a link if you have)

I use relative paths because I use subversion, so directory location portability is useful.

Re: xmlns of models created in EMF standalone [message #644366 is a reply to message #644362] Fri, 10 December 2010 17:18 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
Syd,

See what java.io.File.getAbsolutePath does when you given it a relative
path, i.e., one that doesn't start with / nor a drive letter.


Syd wrote:
>
> Thanks for the quick reply Ed, your knowledge passed on is one reason
> why EMF is so successful!
>
> Ed Merks wrote on Fri, 10 December 2010 10:47
>> Your EPackage doesn't have an nsURI. Best you invoke
>> Diagnostician.INSTANCE.validate on your EPackage to ensure it's valid.
>
>
> Setting an nsURI in the package worked, thanks for the Diagnostician
> tip as well.
>
> Ed Merks wrote on Fri, 10 December 2010 10:47
>> You'll see in the history that I am always telling people to use
>> absolute paths...
>
>
> I did a quick search but couldn't find out why you prefer absolute
> paths? (please send a link if you have)
>
> I use relative paths because I use subversion, so directory location
> portability is useful.
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: xmlns of models created in EMF standalone [message #644372 is a reply to message #644366] Fri, 10 December 2010 17:46 Go to previous message
Syd  is currently offline Syd Friend
Messages: 3
Registered: December 2010
Junior Member
Ed Merks wrote on Fri, 10 December 2010 12:18

See what java.io.File.getAbsolutePath does when you given it a relative
path, i.e., one that doesn't start with / nor a drive letter.



I see, I'll be careful.

I found the following link for anyone else: http://blogs.sun.com/foo/entry/jdk_getabsolutepath_doesn_t

Thanks and have a good weekend, Ed.
Previous Topic:[CDO] cdo delta notification and transactional editor domains
Next Topic:[CDO] Connection timeout
Goto Forum:
  


Current Time: Fri Apr 26 04:50:33 GMT 2024

Powered by FUDForum. Page generated in 0.03121 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top