Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » UML2 » import XMI per Input Stream(Fail while trying to import files into emf using inputstreams)
import XMI per Input Stream [message #759245] Sun, 27 November 2011 21:43 Go to next message
Alex Miranski is currently offline Alex Miranski
Messages: 2
Registered: November 2011
Junior Member
Hey Guys,

I'm trying to build a EMF-Ecore-Model from a XMI-File. Not a big Thing, but the File is only available as ByteArray. Im using Maven for building with Emf v2.6.0 . Some googleing gave me the following lines of Code.

public Package importModel(final byte[] modelData) {
        Package model = null;

        EPackage.Registry.INSTANCE.put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
        Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);

        try {
            final ByteArrayInputStream bais = new ByteArrayInputStream(modelData);
            Resource resource = new XMIResourceImpl();

            // Create XMLMap, used for proper XML mapping. This is an empty Map,
            // it is using generated annotations for proper streaming
            XMIResource.XMLMap xmlMap = new XMLMapImpl();

            // Create options map for loading
            Map options = new HashMap();

            // Populate options
            options.put(XMIResource.OPTION_XML_MAP, xmlMap);

            try {
                resource.load(bais, options);
            } catch (Exception e) {
                e.printStackTrace();
            }




But the resource.load(); throws "org.eclipse.emf.ecore.resource.Resource$IOWrappedException: Package with uri 'http: //www.eclipse.org/uml2/2.1.0/UML' not found." (added space so its not a link). There are a lot of Threads around the Internet, but mostly the two lines before the try block solves the Problem for them. But i dont want to write the data to harddisk, just to get an URI and load it again. So there is no "Extension" that the factory map can use.

Any suggestions? I'm thankful for every Advise.


Greetz
Re: import XMI per Input Stream [message #759261 is a reply to message #759245] Mon, 28 November 2011 02:00 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 4026
Registered: July 2009
Senior Member
Hi

You're running standalone so you have to manually provide all the
relevant registrations that Eclipse plugins provide automatically.

"Package with uri 'xxx' not found." means that neither your
ResourceSet.getPackageRegistry() no the global package registry has a
registration for "xxx".

The UML2 project appears not to provide a standalone init routine to
make this easy.

You may find it sufficient to just do UMLPackage.eINSTANCE.getClass().
Otherwise you may find org.eclipse.ocl.uml.OCL.initialize(ResourceSet)
helpful.

Regards

Ed Willink

On 28/11/2011 02:43, bloodyworld.emf wrote:
> Hey Guys,
>
> I'm trying to build a EMF-Ecore-Model from a XMI-File. Not a big
> Thing, but the File is only available as ByteArray. Im using Maven for
> building with Emf v2.6.0 . Some googleing gave me the following lines
> of Code.
>
> public Package importModel(final byte[] modelData) {
> Package model = null;
>
> EPackage.Registry.INSTANCE.put(UMLPackage.eNS_URI,
> UMLPackage.eINSTANCE);
>
> Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION,
> UMLResource.Factory.INSTANCE);
>
> try {
> final ByteArrayInputStream bais = new
> ByteArrayInputStream(modelData);
> Resource resource = new XMIResourceImpl();
>
> // Create XMLMap, used for proper XML mapping. This is an
> empty Map,
> // it is using generated annotations for proper streaming
> XMIResource.XMLMap xmlMap = new XMLMapImpl();
>
> // Create options map for loading
> Map options = new HashMap();
>
> // Populate options
> options.put(XMIResource.OPTION_XML_MAP, xmlMap);
>
> try {
> resource.load(bais, options);
> } catch (Exception e) {
> e.printStackTrace();
> }
>
>
>
> But the resource.load(); throws
> "org.eclipse.emf.ecore.resource.Resource$IOWrappedException: Package
> with uri 'http: //www.eclipse.org/uml2/2.1.0/UML' not found." (added
> space so its not a link). There are a lot of Threads around the
> Internet, but mostly the two lines before the try block solves the
> Problem for them. But i dont want to write the data to harddisk, just
> to get an URI and load it again. So there is no "Extension" that the
> factory map can use.
>
> Any suggestions? I'm thankful for every Advise.
>
>
> Greetz
>
Re: import XMI per Input Stream [message #764384 is a reply to message #759245] Sun, 11 December 2011 23:21 Go to previous message
Alex Miranski is currently offline Alex Miranski
Messages: 2
Registered: November 2011
Junior Member
After a lot of testing i get it to work. I used the Initialisation-stuff from here: http: //stackoverflow.com/questions/1404343/eclipse-modelling-framework-emf-how-to-get-the-domainmodel-from-a-magicdraw-u

And i create a Resource with a Fake-URI, seems to work.

final ByteArrayInputStream modelStream = new ByteArrayInputStream(modelArray);
final Resource resource = resourceSet.createResource(URI.createURI("fakeResource.uml"));
resource.load(modelStream, null);
model = (Package) EcoreUtil.getObjectByType(resource.getContents(), UMLPackage.Literals.PACKAGE);
Previous Topic:org.eclipse.uml2.uml.Type::createAssociation
Next Topic:Problem creating a metamodel that extends UML2
Goto Forum:
  


Current Time: Thu Aug 21 04:30:22 EDT 2014

Powered by FUDForum. Page generated in 0.02624 seconds