Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » a specific extension to parse a textual model
a specific extension to parse a textual model [message #657584] Thu, 03 March 2011 11:16 Go to next message
Fi Fi is currently offline Fi Fi
Messages: 133
Registered: March 2010
Senior Member
Hi All,
I have this code that parses my textual model to an xmi model
package model;





import java.io.IOException;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.xtext.example.PrescenarioStandaloneSetup;





public class Main {

	public static void main(String[] args) throws IOException {
		PrescenarioStandaloneSetup.doSetup();
		ResourceSet resourceSet = new ResourceSetImpl();
		Resource resource = resourceSet.createResource(URI.createURI("src/model/a.scn"));
		resource.load(null);
		Resource resourceAsXmi = resourceSet.createResource(URI.createURI("src/model/aaa.xmi"));
		resourceAsXmi.getContents().addAll(resource.getContents());
		resourceAsXmi.save(null);
	}

}


now, I 'd like to change the xmi extension with my specific extension ".prescenario"
yet, I had a specific editor (in Example EMF model Creation wizards) generated from an EMF project that support this extension.
but when I change the extension , I have this exception
Exception in thread "main" java.lang.NullPointerException
	at model.Main.main(Main.java:27)

please help my
thanks

[Updated on: Thu, 03 March 2011 11:17]

Report message to a moderator

Re: a specific extension after parsing an [message #657764 is a reply to message #657584] Thu, 03 March 2011 21:59 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian Zarnekow
Messages: 2908
Registered: July 2009
Senior Member
Hi Faiez,

please make sure that the resource factory for your specific extension
is properly registered.

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

Am 03.03.11 12:16, schrieb Faiez:
> Hi All,
> I have this code that parses my textual model to an xmi model package
> model;
>
>
>
>
>
> import java.io.IOException;
>
> import org.eclipse.emf.common.util.URI;
> import org.eclipse.emf.ecore.resource.Resource;
> import org.eclipse.emf.ecore.resource.ResourceSet;
> import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
> import org.xtext.example.PrescenarioStandaloneSetup;
>
>
>
>
>
> public class Main {
>
> public static void main(String[] args) throws IOException {
> PrescenarioStandaloneSetup.doSetup();
> ResourceSet resourceSet = new ResourceSetImpl();
> Resource resource =
> resourceSet.createResource(URI.createURI("src/model/a.scn"));
> resource.load(null);
> Resource resourceAsXmi =
> resourceSet.createResource(URI.createURI("src/model/aaa.xmi "));
> resourceAsXmi.getContents().addAll(resource.getContents());
> resourceAsXmi.save(null);
> }
>
> }
>
> now, I 'd like to change the xmi extension with my specific extension
> ".prescenario" yet, I had a specific editor (in Example EMF model
> Creation wizards) generated from an EMF project that support this
> extension.
> but when I change the extension , I have this exception Exception in
> thread "main" java.lang.NullPointerException
> at model.Main.main(Main.java:27)
>
> please help my
> thanks
Re: a specific extension after parsing an [message #657820 is a reply to message #657764] Fri, 04 March 2011 08:54 Go to previous messageGo to next message
Fi Fi is currently offline Fi Fi
Messages: 133
Registered: March 2010
Senior Member
Hi Sebastian,
How can I register resource Factory ?
In what class ?
In what project(EMF project or xtext project )?
this is my PrescenarioStandaloneSetup class in xtext project.
as you see, my textual extension is .scn
I 'd define the model extension after parsing the textual model (by default .xmi)

package org.xtext.example;

import org.eclipse.emf.ecore.EPackage;
import org.eclipse.xtext.ISetup;
import org.eclipse.emf.ecore.resource.Resource;

import com.google.inject.Guice;
import com.google.inject.Injector;

/**
 * Generated from StandaloneSetup.xpt!
 */
@SuppressWarnings("all")
public class PrescenarioStandaloneSetupGenerated implements ISetup {

	public Injector createInjectorAndDoEMFRegistration() {
		org.eclipse.xtext.common.TerminalsStandaloneSetup.doSetup();

		Injector injector = createInjector();
		register(injector);
		return injector;
	}
	
	public Injector createInjector() {
		return Guice.createInjector(new org.xtext.example.PrescenarioRuntimeModule());
	}
	
	public void register(Injector injector) {
	if (!EPackage.Registry.INSTANCE.containsKey("http://www.xtext.org/example/Prescenario")) {
		EPackage.Registry.INSTANCE.put("http://www.xtext.org/example/Prescenario", org.xtext.example.prescenario.PrescenarioPackage.eINSTANCE);
	}

		org.eclipse.xtext.resource.IResourceFactory resourceFactory = injector.getInstance(org.eclipse.xtext.resource.IResourceFactory.class);
		org.eclipse.xtext.resource.IResourceServiceProvider serviceProvider = injector.getInstance(org.eclipse.xtext.resource.IResourceServiceProvider.class);
		Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("scn", resourceFactory);
		org.eclipse.xtext.resource.IResourceServiceProvider.Registry.INSTANCE.getExtensionToFactoryMap().put("scn", serviceProvider);
		




	}
}
Re: a specific extension after parsing an [message #657842 is a reply to message #657820] Fri, 04 March 2011 10:16 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian Zarnekow
Messages: 2908
Registered: July 2009
Senior Member
Hi Faiez,

override the register(Injector) method of the
PrescenarioStandaloneSetupGenerated in PrescenarioStandaloneSetup and
add lines like

Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put(
"prescenario", PrescenarioFactory.eINSTANCE).

Please look at the plugin.xml of the project that contains the model
code that was produced from the EMF model creation wizard for the exact
class name of the EPackage and the EFactory.
The EMF book will provide a lot more insight in these mechanisms.

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

Am 04.03.11 09:54, schrieb Faiez:
> Hi Sebastian,
> How can I register resource Factory ?
> In what class ?
> In what project(EMF project or xtext project )?
> this is my PrescenarioStandaloneSetup class in xtext project.
> as you see, my textual extension is .scn
> I 'd define the model extension after parsing the textual model (by
> default .xmi)
>
>
> package org.xtext.example;
>
> import org.eclipse.emf.ecore.EPackage;
> import org.eclipse.xtext.ISetup;
> import org.eclipse.emf.ecore.resource.Resource;
>
> import com.google.inject.Guice;
> import com.google.inject.Injector;
>
> /**
> * Generated from StandaloneSetup.xpt!
> */
> @SuppressWarnings("all")
> public class PrescenarioStandaloneSetupGenerated implements ISetup {
>
> public Injector createInjectorAndDoEMFRegistration() {
> org.eclipse.xtext.common.TerminalsStandaloneSetup.doSetup();
>
> Injector injector = createInjector();
> register(injector);
> return injector;
> }
>
> public Injector createInjector() {
> return Guice.createInjector(new
> org.xtext.example.PrescenarioRuntimeModule());
> }
>
> public void register(Injector injector) {
> if
> (!EPackage.Registry.INSTANCE.containsKey("http://www.xtext.org/example/Prescenario"))
> {
> EPackage.Registry.INSTANCE.put("http://www.xtext.org/example/Prescenario",
> org.xtext.example.prescenario.PrescenarioPackage.eINSTANCE);
> }
>
> org.eclipse.xtext.resource.IResourceFactory resourceFactory =
> injector.getInstance(org.eclipse.xtext.resource.IResourceFac tory.class);
> org.eclipse.xtext.resource.IResourceServiceProvider serviceProvider =
> injector.getInstance(org.eclipse.xtext.resource.IResourceSer viceProvider.class);
>
> Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "scn",
> resourceFactory);
> org.eclipse.xtext.resource.IResourceServiceProvider.Registry .INSTANCE.getExtensionToFactoryMap().put( "scn",
> serviceProvider);
>
>
>
>
>
> }
> }
>
Re: a specific extension after parsing an [message #657900 is a reply to message #657842] Fri, 04 March 2011 14:48 Go to previous messageGo to next message
Fi Fi is currently offline Fi Fi
Messages: 133
Registered: March 2010
Senior Member
Hi Sebastian,
this is the plugin.xml
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>

<!--
 <copyright>
 </copyright>

 $Id$
-->
<plugin>

   <extension point="org.eclipse.emf.ecore.generated_package">
      <package
            uri="http://www.xtext.org/example/Prescenario"
            class="prescenario.PrescenarioPackage"
            genModel="model/Prescenario.genmodel"/>
   </extension>

</plugin>

I add this line
		Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put("prescenario", prescenario.PrescenarioPackage.eINSTANCE);


in the injector class that will be

package org.xtext.example;

import org.eclipse.emf.ecore.EPackage;
import org.eclipse.xtext.ISetup;
import org.eclipse.emf.ecore.resource.Resource;
import com.google.inject.Guice;
import com.google.inject.Injector;

/**
 * Generated from StandaloneSetup.xpt!
 */
@SuppressWarnings("all")
public class PrescenarioStandaloneSetupGenerated implements ISetup {

	public Injector createInjectorAndDoEMFRegistration() {
		org.eclipse.xtext.common.TerminalsStandaloneSetup.doSetup();

		Injector injector = createInjector();
		register(injector);
		return injector;
	}
	
	public Injector createInjector() {
		return Guice.createInjector(new org.xtext.example.PrescenarioRuntimeModule());
	}
	
	public void register(Injector injector) {
	if (!EPackage.Registry.INSTANCE.containsKey("http://www.xtext.org/example/Prescenario")) {
		EPackage.Registry.INSTANCE.put("http://www.xtext.org/example/Prescenario", org.xtext.example.prescenario.PrescenarioPackage.eINSTANCE);
	}

		org.eclipse.xtext.resource.IResourceFactory resourceFactory = injector.getInstance(org.eclipse.xtext.resource.IResourceFactory.class);
		org.eclipse.xtext.resource.IResourceServiceProvider serviceProvider = injector.getInstance(org.eclipse.xtext.resource.IResourceServiceProvider.class);
		Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("scn", resourceFactory);
		org.eclipse.xtext.resource.IResourceServiceProvider.Registry.INSTANCE.getExtensionToFactoryMap().put("scn", serviceProvider);
		Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put("prescenario", prescenario.PrescenarioPackage.eINSTANCE);


	}
}

but when I 'd parse my .scn file with this code
package model;





import java.io.IOException;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.xtext.example.PrescenarioStandaloneSetup;





public class Main {

	public static void main(String[] args) throws IOException {
		PrescenarioStandaloneSetup.doSetup();
		ResourceSet resourceSet = new ResourceSetImpl();
		Resource resource = resourceSet.createResource(URI.createURI("src/model/a.scn"));
		resource.load(null);
		Resource resourceAsXmi = resourceSet.createResource(URI.createURI("src/model/zzz.prescenario"));
		resourceAsXmi.getContents().addAll(resource.getContents());
		resourceAsXmi.save(null);
	}

}

I find this error
java.lang.ClassCastException: org.xtext.example.prescenario.impl.PrescenarioFactoryImpl cannot be cast to prescenario.PrescenarioFactory
	at prescenario.impl.PrescenarioFactoryImpl.init(PrescenarioFactoryImpl.java:34)
	at prescenario.PrescenarioFactory.<clinit>(PrescenarioFactory.java:26)
	at prescenario.impl.PrescenarioPackageImpl.<init>(PrescenarioPackageImpl.java:98)
	at prescenario.impl.PrescenarioPackageImpl.init(PrescenarioPackageImpl.java:124)
	at prescenario.PrescenarioPackage.<clinit>(PrescenarioPackage.java:60)
	at org.xtext.example.PrescenarioStandaloneSetupGenerated.register(PrescenarioStandaloneSetupGenerated.java:38)
	at org.xtext.example.PrescenarioStandaloneSetupGenerated.createInjectorAndDoEMFRegistration(PrescenarioStandaloneSetupGenerated.java:21)
	at org.xtext.example.PrescenarioStandaloneSetup.doSetup(PrescenarioStandaloneSetup.java:11)
	at model.Main.main(Main.java:22)
Exception in thread "main" java.lang.ClassCastException: prescenario.impl.PrescenarioPackageImpl cannot be cast to org.eclipse.emf.ecore.resource.Resource$Factory
	at org.eclipse.emf.ecore.resource.impl.ResourceFactoryRegistryImpl.convert(ResourceFactoryRegistryImpl.java:100)
	at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$2.delegatedGetFactory(ResourceSetImpl.java:449)
	at org.eclipse.emf.ecore.resource.impl.ResourceFactoryRegistryImpl.getFactory(ResourceFactoryRegistryImpl.java:151)
	at org.eclipse.emf.ecore.resource.impl.ResourceFactoryRegistryImpl.getFactory(ResourceFactoryRegistryImpl.java:92)
	at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.createResource(ResourceSetImpl.java:422)
	at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.createResource(ResourceSetImpl.java:414)
	at model.Main.main(Main.java:26)


please help me.
thanks

[Updated on: Fri, 04 March 2011 14:50]

Report message to a moderator

Re: a specific extension after parsing an [message #657914 is a reply to message #657900] Fri, 04 March 2011 15:17 Go to previous message
Sebastian Zarnekow is currently offline Sebastian Zarnekow
Messages: 2908
Registered: July 2009
Senior Member
Hi Faiez,

you'll to register a factory and the package in this line:

Instead of
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap(
).put("prescenario", prescenario.PrescenarioPackage.eINSTANCE);

you'll have to use
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap(
).put("prescenario", prescenario.PrescenarioFactory.eINSTANCE);

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

Am 04.03.11 15:48, schrieb Faiez:
> Hi Sebastian,
> this is the plugin.xml
> <?xml version="1.0" encoding="UTF-8"?>
> <?eclipse version="3.0"?>
>
> <!--
> <copyright>
> </copyright>
>
> $Id$
> -->
> <plugin>
>
> <extension point="org.eclipse.emf.ecore.generated_package">
> <package
> uri="http://www.xtext.org/example/Prescenario"
> class="prescenario.PrescenarioPackage"
> genModel="model/Prescenario.genmodel"/>
> </extension>
>
> </plugin>
>
> I add this line
> Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap(
> ).put("prescenario", prescenario.PrescenarioPackage.eINSTANCE);
>
>
> in the injector class that will be
>
> package org.xtext.example;
>
> import org.eclipse.emf.ecore.EPackage;
> import org.eclipse.xtext.ISetup;
> import org.eclipse.emf.ecore.resource.Resource;
> import com.google.inject.Guice;
> import com.google.inject.Injector;
>
> /**
> * Generated from StandaloneSetup.xpt!
> */
> @SuppressWarnings("all")
> public class PrescenarioStandaloneSetupGenerated implements ISetup {
>
> public Injector createInjectorAndDoEMFRegistration() {
> org.eclipse.xtext.common.TerminalsStandaloneSetup.doSetup();
>
> Injector injector = createInjector();
> register(injector);
> return injector;
> }
>
> public Injector createInjector() {
> return Guice.createInjector(new
> org.xtext.example.PrescenarioRuntimeModule());
> }
>
> public void register(Injector injector) {
> if
> (!EPackage.Registry.INSTANCE.containsKey("http://www.xtext.org/example/Prescenario"))
> {
> EPackage.Registry.INSTANCE.put("http://www.xtext.org/example/Prescenario",
> org.xtext.example.prescenario.PrescenarioPackage.eINSTANCE);
> }
>
> org.eclipse.xtext.resource.IResourceFactory resourceFactory =
> injector.getInstance(org.eclipse.xtext.resource.IResourceFac tory.class);
> org.eclipse.xtext.resource.IResourceServiceProvider serviceProvider =
> injector.getInstance(org.eclipse.xtext.resource.IResourceSer viceProvider.class);
>
> Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "scn",
> resourceFactory);
> org.eclipse.xtext.resource.IResourceServiceProvider.Registry .INSTANCE.getExtensionToFactoryMap().put( "scn",
> serviceProvider);
> Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap(
> ).put("prescenario", prescenario.PrescenarioPackage.eINSTANCE);
>
>
> }
> }
>
> but when I 'd parse my .scn file with this code
> package model;
>
>
>
>
>
> import java.io.IOException;
>
> import org.eclipse.emf.common.util.URI;
> import org.eclipse.emf.ecore.resource.Resource;
> import org.eclipse.emf.ecore.resource.ResourceSet;
> import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
> import org.xtext.example.PrescenarioStandaloneSetup;
>
>
>
>
>
> public class Main {
>
> public static void main(String[] args) throws IOException {
> PrescenarioStandaloneSetup.doSetup();
> ResourceSet resourceSet = new ResourceSetImpl();
> Resource resource =
> resourceSet.createResource(URI.createURI("src/model/a.scn"));
> resource.load(null);
> Resource resourceAsXmi =
> resourceSet.createResource(URI.createURI("src/model/zzz.prescenario "));
> resourceAsXmi.getContents().addAll(resource.getContents());
> resourceAsXmi.save(null);
> }
>
> }
>
> I find this error
> java.lang.ClassCastException:
> org.xtext.example.prescenario.impl.PrescenarioFactoryImpl cannot be cast
> to prescenario.PrescenarioFactory
> at
> prescenario.impl.PrescenarioFactoryImpl.init(PrescenarioFact oryImpl.java:34)
>
> at prescenario.PrescenarioFactory.<clinit>(PrescenarioFactory.java:26)
> at
> prescenario.impl.PrescenarioPackageImpl.<init>(PrescenarioPackageImpl.java:98)
>
> at
> prescenario.impl.PrescenarioPackageImpl.init(PrescenarioPack ageImpl.java:124)
>
> at prescenario.PrescenarioPackage.<clinit>(PrescenarioPackage.java:60)
> at
> org.xtext.example.PrescenarioStandaloneSetupGenerated.regist er(PrescenarioStandaloneSetupGenerated.java:38)
>
> at
> org.xtext.example.PrescenarioStandaloneSetupGenerated.create InjectorAndDoEMFRegistration(PrescenarioStandaloneSetupGener ated.java:21)
>
> at
> org.xtext.example.PrescenarioStandaloneSetup.doSetup(Prescen arioStandaloneSetup.java:11)
>
> at model.Main.main(Main.java:22)
> Exception in thread "main" java.lang.ClassCastException:
> prescenario.impl.PrescenarioPackageImpl cannot be cast to
> org.eclipse.emf.ecore.resource.Resource$Factory
> at
> org.eclipse.emf.ecore.resource.impl.ResourceFactoryRegistryI mpl.convert(ResourceFactoryRegistryImpl.java:100)
>
> at
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$2.delega tedGetFactory(ResourceSetImpl.java:449)
>
> at
> org.eclipse.emf.ecore.resource.impl.ResourceFactoryRegistryI mpl.getFactory(ResourceFactoryRegistryImpl.java:151)
>
> at
> org.eclipse.emf.ecore.resource.impl.ResourceFactoryRegistryI mpl.getFactory(ResourceFactoryRegistryImpl.java:92)
>
> at
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.createRe source(ResourceSetImpl.java:422)
>
> at
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.createRe source(ResourceSetImpl.java:414)
>
> at model.Main.main(Main.java:26)
>
>
> please help me.
> thanks
Previous Topic:Formatting empty brackets
Next Topic:Is there any maven repo for xtext?
Goto Forum:
  


Current Time: Fri Oct 24 13:41:12 GMT 2014

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

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