Reading a _java.xmi file programmatically [message #1249462] |
Tue, 18 February 2014 04:22  |
Eclipse User |
|
|
|
Hi,
Now that I have my _java.xmi File, I am trying to load it in a Java Program.
I found a code snippet in the documentation (MoDisco Documentation > MoDisco User Guide > Technologies > Java > Java discoverer > Java Discoverer API), but as long is I am working on my analysis tool, I do not need to rediscover my Java Program each time. My idea is to work with the _java.xmi file.
Here the code I used:
ResourceSetImpl resourceSet = new ResourceSetImpl();
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", new XMIResourceFactoryImpl());
URI uri = URI.createFileURI("C:/..../simple.java_java.xmi");
Resource resource = resourceSet.createResource(uri);
try {
resource.load(null);
Model javaModel = (Model) resource.getContents().get(0);
//Example from MoDisco User Guide "Java Discoverer API":
EList<ClassFile> classFiles = javaModel.getClassFiles();
for (ClassFile classFile : classFiles) {
System.out.println(classFile.getName());
}
} catch (IOException e) {
e.printStackTrace();
}
With following required bundles:
Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime,
org.eclipse.emf.common,
org.eclipse.gmt.modisco.java;bundle-version="0.11.1",
org.eclipse.emf.ecore,
org.eclipse.emf.ecore.xmi
I get this error:
org.eclipse.emf.ecore.resource.Resource$IOWrappedException: Package with uri 'http://www.eclipse.org/MoDisco/Java/0.2.incubation/java' not found. (file:/C:/Users/jbr/code/ScoutDesignWS/simple.java/simple.java_java.xmi, 2, 208)
at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.handleErrors(XMLLoadImpl.java:77)
at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl.java:185)
at org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLResourceImpl.java:253)
at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1518)
at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1297)
at simple.modisco.Main.main(Main.java:25)
Caused by: org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri 'http://www.eclipse.org/MoDisco/Java/0.2.incubation/java' not found. (file:/C:/Users/jbr/code/ScoutDesignWS/simple.java/simple.java_java.xmi, 2, 208)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.getPackageForURI(XMLHandler.java:2599)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.getFactoryForPrefix(XMLHandler.java:2429)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectByType(XMLHandler.java:1306)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.createTopObject(XMLHandler.java:1475)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.processElement(XMLHandler.java:1026)
at org.eclipse.emf.ecore.xmi.impl.XMIHandler.processElement(XMIHandler.java:77)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHandler.java:1008)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHandler.java:719)
at org.eclipse.emf.ecore.xmi.impl.XMIHandler.startElement(XMIHandler.java:163)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(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:175)
... 4 more
This question is probably EMF basics, but I would really appreciate some help.
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: Reading a _java.xmi file programmatically [message #1250647 is a reply to message #1250534] |
Wed, 19 February 2014 07:51   |
Eclipse User |
|
|
|
Hello Hugo,
I tried Papyrus and modelled a (very bad ) uml-diagram, of course it didn't work with my code while I didn't change
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("uml",
---> new XMIResourceFactoryImpl());
Now we were able to even read the XMI-file, pretending it was UML:
package testXmiReader;
import java.io.IOException;
import java.util.Map;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.resource.UMLResource;
public class Test {
public static void main(String[] args) {
URI uri =
URI.createURI("file:///C:/.../bruecken.xmi");
try {
ResourceSet resourceSet = new ResourceSetImpl();
// register UML
Map packageRegistry = resourceSet.getPackageRegistry();
packageRegistry.put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
// Register XML resource as UMLResource.Factory.Instance
Map extensionFactoryMap = Resource.Factory.Registry.INSTANCE
.getExtensionToFactoryMap();
extensionFactoryMap.put(UMLResource.FILE_EXTENSION,
UMLResource.Factory.INSTANCE);
extensionFactoryMap.put("xmi", UMLResource.Factory.INSTANCE);
Resource resource = (Resource) resourceSet.createResource(uri);
// try to load the file into resource
resource.load(null);
System.out.println("XMI Resource has "
+ resource.getContents().size() + " objects.");
for (EObject obj : resource.getContents()) {
System.out.println(obj.toString());
}
}// end of try
catch (NullPointerException | IOException e) {
e.printStackTrace();
}
}
}
As output we get 3 EObjects, which are not pretty readable, but that's not the problem atm.
I'm just afraid that there are not all elements of our XMI-file imported, because the output text does contain IDs of - for instance attributes we have in the .xmi, but it doesn't contain IDs of tagged values
(Edit: relations would also be very important)
But at least, the main-structure of our document should be mapped to the Ecore Objects
Now I'm thinking of how to display the Model, so I can see which parts are missing.
If that's too expensive, I'll try to get through the EObject via methods.
Do you have some advice for my further progress?
At least I'm happy that we're finally able to import our file, thanks for help so far!
[Updated on: Wed, 19 February 2014 08:12] by Moderator
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04954 seconds