Eclipse Community Forums - RDF feed
https://www.eclipse.org/forums/
Eclipse Community ForumsHow to tell Ressource.getResource to look for the meta model in a local file
https://www.eclipse.org/forums/index.php/mv/msg/1100320/1811216/#msg_1811216
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
]]>Afe Fer2019-08-28T19:29:16-00:00Re: How to tell Ressource.getResource to look for the meta model in a local file
https://www.eclipse.org/forums/index.php/mv/msg/1100320/1811221/#msg_1811221
Your generated package will register itself when it's initialized. This is why the generated example includes code like this:
Just using an idiom like XyxPackage.eINSTANCE.getName(), will also ensure that the package is initialized and registered. Likely that's all you need.]]>Ed Merks2019-08-29T02:52:53-00:00Re: How to tell Ressource.getResource to look for the meta model in a local file
https://www.eclipse.org/forums/index.php/mv/msg/1100320/1811228/#msg_1811228
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