Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Deserializing EPackages with BinaryResourceImpl
Deserializing EPackages with BinaryResourceImpl [message #427967] Sun, 08 March 2009 10:07 Go to next message
Eike Stepper is currently offline Eike StepperFriend
Messages: 6682
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------020504000100060204080907
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Hi,

I discovered a problem when deserializing EPackages (that extend other
EPackages) with BinaryResourceImpl.
I guess part of the problem is in my code because I'm using my own,
non-delegating package registry and I don't use a special ResourceSet
for deserializing the packages. But I noticed that the deserialization
code in BinaryResourceImpl wouldn't pick up referenced packaged from my
registry anyway:

| *protected *EPackageData readEPackage() *throws *IOException
{
*int *id = readCompressedInt();
*if *(ePackageDataList.size() <= id)
{
EPackageData ePackageData = *new *EPackageData();
String nsURI = readString();
URI uri = readURI();
*if *(resourceSet != *null*)
{
ePackageData.ePackage = _*EPackage.Registry.INSTANCE*_.getEPackage(nsURI);
*if *(ePackageData.ePackage == *null*)
{
ePackageData.ePackage = (EPackage)resourceSet.getEObject(uri, *true*);
}
}
*else*
{
ePackageData.ePackage = EPackage.Registry.INSTANCE.getEPackage(nsURI);
}
ePackageData.eClassData = *new *EClassData[ePackageData.ePackage.getEClassifiers().size()];
ePackageDataList.add(ePackageData);
*return *ePackageData;
}
*else*
{
*return *ePackageDataList.get(id);
}
}|


I find it particularly difficult to understand the relation between
packages and their (implicit?) resources.

In my case I have 2 packages:
- modelXinterfaces
- modelXimpl (references model elements from modelXinterfaces)

When I read modelXimpl from a binary stream the references to
modelXinterfaces are resolved against the global registry and not the
deserialized version (that may already be present or come later). As I
said, I guess my code also has a problem:

| *public static **void *writePackage(ExtendedDataOutput out, EPackage ePackage, *boolean *zipped) *throws *IOException
{
*byte*[] bytes = EMFUtil.getPackageBytes(ePackage, zipped);
out.writeString(ePackage.getNsURI());
out.writeBoolean(zipped);
out.writeByteArray(bytes);
}

*public static *EPackage readPackage(ExtendedDataInput in) *throws *IOException
{
String uri = in.readString();
*boolean *zipped = in.readBoolean();
*byte*[] bytes = in.readByteArray();
*return *EMFUtil.createPackage(uri, bytes, zipped);
}|


And in EMFUtil:

| *public static **byte*[] getPackageBytes(EPackage ePackage, *boolean *zipped)
{
*try*
{
Resource resource = ePackage.eResource();
*if *(resource == *null*)
{
resource = createPackageResource(ePackage.getNsURI());
resource.getContents().add(ePackage);
}

ByteArrayOutputStream baos = *new *ByteArrayOutputStream();
resource.save(baos, createResourceOptions(zipped));
*return *baos.toByteArray();
}
*catch *(Exception ex)
{
*throw *WrappedException.wrap(ex);
}
}

*public static *EPackage createPackage(String uri, *byte*[] bytes, *boolean *zipped)
{
*try*
{
ByteArrayInputStream bais = *new *ByteArrayInputStream(bytes);
Resource resource = createPackageResource(uri);
resource.load(bais, createResourceOptions(zipped));

EList<EObject> contents = resource.getContents();
*return *(EPackage)contents.get(0);
}
*catch *(Exception ex)
{
*throw *WrappedException.wrap(ex);
}
}

*private static *Resource createPackageResource(String uri)
{
Resource.Factory resourceFactory = *new *EcoreResourceFactoryImpl();
*return *resourceFactory.createResource(URI.createURI(uri));
}

*private static *Map<String, Object> createResourceOptions(*boolean *zipped)
{
Map<String, Object> options = *new *HashMap<String, Object>();
*if *(zipped)
{
options.put(Resource.OPTION_ZIP, *true*);
}

*return *options;
}|


Any ideas what I could do better?

Cheers
/Eike

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



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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body bgcolor="#ffffff" text="#000000">
Hi,<br>
<br>
I discovered a problem when deserializing EPackages (that extend other
EPackages) with BinaryResourceImpl.<br>
I guess part of the problem is in my code because I'm using my own,
non-delegating package registry and I don't use a special ResourceSet
for deserializing the packages. But I noticed that the deserialization
code in BinaryResourceImpl wouldn't pick up referenced packaged from my
registry anyway:<br>
<br>
<title></title>
<style type="text/css">
<!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!-- ======================================================== -->
<!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard markus@jave.de = -->
<!-- = Further information: http://www.java2html.de = -->
<div class="java" align="left">
<table bgcolor="#ffffff" border="0" cellpadding="3" cellspacing="0">
<tbody>
<tr>
<!-- start source code --> <td align="left" nowrap="nowrap"
valign="top"> <code><font color="#ffffff">


Re: Deserializing EPackages with BinaryResourceImpl (CORRECTION) [message #427968 is a reply to message #427967] Sun, 08 March 2009 10:20 Go to previous messageGo to next message
Eike Stepper is currently offline Eike StepperFriend
Messages: 6682
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------030509010106040809090608
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Sorry, I just realized that I'm *not* using a BinaryResourceImpl!
It's an EcoreResourceImpl.

While I still think that BinaryResourceImpl would have its own problem
(from looking at its code), this is cliearly not my problem at this moment.
My problem is still there. I need a way to resolve model element
references against my own package registry.
And also important: If my own package registry does not have the
referenced package the deserialized reference must somehow stay a proxy
until the needed package of the target element is there...

Cheers
/Eike

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




Eike Stepper schrieb:
> Hi,
>
> I discovered a problem when deserializing EPackages (that extend other
> EPackages) with BinaryResourceImpl.
> I guess part of the problem is in my code because I'm using my own,
> non-delegating package registry and I don't use a special ResourceSet
> for deserializing the packages. But I noticed that the deserialization
> code in BinaryResourceImpl wouldn't pick up referenced packaged from
> my registry anyway:
>
> | *protected *EPackageData readEPackage() *throws *IOException
> {
> *int *id = readCompressedInt();
> *if *(ePackageDataList.size() <= id)
> {
> EPackageData ePackageData = *new *EPackageData();
> String nsURI = readString();
> URI uri = readURI();
> *if *(resourceSet != *null*)
> {
> ePackageData.ePackage = _*EPackage.Registry.INSTANCE*_.getEPackage(nsURI);
> *if *(ePackageData.ePackage == *null*)
> {
> ePackageData.ePackage = (EPackage)resourceSet.getEObject(uri, *true*);
> }
> }
> *else*
> {
> ePackageData.ePackage = EPackage.Registry.INSTANCE.getEPackage(nsURI);
> }
> ePackageData.eClassData = *new *EClassData[ePackageData.ePackage.getEClassifiers().size()];
> ePackageDataList.add(ePackageData);
> *return *ePackageData;
> }
> *else*
> {
> *return *ePackageDataList.get(id);
> }
> }|
>
>
> I find it particularly difficult to understand the relation between
> packages and their (implicit?) resources.
>
> In my case I have 2 packages:
> - modelXinterfaces
> - modelXimpl (references model elements from modelXinterfaces)
>
> When I read modelXimpl from a binary stream the references to
> modelXinterfaces are resolved against the global registry and not the
> deserialized version (that may already be present or come later). As I
> said, I guess my code also has a problem:
>
> | *public static **void *writePackage(ExtendedDataOutput out, EPackage ePackage, *boolean *zipped) *throws *IOException
> {
> *byte*[] bytes = EMFUtil.getPackageBytes(ePackage, zipped);
> out.writeString(ePackage.getNsURI());
> out.writeBoolean(zipped);
> out.writeByteArray(bytes);
> }
>
> *public static *EPackage readPackage(ExtendedDataInput in) *throws *IOException
> {
> String uri = in.readString();
> *boolean *zipped = in.readBoolean();
> *byte*[] bytes = in.readByteArray();
> *return *EMFUtil.createPackage(uri, bytes, zipped);
> }|
>
>
> And in EMFUtil:
>
> | *public static **byte*[] getPackageBytes(EPackage ePackage, *boolean *zipped)
> {
> *try*
> {
> Resource resource = ePackage.eResource();
> *if *(resource == *null*)
> {
> resource = createPackageResource(ePackage.getNsURI());
> resource.getContents().add(ePackage);
> }
>
> ByteArrayOutputStream baos = *new *ByteArrayOutputStream();
> resource.save(baos, createResourceOptions(zipped));
> *return *baos.toByteArray();
> }
> *catch *(Exception ex)
> {
> *throw *WrappedException.wrap(ex);
> }
> }
>
> *public static *EPackage createPackage(String uri, *byte*[] bytes, *boolean *zipped)
> {
> *try*
> {
> ByteArrayInputStream bais = *new *ByteArrayInputStream(bytes);
> Resource resource = createPackageResource(uri);
> resource.load(bais, createResourceOptions(zipped));
>
> EList<EObject> contents = resource.getContents();
> *return *(EPackage)contents.get(0);
> }
> *catch *(Exception ex)
> {
> *throw *WrappedException.wrap(ex);
> }
> }
>
> *private static *Resource createPackageResource(String uri)
> {
> Resource.Factory resourceFactory = *new *EcoreResourceFactoryImpl();
> *return *resourceFactory.createResource(URI.createURI(uri));
> }
>
> *private static *Map<String, Object> createResourceOptions(*boolean *zipped)
> {
> Map<String, Object> options = *new *HashMap<String, Object>();
> *if *(zipped)
> {
> options.put(Resource.OPTION_ZIP, *true*);
> }
>
> *return *options;
> }|
>
>
> Any ideas what I could do better?
>
> Cheers
> /Eike
>
> ----
> http://thegordian.blogspot.com
>
>

--------------030509010106040809090608
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">
Sorry, I just realized that I'm *not* using a BinaryResourceImpl!<br>
It's an EcoreResourceImpl.<br>
<br>
While I still think that BinaryResourceImpl would have its own problem
(from looking at its code), this is cliearly not my problem at this
moment.<br>
My problem is still there. I need a way to resolve model element
references against my own package registry.<br>
And also important: If my own package registry does not have the
referenced package the deserialized reference must somehow stay a proxy
until the needed package of the target element is there...<br>
<br>
Cheers<br>
/Eike<br>
<br>
----<br>
<a class="moz-txt-link-freetext" href="http://thegordian.blogspot.com">http://thegordian.blogspot.com</a><br>
<br>
<br>
<br>
<br>
Eike Stepper schrieb:
<blockquote cite="mid:gp05di$5dg$1@build.eclipse.org" type="cite">Hi,<br>
<br>
I discovered a problem when deserializing EPackages (that extend other
EPackages) with BinaryResourceImpl.<br>
I guess part of the problem is in my code because I'm using my own,
non-delegating package registry and I don't use a special ResourceSet
for deserializing the packages. But I noticed that the deserialization
code in BinaryResourceImpl wouldn't pick up referenced packaged from my
registry anyway:<br>
<br>
<title></title>
<style type="text/css">
<!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }-->
</style>
<meta http-equiv="Content-Type" content="text/html; ">
<!-- ======================================================== -->
<!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard markus@jave.de = --><!-- = Further information: http://www.java2html.de = -->
<div class="java" align="left">
<table bgcolor="#ffffff" border="0" cellpadding="3" cellspacing="0">
<tbody>
<tr>
<!-- start source code --> <td align="left" nowrap="nowrap"
valign="top"> <code><font color="#ffffff">


Re: Deserializing EPackages with BinaryResourceImpl (CORRECTION) [message #427970 is a reply to message #427968] Sun, 08 March 2009 14:07 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------020602080808070809060301
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Eike,

A package might be needed because you refer to it as data from another
model (as in reading a .ecore file) or it might be needed because it's
being used as a meta model to deserialize and instance of that package's
classes. In the later case, it simply *must *be found to create the
right types of objects, e.g., in XMLHandler.getPackageForURI we must
find the package and a proxy will not do. It must be found in the
registry, or with the help of extended meta data... In the former case,
I expect a proxy will be created with no effort to resolve it until later...

Am I missing something?



Eike Stepper wrote:
> Sorry, I just realized that I'm *not* using a BinaryResourceImpl!
> It's an EcoreResourceImpl.
>
> While I still think that BinaryResourceImpl would have its own problem
> (from looking at its code), this is cliearly not my problem at this
> moment.
> My problem is still there. I need a way to resolve model element
> references against my own package registry.
> And also important: If my own package registry does not have the
> referenced package the deserialized reference must somehow stay a
> proxy until the needed package of the target element is there...
>
> Cheers
> /Eike
>
> ----
> http://thegordian.blogspot.com
>
>
>
>
> Eike Stepper schrieb:
>> Hi,
>>
>> I discovered a problem when deserializing EPackages (that extend
>> other EPackages) with BinaryResourceImpl.
>> I guess part of the problem is in my code because I'm using my own,
>> non-delegating package registry and I don't use a special ResourceSet
>> for deserializing the packages. But I noticed that the
>> deserialization code in BinaryResourceImpl wouldn't pick up
>> referenced packaged from my registry anyway:
>>
>> | *protected *EPackageData readEPackage() *throws *IOException
>> {
>> *int *id = readCompressedInt();
>> *if *(ePackageDataList.size() <= id)
>> {
>> EPackageData ePackageData = *new *EPackageData();
>> String nsURI = readString();
>> URI uri = readURI();
>> *if *(resourceSet != *null*)
>> {
>> ePackageData.ePackage = _*EPackage.Registry.INSTANCE*_.getEPackage(nsURI);
>> *if *(ePackageData.ePackage == *null*)
>> {
>> ePackageData.ePackage = (EPackage)resourceSet.getEObject(uri, *true*);
>> }
>> }
>> *else*
>> {
>> ePackageData.ePackage = EPackage.Registry.INSTANCE.getEPackage(nsURI);
>> }
>> ePackageData.eClassData = *new *EClassData[ePackageData.ePackage.getEClassifiers().size()];
>> ePackageDataList.add(ePackageData);
>> *return *ePackageData;
>> }
>> *else*
>> {
>> *return *ePackageDataList.get(id);
>> }
>> }|
>>
>>
>> I find it particularly difficult to understand the relation between
>> packages and their (implicit?) resources.
>>
>> In my case I have 2 packages:
>> - modelXinterfaces
>> - modelXimpl (references model elements from modelXinterfaces)
>>
>> When I read modelXimpl from a binary stream the references to
>> modelXinterfaces are resolved against the global registry and not the
>> deserialized version (that may already be present or come later). As
>> I said, I guess my code also has a problem:
>>
>> | *public static **void *writePackage(ExtendedDataOutput out, EPackage ePackage, *boolean *zipped) *throws *IOException
>> {
>> *byte*[] bytes = EMFUtil.getPackageBytes(ePackage, zipped);
>> out.writeString(ePackage.getNsURI());
>> out.writeBoolean(zipped);
>> out.writeByteArray(bytes);
>> }
>>
>> *public static *EPackage readPackage(ExtendedDataInput in) *throws *IOException
>> {
>> String uri = in.readString();
>> *boolean *zipped = in.readBoolean();
>> *byte*[] bytes = in.readByteArray();
>> *return *EMFUtil.createPackage(uri, bytes, zipped);
>> }|
>>
>>
>> And in EMFUtil:
>>
>> | *public static **byte*[] getPackageBytes(EPackage ePackage, *boolean *zipped)
>> {
>> *try*
>> {
>> Resource resource = ePackage.eResource();
>> *if *(resource == *null*)
>> {
>> resource = createPackageResource(ePackage.getNsURI());
>> resource.getContents().add(ePackage);
>> }
>>
>> ByteArrayOutputStream baos = *new *ByteArrayOutputStream();
>> resource.save(baos, createResourceOptions(zipped));
>> *return *baos.toByteArray();
>> }
>> *catch *(Exception ex)
>> {
>> *throw *WrappedException.wrap(ex);
>> }
>> }
>>
>> *public static *EPackage createPackage(String uri, *byte*[] bytes, *boolean *zipped)
>> {
>> *try*
>> {
>> ByteArrayInputStream bais = *new *ByteArrayInputStream(bytes);
>> Resource resource = createPackageResource(uri);
>> resource.load(bais, createResourceOptions(zipped));
>>
>> EList<EObject> contents = resource.getContents();
>> *return *(EPackage)contents.get(0);
>> }
>> *catch *(Exception ex)
>> {
>> *throw *WrappedException.wrap(ex);
>> }
>> }
>>
>> *private static *Resource createPackageResource(String uri)
>> {
>> Resource.Factory resourceFactory = *new *EcoreResourceFactoryImpl();
>> *return *resourceFactory.createResource(URI.createURI(uri));
>> }
>>
>> *private static *Map<String, Object> createResourceOptions(*boolean *zipped)
>> {
>> Map<String, Object> options = *new *HashMap<String, Object>();
>> *if *(zipped)
>> {
>> options.put(Resource.OPTION_ZIP, *true*);
>> }
>>
>> *return *options;
>> }|
>>
>>
>> Any ideas what I could do better?
>>
>> Cheers
>> /Eike
>>
>> ----
>> http://thegordian.blogspot.com
>>
>>

--------------020602080808070809060301
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">
Eike,<br>
<br>
A package might be needed because you refer to it as data from another
model (as in reading a .ecore file) or it might be needed because it's
being used as a meta model to deserialize and instance of that
package's classes.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Deserializing EPackages with BinaryResourceImpl (CORRECTION) [message #427972 is a reply to message #427970] Sun, 08 March 2009 15:01 Go to previous messageGo to next message
Eike Stepper is currently offline Eike StepperFriend
Messages: 6682
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------080803000309010409010205
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Ed Merks schrieb:
> Eike,
>
> A package might be needed because you refer to it as data from another
> model (as in reading a .ecore file)
I think this is my case and my problem.

> or it might be needed because it's being used as a meta model to
> deserialize and instance of that package's classes.
This case does also exist in CDO. Indeed this is the real, final reason
why the first case occurs, too, since we never transfer packages on
their own. If it happens then only because instances of these packages
are to be transfered thereafter and we know that the receiver of the
instances does *not* yet know these packages.

> In the later case, it simply *must *be found to create the right types
> of objects, e.g., in XMLHandler.getPackageForURI we must find the
> package and a proxy will not do. It must be found in the registry, or
> with the help of extended meta data... In the former case, I expect a
> proxy will be created with no effort to resolve it until later...
It's definitely the case that the global registry is consulted at the
receiver side to resolve these references. It took me two days to find
out why I couldn't find additional information in a Map<EModelElement,
CDOIDMeta>, although it *seemed* to be there. The reason is that, when
deserializing a derived package the reference targets are taken from the
global registry (where a generated model version with the same nsURI is
registered). I guess that I never would have found this subtle problem
if value equality was used instead of pointer equality.

To be more concrete:

1) I serialized two packages A and B where some elements of B refer to
elements from A.
2) On the other side I deserialize A and put it in my registry.
My registry does not delegate to the global registry.
My registry associates some additional information with each model
element of A
3) Then I deserialize B and put it in my registry.
My registry associates some additional information with each model
element of B
4) Now I ask for b.getEAllStructuralFeatures() where b is a class from B
that extends a class from A.
The resulting feature list contains some features from the dynamic
class in B plus some features from a *generated* class in A' where A' is
registered in the global registry with the same nsURI as A

As I said, I think that I did not do enough to give EMF a chance to
resolve against the right package, but I'd expect at least an exception
being thrown, instead of silently delegating to the global registry. So
my questions:

1) How can I ensure that references are resolved against my own registry
(resource set?)?
2) How can I ensure that an exception is thrown if my registry (resource
set?) can't provide the needed target object?

Cheers
/Eike

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




>
> Am I missing something?
>
>
>
> Eike Stepper wrote:
>> Sorry, I just realized that I'm *not* using a BinaryResourceImpl!
>> It's an EcoreResourceImpl.
>>
>> While I still think that BinaryResourceImpl would have its own
>> problem (from looking at its code), this is cliearly not my problem
>> at this moment.
>> My problem is still there. I need a way to resolve model element
>> references against my own package registry.
>> And also important: If my own package registry does not have the
>> referenced package the deserialized reference must somehow stay a
>> proxy until the needed package of the target element is there...
>>
>> Cheers
>> /Eike
>>
>> ----
>> http://thegordian.blogspot.com
>>
>>
>>
>>
>> Eike Stepper schrieb:
>>> Hi,
>>>
>>> I discovered a problem when deserializing EPackages (that extend
>>> other EPackages) with BinaryResourceImpl.
>>> I guess part of the problem is in my code because I'm using my own,
>>> non-delegating package registry and I don't use a special
>>> ResourceSet for deserializing the packages. But I noticed that the
>>> deserialization code in BinaryResourceImpl wouldn't pick up
>>> referenced packaged from my registry anyway:
>>>
>>> | *protected *EPackageData readEPackage() *throws *IOException
>>> {
>>> *int *id = readCompressedInt();
>>> *if *(ePackageDataList.size() <= id)
>>> {
>>> EPackageData ePackageData = *new *EPackageData();
>>> String nsURI = readString();
>>> URI uri = readURI();
>>> *if *(resourceSet != *null*)
>>> {
>>> ePackageData.ePackage = _*EPackage.Registry.INSTANCE*_.getEPackage(nsURI);
>>> *if *(ePackageData.ePackage == *null*)
>>> {
>>> ePackageData.ePackage = (EPackage)resourceSet.getEObject(uri, *true*);
>>> }
>>> }
>>> *else*
>>> {
>>> ePackageData.ePackage = EPackage.Registry.INSTANCE.getEPackage(nsURI);
>>> }
>>> ePackageData.eClassData = *new *EClassData[ePackageData.ePackage.getEClassifiers().size()];
>>> ePackageDataList.add(ePackageData);
>>> *return *ePackageData;
>>> }
>>> *else*
>>> {
>>> *return *ePackageDataList.get(id);
>>> }
>>> }|
>>>
>>>
>>> I find it particularly difficult to understand the relation between
>>> packages and their (implicit?) resources.
>>>
>>> In my case I have 2 packages:
>>> - modelXinterfaces
>>> - modelXimpl (references model elements from modelXinterfaces)
>>>
>>> When I read modelXimpl from a binary stream the references to
>>> modelXinterfaces are resolved against the global registry and not
>>> the deserialized version (that may already be present or come
>>> later). As I said, I guess my code also has a problem:
>>>
>>> | *public static **void *writePackage(ExtendedDataOutput out, EPackage ePackage, *boolean *zipped) *throws *IOException
>>> {
>>> *byte*[] bytes = EMFUtil.getPackageBytes(ePackage, zipped);
>>> out.writeString(ePackage.getNsURI());
>>> out.writeBoolean(zipped);
>>> out.writeByteArray(bytes);
>>> }
>>>
>>> *public static *EPackage readPackage(ExtendedDataInput in) *throws *IOException
>>> {
>>> String uri = in.readString();
>>> *boolean *zipped = in.readBoolean();
>>> *byte*[] bytes = in.readByteArray();
>>> *return *EMFUtil.createPackage(uri, bytes, zipped);
>>> }|
>>>
>>>
>>> And in EMFUtil:
>>>
>>> | *public static **byte*[] getPackageBytes(EPackage ePackage, *boolean *zipped)
>>> {
>>> *try*
>>> {
>>> Resource resource = ePackage.eResource();
>>> *if *(resource == *null*)
>>> {
>>> resource = createPackageResource(ePackage.getNsURI());
>>> resource.getContents().add(ePackage);
>>> }
>>>
>>> ByteArrayOutputStream baos = *new *ByteArrayOutputStream();
>>> resource.save(baos, createResourceOptions(zipped));
>>> *return *baos.toByteArray();
>>> }
>>> *catch *(Exception ex)
>>> {
>>> *throw *WrappedException.wrap(ex);
>>> }
>>> }
>>>
>>> *public static *EPackage createPackage(String uri, *byte*[] bytes, *boolean *zipped)
>>> {
>>> *try*
>>> {
>>> ByteArrayInputStream bais = *new *ByteArrayInputStream(bytes);
>>> Resource resource = createPackageResource(uri);
>>> resource.load(bais, createResourceOptions(zipped));
>>>
>>> EList<EObject> contents = resource.getContents();
>>> *return *(EPackage)contents.get(0);
>>> }
>>> *catch *(Exception ex)
>>> {
>>> *throw *WrappedException.wrap(ex);
>>> }
>>> }
>>>
>>> *private static *Resource createPackageResource(String uri)
>>> {
>>> Resource.Factory resourceFactory = *new *EcoreResourceFactoryImpl();
>>> *return *resourceFactory.createResource(URI.createURI(uri));
>>> }
>>>
>>> *private static *Map<String, Object> createResourceOptions(*boolean *zipped)
>>> {
>>> Map<String, Object> options = *new *HashMap<String, Object>();
>>> *if *(zipped)
>>> {
>>> options.put(Resource.OPTION_ZIP, *true*);
>>> }
>>>
>>> *return *options;
>>> }|
>>>
>>>
>>> Any ideas what I could do better?
>>>
>>> Cheers
>>> /Eike
>>>
>>> ----
>>> http://thegordian.blogspot.com
>>>
>>>

--------------080803000309010409010205
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">
Ed Merks schrieb:
<blockquote cite="mid:gp0jei$m50$1@build.eclipse.org" type="cite">
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
Eike,<br>
<br>
A package might be needed because you refer to it as data from another
model (as in reading a .ecore file) </blockquote>
I think this is my case and my problem.<br>
<br>
<blockquote cite="mid:gp0jei$m50$1@build.eclipse.org" type="cite">or it
might be needed because it's
being used as a meta model to deserialize and instance of that
package's classes.


Re: Deserializing EPackages with BinaryResourceImpl (CORRECTION) [message #427974 is a reply to message #427972] Sun, 08 March 2009 16:35 Go to previous message
Eike Stepper is currently offline Eike StepperFriend
Messages: 6682
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------070201070604060408080500
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Ed,

Comments below...


Eike Stepper schrieb:
>>>> And in EMFUtil:
>>>>
>>>> | *private static *Resource createPackageResource(String uri)
>>>> {
>>>> Resource.Factory resourceFactory = *new *EcoreResourceFactoryImpl();
>>>> *return *resourceFactory.createResource(URI.createURI(uri));
>>>> }
>>>> |
>>>>
>>>>
I think I solved the problem with this change:

| *private static *Resource createPackageResource(String uri, EPackage.Registry packageRegistry)
{
ResourceSet resourceSet = *new *ResourceSetImpl();
*_resourceSet.setPackageRegistry(packageRegistry);_*

Resource.Factory resourceFactory = *new *EcoreResourceFactoryImpl();
resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put( "*", resourceFactory);
resourceSet.getResourceFactoryRegistry().getProtocolToFactor yMap().put( "*", resourceFactory);

*return *resourceSet.createResource(URI.createURI(uri));
}|



Cheers
/Eike

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


--------------070201070604060408080500
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">
Ed,<br>
<br>
Comments below...<br>
<br>
<br>
Eike Stepper schrieb:
<blockquote cite="mid:gp0mkl$vu6$1@build.eclipse.org" type="cite">
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
<blockquote cite="mid:gp0jei$m50$1@build.eclipse.org" type="cite">
<blockquote cite="mid:gp065t$5dg$2@build.eclipse.org" type="cite">
<blockquote cite="mid:gp05di$5dg$1@build.eclipse.org" type="cite">And
in EMFUtil:<br>
<br>
<title></title>
<style type="text/css">
<!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }-->
</style>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<!-- ======================================================== -->
<!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard markus@jave.de = --><!-- = Further information: http://www.java2html.de = -->
<div class="java" align="left">
<table bgcolor="#ffffff" border="0" cellpadding="3"
cellspacing="0">
<tbody>
<tr>
<!-- start source code --> <td align="left" nowrap="nowrap"
valign="top"> <code><font color="#ffffff"></font><font color="#ffffff">


Previous Topic:Problem persisting ChangeDescription (Validation problem)
Next Topic:ISaveablesSource and ISaveablePart interfaces
Goto Forum:
  


Current Time: Thu Apr 25 11:46:47 GMT 2024

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

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

Back to the top