Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » "a registered resource factory is needed" outside the generated editor
"a registered resource factory is needed" outside the generated editor [message #377406] Thu, 01 May 2003 17:12 Go to next message
Chris Wege is currently offline Chris WegeFriend
Messages: 16
Registered: July 2009
Junior Member
I have an EMF model which is running perfectly in the generated editor.
Now I want to load the model from a file in some code outside of the
generated editor. I get the exception below. For loading the file I
perform the steps as in the generated editor (see below).

How do I register the required resource factory?

Thanks for any hints,
Chris

=== code snippet starts here ===
List factories = new ArrayList();
factories.add(new ResourceItemProviderAdapterFactory());
factories.add(new ModelItemProviderAdapterFactory());
adapterFactory = new ComposedAdapterFactory(factories);
BasicCommandStack commandStack = new BasicCommandStack();
editingDomain = new AdapterFactoryEditingDomain(adapterFactory,
commandStack);

String inputFile = "f:/workspace/eva/Releases13.model";
Resource resource = editingDomain.loadResource
(URI.createFileURI(inputFile).toString());
=== code snippet ends here ===


=== console output starts here ===
java.lang.RuntimeException: Cannot create a resource for
'file:/f:/workspace/eva/Releases13.model'; a registered resource factory
is needed
at
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResou rce(ResourceSetImpl.java:282)
at
org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain.load Resource(AdapterFactoryEditingDomain.java:284)
at eva.fs.tests.AllTests.loadFile(AllTests.java:202)
at eva.fs.tests.AllTests.buildEd(AllTests.java:162)
at eva.fs.tests.EvaEditionTest.setUp(EvaEditionTest.java:24)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTe sts(RemoteTestRunner.java:386)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(R emoteTestRunner.java:270)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main( RemoteTestRunner.java:161)
Re: "a registered resource factory is needed" outside the generated editor [message #377407 is a reply to message #377406] Thu, 01 May 2003 18:35 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: shu.us.ibm.com

Chris,

You need to register resource factory.

You can use call
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put
to register resource factory.

And this is sample codes for xmi format file which is using
XMIResource interface.
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put
("*", <--- your file extension
new XMIResourceFactoryImpl() <--- the class to resource factory
{
public Resource createResource(URI uri)
{
XMIResource xmiResource = new XMIResourceImpl(uri);
return xmiResource;
}
});

You can provide your own resource factory class and resource class to
handle
your own file (if it is not xmi or xml file).

For more infomation, please refer to EMF users guide, session 3.4.2 and
3.4.3.

Regards,
Shu

Chris Wege wrote:

> I have an EMF model which is running perfectly in the generated editor.
> Now I want to load the model from a file in some code outside of the
> generated editor. I get the exception below. For loading the file I
> perform the steps as in the generated editor (see below).

> How do I register the required resource factory?

> Thanks for any hints,
> Chris

> === code snippet starts here ===
> List factories = new ArrayList();
> factories.add(new ResourceItemProviderAdapterFactory());
> factories.add(new ModelItemProviderAdapterFactory());
> adapterFactory = new ComposedAdapterFactory(factories);
> BasicCommandStack commandStack = new BasicCommandStack();
> editingDomain = new AdapterFactoryEditingDomain(adapterFactory,
> commandStack);

> String inputFile = "f:/workspace/eva/Releases13.model";
> Resource resource = editingDomain.loadResource
> (URI.createFileURI(inputFile).toString());
> === code snippet ends here ===


> === console output starts here ===
> java.lang.RuntimeException: Cannot create a resource for
> 'file:/f:/workspace/eva/Releases13.model'; a registered resource factory
> is needed
> at
>
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResou rce(ResourceSetImpl.java:282)
> at
>
org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain.load Resource(AdapterFactoryEditingDomain.java:284)
> at eva.fs.tests.AllTests.loadFile(AllTests.java:202)
> at eva.fs.tests.AllTests.buildEd(AllTests.java:162)
> at eva.fs.tests.EvaEditionTest.setUp(EvaEditionTest.java:24)
> at junit.framework.TestCase.runBare(TestCase.java:125)
> at junit.framework.TestResult$1.protect(TestResult.java:106)
> at junit.framework.TestResult.runProtected(TestResult.java:124)
> at junit.framework.TestResult.run(TestResult.java:109)
> at junit.framework.TestCase.run(TestCase.java:118)
> at junit.framework.TestSuite.runTest(TestSuite.java:208)
> at junit.framework.TestSuite.run(TestSuite.java:203)
> at junit.framework.TestSuite.runTest(TestSuite.java:208)
> at junit.framework.TestSuite.run(TestSuite.java:203)
> at
>
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTe sts(RemoteTestRunner.java:386)
> at
>
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(R emoteTestRunner.java:270)
> at
>
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main( RemoteTestRunner.java:161)
Re: "a registered resource factory is needed" outside the generated editor [message #377408 is a reply to message #377407] Fri, 02 May 2003 10:40 Go to previous messageGo to next message
Chris Wege is currently offline Chris WegeFriend
Messages: 16
Registered: July 2009
Junior Member
That helped.

I was wondering why I don't have to do this when I am using the
generated editor plugin. But the plugin seems to register the resource
factories during plugin initialization - which is missing in my
stand-alone app.

Next time I will do RTFM.

Thanks a lot,
Chris
Re: " java.lang.RuntimeException: Cannot create a resource for 'file:///C:\Users\kawtar NAJI\ru [message #1774817 is a reply to message #377408] Thu, 19 October 2017 22:33 Go to previous messageGo to next message
kawtar Naji is currently offline kawtar NajiFriend
Messages: 4
Registered: October 2017
Junior Member
Hi,
I want to go from an xtext model to its EMF model but i got this exception can anyone help me to solve this problem.
thanks in advance .
  • Attachment: Capture.PNG
    (Size: 65.65KB, Downloaded 31 times)
Re: " java.lang.RuntimeException: Cannot create a resource for 'file:///C:\Users\kawtar NAJI\ru [message #1774848 is a reply to message #1774817] Fri, 20 October 2017 13:47 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 5573
Registered: July 2009
Senior Member
Hi

I'm not sure what you really mean. There is an *.ecore file associated with Xtext, optionally manually entered, optional autogenerated. Use ctrl-shift-R to locate it by name.

If you want to see a model perspective of the *.xtext in the Sample Ecore Editor. Sorry that stopped working a couple of years ago.

Regards

Ed Willink
Re: " java.lang.RuntimeException: Cannot create a resource for 'file:///C:\Users\kawtar NAJI\ru [message #1774888 is a reply to message #1774848] Fri, 20 October 2017 20:06 Go to previous messageGo to next message
kawtar Naji is currently offline kawtar NajiFriend
Messages: 4
Registered: October 2017
Junior Member
actually i have the xtext model and i want to transform it 's EMF Model and parse the graphe generated using java in order to add some nodes or update it or delete it om my EMF Model , but i get this error
Re: " java.lang.RuntimeException: Cannot create a resource for 'file:///C:\Users\kawtar NAJI\ru [message #1774910 is a reply to message #1774888] Sat, 21 October 2017 06:06 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 5573
Registered: July 2009
Senior Member
Hi

Your visual snippet has so many things that make me uncomfortable I'm not sure where to start.

But first, do not attach a new problem to a 14 year old thread. Software moves much to fast for there to be any sensible prospect that your new problem is a continuation.

Do not use visual snippets. Your code cannot be cut and paste as a mini repro. Your exceptions are hideously truncated forcing major guessing. Why not cut and paste so that we see it all and Google searches can find similar content?

I am a Windows user, but I never use "\" in code. "/" works fine in all C/Java based software.

I suspect that your problem is "file:///". I would need to read the specification to be sure what it actually means. URI.createFileURI is so much easier.

Regards

Ed Willink
Re: " java.lang.RuntimeException: Cannot create a resource for 'file:///C:\Users\kawtar NAJI\ru [message #1774930 is a reply to message #1774910] Sat, 21 October 2017 14:20 Go to previous messageGo to next message
kawtar Naji is currently offline kawtar NajiFriend
Messages: 4
Registered: October 2017
Junior Member
this is what happen when i remove the file:///
Exception in thread "main" org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: java.net.MalformedURLException: unknown protocol: c
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDemandLoadException(ResourceSetImpl.java:319)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:278)
at org.eclipse.xtext.resource.XtextResourceSet.getResource(XtextResourceSet.java:265)
at org.eclipse.xtext.resource.SynchronizedXtextResourceSet.getResource(SynchronizedXtextResourceSet.java:25)
at org.xtext.example.Test.loadScript(Test.java:40)
at org.xtext.example.Test.main(Test.java:65)
Caused by: java.net.MalformedURLException: unknown protocol: c
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at org.eclipse.emf.ecore.resource.impl.URIHandlerImpl.createInputStream(URIHandlerImpl.java:199)
at org.eclipse.emf.ecore.resource.impl.ExtensibleURIConverterImpl.createInputStream(ExtensibleURIConverterImpl.java:360)
at org.eclipse.xtext.resource.XtextResourceSet$1.createInputStream(XtextResourceSet.java:298)
at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1269)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoad(ResourceSetImpl.java:259)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:274)
... 4 more
  • Attachment: Capture.PNG
    (Size: 23.20KB, Downloaded 23 times)
Re: " java.lang.RuntimeException: Cannot create a resource for 'file:///C:\Users\kawtar NAJI\ru [message #1774931 is a reply to message #1774930] Sat, 21 October 2017 14:30 Go to previous messageGo to next message
kawtar Naji is currently offline kawtar NajiFriend
Messages: 4
Registered: October 2017
Junior Member
this is my code :
package org.xtext.example;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.eclipse.xtext.resource.IResourceFactory;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.XtextResourceSet;

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

import projetm.ProjetmPackage;



public class Test {



public static void loadScript() {

Injector injector = new ProjetStandaloneSetupGenerated().createInjectorAndDoEMFRegistration();
ResourceSet resourceSet = injector.getInstance(ResourceSet.class);
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put
("*",
new XMIResourceFactoryImpl());
resourceSet.getPackageRegistry().put
(ProjetmPackage.eNS_URI,
ProjetmPackage.eINSTANCE);

//resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
Resource r1 = resourceSet.getResource(URI.createURI("file:///C:\\Users\\kawtar NAJI\\runtime-EclipseXtext\\TesteEMF\\test.projet"),true);
Resource r2 = null;
try {
r1.load(null);
r2 = resourceSet.createResource(URI.createURI("file:///C:\\Users\\kawtar NAJI\\runtime-EclipseXtext\\TesteEMF\\test1.xmi"));
System.out.println(r1.toString());
r2.getContents().add(r1.getContents().get(0));

r2.save(null);
EcoreUtil.resolveAll(r1);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}




}



public static void main(String[] args) {

Test.loadScript();

}

}

Re: " java.lang.RuntimeException: Cannot create a resource for 'file:///C:\Users\kawtar NAJI\ru [message #1774932 is a reply to message #1774931] Sat, 21 October 2017 15:47 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 5573
Registered: July 2009
Senior Member
"URI.createFileURI is so much easier."
Re: " java.lang.RuntimeException: Cannot create a resource for 'file:///C:\Users\kawtar NAJI\ru [message #1774933 is a reply to message #1774931] Sat, 21 October 2017 15:48 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 29115
Registered: July 2009
Senior Member
As was already suggested a \ in a URI is a bad idea because it is NOT a path separator in a URI, it's a path separator only on Windows and only as far as java.io.File is concerned. Generally when using file-based access, use URI.createFileURI(file.getAbsolutePath())) to create the URI from the java.io.File. Make sure it's absolute.
Previous Topic:Documentation/specification/standard for the BInaryResource format?
Next Topic:out of date model
Goto Forum:
  


Current Time: Wed Jan 17 07:42:46 GMT 2018

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

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