Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Send a xml as inputStream
Send a xml as inputStream [message #482336] Wed, 26 August 2009 10:00 Go to next message
Eclipse UserFriend
Originally posted by: cuentaosiris.gmail.com

Hello,

I have a client/server on different pc (different networks), and I would
like to send a xml as inputStream from client to server.

Serialize. I'm using this code in the client

...create EObject company (Dymamic EMF)
ResourceSet resourceSet1 = new ResourceSetImpl();
resourceSet1.getResourceFactoryRegistry().getExtensionToFact oryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION,
new XMLResourceFactoryImpl());
XMLResourceImpl res1 =
(XMLResourceImpl)resourceSet1.createResource(URI.createURI("*.xml "));
res1.getContents().add(company); //use Dynamic EMF
res1.getDefaultSaveOptions().put(XMLResource.OPTION_DECLARE_ XML,Boolean.FALSE);
res1.getDefaultSaveOptions().put(XMLResource.OPTION_SCHEMA_L OCATION,
Boolean.TRUE);
res1.setEncoding("UTF-8");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
res1.save(byteArrayOutputStream,null);
byte[] buffer = byteArrayOutputStream.toByteArray();
ByteArrayInputStream byteArrayInputStream = new
ByteArrayInputStream(buffer);
byteArrayOutputStream.close();
InputSource inputSource = new InputSource(byteArrayInputStream);
InputStream inputStream = inputSource.getByteStream();
call ther server method......


Deserialize. I'm using this code in the server

InputStream inputStream = inputSource.getByteStream();
ResourceSet load_resourceSet = new ResourceSetImpl();
load_resourceSet.getResourceFactoryRegistry().getExtensionTo FactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION ,
new XMLResourceFactoryImpl());
XMLResourceImpl load_resource =
(XMLResourceImpl)load_resourceSet.createResource(URI.createU RI( "*.xml"));
load_resource.load(inputStream,null);
EObject readCompanyEObject = load_resource.getContents().get(0);

but, not work!!, thorws this exception in the server

org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri
'http:///com.example.company' not found.
(file:///C:/OSGiWorkspaceEMF/*.xml, 1, 79)

But when I create the .ecore file in client (serialize) and later I copy
this in the server, work fine!!. Is necessary the .ecore file????

ResourceSet metaResourceSet = new ResourceSetImpl();
metaResourceSet.getResourceFactoryRegistry().getExtensionToF actoryMap().put( "ecore",
new XMLResourceFactoryImpl());
Resource metaResource =
metaResourceSet.createResource(URI.createURI("./company.ecore "));
metaResource.getContents().add(companyPackage);
try {
metaResource.save(null);
} catch (IOException e) {
e.printStackTrace();
}

Thanks.
Re: Send a xml as inputStream [message #482343 is a reply to message #482336] Wed, 26 August 2009 10:15 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Fer,

Comments below.

Fer wrote:
> Hello,
>
> I have a client/server on different pc (different networks), and I
> would like to send a xml as inputStream from client to server.
>
> Serialize. I'm using this code in the client
>
> ..create EObject company (Dymamic EMF)
> ResourceSet resourceSet1 = new ResourceSetImpl();
> resourceSet1.getResourceFactoryRegistry().getExtensionToFact oryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION,
> new XMLResourceFactoryImpl());
> XMLResourceImpl res1 =
> (XMLResourceImpl)resourceSet1.createResource(URI.createURI("*.xml "));
> res1.getContents().add(company); //use Dynamic EMF
> res1.getDefaultSaveOptions().put(XMLResource.OPTION_DECLARE_ XML,Boolean.FALSE);
>
> res1.getDefaultSaveOptions().put(XMLResource.OPTION_SCHEMA_L OCATION,
> Boolean.TRUE);
> res1.setEncoding("UTF-8");
> ByteArrayOutputStream byteArrayOutputStream = new
> ByteArrayOutputStream();
> res1.save(byteArrayOutputStream,null);
> byte[] buffer = byteArrayOutputStream.toByteArray();
> ByteArrayInputStream byteArrayInputStream = new
> ByteArrayInputStream(buffer);
> byteArrayOutputStream.close();
> InputSource inputSource = new InputSource(byteArrayInputStream);
> InputStream inputStream = inputSource.getByteStream();
> call ther server method......
>
>
> Deserialize. I'm using this code in the server
>
> InputStream inputStream = inputSource.getByteStream();
> ResourceSet load_resourceSet = new ResourceSetImpl();
> load_resourceSet.getResourceFactoryRegistry().getExtensionTo FactoryMap().put(Resource.Factory.Registry..DEFAULT_EXTENSIO N,
> new XMLResourceFactoryImpl());
> XMLResourceImpl load_resource =
> (XMLResourceImpl)load_resourceSet.createResource(URI.createU RI( "*.xml"));
> load_resource.load(inputStream,null);
> EObject readCompanyEObject = load_resource.getContents().get(0);
>
> but, not work!!, thorws this exception in the server
>
> org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri
> 'http:///com.example.company' not found.
> (file:///C:/OSGiWorkspaceEMF/*.xml, 1, 79)
It's important that the server know about the model. Either it should
call XyzPackage.eINSTANCE.eClass() to ensure that the generated package
is initialized and registered, or it should ensure that the Ecore model
is loaded and registered in the package registry of the resource set
being used to do the loading.
>
> But when I create the .ecore file in client (serialize) and later I
> copy this in the server, work fine!!. Is necessary the .ecore file????
>
> ResourceSet metaResourceSet = new ResourceSetImpl();
> metaResourceSet.getResourceFactoryRegistry().getExtensionToF actoryMap().put( "ecore",
> new XMLResourceFactoryImpl());
> Resource metaResource =
> metaResourceSet.createResource(URI.createURI("./company.ecore "));
> metaResource.getContents().add(companyPackage);
> try {
> metaResource.save(null);
> } catch (IOException e) {
> e.printStackTrace();
> }
>
> Thanks.
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Send a xml as inputStream [message #482421 is a reply to message #482336] Wed, 26 August 2009 13:47 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: cuentaosiris.gmail.com

Hello Ed,

Thanks a lot for your reply,

When you say that It's important that the server know about the
model,then... I need to put the *.ecore file generated in client to
server???, and how ensure I that the Ecore model is loaded and registered
in the package registry of the resource set (the directories are not the
same, because are different pc)??

I'm Sorry, but I don't understand that I have to do neither as to do it.

You would be able to help me a little more?

Thank!!!.
Re: Send a xml as inputStream [message #482439 is a reply to message #482343] Wed, 26 August 2009 14:22 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: cuentaosiris.gmail.com

Hello Ed,

Thanks a lot for your reply, When you say that It's important that the
server know about the model,then... I need to put the *.ecore file
generated in client to server???, and how ensure I that the Ecore model is
loaded and registered in the package registry of the resource set (the
directories are not the same, because are different pc)??

I'm Sorry, but I don't understand that I have to do neither as to do it.

You would be able to help me a little more?

Thank!!!.
Re: Send a xml as inputStream [message #482452 is a reply to message #482439] Wed, 26 August 2009 15:10 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Fer,

There are articles about using the dynamic stuff. You'd need to send
the Ecore resource much the same way, get the EPackage out of that
resource, and register it's nsURI in the
ResourceSet.getPackageRegistry(). I don't have time for more detail
because my flight will take off soon...


Fer wrote:
> Hello Ed,
>
> Thanks a lot for your reply, When you say that It's important that the
> server know about the model,then... I need to put the *.ecore file
> generated in client to server???, and how ensure I that the Ecore
> model is loaded and registered in the package registry of the resource
> set (the directories are not the same, because are different pc)??
>
> I'm Sorry, but I don't understand that I have to do neither as to do it.
>
> You would be able to help me a little more?
>
> Thank!!!.
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Send a xml as inputStream [message #482470 is a reply to message #482452] Wed, 26 August 2009 16:44 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: cuentaosiris.gmail.com

Hello Ed,

Thanks a lot!!, I have sent the ecore model in the same way that the
*.xml, I copy my code for more detail

******Serialize*******

ResourceSet resourceSet1 = new ResourceSetImpl();

XML FIRST

resourceSet1.getResourceFactoryRegistry().getExtensionToFact oryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION,
new XMLResourceFactoryImpl());
XMLResourceImpl resource_XML =
(XMLResourceImpl)resourceSet1.createResource(URI.createURI("*.xml "));
....
resource_XML.save(byteArrayOutputStream_XML,null);
...
InputStream inputStream_XML = inputSource_XML.getByteStream();


ECORE MODEL SECOND

resourceSet1.getResourceFactoryRegistry().getExtensionToFact oryMap().put( "ecore",
new XMLResourceFactoryImpl());
XMLResourceImpl resource_ECORE =
(XMLResourceImpl)resourceSet1.createResource(URI.createURI("./company.ecore "));
....
resource_ECORE.save(byteArrayOutputStream_ECORE,null);
...
InputStream inputStream_ECORE = inputSource_ECORE.getByteStream();

******Deserialize*******

ResourceSet load_resourceSet = new ResourceSetImpl();

(the order to deserialize is important, else not work)

ECORE MODEL FIRST!!

load_resourceSet.getResourceFactoryRegistry().getExtensionTo FactoryMap().put( "ecore",
new XMLResourceFactoryImpl());
XMLResourceImpl load_resource_ECORE =
(XMLResourceImpl)load_resourceSet.createResource(URI.createU RI( "./company.ecore"));
load_resource_ECORE.load(inputStream_ECORE,null);
...

XML SECOND

load_resourceSet.getResourceFactoryRegistry().getExtensionTo FactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION ,
new XMLResourceFactoryImpl());
XMLResourceImpl load_resource_XML =
(XMLResourceImpl)load_resourceSet.createResource(URI.createU RI( "*.xml"));
load_resource_XML.load(inputStream_XML,null);
...

Thus I have two stream, the first stream for the xml and a second stream
for the ecore model, this two streams are sent from client to server, and
work fine!!, thus is not necessary to copy the .ecore file in the server!!.

This way to do it is correct???

Thanks!!
Re: Send a xml as inputStream [message #482533 is a reply to message #482470] Wed, 26 August 2009 22:59 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Fer,

If you also serialized a schema location so that it finds that loaded
Ecore resource that way, then I can see how that would work nicely.


Fer wrote:
> Hello Ed,
>
> Thanks a lot!!, I have sent the ecore model in the same way that the
> *.xml, I copy my code for more detail
>
> ******Serialize*******
>
> ResourceSet resourceSet1 = new ResourceSetImpl();
>
> XML FIRST
>
> resourceSet1.getResourceFactoryRegistry().getExtensionToFact oryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION,
> new XMLResourceFactoryImpl());
> XMLResourceImpl resource_XML =
> (XMLResourceImpl)resourceSet1.createResource(URI.createURI("*.xml "));
> ...
> resource_XML.save(byteArrayOutputStream_XML,null);
> ..
> InputStream inputStream_XML = inputSource_XML.getByteStream();
>
>
> ECORE MODEL SECOND
>
> resourceSet1.getResourceFactoryRegistry().getExtensionToFact oryMap().put( "ecore",
> new XMLResourceFactoryImpl());
> XMLResourceImpl resource_ECORE =
> (XMLResourceImpl)resourceSet1.createResource(URI.createURI("./company.ecore "));
>
> ...
> resource_ECORE.save(byteArrayOutputStream_ECORE,null);
> ..
> InputStream inputStream_ECORE = inputSource_ECORE.getByteStream();
>
> ******Deserialize*******
>
> ResourceSet load_resourceSet = new ResourceSetImpl();
>
> (the order to deserialize is important, else not work)
>
> ECORE MODEL FIRST!!
>
> load_resourceSet.getResourceFactoryRegistry().getExtensionTo FactoryMap().put( "ecore",
> new XMLResourceFactoryImpl());
> XMLResourceImpl load_resource_ECORE =
> (XMLResourceImpl)load_resourceSet.createResource(URI.createU RI( "./company.ecore"));
>
> load_resource_ECORE.load(inputStream_ECORE,null);
> ..
>
> XML SECOND
>
> load_resourceSet.getResourceFactoryRegistry().getExtensionTo FactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION ,
> new XMLResourceFactoryImpl());
> XMLResourceImpl load_resource_XML =
> (XMLResourceImpl)load_resourceSet.createResource(URI.createU RI( "*.xml"));
> load_resource_XML.load(inputStream_XML,null);
> ..
>
> Thus I have two stream, the first stream for the xml and a second
> stream for the ecore model, this two streams are sent from client to
> server, and work fine!!, thus is not necessary to copy the .ecore file
> in the server!!.
>
> This way to do it is correct???
>
> Thanks!!
>
>
>
>
>
>
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Send a xml as inputStream [message #482847 is a reply to message #482533] Fri, 28 August 2009 10:34 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: cuentaosiris.gmail.com

Hello Ed,

I have changed the XML by XMI, ah!, I'm running Eclipse headless (Equinox
standalone).

//create a package that represents company
EPackage companyPackage = ecoreFactory.createEPackage();
companyPackage.setName("company");
companyPackage.setNsPrefix("company");
companyPackage.setNsURI("http:///com.example.company");
companyPackage.getEClassifiers().add(employeeClass);
companyPackage.getEClassifiers().add(departmentClass);
companyPackage.getEClassifiers().add(companyClass);

...

resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put( "xmi",
new XMIResourceFactoryImpl());
URI uri_XMI = URI.createURI("./company.xmi");
XMIResourceImpl res_XMI =
(XMIResourceImpl)resourceSet.createResource(uri_XMI);

I copy the xmi generated for more detail.

<company:Company xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:company="http:///com.example.company"
xsi:schemaLocation="http:///com.example.company ./company.ecore"
name="MyCompany">
<department number="123">
<employees name="John"/>
</department>
</company:Company>


Do I need to change something more...??,
How I serialize a schema location??

Thanks.
Re: Send a xml as inputStream [message #482864 is a reply to message #482847] Fri, 28 August 2009 11:21 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Fer,

Comments below.

Fer wrote:
> Hello Ed,
>
> I have changed the XML by XMI, ah!, I'm running Eclipse headless
> (Equinox standalone).
>
> //create a package that represents company
> EPackage companyPackage = ecoreFactory.createEPackage();
> companyPackage.setName("company");
> companyPackage.setNsPrefix("company");
> companyPackage.setNsURI("http:///com.example.company");
> companyPackage.getEClassifiers().add(employeeClass);
Where are these EClasses come from?
> companyPackage.getEClassifiers().add(departmentClass);
> companyPackage.getEClassifiers().add(companyClass);
>
> ..
>
> resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put( "xmi",
> new XMIResourceFactoryImpl());
> URI uri_XMI = URI.createURI("./company.xmi");
> XMIResourceImpl res_XMI =
> (XMIResourceImpl)resourceSet.createResource(uri_XMI);
>
> I copy the xmi generated for more detail.
>
> <company:Company xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:company="http:///com.example.company"
> xsi:schemaLocation="http:///com.example.company ./company.ecore"
> name="MyCompany">
> <department number="123">
> <employees name="John"/>
> </department>
> </company:Company>
>
>
> Do I need to change something more...??, How I serialize a schema
> location??
You already did: xsi:schemaLocation="http:///com.example.company
../company.ecore"

Is something still not working?
>
> Thanks.
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Send a xml as inputStream [message #482903 is a reply to message #482864] Fri, 28 August 2009 13:46 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: cuentaosiris.gmail.com

Hello Ed,

mmm, at this moment is working fine!!, but..., I would like to know if is
the best way or there is other better way (this is my doubt), because I
don't use xsd schema else ecore model and xmi (both like stream to the
server).

when you ask me...."Where are these EClasses come from?", only I copy the
EClass definition.

...
EClass companyClass = ecoreFactory.createEClass();
companyClass.setName("Company");
EClass departmentClass = ecoreFactory.createEClass();
departmentClass.setName("Department");
EClass employeeClass = ecoreFactory.createEClass();
employeeClass.setName("Employee");
... and more...


Thank a lot!!.
Re: Send a xml as inputStream [message #482933 is a reply to message #482903] Fri, 28 August 2009 15:10 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Fer,

No, this looks fine, at least from what you've shown...


Fer wrote:
> Hello Ed,
>
> mmm, at this moment is working fine!!, but..., I would like to know if
> is the best way or there is other better way (this is my doubt),
> because I don't use xsd schema else ecore model and xmi (both like
> stream to the server).
> when you ask me...."Where are these EClasses come from?", only I copy
> the EClass definition.
>
> ..
> EClass companyClass = ecoreFactory.createEClass();
> companyClass.setName("Company");
> EClass departmentClass = ecoreFactory.createEClass();
> departmentClass.setName("Department");
> EClass employeeClass = ecoreFactory.createEClass();
> employeeClass.setName("Employee");
> .. and more...
>
>
> Thank a lot!!.
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:EcrossReferenceAdapter ClassCastException
Next Topic:EMF Compare and CompareDialog
Goto Forum:
  


Current Time: Fri Mar 29 11:43:47 GMT 2024

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

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

Back to the top