Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » [EMF] Find out if a file is a model,(How to find out if a file represents a model (in general, not a particular model type).)
[EMF] Find out if a file is a model, [message #1064731] Thu, 20 June 2013 12:51 Go to next message
Juan Pedro Silva is currently offline Juan Pedro Silva
Messages: 258
Registered: July 2009
Senior Member
Hello everyone.
I am trying to find out (programmatically) if a file represents a model file (in general, no a model that corresponds to a particular metamodel). I am also trying to identify if a file is a diagram of some type, but the same reasoning applies.

It occurred to me that I could check if the editor associated to that file was a specialization of MultipageEditor (for instance). For that, I would do:
IEditorDescriptor  ied = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(iFile.getName());
and later something like:
Editor.class.isAssignableFrom(MultiPageEditorPart.class)

But there I found no way of getting the editor class or a reference to the editor from the IEditorDescriptor (at least through API) without opening an editor for each file. Checking against the registered editor's ID is unfeasible, as I would have to have an exhaustive list of all possible model editors in the world, which I can't.

Have anyone got any ideas of how to pull this off? I know this is not a bullet-proof approach, but it is the best approach that occurred to me. Checking for XMIResource would not distinguish models from diagrams, among other things. Any alternative suggestions would be greatly appreciated!

I look forward to your answer.
Thanks in advance!
JP

[Updated on: Thu, 20 June 2013 12:52]

Report message to a moderator

Re: [EMF] Find out if a file is a model, [message #1064753 is a reply to message #1064731] Thu, 20 June 2013 16:01 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 25747
Registered: July 2009
Senior Member
Juan,

You could try to load its URI (are these files in the workspace?) into a
resource set and, if it loads successfully, inspect the contents of the
resource.


On 20/06/2013 6:51 PM, Juan Pedro Silva wrote:
> Hello everyone.
> I am trying to find out (programmatically) if a file represents a
> model file (in general, no a model that corresponds to a particular
> metamodel). I am also trying to identify if a file is a diagram of
> some type, but the same reasoning applies.
>
> It occurred to me that I could check if the editor associated to that
> file was a specialization of MultipageEditor (for instance). For that,
> I would do:
>
> IEditorDescriptor ied =
> PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(iFile.getName());and
> later something like:
> Editor.class.isAssignableFrom(MultiPageEditorPart.class)
> But there is no way of getting the editor class or a reference to the
> editor from the IEditorDescriptor (at least through API) without
> opening an editor for each file. Checking against the registered
> editor's ID is unfeasible, as I would have to have an exhaustive list
> of all possible model editors in the world, which I can't.
> Have anyone got any ideas of how to pull this off? I know this is not
> a bullet-proof approach, but it is the best approach that occurred to
> me. Checking for XMIResource would not distinguish models from
> diagrams, among other things. Any alternative suggestions would be
> greatly appreciated!
>
> I look forward to your answer.
> Thanks in advance!
> JP
Re: [EMF] Find out if a file is a model, [message #1064771 is a reply to message #1064731] Thu, 20 June 2013 19:15 Go to previous messageGo to next message
Christian W. Damus is currently offline Christian W. Damus
Messages: 701
Registered: July 2009
Senior Member
Hi, JP,

EMF's support for content types should help with this. If your
genmodel specifies a content-type ID, then EMF will generate a
content-type describer extension that lets the platform's
IContentTypeManager recognize the content type of files that store
instances of your model.

HTH,

Christian


On 2013-06-20 16:51:09 +0000, Juan Pedro Silva said:

> Hello everyone.
> I am trying to find out (programmatically) if a file represents a model
> file (in general, no a model that corresponds to a particular
> metamodel). I am also trying to identify if a file is a diagram of some
> type, but the same reasoning applies.
>
> It occurred to me that I could check if the editor associated to that
> file was a specialization of MultipageEditor (for instance). For that,
> I would do:
>
> IEditorDescriptor ied =
> PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(iFile.getName());and
> later something like:
> Editor.class.isAssignableFrom(MultiPageEditorPart.class)
> But there is no way of getting the editor class or a reference to the
> editor from the IEditorDescriptor (at least through API) without
> opening an editor for each file. Checking against the registered
> editor's ID is unfeasible, as I would have to have an exhaustive list
> of all possible model editors in the world, which I can't.
> Have anyone got any ideas of how to pull this off? I know this is not a
> bullet-proof approach, but it is the best approach that occurred to me.
> Checking for XMIResource would not distinguish models from diagrams,
> among other things. Any alternative suggestions would be greatly
> appreciated!
>
> I look forward to your answer.
> Thanks in advance!
> JP
Re: [EMF] Find out if a file is a model, [message #1064836 is a reply to message #1064771] Fri, 21 June 2013 06:11 Go to previous messageGo to next message
Juan Pedro Silva is currently offline Juan Pedro Silva
Messages: 258
Registered: July 2009
Senior Member
Thanks Ed and Christian for your quick reply.
Both approaches are fine with me, but I need a little more details to grasp them completely.

Ed, yes, the files are within the workspace. Lets say I load the resource to inspect its contents, and it is a XMI or XML resource. Can I safely assume that all (most) models made with EMF will contain Ecore's namespace in their headers?, or would you perform a different check?

Christian, is there a base (or generic) content type that is used by default?, or does the content type follow a hierarchy?. I like your approach, but I don't know if is there a way of avoiding having to check for all content types' IDs declared for all metamodels (not just the ones I made). I see an old thread, by Ed and others, on the subject of content types and their base types (here), and I followed the referred bug, but I am not certain that org.eclipse.emf.ecore.xmi is the base type for all models, and I cant' find content types declarations in the different metamodel's plugin.xml I've got around to verify that.

Thanks again to both of you for your help.
Regards,
JP

[Updated on: Fri, 21 June 2013 06:46]

Report message to a moderator

Re: [EMF] Find out if a file is a model, [message #1064842 is a reply to message #1064836] Fri, 21 June 2013 06:25 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 25747
Registered: July 2009
Senior Member
Juan,

Comments below.

On 21/06/2013 12:11 PM, Juan Pedro Silva wrote:
> Thanks Ed and Christian for your quick reply.
> Both approaches are fine with me, but I need a little more details to
> grasp them completely.
>
> Ed, yes, the files are within the workspace. Lets say I load the
> resource to inspect its contents, and it is a XMI or XML resource. Can
> I safely assume that all (most) models made with EMF will contain
> Ecore's namespace in their headers?, or would you perform a different
> check?
If it loads, you can inspect the contents themselves. E.g., they'll be
EObjects so you can get their eClass() and from that getEPackage to see
which model it's from.
>
> Christian, is there a default (or generic) content type declared by
> genmodels?, or does the content type ID follow a hierarchical
> nomenclature?.
Not all models define content types and in fact most don't but rather
register a factory based on file extension.
> I like your approach, but I don't know if is there a way of avoiding
> having to check for all IDs declared for all metamodels (not just the
> ones I made). I may follow an hybrid approach: use the content type to
> identify models corresponding to known metamodels, and load resources
> for unknown ones.
Also, Xtext defines models that have a textual representation, so you
can't just rely on assuming there will be XML content. You can look at
org.eclipse.emf.ecore.xmi.impl.RootXMLContentHandlerImpl.contentDescription(URI,
InputStream, Map<?, ?>, Map<Object, Object>) to see how content
descriptions are created by scanning the root element of the XML.
>
> Thanks again to both of you for your help.
> Regards,
> JP
Re: [EMF] Find out if a file is a model, [message #1064846 is a reply to message #1064842] Fri, 21 June 2013 06:47 Go to previous messageGo to next message
Juan Pedro Silva is currently offline Juan Pedro Silva
Messages: 258
Registered: July 2009
Senior Member
Thanks, Ed.
Great help!
Re: [EMF] Find out if a file is a model, [message #1064901 is a reply to message #1064731] Fri, 21 June 2013 10:57 Go to previous messageGo to next message
Juan Pedro Silva is currently offline Juan Pedro Silva
Messages: 258
Registered: July 2009
Senior Member
Ed, it got it working. Just a derived question.

I load the resources like this:
try {
ResourceSet fResourceSet = new ResourceSetImpl();
fResourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", new XMLResourceFactoryImpl());
org.eclipse.emf.ecore.resource.Resource resource = fResourceSet.getResource(uri, true);
...
}catch(Exception e) {...}

Everything works as expected (it either loads a resource, or throws an exception) with all type of files except for svg images. When getting an svg resource, it sorts of enters a loop in line 242 from org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl:
xmlLoad.load(this, inputStream, options);
which never returns.

I can avoid this by checking for file extension, but is there some other more elegant way to prevent it?, like registering a resource factory for "svg"? I could find none.

Thanks again.
Re: [EMF] Find out if a file is a model, [message #1064906 is a reply to message #1064901] Fri, 21 June 2013 11:05 Go to previous message
Ed Merks is currently offline Ed Merks
Messages: 25747
Registered: July 2009
Senior Member
Juan,

Comments below.

On 21/06/2013 4:57 PM, Juan Pedro Silva wrote:
> Ed, it got it working. Just a derived question.
>
> I load the resources like this:
>
> try {
> ResourceSet fResourceSet = new ResourceSetImpl();
> fResourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*",
> new XMLResourceFactoryImpl());
I assume you're running this in the IDE, in which case all the factories
you need should already be registered. So you should not do this
registration but rather allow the registered factory to do its job...
There is already a global default registration to use
XMIResourceFactoryImpl.
> org.eclipse.emf.ecore.resource.Resource resource =
> fResourceSet.getResource(uri, true);
> ..
> }catch(Exception e) {...}
> Everything works as expected (it either loads a resource, or throws an
> exception) with all type of files except for svg images. When getting
> an svg resource, it sorts of enters a loop in line 242 from
> org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl:
> xmlLoad.load(this, inputStream, options); which never returns.
There's no loop here so the stack must be deeper... Is an SVG image an
XML file? It might contain all sorts of XML things that are expensive
to load... Loading it as XMI might not have the same problem...
>
> I can avoid this by checking for file extension, but is there some
> other more elegant way to prevent it?, like registering a resource
> factory for "svg"? I could find none.
>
> Thanks again.
Previous Topic:Xcore syntax for object creation and initialization
Next Topic:How to correctly compare EDataType?
Goto Forum:
  


Current Time: Sun Apr 20 12:29:55 EDT 2014

Powered by FUDForum. Page generated in 0.04987 seconds