Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » EMF Serialization on XMI
EMF Serialization on XMI [message #798460] Tue, 14 February 2012 18:20 Go to next message
Luca Gherardi is currently offline Luca GherardiFriend
Messages: 62
Registered: November 2010
Member
Hi all,

I have some doubts about the serialization of an EMF model on an XMI file.

I looked some examples online and also the auto-generated test code.

Currently this is my code (where MY_MODEL is the instance of the model I want to serialize):

// Create a resource set to hold the resources.
ResourceSet resourceSet = new ResourceSetImpl();

// Register the appropriate resource factory to handle all file extensions.
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, 
						new XMIResourceFactoryImpl());

// Register the package to ensure it is available during loading.
resourceSet.getPackageRegistry().put(featureModelPackage.eNS_URI, featureModelPackage.eINSTANCE);

// If there are no arguments, emit an appropriate usage message.
try {
	Resource resource = resourceSet.createResource(URI.createURI("http:///" + MY_MODEL.getName() + ".xmi"));
	resource.getContents().add(MY_MODEL);
	resource.save(System.out, null);
					
	ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		
	FileOutputStream fo = new FileOutputStream(new File("/tmp/model.rtt"));
					
	resource.save(fo, null);
}
catch (IOException exception) {
	exception.printStackTrace();
}


My questions are:

1) Is this the best way for doing the serialization on XMI?
2) What does the following line of code do? Why does it need an URI?

Resource resource = resourceSet.createResource(URI.createURI("http:///" + MY_MODEL.getName() + ".xmi"));


Thanks in advance,
Luca
Re: EMF Serialization on XMI [message #798508 is a reply to message #798460] Tue, 14 February 2012 19:31 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Luca,

Comments below.

On 14/02/2012 7:20 PM, Luca Mising name wrote:
> Hi all,
>
> I have some doubts about the serialization of an EMF model on an XMI
> file.
>
> I looked some examples online and also the auto-generated test code.
>
> Currently this is my code (where MY_MODEL is the instance of the model
> I want to serialize):
>
>
> // Create a resource set to hold the resources.
> ResourceSet resourceSet = new ResourceSetImpl();
>
> // Register the appropriate resource factory to handle all file
> extensions.
> resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION,
> new XMIResourceFactoryImpl());
>
> // Register the package to ensure it is available during loading.
> resourceSet.getPackageRegistry().put(featureModelPackage.eNS_URI,
> featureModelPackage.eINSTANCE);
>
> // If there are no arguments, emit an appropriate usage message.
> try {
> Resource resource =
> resourceSet.createResource(URI.createURI("http:///" +
> MY_MODEL.getName() + ".xmi"));
> resource.getContents().add(MY_MODEL);
> resource.save(System.out, null);
>
> ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
>
> FileOutputStream fo = new FileOutputStream(new
> File("/tmp/model.rtt"));
>
> resource.save(fo, null);
> }
> catch (IOException exception) {
> exception.printStackTrace();
> }
>
>
> My questions are:
>
> 1) Is this the best way for doing the serialization on XMI?
No.
> 2) What does the following line of code do? Why does it need an URI?
It create a resource and determines which type based on the extension.
>
>
> Resource resource =
> resourceSet.createResource(URI.createURI("http:///" +
> MY_MODEL.getName() + ".xmi"));
If you want to save to some file location, using
URI.createFileURI(file.getAbsolutePath()) to create the URI and use that
in createResource and then just call save on the resource.
>
> Thanks in advance,
> Luca


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: EMF Serialization on XMI [message #798522 is a reply to message #798508] Tue, 14 February 2012 19:52 Go to previous messageGo to next message
Luca Gherardi is currently offline Luca GherardiFriend
Messages: 62
Registered: November 2010
Member
Thanks ed, ser below

Ed Merks wrote on Tue, 14 February 2012 14:31
Luca,

Comments below.

On 14/02/2012 7:20 PM, Luca Mising name wrote:
> Hi all,
>
> I have some doubts about the serialization of an EMF model on an XMI
> file.
>
> I looked some examples online and also the auto-generated test code.
>
> Currently this is my code (where MY_MODEL is the instance of the model
> I want to serialize):
>
>
> // Create a resource set to hold the resources.
> ResourceSet resourceSet = new ResourceSetImpl();
>
> // Register the appropriate resource factory to handle all file
> extensions.
> resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION,
> new XMIResourceFactoryImpl());
>
> // Register the package to ensure it is available during loading.
> resourceSet.getPackageRegistry().put(featureModelPackage.eNS_URI,
> featureModelPackage.eINSTANCE);
>
> // If there are no arguments, emit an appropriate usage message.
> try {
> Resource resource =
> resourceSet.createResource(URI.createURI("http:///" +
> MY_MODEL.getName() + ".xmi"));
> resource.getContents().add(MY_MODEL);
> resource.save(System.out, null);
>
> ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
>
> FileOutputStream fo = new FileOutputStream(new
> File("/tmp/model.rtt"));
>
> resource.save(fo, null);
> }
> catch (IOException exception) {
> exception.printStackTrace();
> }
>
>
> My questions are:
>
> 1) Is this the best way for doing the serialization on XMI?
No.
> 2) What does the following line of code do? Why does it need an URI?
It create a resource and determines which type based on the extension.
>
>
> Resource resource =
> resourceSet.createResource(URI.createURI("http:///" +
> MY_MODEL.getName() + ".xmi"));
If you want to save to some file location, using
URI.createFileURI(file.getAbsolutePath()) to create the URI and use that
in createResource and then just call save on the resource.
>
> Thanks in advance,
> Luca


So what's the best practice for serialization?
Is giving a file path during the resource creation and then saving on the resource?

Thanks!
Re: EMF Serialization on XMI [message #798859 is a reply to message #798522] Wed, 15 February 2012 06:44 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Luca,

Generally you give the URI from which you want to load and to which you
want to save. In the IDE you'd generally use URI as created by
platformResourceURI, in other cases (stand alone), a file system URI
would be common.


On 14/02/2012 8:52 PM, Luca wrote:
> Thanks ed, ser below
>
> Ed Merks wrote on Tue, 14 February 2012 14:31
>> Luca,
>>
>> Comments below.
>>
>> On 14/02/2012 7:20 PM, Luca Mising name wrote:
>> > Hi all,
>> >
>> > I have some doubts about the serialization of an EMF model on an
>> XMI > file.
>> >
>> > I looked some examples online and also the auto-generated test code.
>> >
>> > Currently this is my code (where MY_MODEL is the instance of the
>> model > I want to serialize):
>> >
>> >
>> > // Create a resource set to hold the resources.
>> > ResourceSet resourceSet = new ResourceSetImpl();
>> >
>> > // Register the appropriate resource factory to handle all file >
>> extensions.
>> >
>> resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION,
>> > new XMIResourceFactoryImpl());
>> >
>> > // Register the package to ensure it is available during loading.
>> > resourceSet.getPackageRegistry().put(featureModelPackage.eNS_URI, >
>> featureModelPackage.eINSTANCE);
>> >
>> > // If there are no arguments, emit an appropriate usage message.
>> > try {
>> > Resource resource = >
>> resourceSet.createResource(URI.createURI("http:///" + >
>> MY_MODEL.getName() + ".xmi"));
>> > resource.getContents().add(MY_MODEL);
>> > resource.save(System.out, null);
>> >
>> > ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
>> >
>> > FileOutputStream fo = new FileOutputStream(new >
>> File("/tmp/model.rtt"));
>> >
>> > resource.save(fo, null);
>> > }
>> > catch (IOException exception) {
>> > exception.printStackTrace();
>> > }
>> >
>> >
>> > My questions are:
>> >
>> > 1) Is this the best way for doing the serialization on XMI?
>> No.
>> > 2) What does the following line of code do? Why does it need an URI?
>> It create a resource and determines which type based on the extension.
>> >
>> >
>> > Resource resource = >
>> resourceSet.createResource(URI.createURI("http:///" + >
>> MY_MODEL.getName() + ".xmi"));
>> If you want to save to some file location, using
>> URI.createFileURI(file.getAbsolutePath()) to create the URI and use
>> that in createResource and then just call save on the resource.
>> >
>> > Thanks in advance,
>> > Luca
>
>
> So what's the best practice for serialization?
> Is giving a file path during the resource creation and then saving on
> the resource?
>
> Thanks!


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:[CDO] Performance Issues on loading large amount of data
Next Topic:EMF/GWT: Resolving non-Containment references
Goto Forum:
  


Current Time: Sat Apr 27 04:12:50 GMT 2024

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

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

Back to the top