|
|
|
Re: how do I go from an ecore uri to genmodel object [message #420121 is a reply to message #420113] |
Wed, 18 June 2008 15:31 |
Ed Merks Messages: 33142 Registered: July 2009 |
Senior Member |
|
|
Gary,
Comments below.
gary thompson wrote:
> Ed Merks wrote:
>
>> Gary,
>
>> Comments below.
>
>> gary thompson wrote:
>>> Dear All
>>> As usual I am perplexed ;-)
>> Then you've come to the right place. :-P
>>> I would like to convert from a uri in a annotation to a genmodel
>>> object within a jet template could someone suggest how i go about it??
>>>
>>> e.g. I have ecore:EDataType
>>> http://www.eclipse.org/emf/2002/Ecore#//EString and would like to
>>> find the relevant genmodel element a so i can get the class name.
>> So first you'd want to get the actual EDataType instance.
>> ResourceSet.getEObject given the URI
>> "http://www.eclipse.org/emf/2002/Ecore#//EString" should locate it
>> and then GenModel.findGenClassifier should be able to find the
>> GenModel wrapper for it.
>>>
>>> As a passing point I am using annotations to create private internal
>>> implementation fields within my emitted classes via jet dynamic
>>> templates. Is there a better way to do it? (i.e. can i get the emf
>>> framework to do more)
>> That seems like the only way, which makes it the best way.
>>>
>>>
>>> regards
>>> gary
>>>
>
>
> Well that seemed to work (almost) so the code i came up with is <% int
> xxxx_lastGenFeatureIndex =
> genClass.getDeclaredFieldGenFeatures().size()-1;
> if (genFeature ==
> genClass.getDeclaredFieldGenFeatures().get(xxxx_lastGenFeatu reIndex)) {
> for (org.eclipse.emf.ecore.EAnnotation xxxx_annotation:
> genClass.getEcoreModelElement().getEAnnotations()) {
> if
> (xxxx_annotation.getSource().equals("http://www.leeds.ac.uk/xxxx-ecore-1.0/xxxx_genmodel"))
> {
> org.eclipse.emf.ecore.EAnnotation xxxx_staticAnnotation =
> xxxx_annotation.getEAnnotation("static");
> if (xxxx_staticAnnotation != null) {
> String xxxx_staticModifiers = (String)
> xxxx_staticAnnotation.getDetails().get("modifiers");
> String xxxx_staticName = (String)
> xxxx_staticAnnotation.getDetails().get("name");
> String xxxx_staticType = (String)
> xxxx_staticAnnotation.getDetails().get("type");
> String xxxx_staticDefault = (String)
> xxxx_staticAnnotation.getDetails().get("default");
> String[] xxxx_staticTypeFields =
> xxxx_staticType.split("\\s+");
> int xxxx_n_type_strings = xxxx_staticTypeFields.length;
> org.eclipse.emf.common.util.URI xxxx_staticTypeURI =
> org.eclipse.emf.common.util.URI.createURI(xxxx_staticTypeFie lds[xxxx_n_type_strings-1]);
> org.eclipse.emf.ecore.EClassifier xxxx_eclassifier =
> ((org.eclipse.emf.ecore.EClassifier)genModel.eResource().get ResourceSet().getEObject(xxxx_staticTypeURI,true));
>
>
> genModel.addImport(xxxx_eclassifier.getInstanceClass().getNa me());
> GenDataType xxxx_genDataType =
> (GenDataType) genModel.findGenClassifier(xxxx_eclassifier);
> String xxxx_className =
> xxxx_genDataType.getImportedInstanceClassName();
>
> // todo add documentation handler %>
> /**
> * todo add documentation code
> * @generated
> */ <%=xxxx_staticModifiers%> <%=xxxx_className%>
> <%=xxxx_staticName%> = <%=xxxx_staticDefault%>;
>
> and this mostly works as we have uris of the form
It's like an eye chart!
>
> ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString
>
> however for my internal data types i.e datatypes I define in my ecore
> model the references to EDataTypes that work in most places which have
> the form #//ECSet don't work here. This appears to be because they
> are not complete uris they are just references within the current
> document and the code
> org.eclipse.emf.common.util.URI.createURI(xxxx_staticTypeFie lds[xxxx_n_type_strings-1])
>
So you could check for that (uri.trimFragment().isEmpty()) and replace
the empty URI with the URI of the containing resource before you
continue with the lookup.
>
> fails with a file not found exception wrapped in an invocation target
> exception. As usual any help gratefully received!
>
>
> regards
> gary
>
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Powered by
FUDForum. Page generated in 0.02994 seconds