Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Load an ecore file programmatically
Load an ecore file programmatically [message #1754121] Tue, 14 February 2017 15:54 Go to next message
Fy Za is currently offline Fy ZaFriend
Messages: 245
Registered: March 2010
Senior Member
I try to load an ecore file which already exists in the plugins folder (I see it in the "registered epackages view, yet)
I find this code in the forum
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// Create a resource set.
		ResourceSet resourceSet = new ResourceSetImpl();

		// Register the default resource factory -- only needed for stand-alone!
		resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(
		"ecore", new EcoreResourceFactoryImpl());

		// Register the package -- only needed for stand-alone!
		EcorePackage ecorePackage = EcorePackage.eINSTANCE;

		// Get the URI of the model file.
		URI fileURI = URI.createPlatformPluginURI("platform:/plugin/org.eclipse.mymetamodel/model/MyMetaModel.ecore",false);

		// Demand load the resource for this file.
		Resource resource = resourceSet.getResource(fileURI, true);

		// Print the contents of the resource to System.out.
		try
		{
		resource.save(System.out, Collections.EMPTY_MAP);
		}
		catch (IOException e) {}
	}

but I have this error

Exception in thread "main" org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: java.net.MalformedURLException: unknown protocol: platform
	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.emf.ecore.resource.impl.ResourceSetImpl.getResource(ResourceSetImpl.java:406)
	at faiez.test.odesign.LoadEmf.main(LoadEmf.java:32)
Caused by: java.net.MalformedURLException: unknown protocol: platform
	at java.net.URL.<init>(URL.java:600)
	at java.net.URL.<init>(URL.java:490)
	at java.net.URL.<init>(URL.java:439)
	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.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)
	... 2 more



Any suggestions?

best regards
Re: Load an ecore file programmatically [message #1754141 is a reply to message #1754121] Tue, 14 February 2017 17:19 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
The platform protocol is registered by the Eclipse runtime so it doesn't work standalone. Why don't you just use the generated MyMetaModelPackage.eINSTANCE instead of trying to load it? If you must load it stand alone, using something like archive:file:/<file-system-location-of-jar>!/model/MyMetaModel.ecore. Or get a URL to it from the classpath.

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Load an ecore file programmatically [message #1754311 is a reply to message #1754141] Thu, 16 February 2017 10:11 Go to previous messageGo to next message
Fy Za is currently offline Fy ZaFriend
Messages: 245
Registered: March 2010
Senior Member
Thanks Ed!
I want to load the metamodel because I would like use it as a model (instance of Ecore.ecore) to do a model transformation.
Indeed, I want to search the efeatures, classifiers, etc. in the metamodel
Re: Load an ecore file programmatically [message #1754312 is a reply to message #1754311] Thu, 16 February 2017 10:13 Go to previous messageGo to next message
Fy Za is currently offline Fy ZaFriend
Messages: 245
Registered: March 2010
Senior Member
I want to transform the metamodel something.ecore to another format
Re: Load an ecore file programmatically [message #1754315 is a reply to message #1754312] Thu, 16 February 2017 10:52 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
I see. If the generated model is present, perhaps you can use MyMetaModelPackage.eNS_URI as the URI so that the transformation technology loads (fetches from the package registry), the MyMetaModelPackage.eINSTANCE...

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Load an ecore file programmatically [message #1831701 is a reply to message #1754121] Thu, 27 August 2020 02:04 Go to previous message
Michael Pigott is currently offline Michael PigottFriend
Messages: 1
Registered: August 2020
Junior Member
Hi, I am able to get your code to work if I change:

// Get the URI of the model file.
URI fileURI = URI.createPlatformPluginURI("platform:/plugin/org.eclipse.mymetamodel/model/MyMetaModel.ecore",false);


to:

// Get the URI of the model file.
URI fileURI = URI.createFileURI("/path/to/saved/file.ecore");


I hope that helps!
Previous Topic:EMF, GWT ELIST and serialisation
Next Topic:How to get enums per emf reflections?
Goto Forum:
  


Current Time: Thu Apr 18 13:47:51 GMT 2024

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

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

Back to the top