Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Reading Xtext model file in Java Apps
Reading Xtext model file in Java Apps [message #62206] Wed, 22 July 2009 04:08 Go to next message
Eclipse UserFriend
Originally posted by: ilyas.keser.gmail.com

Hi @all,

how can I access a Xtext model file in a java application? I am trying
to read it with:

@SuppressWarnings("unused")
ConfDSLPackage confDSLPackage = onfDSLPackage.eINSTANCE;

ResourceSet resourceSet = new ResourceSetImpl();

resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put( ".conf",
new ConfDSLFactoryImpl());

URI uri =
URI.createURI(" platform:/resource/myproject/src/de/test/mydsl/Configuration .conf ");
Resource resource = resourceSet.createResource(uri);

try {
resource.load(null);
Configuration conf = (Configuration)resource.getContents().get(0);
} catch (Exception e) {
System.out.println("Failed to read " + uri);
}

resource.load(null) throws an exception. Any ideas?

Thanks
Ilyas
Re: Reading Xtext model file in Java Apps [message #62258 is a reply to message #62206] Wed, 22 July 2009 04:29 Go to previous messageGo to next message
Eclipse UserFriend
Hi Ilyas,

maybe you want to be more specific about the exception that you catch.
Otherwise it is hardly possible to answer your question.

Where does the class ConfDSLFactoryImpl come from? Do you mix up an
Editor that was generated by EMF and an Xtext-based DSL file?

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 22.07.2009 10:08 Uhr, schrieb Ilyas Keser:
> Hi @all,
>
> how can I access a Xtext model file in a java application? I am trying
> to read it with:
>
> @SuppressWarnings("unused")
> ConfDSLPackage confDSLPackage = onfDSLPackage.eINSTANCE;
>
> ResourceSet resourceSet = new ResourceSetImpl();
> resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put( ".conf",
> new ConfDSLFactoryImpl());
>
> URI uri =
> URI.createURI(" platform:/resource/myproject/src/de/test/mydsl/Configuration .conf ");
>
> Resource resource = resourceSet.createResource(uri);
>
> try {
> resource.load(null);
> Configuration conf = (Configuration)resource.getContents().get(0);
> } catch (Exception e) {
> System.out.println("Failed to read " + uri);
> }
>
> resource.load(null) throws an exception. Any ideas?
>
> Thanks
> Ilyas
Re: Reading Xtext model file in Java Apps [message #62282 is a reply to message #62206] Wed, 22 July 2009 04:30 Go to previous messageGo to next message
Eclipse UserFriend
Hi Ilyas,

You should use the ConfDSLStandaloneSetup for this purpose. It takes care
of registering the model and resoruce factory (which btw should map "conf"
and not ".conf") and initializes the required Google Guice injector. See
the following FAQ entry for an example:
http://wiki.eclipse.org/Xtext/FAQ#How_do_I_load_my_model_in_ a_standalone_Java_application.C2.A0.3F.

Hope that helps,

--knut

Ilyas Keser wrote:

> Hi @all,

> how can I access a Xtext model file in a java application? I am trying
> to read it with:

> @SuppressWarnings("unused")
> ConfDSLPackage confDSLPackage = onfDSLPackage.eINSTANCE;

> ResourceSet resourceSet = new ResourceSetImpl();

>
resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put( ".conf",
> new ConfDSLFactoryImpl());

> URI uri =
>
URI.createURI(" platform:/resource/myproject/src/de/test/mydsl/Configuration .conf ");
> Resource resource = resourceSet.createResource(uri);

> try {
> resource.load(null);
> Configuration conf = (Configuration)resource.getContents().get(0);
> } catch (Exception e) {
> System.out.println("Failed to read " + uri);
> }

> resource.load(null) throws an exception. Any ideas?

> Thanks
> Ilyas
Re: Reading Xtext model file in Java Apps [message #62306 is a reply to message #62258] Wed, 22 July 2009 04:50 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: ilyas.keser.gmail.com

Hi Sebastian,

the thrown exception is a NPE and resourceSet.createResource(uri)
returns null.

ConfDSLFactoryImpl is generated by Xtext and is in the package
.../src-gen/de/test/mydsl/confDSL.impl.

I am using a Xtext grammar to define a configuration file. After
generating the Xtext artefacts, I created a model file with generated
DSL editor. Just, I am trying to read/access this model file in a Java
Application.

Regards,
ILyas



Sebastian Zarnekow schrieb:
> Hi Ilyas,
>
> maybe you want to be more specific about the exception that you catch.
> Otherwise it is hardly possible to answer your question.
>
> Where does the class ConfDSLFactoryImpl come from? Do you mix up an
> Editor that was generated by EMF and an Xtext-based DSL file?
>
> Regards,
> Sebastian
Re: Reading Xtext model file in Java Apps [message #62379 is a reply to message #62306] Wed, 22 July 2009 05:29 Go to previous messageGo to next message
Eclipse UserFriend
Hi Ilyas,

the ConfDSLFactoryImpl is not a ResourceFactory but an EFactory. It is
not intendent to put an EFactory into the FactoryToExtensionMap.
Please try Knut's suggestion. It should work fine.

Hope that helps,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com


Am 22.07.2009 10:50 Uhr, schrieb Ilyas Keser:
> Hi Sebastian,
>
> the thrown exception is a NPE and resourceSet.createResource(uri)
> returns null.
>
> ConfDSLFactoryImpl is generated by Xtext and is in the package
> .../src-gen/de/test/mydsl/confDSL.impl.
>
> I am using a Xtext grammar to define a configuration file. After
> generating the Xtext artefacts, I created a model file with generated
> DSL editor. Just, I am trying to read/access this model file in a Java
> Application.
>
> Regards,
> ILyas
>
>
>
> Sebastian Zarnekow schrieb:
>> Hi Ilyas,
>>
>> maybe you want to be more specific about the exception that you catch.
>> Otherwise it is hardly possible to answer your question.
>>
>> Where does the class ConfDSLFactoryImpl come from? Do you mix up an
>> Editor that was generated by EMF and an Xtext-based DSL file?
>>
>> Regards,
>> Sebastian
Re: Reading Xtext model file in Java Apps [message #62403 is a reply to message #62379] Wed, 22 July 2009 06:03 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: ilyas.keser.gmail.com

Sebastian & Knut, thanks for your replies. It works for me.

The method createInjectorAndDoEMFRegistration() isn't static anymore. Is
it OK, if I modify the first line of the snippet (in wiki) as follows:

Injector injector = new
ConfDSLStandaloneSetup().createInjectorAndDoEMFRegistration( );

Regards,
ILyas


Sebastian Zarnekow schrieb:
> Hi Ilyas,
>
> the ConfDSLFactoryImpl is not a ResourceFactory but an EFactory. It is
> not intendent to put an EFactory into the FactoryToExtensionMap.
> Please try Knut's suggestion. It should work fine.
>
> Hope that helps,
> Sebastian
Re: Reading Xtext model file in Java Apps [message #62427 is a reply to message #62403] Wed, 22 July 2009 07:34 Go to previous message
Eclipse UserFriend
Hi Ilyas,

please feel free to update the wiki.

Thanks,
Sebastian

--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 22.07.2009 12:03 Uhr, schrieb Ilyas Keser:
> Sebastian & Knut, thanks for your replies. It works for me.
>
> The method createInjectorAndDoEMFRegistration() isn't static anymore. Is
> it OK, if I modify the first line of the snippet (in wiki) as follows:
>
> Injector injector = new
> ConfDSLStandaloneSetup().createInjectorAndDoEMFRegistration( );
>
> Regards,
> ILyas
>
>
> Sebastian Zarnekow schrieb:
>> Hi Ilyas,
>>
>> the ConfDSLFactoryImpl is not a ResourceFactory but an EFactory. It is
>> not intendent to put an EFactory into the FactoryToExtensionMap.
>> Please try Knut's suggestion. It should work fine.
>>
>> Hope that helps,
>> Sebastian
Previous Topic:[Xtext-0.7.1] some problems with the first steps
Next Topic:[Xtext] MWE problem: Allowing backtracing in AntlrDelegating fragment yields to error
Goto Forum:
  


Current Time: Wed May 07 19:08:09 EDT 2025

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

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

Back to the top