Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » How to tell Ressource.getResource to look for the meta model in a local file
How to tell Ressource.getResource to look for the meta model in a local file [message #1811216] Wed, 28 August 2019 19:29 Go to next message
Afe Fer is currently offline Afe FerFriend
Messages: 8
Registered: August 2019
Junior Member
Hi,
I am reading the book Eclipse Modeling Framwork and I am implementing the PurchaseOrder eample.

I managed to create the Meta model, I have the Ecore file and I generated the code.
I can create models and export them to XMI files with the following code

THE CODE

public class LoadSaveTest {
public static void main(String[] args) throws IOException {
Webpage aWebpage = new WebpageImpl();

ResourceSet aResourceSet = new ResourceSetImpl();
aResourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xml", new XMLResourceFactoryImpl());
URI fileURI = URI.createFileURI(new File("myfile.xml").getAbsolutePath());
Resource aResource = aResourceSet.createResource(fileURI);
aResource.getContents().add(aWebpage);
aResource.save(null);
}
}
Now I have implemented a load method to load the model from myfile.xml.
The code again is very simple :
public Webpage load(String fileName) {
ResourceSet aResourceSet = new ResourceSetImpl();
aResourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xml", new XMLResourceFactoryImpl());
URI fileURI = URI.createFileURI(new File(fileName).getAbsolutePath());
Resource aResource = aResourceSet.getResource(fileURI, true);
Webpage theSameWebpage = (Webpage) aResource.getContents().get(0);
return theSameWebpage;

}
I can call load from the same main() that exported the model to the xmi file and it works fine.

THE PROBLEM

Now I want to load the model in myfile.xmi from another class (In the same package actually). I simply implement it this way :

import Java and eclipse stuf;
import webpage.Webpage;
import webpage.impl.WebpageImpl;

public class LoadTest {
public static void main(String[] args) throws IOException {
Webpage aWebpage;
aWebpage = LoadSaveTest load("myfile.xml");
}
}

And then I have the following error:
Exception in thread "main" org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri 'http://www.example.org/webpage' not found. (file:/C:/Users/user/Documents/Temporaire/eclipse-workspace/Webpage/myfile.xml, 2, 66)
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 LoadSaveTest.load(LoadSaveTest.java:42)
at LoadTest.main(LoadTest.java:16)
Caused by: org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri 'http://www.example.org/webpage' not found. (file:/C:/Users/user/Documents/Temporaire/eclipse-workspace/Webpage/myfile.xml, 2, 66)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.getPackageForURI(XMLHandler.java:2453)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.getFactoryForPrefix(XMLHandler.java:2285)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectByType(XMLHandler.java:1239)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.createTopObject(XMLHandler.java:1336)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.processElement(XMLHandler.java:970)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHandler.java:953)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHandler.java:684)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.emptyElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$ContentDriver.scanRootElementHook(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown Source)
at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl.java:179)
at org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLResourceImpl.java:180)
at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1563)
at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1342)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoad(ResourceSetImpl.java:259)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:274)
... 3 more

THE QUESTIONS
1- Why invoquing the same method that worked in my first class crashes when I call it from another one?
2- How to tell my method to look for the meta model in a specific local file instead of looking in the URI of the xmi file?

Thx in advance
Re: How to tell Ressource.getResource to look for the meta model in a local file [message #1811221 is a reply to message #1811216] Thu, 29 August 2019 02:52 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30685
Registered: July 2009
Senior Member
You appear to have a generated model and you should take note that when running stand alone, none of the extension points in the plugin.xml will do their normal registrations that they do when running with Equinox (the OSGi-runtime).

Your generated package will register itself when it's initialized. This is why the generated example includes code like this:
	resourceSet.getPackageRegistry().put
			(XyzPackage.eNS_URI, 
			 XyzPackage.eINSTANCE);]

Just using an idiom like XyxPackage.eINSTANCE.getName(), will also ensure that the package is initialized and registered. Likely that's all you need.
Re: How to tell Ressource.getResource to look for the meta model in a local file [message #1811228 is a reply to message #1811221] Thu, 29 August 2019 07:12 Go to previous message
Afe Fer is currently offline Afe FerFriend
Messages: 8
Registered: August 2019
Junior Member
Hi Ed,

Thanks a lot for your answer. It worked great.
I just added your code before calling LoadSaveTest load("myfile.xml"); in LoadTest replacing WyzPackage with WebpagePackage

Now a subsidiary question would be :
do I need to generate the code of my model to explore and/or change it?
I will preferably develop this in a dedicated post here :
https://www.eclipse.org/forums/index.php/m/1811227/#msg_1811227

Thks again.
Previous Topic:Do I need to generate the code of the meta model to explore/edit existing models
Next Topic:Do I need to generate the code of the meta model to explore/edit existing models
Goto Forum:
  


Current Time: Sat Dec 07 13:47:50 GMT 2019

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

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

Back to the top