Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » MoDisco » Reading a _java.xmi file programmatically(Package with uri 'http://www.eclipse.org/MoDisco/Java/0.2.incubation/java' not found.)
Reading a _java.xmi file programmatically [message #1249462] Tue, 18 February 2014 09:22 Go to next message
Jeremie Bresson is currently offline Jeremie Bresson
Messages: 708
Registered: October 2011
Senior Member
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 #1249713 is a reply to message #1249462] Tue, 18 February 2014 14:49 Go to previous messageGo to next message
Aaron Kampichler is currently offline Aaron Kampichler
Messages: 9
Registered: February 2014
Junior Member
Hello,

though I'm not using MoDisco, but an Enterprise Architect xmi-file, I get nearly the same error-message.

The code I'm trying is:

URI uri = URI.createURI("file:///.../bruecken.xmi");

	        // create resource set and resource
	        try{
	          ResourceSet resourceSet = new ResourceSetImpl();
	          // Register XML resource factory
                  resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi",
	          new XMIResourceFactoryImpl());
	          
	          Resource resource = resourceSet.createResource(uri);
	          System.out.println(resource);
	          [i]resource.load(null);[/i]
	          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();
	        }



Do you think EMF still supports xmi-files?

We've just been able to import an .ecore with that code, no xmi or uml Confused
Re: Reading a _java.xmi file programmatically [message #1249736 is a reply to message #1249462] Tue, 18 February 2014 15:16 Go to previous messageGo to next message
Hugo Bruneliere is currently offline Hugo Bruneliere
Messages: 604
Registered: July 2009
Senior Member
Hello,

Your Java model serialized in XMI has to be treated as any EMF model serialized in XMI (nothing specific to MoDisco here, apart from the fact that your model conforms to the MoDisco Java metamodel).
You could take a look to this tutorial dealing with EMF model persistence, or ask directly your question on the EMF forum.


------------------------------------------
Hugo Bruneliere - R&D Engineer
AtlanMod research team (Inria, EMN & LINA)
Ecole des Mines de Nantes
Nantes - France
------------------------------------------
Re: Reading a _java.xmi file programmatically [message #1249895 is a reply to message #1249736] Tue, 18 February 2014 18:54 Go to previous messageGo to next message
Jeremie Bresson is currently offline Jeremie Bresson
Messages: 118
Registered: November 2010
Senior Member
Thank you for our answer and for the pointer (the article written by Lars is much better than the other snippet I found).

Here the code that is working for me (again I am not an EMF expert):
// Initialize the model
JavaPackage.eINSTANCE.eClass();

// Register the XMI resource factory for the .xmi extension
Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
reg.getExtensionToFactoryMap().put("xmi", new XMIResourceFactoryImpl());

// Obtain a new resource set
ResourceSet resourceSet = new ResourceSetImpl();

// Get the resource
URI uri = URI.createFileURI("C:/..../simple.java_java.xmi");
Resource resource = resourceSet.getResource(uri, true);

Model javaModel = (Model) resource.getContents().get(0);

//Read the top-packages contained in the model
EList<Package> pagckages = javaModel.getOwnedElements();
for (Package p : pagckages) {
	System.out.println(p.getName());
}


Of course, as with any EMF model it is good to keep in mind the tree corresponding to the model instance.

http://www.eclipse.org/forums/index.php/fa/17492/

The Java methods will allow navigating through the tree (for example getOwnedElements(): it provides access to the Package child nodes of the "Model simple.java" node).

Does MoDisco provides Utility methods to have access to the elements displayed in the MoDisco Browser (ClassDeclaration, InterfaceDeclaration and so on), or do I need to write the methods I need myself?
Re: Reading a _java.xmi file programmatically [message #1250469 is a reply to message #1249895] Wed, 19 February 2014 09:19 Go to previous messageGo to next message
Hugo Bruneliere is currently offline Hugo Bruneliere
Messages: 604
Registered: July 2009
Senior Member
Hello Jeremie,

To handle your Java model programmatically, you have two options:
* Using the generic EMF Reflective API that is independent from any metamodel defined in Ecore
* Using the dedicated API generated by EMF from your metamodel definition in Ecore

MoDisco provides this Java metamodel-specific API that you can use to manipulate corresponding models (cf. the MoDisco Java metamodel plugin).


------------------------------------------
Hugo Bruneliere - R&D Engineer
AtlanMod research team (Inria, EMN & LINA)
Ecole des Mines de Nantes
Nantes - France
------------------------------------------
Re: Reading a _java.xmi file programmatically [message #1250476 is a reply to message #1249895] Wed, 19 February 2014 09:27 Go to previous messageGo to next message
Aaron Kampichler is currently offline Aaron Kampichler
Messages: 9
Registered: February 2014
Junior Member
Hello Jeremie,

I don't figure out many differences to the old snippet we tried, and unfortunately the new one that worked for you doesn't solve my problems.

I still get the same
"Package with uri 'http: // schema . omg . org / spec / XMI / 2.1' not found." exception Mad

Did you change anything in your xmi-file?

I'm getting desperate solving this issue..
Re: Reading a _java.xmi file programmatically [message #1250478 is a reply to message #1250476] Wed, 19 February 2014 09:30 Go to previous messageGo to next message
Hugo Bruneliere is currently offline Hugo Bruneliere
Messages: 604
Registered: July 2009
Senior Member
Hello Aaron,

You said that the XMI file you have has been produced by Enterprise Architect: are you sure that this EA "export" is EMF compatible and relies on an EMF-based (Ecore) metamodel?

Kind regards,


------------------------------------------
Hugo Bruneliere - R&D Engineer
AtlanMod research team (Inria, EMN & LINA)
Ecole des Mines de Nantes
Nantes - France
------------------------------------------
Re: Reading a _java.xmi file programmatically [message #1250502 is a reply to message #1250478] Wed, 19 February 2014 09:59 Go to previous messageGo to next message
Aaron Kampichler is currently offline Aaron Kampichler
Messages: 9
Registered: February 2014
Junior Member
Hello Hugo,

I'm not sure if it's compatible with Ecore, but I also tried other XMI-files - while I still don't know if the other ones I tried were so.

Do you have an example-XMI that's compatible?
..As I said, the only files that worked for me were .ecore's

For your information - EA provides the export as .ecore, but as I was told by my colleague, we have to use the .xmi export, because the .ecore export ignores some important content (all above tagged values).

Do you have an idea how i could make the EA-XMI EMF-readable, as far as this is the problem?

That's what I've been searching for the last days, but if I don't get any answers we'll try to read the XMI manually with an XML-Reader and assign the different Types to new classes.

Thanks for help!
Re: Reading a _java.xmi file programmatically [message #1250511 is a reply to message #1250502] Wed, 19 February 2014 10:05 Go to previous messageGo to next message
Hugo Bruneliere is currently offline Hugo Bruneliere
Messages: 604
Registered: July 2009
Senior Member
In the EMF world, an Ecore file stores a metamodel (e.g. UML) whereas an XMI file stores a model that conforms to a metamodel (defined in Ecore).
Both are at different levels of abstraction, cf. this explanatory presentation.

I know that people from Enterprise Architect have been working on an EMF export specifically for UML models, but I don't know if this is already available.


------------------------------------------
Hugo Bruneliere - R&D Engineer
AtlanMod research team (Inria, EMN & LINA)
Ecole des Mines de Nantes
Nantes - France
------------------------------------------

[Updated on: Wed, 19 February 2014 10:07]

Report message to a moderator

Re: Reading a _java.xmi file programmatically [message #1250523 is a reply to message #1250511] Wed, 19 February 2014 10:16 Go to previous messageGo to next message
Aaron Kampichler is currently offline Aaron Kampichler
Messages: 9
Registered: February 2014
Junior Member
So atm there is probably no way to get the EA-XMI imported to EMF?

Or is there a chance for my code being the problem?

An EMF-compatible XMI would be helpful testing this, but I don't know where to get one. Not that Rolling Eyes easy as you think it should be
Re: Reading a _java.xmi file programmatically [message #1250534 is a reply to message #1250523] Wed, 19 February 2014 10:30 Go to previous messageGo to next message
Hugo Bruneliere is currently offline Hugo Bruneliere
Messages: 604
Registered: July 2009
Senior Member
Well, you could use Eclipse Papyrus for instance to build a simple UML model and then compare it with a similar one built using EA.

------------------------------------------
Hugo Bruneliere - R&D Engineer
AtlanMod research team (Inria, EMN & LINA)
Ecole des Mines de Nantes
Nantes - France
------------------------------------------
Re: Reading a _java.xmi file programmatically [message #1250647 is a reply to message #1250534] Wed, 19 February 2014 12:51 Go to previous messageGo to next message
Aaron Kampichler is currently offline Aaron Kampichler
Messages: 9
Registered: February 2014
Junior Member
Hello Hugo,

I tried Papyrus and modelled a (very bad Laughing ) 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)
Confused


But at least, the main-structure of our document should be mapped to the Ecore Objects Smile

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 13:12]

Report message to a moderator

Re: Reading a _java.xmi file programmatically [message #1250669 is a reply to message #1250647] Wed, 19 February 2014 13:21 Go to previous messageGo to next message
Hugo Bruneliere is currently offline Hugo Bruneliere
Messages: 604
Registered: July 2009
Senior Member
I guess one of my previous answers to this thread could help you.

------------------------------------------
Hugo Bruneliere - R&D Engineer
AtlanMod research team (Inria, EMN & LINA)
Ecole des Mines de Nantes
Nantes - France
------------------------------------------
Re: Reading a _java.xmi file programmatically [message #1251520 is a reply to message #1250669] Thu, 20 February 2014 09:58 Go to previous messageGo to next message
Aaron Kampichler is currently offline Aaron Kampichler
Messages: 9
Registered: February 2014
Junior Member
There is the problem that every EObject seems to be an "AnyType" Object, so I can't access the properties.
I don't know if I'm missing something, but as I'm not able to access Data so I can map it to our Object-Structure, EMF seems to be useless Confused
Re: Reading a _java.xmi file programmatically [message #1251531 is a reply to message #1251520] Thu, 20 February 2014 10:14 Go to previous messageGo to next message
Aaron Kampichler is currently offline Aaron Kampichler
Messages: 9
Registered: February 2014
Junior Member
I just ran over JET (Java emitter template), could there be a way of using the XMIResource to generate JET-Java-Output?
Re: Reading a _java.xmi file programmatically [message #1251543 is a reply to message #1251531] Thu, 20 February 2014 10:29 Go to previous message
Hugo Bruneliere is currently offline Hugo Bruneliere
Messages: 604
Registered: July 2009
Senior Member
Hello Aaron,

These are now really EMF-specific problems/questions.
I encourage you to post them on the EMF forum where you will get more precise answers.

Kind regards,


------------------------------------------
Hugo Bruneliere - R&D Engineer
AtlanMod research team (Inria, EMN & LINA)
Ecole des Mines de Nantes
Nantes - France
------------------------------------------
Previous Topic:Discovering a Java Project
Next Topic:Feature type does not exist on java
Goto Forum:
  


Current Time: Wed Sep 24 02:31:45 GMT 2014

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

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