Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Serialize an EObject without changing its resource
Serialize an EObject without changing its resource [message #427526] Fri, 20 February 2009 08:02 Go to next message
Peter Kullmann is currently offline Peter KullmannFriend
Messages: 240
Registered: July 2009
Senior Member
Hi,

I wonder whether it is possible to serialize a single EObject into a
String without changing its resource (or any other side effects).

Here's why: We have an EMF model which is loaded from an XML file. The
application has a special viewer that shows a nicely displayed object
inside of a browser. For this, I would like to use the serialized form
of that object, run it through a stylesheet processor and put the result
into the browser.
This worked when I was using my own model code where one could just call
object:serialize(some stream). With EMF Serialization it goes like this:

Resource resource = ...
resource.getContents().add(object);
resource.save(out, null);

And it works. But, it changes the resource where the object was
contained in and the rest of the application starts to fail... for
instance, object.eContainer() is suddenly null.

Thanks for any hints
Peter
Re: Serialize an EObject without changing its resource [message #427527 is a reply to message #427526] Fri, 20 February 2009 08:50 Go to previous messageGo to next message
Eike Stepper is currently offline Eike StepperFriend
Messages: 6690
Registered: July 2009
Senior Member
Peter,

I think it's absolutely necessary to use a Resource for serialization
because all the persistence logic is concentrated there.
If you can't afford to change the object during this process you could
try to copy it and add that copy to the resource.
I'm not so used to the copy utilities like EcoreUtil.copy(). You'll have
to ensure that you don't copy too much (e.g. deep copy)...

Cheers
/Eike

----
http://thegordian.blogspot.com



Peter Kullmann schrieb:
> Hi,
>
> I wonder whether it is possible to serialize a single EObject into a
> String without changing its resource (or any other side effects).
>
> Here's why: We have an EMF model which is loaded from an XML file. The
> application has a special viewer that shows a nicely displayed object
> inside of a browser. For this, I would like to use the serialized form
> of that object, run it through a stylesheet processor and put the
> result into the browser.
> This worked when I was using my own model code where one could just
> call object:serialize(some stream). With EMF Serialization it goes
> like this:
>
> Resource resource = ...
> resource.getContents().add(object);
> resource.save(out, null);
>
> And it works. But, it changes the resource where the object was
> contained in and the rest of the application starts to fail... for
> instance, object.eContainer() is suddenly null.
>
> Thanks for any hints
> Peter


Re: Serialize an EObject without changing its resource [message #427528 is a reply to message #427527] Fri, 20 February 2009 09:08 Go to previous messageGo to next message
Peter Kullmann is currently offline Peter KullmannFriend
Messages: 240
Registered: July 2009
Senior Member
Thanks a lot, Eike. Copy works like charm.
Peter

Eike Stepper schrieb:
> Peter,
>
> I think it's absolutely necessary to use a Resource for serialization
> because all the persistence logic is concentrated there.
> If you can't afford to change the object during this process you could
> try to copy it and add that copy to the resource.
> I'm not so used to the copy utilities like EcoreUtil.copy(). You'll have
> to ensure that you don't copy too much (e.g. deep copy)...
>
> Cheers
> /Eike
>
> ----
> http://thegordian.blogspot.com
>
>
>
> Peter Kullmann schrieb:
>> Hi,
>>
>> I wonder whether it is possible to serialize a single EObject into a
>> String without changing its resource (or any other side effects).
>>
>> Here's why: We have an EMF model which is loaded from an XML file. The
>> application has a special viewer that shows a nicely displayed object
>> inside of a browser. For this, I would like to use the serialized form
>> of that object, run it through a stylesheet processor and put the
>> result into the browser.
>> This worked when I was using my own model code where one could just
>> call object:serialize(some stream). With EMF Serialization it goes
>> like this:
>>
>> Resource resource = ...
>> resource.getContents().add(object);
>> resource.save(out, null);
>>
>> And it works. But, it changes the resource where the object was
>> contained in and the rest of the application starts to fail... for
>> instance, object.eContainer() is suddenly null.
>>
>> Thanks for any hints
>> Peter
Re: Serialize an EObject without changing its resource [message #427531 is a reply to message #427528] Fri, 20 February 2009 11:30 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33217
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------080500060904060207020602
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Peter,

This option is likely to serve your purpose as well:

/**
* Write only the subtree starting at the specified list of EObjects,
* which must be objects contained by the resource.
*/
String OPTION_ROOT_OBJECTS = "ROOT_OBJECTS";



Peter Kullmann wrote:
> Thanks a lot, Eike. Copy works like charm.
> Peter
>
> Eike Stepper schrieb:
>> Peter,
>>
>> I think it's absolutely necessary to use a Resource for serialization
>> because all the persistence logic is concentrated there.
>> If you can't afford to change the object during this process you
>> could try to copy it and add that copy to the resource.
>> I'm not so used to the copy utilities like EcoreUtil.copy(). You'll
>> have to ensure that you don't copy too much (e.g. deep copy)...
>>
>> Cheers
>> /Eike
>>
>> ----
>> http://thegordian.blogspot.com
>>
>>
>>
>> Peter Kullmann schrieb:
>>> Hi,
>>>
>>> I wonder whether it is possible to serialize a single EObject into a
>>> String without changing its resource (or any other side effects).
>>>
>>> Here's why: We have an EMF model which is loaded from an XML file.
>>> The application has a special viewer that shows a nicely displayed
>>> object inside of a browser. For this, I would like to use the
>>> serialized form of that object, run it through a stylesheet
>>> processor and put the result into the browser.
>>> This worked when I was using my own model code where one could just
>>> call object:serialize(some stream). With EMF Serialization it goes
>>> like this:
>>>
>>> Resource resource = ...
>>> resource.getContents().add(object);
>>> resource.save(out, null);
>>>
>>> And it works. But, it changes the resource where the object was
>>> contained in and the rest of the application starts to fail... for
>>> instance, object.eContainer() is suddenly null.
>>>
>>> Thanks for any hints
>>> Peter

--------------080500060904060207020602
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Peter,<br>
<br>
This option is likely to serve your purpose as well:<br>
<blockquote><small>


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Teneo/Hibernate NonUniqueObjectException resolving
Next Topic:hexbinary and Edit custom itemprovider for storing binaries(images)
Goto Forum:
  


Current Time: Mon Sep 23 19:30:32 GMT 2024

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

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

Back to the top