Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Creating a new Resource Type
Creating a new Resource Type [message #1726687] Tue, 15 March 2016 18:04 Go to next message
aurel pestell is currently offline aurel pestellFriend
Messages: 90
Registered: October 2013
Location: Sweden
Member
Hi,

In my project I have the following code:
ResourceSet resourceSet = new ResourceSetImpl();
		resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, new ExecutableFamiliesResponseGENResourceFactoryImpl());
		resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, new ExecutableFamiliesErrorResponseGENResourceFactoryImpl());
		resourceSet.getPackageRegistry().put(ExecutableFamiliesResponseGENPackage.eNS_URI, ExecutableFamiliesResponseGENPackage.eINSTANCE);
		resourceSet.getPackageRegistry().put(ExecutableFamiliesErrorResponseGENPackage.eNS_URI, ExecutableFamiliesErrorResponseGENPackage.eINSTANCE);

		InputSource inputSource = new InputSource(new StringReader(xmlContent));
		XMLResource resource = (XMLResource) resourceSet.createResource(URI.createFileURI("xml"));
resource.load(inputSource, null);


How does EMF decide what type will be "resource" ?
Because my input can be either "ExecutableFamiliesResponseGENResourceFactory" or "ExecutableFamiliesErrorResponseGENResourceFactory".

(Sorry I don't have a deep understanding of EMF so my question might sound stupid but I can't find a good explanation)

Thank you.

A.
Re: Creating a new Resource Type [message #1726716 is a reply to message #1726687] Tue, 15 March 2016 21:30 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Comments below.

On 16.03.2016 02:04, aurel pestell wrote:
> Hi,
>
> In my project I have the following code:
> ResourceSet resourceSet = new ResourceSetImpl();
>
> resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION,
> new ExecutableFamiliesResponseGENResourceFactoryImpl());
>
> resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION,
> new ExecutableFamiliesErrorResponseGENResourceFactoryImpl());
The replaces the previous registration in the map.
>
> resourceSet.getPackageRegistry().put(ExecutableFamiliesResponseGENPackage.eNS_URI,
> ExecutableFamiliesResponseGENPackage.eINSTANCE);
>
> resourceSet.getPackageRegistry().put(ExecutableFamiliesErrorResponseGENPackage.eNS_URI,
> ExecutableFamiliesErrorResponseGENPackage.eINSTANCE);
>
> InputSource inputSource = new InputSource(new
> StringReader(xmlContent));
> XMLResource resource = (XMLResource)
> resourceSet.createResource(URI.createFileURI("xml"));
This isn't going to work is it? You'll generally want an absolute file
system path as the argument for createFileURI.
> resource.load(inputSource, null);
>
> How does EMF decide what type will be "resource" ?
It has several ways, but you've only registered based on file extension
and only a default (and of course these can be only one default).
> Because my input can be either
> "ExecutableFamiliesResponseGENResourceFactory" or
> "ExecutableFamiliesErrorResponseGENResourceFactory".
>
> (Sorry I don't have a deep understanding of EMF so my question might
> sound stupid but I can't find a good explanation)
If you need to use different resource factories for resources with a
common extension, e.g., *.xml, you should make use of EMF's content type
support. E.g., set the content type identifier in the GenPackage, and
regenerate. Also try generating the *.tests project and look at the
XyzExample.java to see how it's used standalone. In an Eclipse
application you should not write code like the above because the
plugin.xml registrations are sufficient.
>
> Thank you.
>
> A.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Creating a new Resource Type [message #1726770 is a reply to message #1726716] Wed, 16 March 2016 11:23 Go to previous messageGo to next message
aurel pestell is currently offline aurel pestellFriend
Messages: 90
Registered: October 2013
Location: Sweden
Member
Thanks, the code runs in a plugin environement using equinox so I got rid of the code for registration.
I added a Type Content Identifier and regenerated the models and get my extensions updates and a new content type field appeared in each package interfaces.
But I am not sure how to use that.
I tried that for testing:
XMLResource resource = (XMLResource) resourceSet.createResource(URI.createURI("resources\\fakeResponse.xml"),
				ExecutableFamiliesResponseGENPackage.eCONTENT_TYPE);


It works because I explicitely told the resourceset what Type it is but what if I don't know in advance what type it can be ?
Thanks.
Re: Creating a new Resource Type [message #1726778 is a reply to message #1726770] Wed, 16 March 2016 13:10 Go to previous messageGo to next message
aurel pestell is currently offline aurel pestellFriend
Messages: 90
Registered: October 2013
Location: Sweden
Member
I start to understand what I want should be done by validating the input xml against a list of xml schemas and determine which type I should use from there Sad
Re: Creating a new Resource Type [message #1726800 is a reply to message #1726778] Wed, 16 March 2016 15:11 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

You should find that the XML namespace of the root XMI element tells you
which schema to use.

Regards

Ed Willink


On 16/03/2016 13:10, aurel pestell wrote:
> I start to understand what I want should be done by validating the input
> xml against a list of xml schemas and determine which type I should use
> from there :(
Re: Creating a new Resource Type [message #1726807 is a reply to message #1726800] Wed, 16 March 2016 15:46 Go to previous messageGo to next message
aurel pestell is currently offline aurel pestellFriend
Messages: 90
Registered: October 2013
Location: Sweden
Member
Aw sorry by "schema" I meant the xsd file that describes the elements of the input XML (and not the one that describes XML itself like W3C_XML_SCHEMA_NS_URI).
Re: Creating a new Resource Type [message #1726809 is a reply to message #1726800] Wed, 16 March 2016 16:09 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Yes, and that's what the content-type based registrations do. I.e.,
look at the namespaces used on the root element.

Of course when you create a new resource you must specify the content
type, but when reading existing content, the type can be determined (and
is determine) from the namespace of the root element.


On 16.03.2016 23:11, Ed Willink wrote:
> Hi
>
> You should find that the XML namespace of the root XMI element tells you
> which schema to use.
>
> Regards
>
> Ed Willink
>
>
> On 16/03/2016 13:10, aurel pestell wrote:
>> I start to understand what I want should be done by validating the input
>> xml against a list of xml schemas and determine which type I should use
>> from there :(
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Creating a new Resource Type [message #1726810 is a reply to message #1726807] Wed, 16 March 2016 16:09 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Aurel,

It should just work.

On 16.03.2016 23:46, aurel pestell wrote:
> Aw sorry by "schema" I meant the xsd file that describes the elements of
> the input XML (and not the one that describes XML itself like
> W3C_XML_SCHEMA_NS_URI).


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Creating a new Resource Type [message #1726811 is a reply to message #1726807] Wed, 16 March 2016 16:10 Go to previous messageGo to next message
aurel pestell is currently offline aurel pestellFriend
Messages: 90
Registered: October 2013
Location: Sweden
Member
[message supprimé]

[Updated on: Wed, 16 March 2016 16:15]

Report message to a moderator

Re: Creating a new Resource Type [message #1726824 is a reply to message #1726809] Wed, 16 March 2016 16:43 Go to previous messageGo to next message
aurel pestell is currently offline aurel pestellFriend
Messages: 90
Registered: October 2013
Location: Sweden
Member
Using existing resource does not seem to work unfortunately.
This code:

XMLResource resource = (XMLResource) resourceSet.getResource(URI.createFileURI("C:\\yappclipse_2016_01_21\\yapp.capp.api\\resources\\fakeResponse.xml"), true);


gives me:

Quote:
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri 'null' not found.


Doing the registration does not help:
resourceSet.getResourceFactoryRegistry().getContentTypeToFactoryMap().put(ExecutableFamiliesResponseGENPackage.eCONTENT_TYPE, new ExecutableFamiliesResponseGENResourceFactoryImpl());
		resourceSet.getURIConverter().getContentHandlers().add(new RootXMLContentHandlerImpl(ExecutableFamiliesResponseGENPackage.eCONTENT_TYPE, null, null, (String) null, null));
		resourceSet.getResourceFactoryRegistry().getContentTypeToFactoryMap().put(ExecutableFamiliesErrorResponseGENPackage.eCONTENT_TYPE, new ExecutableFamiliesErrorResponseGENResourceFactoryImpl());
		resourceSet.getURIConverter().getContentHandlers().add(new RootXMLContentHandlerImpl(ExecutableFamiliesErrorResponseGENPackage.eCONTENT_TYPE, null, null, (String) null, null));
Re: Creating a new Resource Type [message #1726856 is a reply to message #1726824] Thu, 17 March 2016 04:10 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Aurel,

I can of course only guess at your specific details, but it seems
evident that a non-namespace schema is involved. If more than one such
is involved, it seems evident that you can't use the namespace (null
namespace) to distinguish the two cases.

I also don't think the arguments to RootXMLContentHandlerImpl are all
that sensible. E.g., the argument for the extensions should mention xml
has an extension shouldn't it.

And again, the plugin.xml registrations should handle this. Why do you
need to do it manually/programmatically?


On 17.03.2016 00:43, aurel pestell wrote:
> Using existing resource does not seem to work unfortunately.
> This code:
>
> XMLResource resource = (XMLResource)
> resourceSet.getResource(URI.createFileURI("C:\\yappclipse_2016_01_21\\yapp.capp.api\\resources\\fakeResponse.xml"),
> true);
>
> gives me:
>
> Quote:
>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException:
>> org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri
>> 'null' not found.
>
>
> Doing the registration does not help:
> resourceSet.getResourceFactoryRegistry().getContentTypeToFactoryMap().put(ExecutableFamiliesResponseGENPackage.eCONTENT_TYPE,
> new ExecutableFamiliesResponseGENResourceFactoryImpl());
> resourceSet.getURIConverter().getContentHandlers().add(new
> RootXMLContentHandlerImpl(ExecutableFamiliesResponseGENPackage.eCONTENT_TYPE,
> null, null, (String) null, null));
>
> resourceSet.getResourceFactoryRegistry().getContentTypeToFactoryMap().put(ExecutableFamiliesErrorResponseGENPackage.eCONTENT_TYPE,
> new ExecutableFamiliesErrorResponseGENResourceFactoryImpl());
> resourceSet.getURIConverter().getContentHandlers().add(new
> RootXMLContentHandlerImpl(ExecutableFamiliesErrorResponseGENPackage.eCONTENT_TYPE,
> null, null, (String) null, null));


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Creating a new Resource Type [message #1726879 is a reply to message #1726856] Thu, 17 March 2016 08:41 Go to previous messageGo to next message
aurel pestell is currently offline aurel pestellFriend
Messages: 90
Registered: October 2013
Location: Sweden
Member
Thank you Ed,

Yes it is hard for you to know without having the full project in hand.

Quote:
And again, the plugin.xml registrations should handle this. Why do you
need to do it manually/programmatically?


Oh that was just a small test in case something went wrong with the extensions but I am removing it.

Quote:
I can of course only guess at your specific details, but it seems
evident that a non-namespace schema is involved.


the first xml schema starts like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
         <!-- XML Schema Generated from XML Document on Thu Mar 03 2016 12:36:08 GMT+0100 (W. Europe Standard Time) -->
         <!-- with XmlGrid.net Free Online Service http://xmlgrid.net -->
         <xs:element name="Envelope">
               <xs:complexType>
                     <xs:sequence>
                           <xs:element name="Body">
                                 <xs:complexType>
                                       <xs:sequence>
                                             <xs:element name="SANDVIK_CAPPRunRRoutineResponse">


and the second one like that:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
         <!-- XML Schema Generated from XML Document on Tue Mar 15 2016 09:52:41 GMT+0100 (W. Europe Standard Time) -->
         <!-- with XmlGrid.net Free Online Service http://xmlgrid.net -->
         <xs:element name="Envelope">
               <xs:complexType>
                     <xs:sequence>
                           <xs:element name="Body">
                                 <xs:complexType>
                                       <xs:sequence>
                                             <xs:element name="SANDVIK_CAPPRunRRoutineResponse_2">


can it be the root of conflicts ?
Re: Creating a new Resource Type [message #1726925 is a reply to message #1726879] Thu, 17 March 2016 13:50 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Aurel,

Yes, I can only guess. But now you make it clear that neither schema
has a namespace, both have the same root element name, in each case with
a nested element named Body but each with a differently named complex
type. It's clear tjat there's no way simply way to determine the schema
simply by inspecting the instance.

On 17.03.2016 16:41, aurel pestell wrote:
> Thank you Ed,
>
> Yes it is hard for you to know without having the full project in hand.
>
> Quote:
>> And again, the plugin.xml registrations should handle this. Why do you
>> need to do it manually/programmatically?
>
>
> Oh that was just a small test in case something went wrong with the
> extensions but I am removing it.
>
> Quote:
>> I can of course only guess at your specific details, but it seems
>> evident that a non-namespace schema is involved.
>
>
> the first xml schema starts like this:
> <?xml version="1.0" encoding="UTF-8" standalone="no"?><xs:schema
> xmlns:xs="http://www.w3.org/2001/XMLSchema"
> attributeFormDefault="unqualified" elementFormDefault="qualified">
> <!-- XML Schema Generated from XML Document on Thu Mar 03 2016
> 12:36:08 GMT+0100 (W. Europe Standard Time) -->
> <!-- with XmlGrid.net Free Online Service http://xmlgrid.net -->
> <xs:element name="Envelope">
> <xs:complexType>
> <xs:sequence>
> <xs:element name="Body">
> <xs:complexType>
> <xs:sequence>
> <xs:element
> name="SANDVIK_CAPPRunRRoutineResponse">
>
> and the second one like that:
>
> <?xml version="1.0" encoding="UTF-8" standalone="no"?><xs:schema
> xmlns:xs="http://www.w3.org/2001/XMLSchema"
> attributeFormDefault="unqualified" elementFormDefault="qualified">
> <!-- XML Schema Generated from XML Document on Tue Mar 15 2016
> 09:52:41 GMT+0100 (W. Europe Standard Time) -->
> <!-- with XmlGrid.net Free Online Service http://xmlgrid.net -->
> <xs:element name="Envelope">
> <xs:complexType>
> <xs:sequence>
> <xs:element name="Body">
> <xs:complexType>
> <xs:sequence>
> <xs:element
> name="SANDVIK_CAPPRunRRoutineResponse_2">
>
> can it be the root of conflicts ?


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Creating a new Resource Type [message #1726960 is a reply to message #1726925] Thu, 17 March 2016 17:34 Go to previous message
aurel pestell is currently offline aurel pestellFriend
Messages: 90
Registered: October 2013
Location: Sweden
Member
I understand now. My problem boiled down to a xml namespace that does not give a difference between the two XMLs which share
the same first elements.
So, sorry, not really an EMF issue but then I learned about using Content Types, so thank you Ed !
Previous Topic:EMF encore to XMI
Next Topic:Project level model constraints
Goto Forum:
  


Current Time: Fri Apr 26 13:35:08 GMT 2024

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

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

Back to the top