Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Importing an XMI file
Importing an XMI file [message #634893] Sun, 24 October 2010 23:28 Go to next message
Kissaki  is currently offline Kissaki Friend
Messages: 6
Registered: October 2010
Junior Member
hey,

I'm trying to use EMF / ECore to parse an XMI file to use it programmaticaly.
Can anyone help me with that?

Currently I'm trying with XMILoadImpl.load
Would this be the correct / best way?

Currently I get "Package with uri 'null' not found."

What I don't get is what those load method params are for ... Shall Resource as well as input source point to my XMI file; both?
The javadoc doesn't really tell me ...


What does the error indicate? Is it a problem with the xmlresource or inputsource?
Or is it parsing the file itself while everything else seemed ok?
"Package with uri 'null' not found."

My current test-code follows:
String filePath = dir + File.separator + "xmi_ex4.xmi";
		URI filePathURI = URI.createFileURI(filePath);
		XMLHelper h = new XMIHelperImpl();
		XMLLoad loader = new XMILoadImpl(h);
		//logger.info("loader: {}", loader);
		
		XMIResource resXMI = new XMIResourceImpl(filePathURI);
		//XMLResource res = new XMLResourceImpl(filePathURI);
		try {
			File xmiFile = new File(filePath);
			FileReader xFR = new FileReader(xmiFile);
			InputSource xmiIS = new InputSource(xFR);
			loader.load(resXMI, xmiIS, new java.util.HashMap());
		} catch (FileNotFoundException e) {
			logger.error("File Not Found exception thrown when loading XMI: {}", e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			logger.error("IO exception thrown when loading XMI: {}", e.getMessage());
			e.printStackTrace();
		}
Re: Importing an XMI file [message #634898 is a reply to message #634893] Mon, 25 October 2010 01:06 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Kissaki,

Comments below.


Kissaki wrote:
> hey,
>
> I'm trying to use EMF / ECore to parse an XMI file to use it
> programmaticaly.
> Can anyone help me with that?
Did you read the introductory overview? Do you have an Ecore model
generated for the type of XMI you're trying to process.
>
> Currently I'm trying with XMILoadImpl.load
> Would this be the correct / best way?
No.
>
> Currently I get "Package with uri 'null' not found."
What does the XMI look like?
>
> What I don't get is what those load method params are for ... Shall
> Resource as well as input source point to my XMI file; both?
> The javadoc doesn't really tell me ...
I'd suggest generating a model, invoking Generate Test Code, and look at
the XyzExample.java.
>
>
> What does the error indicate? Is it a problem with the xmlresource or
> inputsource?
> Or is it parsing the file itself while everything else seemed ok?
> "Package with uri 'null' not found."
It's hard to comment if you don't show the file. But it sounds like you
have an unqualified root element in your XML.
>
> My current test-code follows:
> String filePath = dir + File.separator + "xmi_ex4.xmi";
> URI filePathURI = URI.createFileURI(filePath);
> XMLHelper h = new XMIHelperImpl();
> XMLLoad loader = new XMILoadImpl(h);
Why would you create these direction.
> //logger.info("loader: {}", loader);
>
> XMIResource resXMI = new XMIResourceImpl(filePathURI);
> //XMLResource res = new XMLResourceImpl(filePathURI);
> try {
> File xmiFile = new File(filePath);
> FileReader xFR = new FileReader(xmiFile);
> InputSource xmiIS = new InputSource(xFR);
> loader.load(resXMI, xmiIS, new java.util.HashMap());
You'd use a resource to load the contents. Best to read the
introductory overview.
> } catch (FileNotFoundException e) {
> logger.error("File Not Found exception thrown when loading
> XMI: {}", e.getMessage());
> e.printStackTrace();
> } catch (IOException e) {
> logger.error("IO exception thrown when loading XMI: {}",
> e.getMessage());
> e.printStackTrace();
> }


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Importing an XMI file [message #634899 is a reply to message #634893] Mon, 25 October 2010 01:12 Go to previous messageGo to next message
Bryan Hunt is currently offline Bryan HuntFriend
Messages: 366
Registered: July 2009
Senior Member
There's a much easier way to do this assuming you have an ecore and have generated the java objects, EMF factory and package:

ResourceSet resourceSet = new ResourceSetImpl();
Resource resource = resourceSet.getResource(filePathURI, true);
Object object = resource.getContents().get(0);


Make sure the org.eclispe.emf.ecore.xmi bundle is included in your launch configuration. Also, you may have to add ".xmi" as an extension that is mapped to the XMIResource. ".xml" is read as XMI by default when you include the ecore.xmi bundle. If needed, you can map ".xmi" in an extension, or programmatically through the ResourceSet.

If you don't have an ecore, factory, and package, you will need to create a package and dynamically describe your model by creating instances of EClass which is much more complex. This is described very well in the EMF book.
Re: Importing an XMI file [message #634944 is a reply to message #634899] Mon, 25 October 2010 08:09 Go to previous messageGo to next message
Kissaki  is currently offline Kissaki Friend
Messages: 6
Registered: October 2010
Junior Member
OK, what I want to do in a bit more detail:
I want to import / parse an XMI file exported from UML tools which.
From it I want to read out a state diagram to generate code from it.
For this I currently referenced the emf.ecore, emf.ecore.xmi, emf.common libraries.
Re: Importing an XMI file [message #635048 is a reply to message #634944] Mon, 25 October 2010 13:59 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Kissaki,

Comments below.

Kissaki wrote:
> OK, what I want to do in a bit more detail:
> I want to import / parse an XMI file exported from UML tools which.
> From it I want to read out a state diagram to generate code from it.
So you'll need to use the resource implementations provided by the UML2
project.
> For this I currently referenced the emf.ecore, emf.ecore.xmi,
> emf.common libraries.
That won't be sufficient.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Importing an XMI file [message #635051 is a reply to message #635048] Mon, 25 October 2010 14:28 Go to previous messageGo to next message
Kissaki  is currently offline Kissaki Friend
Messages: 6
Registered: October 2010
Junior Member
Oh, ok.
I thought they were for eclipse editors etc.

Thanks,
I'll check and reply again if I run into problems / fail again.
Re: Importing an XMI file [message #635198 is a reply to message #635048] Mon, 25 October 2010 23:25 Go to previous messageGo to next message
Kissaki  is currently offline Kissaki Friend
Messages: 6
Registered: October 2010
Junior Member
I tried to load the XMI file with UML2, but failed again.
I get the old error.
So could you / someone point out to me what I'm doing wrong?

The UML2 wiki only has info on using it as an eclipse plugin, but not on using it programmaticaly as libs.

As can be seen in the code, I'm using UMLImporter, which I found via javadoc.


UMLImporter i = new UMLImporter();
		ResourceSet resS = i.createResourceSet();
		Resource resource = resS.getResource(filePathURI, true);
		Object object = resource.getContents().get(0);
		logger.info("MA OBJECT: {}", object);


As that wasn't enough I added factoryMap data:
resS.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", new XMIResourceFactoryImpl() {
			public Resource createResource(URI uri)
	        {
		        XMIResource xmiResource = new XMIResourceImpl(uri);
		        return xmiResource;
	        }
		});


But I get the old
Quote:
Exception in thread "main" org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1Diagnos ticWrappedException: org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri 'null' not found. (file:/G:/.../xmi_ex4.xmi, 3, 16)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDe mandLoadException(ResourceSetImpl.java:315)
Re: Importing an XMI file [message #635211 is a reply to message #635198] Tue, 26 October 2010 01:12 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Kissaki,

Comments below.


Kissaki wrote:
> I tried to load the XMI file with UML2, but failed again.
> I get the old error.
> So could you / someone point out to me what I'm doing wrong?
>
> The UML2 wiki only has info on using it as an eclipse plugin, but not
> on using it programmaticaly as libs.
>
> As can be seen in the code, I'm using UMLImporter, which I found via
> javadoc.
>
>
> UMLImporter i = new UMLImporter();
The importer isn't what you use to read UML instances.
> ResourceSet resS = i.createResourceSet();
> Resource resource = resS.getResource(filePathURI, true);
> Object object = resource.getContents().get(0);
> logger.info("MA OBJECT: {}", object);
>
> As that wasn't enough I added factoryMap data:
> resS.getResourceFactoryRegistry().getExtensionToFactoryMap() .put( "xmi",
> new XMIResourceFactoryImpl() {
> public Resource createResource(URI uri)
> {
> XMIResource xmiResource = new XMIResourceImpl(uri);
> return xmiResource;
> }
> });
Ask about UML2 on their newsgroup. You'll need to use the UML2Resource...
>
> But I get the old Quote:
>> Exception in thread "main"
>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1Diagnos
>> ticWrappedException:
>> org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri
>> 'null' not found. (file:/G:/.../xmi_ex4.xmi, 3, 16)
>> at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDe
>> mandLoadException(ResourceSetImpl.java:315)
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Importing an XMI file [message #635518 is a reply to message #635211] Wed, 27 October 2010 07:52 Go to previous messageGo to next message
Kissaki  is currently offline Kissaki Friend
Messages: 6
Registered: October 2010
Junior Member
Thanks.
I made it work.

But having no doc,
a lot of dependencies,
no contact info or forum for UML2,
and having to debug the actual structure of the UML2-parsed XMI is frustrating.
Thus, I will now parse XML myself.

My impression on the eclipse websites is that they are confusing and the doc bad.
It is not first-time user friendly.

Thanks for all your guys help.
Re: Importing an XMI file [message #635573 is a reply to message #635518] Wed, 27 October 2010 10:48 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------020507090408060202030403
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

Kissaki,

Comments below.

Kissaki wrote:
> Thanks.
> I made it work.
>
> But having no doc,
There is documentation, but it could certainly be easier to find.
> a lot of dependencies,
The p2 installer finds all the dependencies so pointing at the Helios
repo should be sufficient for your needs.
> no contact info or forum for UML2,
There is a UML2 forum. news://new.eclipse.org/eclipse.modeling.mdt.uml2
> and having to debug the actual structure of the UML2-parsed XMI is
> frustrating.
Debug?
> Thus, I will now parse XML myself.
UML2 is large and complex. Parsing the XMI for it yourself seems like
an exercised in futility. I imagine you could spend the time on more
important things, i.e., manipulating the structure itself.
>
>
> My impression on the eclipse websites is that they are confusing and
> the doc bad.
There's lots of room for improvement. But little time or investment to
make it happen.
> It is not first-time user friendly.
No, not so much. But users themselves could improve the wiki. They
just typically never find the time to do that, though many find the time
to complain.
>
> Thanks for all your guys help.
It's not as if your question hasn't actually been answered a 100 times
on the UML2 newsgroup. There's even a FAQ on their home page:

http://wiki.eclipse.org/index.php/MDT-UML2-FAQ#What.27s_requ ired_to_load_a_UML_.28.uml.29_resource_from_a_standalone_app lication.3F



--------------020507090408060202030403
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Kissaki,<br>
<br>
Comments below.<br>
<br>
Kissaki wrote:
<blockquote cite="mid:ia8lhu$5tf$1@news.eclipse.org" type="cite">Thanks.
<br>
I made it work.
<br>
<br>
But having no doc,
<br>
</blockquote>
There is documentation, but it could certainly be easier to find.<br>
<blockquote cite="mid:ia8lhu$5tf$1@news.eclipse.org" type="cite">a lot
of dependencies,
<br>
</blockquote>
The p2 installer finds all the dependencies so pointing at the Helios
repo should be sufficient for your needs.<br>
<blockquote cite="mid:ia8lhu$5tf$1@news.eclipse.org" type="cite">no
contact info or forum for UML2,
<br>
</blockquote>
There is a UML2 forum.  <a
href="news://new.eclipse.org/eclipse.modeling.mdt.uml2">news://new.eclipse.org/eclipse.modeling.mdt.uml2</a><br>
<blockquote cite="mid:ia8lhu$5tf$1@news.eclipse.org" type="cite">and
having to debug the actual structure of the UML2-parsed XMI is
frustrating.
<br>
</blockquote>
Debug?<br>
<blockquote cite="mid:ia8lhu$5tf$1@news.eclipse.org" type="cite">Thus,
I will now parse XML myself.</blockquote>
UML2 is large and complex.  Parsing the XMI for it yourself seems like
an exercised in futility. I imagine you could spend the time on more
important things, i.e., manipulating the structure itself.<br>
<blockquote cite="mid:ia8lhu$5tf$1@news.eclipse.org" type="cite"><br>
<br>
My impression on the eclipse websites is that they are confusing and
the doc bad.
<br>
</blockquote>
There's lots of room for improvement.  But little time or investment to
make it happen.<br>
<blockquote cite="mid:ia8lhu$5tf$1@news.eclipse.org" type="cite">It is
not first-time user friendly.
<br>
</blockquote>
No, not so much.  But users themselves could improve the wiki.  They
just typically never find the time to do that, though many find the
time to complain.<br>
<blockquote cite="mid:ia8lhu$5tf$1@news.eclipse.org" type="cite"><br>
Thanks for all your guys help.
<br>
</blockquote>
It's not as if your question hasn't actually been answered a 100 times
on the UML2 newsgroup.  There's even a FAQ on their home page:<br>
<blockquote><a
href=" http://wiki.eclipse.org/index.php/MDT-UML2-FAQ#What.27s_requ ired_to_load_a_UML_.28.uml.29_resource_from_a_standalone_app lication.3F"> http://wiki.eclipse.org/index.php/MDT-UML2-FAQ#What.27s_requ ired_to_load_a_UML_.28.uml.29_resource_from_a_standalone_app lication.3F</a><br>
</blockquote>
<br>
</body>
</html>

--------------020507090408060202030403--


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Importing an XMI file [message #635639 is a reply to message #635573] Wed, 27 October 2010 15:15 Go to previous messageGo to next message
Kissaki  is currently offline Kissaki Friend
Messages: 6
Registered: October 2010
Junior Member
>> a lot of dependencies,
>The p2 installer finds all the dependencies so pointing at the Helios
> repo should be sufficient for your needs.
I'm not familiar with p2 installer.

>> no contact info or forum for UML2,
>There is a UML2 forum. news://new.eclipse.org/eclipse.modeling.mdt.uml2
I am not familiar with the news:// protocol.

>> and having to debug the actual structure of the UML2-parsed XMI is frustrating.
>Debug?
I'm not familiar with what is returned and it just seems to be AnyType lists within each other.
I'm also not familiar with AnyType classes and how I get data out of it, or if I have to instanceof-check all of them and cast them, or whatever.

>> Thus, I will now parse XML myself.
>UML2 is large and complex. Parsing the XMI for it yourself seems like an exercised in futility. I imagine you could spend the time on more important things, i.e., manipulating the structure itself.
I don't need all of it and neither do I need to validate that it's true and valid XMI or UML2 in XMI format.

As long as I can get the data I need out of it ...

> Thanks for all your guys help.
It's not as if your question hasn't actually been answered a 100 times
on the UML2 newsgroup.
Neither was I familiar with newsgroup (news:// protocol) nor can I log in there. And having that much of a hassle to search for something just to make it work ...

> There's even a FAQ on their home page:
> http://wiki.eclipse.org/index.php/MDT-UML2-FAQ#What.27s_requ ired_to_load_a_UML_.28.uml.29_resource_from_a_standalone_app lication.3F
I checked that one, and it didn't really help me much.

[Updated on: Wed, 27 October 2010 15:17]

Report message to a moderator

Re: Importing an XMI file [message #635663 is a reply to message #635639] Wed, 27 October 2010 16:08 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Kissaki,

Comments below.

Kissaki wrote:
>>> no contact info or forum for UML2,
>> There is a UML2 forum. news://new.eclipse.org/eclipse.modeling.mdt.uml2
> I am not familiar with the news:// protocol.
It's quite a standard thing. I guess you're using the web forums, so
eclipse.modeling.mdt.uml2 is the symbolic ID...
>
>>> and having to debug the actual structure of the UML2-parsed XMI is
>>> frustrating.
>> Debug?
> I'm not familiar with what is returned and it just seems to be AnyType
> lists within each other.
Sounds like it's not working well, not recognizing the namespaces.
>
>>> Thus, I will now parse XML myself.
>> UML2 is large and complex. Parsing the XMI for it yourself seems
>> like an exercised in futility. I imagine you could spend the time on
>> more important things, i.e., manipulating the structure itself.
> I don't need all of it and neither do I need to validate that it's
> true and valid XMI or UML2 in XMI format.
Not sure what you need then.
>
> As long as I can get the data I need out of it ...
>
>> Thanks for all your guys help.
> It's not as if your question hasn't actually been answered a 100 times
> on the UML2 newsgroup. Neither was I familiar with newsgroup (news://
> protocol) nor can I log in there. And having that much of a hassle to
> search for something just to make it work ...
>
>> There's even a FAQ on their home page:
>> http://wiki.eclipse.org/index.php/MDT-UML2-FAQ#What.27s_requ ired_to_load_a_UML_.28.uml.29_resource_from_a_standalone_app lication.3F
>>
> I checked that one, and it didn't really help me much.
It doesn't sound like you've read all the much of the introductory EMF
information.


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Links in http://wiki.eclipse.org/Teneo/EclipseLink
Next Topic:[TENEO] EMF and Serializable
Goto Forum:
  


Current Time: Thu Mar 28 08:24:58 GMT 2024

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

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

Back to the top