Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » UML2 » Load uml resource from an uploaded file
Load uml resource from an uploaded file [message #885670] Wed, 13 June 2012 12:21 Go to next message
max power is currently offline max powerFriend
Messages: 9
Registered: May 2012
Junior Member
Hi All!

I'm trying to get an uml-model from an uploaded xmi-file.

For extracting the uml model I used following code:


ResourceSet resourceSet = new ResourceSetImpl();
Registry packageRegistry = resourceSet.getPackageRegistry();
packageRegistry.put("http:||schema.omg.org/spec/XMI/2.1", UMLPackage.eINSTANCE);
packageRegistry.put("http:||schema.omg.org/spec/UML/2.1", UMLPackage.eINSTANCE);
Map<String, Object> extensionToFactoryMap = resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap();
extensionToFactoryMap.put(XMI2UMLResource.FILE_EXTENSION, new XMIResourceFactoryImpl());
resourceSet.getLoadOptions().put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE, Boolean.TRUE);
		
URI uri = URI.createURI("model.xmi");
		
Package root = UML2Util.load(resourceSet, uri, UMLPackage.Literals.PACKAGE);

System.out.println(root);



This code works for files with a known name that are located in my eclipse workspace. What I want to do is to upload a file via my web application and then extract a model from it. For upload I'm using the org.apache.commons.fileupload.FileItem; The upload itself works fine. I have the input stream of the file.

To extract the model from that inputstream I tried to do following (since it worked i.e. for ecore-files):

Resource xmiResource = resourceSet.createResource(URI.createURI(uploadedFile.getName()));
xmiResource.load(uploadedFile.getInputStream(), getLoadParams());

Package root = UML2Util.load(resourceSet, uri, UMLPackage.Literals.PACKAGE);


But the resource could not be loaded. It seems that only UML2Util.load can load the UML model from xmi-files in a correct way.

Is there a way to use the input stream with UML2Util or, maybe, another solution for my problem?

Many thanks in advance!

[Updated on: Wed, 13 June 2012 14:49]

Report message to a moderator

Re: Load uml resource from an uploaded file [message #885814 is a reply to message #885670] Wed, 13 June 2012 16:38 Go to previous messageGo to next message
Carsten Reckord is currently offline Carsten ReckordFriend
Messages: 27
Registered: June 2012
Junior Member
Hi Max,

Comments below.

On 13.06.2012 14:21, max power wrote:
> Hi All!
>
> I'm trying to get an uml-model from an uploaded xmi-file.
>
> For extracting the uml model I used following code:
>
>
>
> ResourceSet resourceSet = new ResourceSetImpl();
> Registry packageRegistry = resourceSet.getPackageRegistry();
> packageRegistry.put("http:||schema.omg.org/spec/XMI/2.1", UMLPackage.eINSTANCE);
> packageRegistry.put("http:||schema.omg.org/spec/UML/2.1", UMLPackage.eINSTANCE);
> Map<String, Object> extensionToFactoryMap = resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap();
> extensionToFactoryMap.put(XMI2UMLResource.FILE_EXTENSION, new XMIResourceFactoryImpl());

This registers the generic XMIResourceFactory for the .xmi file extension.

> resourceSet.getLoadOptions().put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE, Boolean.TRUE);
>
> URI uri = URI.createURI("model.xmi");
>
> Package root = UML2Util.load(resourceSet, uri, UMLPackage.Literals.PACKAGE);
>
> System.out.println(root);

In this case, the URI actually points to an existing resource (i.e. model
file). In this case, despite associating the .xmi extension with the generic
XMIResourceFactory above, the actual content of the file can be used to
detect the content type and correct resource factory
(UMLResourceFactoryImpl). This way, an UMLResource is actually created and
used for loading.


> This code works for files with a known name that a located in my eclipse workspace. What I want to do is to upload a file via my web application and then extract a model from it. For upload I'm using the org.apache.commons.fileupload.FileItem; The upload itself works fine. I have the input stream of the file.
>
> To extract the model from that inputstream I tried to do following (since it worked i.e. for ecore-files):
>
>
> Resource xmiResource = resourceSet.createResource(URI.createURI(uploadedFile.getName()));
> xmiResource.load(uploadedFile.getInputStream(), getLoadParams());

In this case, you are creating the resource with an URI that doesn't point
to an existing resource, just with its name. In this case, the registration
you did above catches and a plain XMIResource is created, which will just
load generic dynamic eobjects, since it has no association with eth UML
metamodel.

>
> Package root = UML2Util.load(resourceSet, uri, UMLPackage.Literals.PACKAGE);
>
>
> But the resource could not be loaded. It seems that only UML2Util.load can load the UML model from xmi-files in a correct way.
>
> Is there a way to use the input stream with UML2Util or, maybe, another solution for my problem?

You could

- use ResourceSet.createResource(URI, String) to pass the expected content
type of the resource ("org.eclipse.uml2.uml"), provided you did set up the
resource factory registry correcty and registered that content type (since
it worked in the local version, this seems to be the case).

- actually use the remote URI to create and load the resource

- register the UMLResourceFactoryImpl for the .xmi extension (probably not a
good idea, because an .xmi file could contain pretty much any kind of model)

>
> Many thanks in advance!
Re: Load uml resource from an uploaded file [message #886167 is a reply to message #885814] Thu, 14 June 2012 11:19 Go to previous message
max power is currently offline max powerFriend
Messages: 9
Registered: May 2012
Junior Member
Carsten, thank you very much for your detailed reply!

It works now:


extensionToFactoryMap.put(XMI2UMLResource.FILE_EXTENSION, new UMLResourceFactoryImpl());		

		
URI uri = URI.createURI(file.getName());
		
Resource xmiResource = resourceSet.createResource(URI.createURI(file.getName()));
xmiResource.load(xmiModelFile.getInputStream(), getLoadParams());
		
Package root = UML2Util.load(resourceSet, uri, UMLPackage.Literals.PACKAGE);

[Updated on: Thu, 14 June 2012 11:19]

Report message to a moderator

Previous Topic:Convert to Ecore Model
Next Topic:how to load int generals from Integer
Goto Forum:
  


Current Time: Wed Apr 24 23:21:18 GMT 2024

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

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

Back to the top