[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.tools.emf] Re: Reading an OMG MOF file into eclipse EMF framework

Jean-Marc,

Clients don't typically access the internals of a resource's implementation. Something like this should work better.  You should use a URI that directly references the instance file.
    // Create a resource set to hold the resources.
    //
    ResourceSet resourceSet = new ResourceSetImpl();
   
    // Register the appropriate resource factory to handle all file extensions.
    //
    resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put
      (Resource.Factory.Registry.DEFAULT_EXTENSION,
       new EMOFResourceFactoryImpl());

    // Construct the URI for the instance file.
    // The argument is treated as a file path only if it denotes an existing file.
    // Otherwise, it's directly treated as a URL.
    //
    File file = new File("c:/stuff/file.extension");
    URI uri = file.isFile() ? URI.createFileURI(file.getAbsolutePath()): URI.createURI(args[0]);

    try
    {
      // Demand load resource for this file.
      //
      Resource resource = resourceSet.getResource(uri, true);
      System.out.println("Loaded " + uri);
      resource.save(System.out, null);
    }
    catch (Exception exception)
    {
      System.out.println("Problem loading " + uri);
      exception.printStackTrace();
    }

Jean-Marc Vanel wrote:
Hi

I'm trying to read an  OMG MOF file like this:

<?xml version='1.0' encoding='UTF-8'?>
<xmi:XMI xmi:version='2.1'
xmlns:xmi='http://schema.omg.org/spec/XMI/2.1'
xmlns:mof='http://schema.omg.org/spec/mof/2.0/emof.xmi' >
<mof:Package xmi:type='mof:Package' xmi:id='135436_1' >
    <name>Data</name>
    <ownedMember xmi:type='mof:Class' xmi:id='my1' >
        <name>My_1</name>
    </ownedMember>
</mof:Package>
</xmi:XMI>

I use the latest version of everything:
net.sourceforge.lpg.lpgjavaruntime_1.1.0.v200706111738.jar org.eclipse.emf.ecore.xmi_2.4.0.v200802090050.jar org.eclipse.ocl.uml_1.2.0.v200801161341.jar
org.eclipse.emf.common_2.4.0.v200802090050.jar org.eclipse.ocl_1.2.0.v200802071345.jar org.eclipse.uml2.common_1.4.0.v200801222150.jar
org.eclipse.emf.ecore_2.4.0.v200802090050.jar org.eclipse.ocl.ecore_1.2.0.v200801161341.jar org.eclipse.uml2.uml_2.2.0.v200802110945.jar

I tried this simple code:

XMLResource r = new XMIResourceImpl();
Helper h = new EMOFHelperImpl(r);
EMOFLoadImpl loader = new EMOFLoadImpl(h);
File f = new File(args[0]);
InputStream s = new FileInputStream(f);
loader.load(r, s, /* options */new HashMap());

but got this exception:

org.eclipse.emf.ecore.xmi.PackageNotFoundException:
  Package with uri 'http://schema.omg.org/spec/MOF/2.0/cmof.xml' not found. (, 8, 74)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.getPackageForURI(XMLHandler.java:2565)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.getFactoryForPrefix(XMLHandler.java:2397)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectByType(XMLHandler.java:1275)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.createTopObject(XMLHandler.java:1444)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.processElement(XMLHandler.java:1009)

Note that I get exactly the same error within the eclipse GUI, trying to open the same file.

However somewhere on the eclipse site (http://www.eclipse.org/modeling/emf/emf/news/relnotes2.0.php) there is this:

An EMOF Resource is now provided and can be used to read or write a serialized Ecore/EMOF model. The EMOF Resource is registered for the ..emof URI suffix.

The Sample Ecore Model Editor can now edit .ecore or .emof files and supports Save As to convert between Ecore and EMOF serializations of a model.

The EMF Model Wizard now also supports import from either .ecore or ..emof files.

Ecore features not included in EMOF are nested in xmi:Extension elements with extender equal to the Ecore namespace (http://www.eclipse.org/emf/2002/Ecore).

This seems like something that is provided but undocumented.
I tried several variants of the XML namespace for MOF.
I also tried another source code:
        EMOFResourceFactoryImpl resfact = new EMOFResourceFactoryImpl();
        Resource rr = resfact.createResource(
                URI.createURI(              "http://schema.omg.org/spec/mof/2.0/emof.xmi"));
        XMLResource r =(XMLResource) rr;
        Helper h = new EMOFHelperImpl(r);
        EMOFLoadImpl loader = new EMOFLoadImpl(h);
        File f = new File(args[0]);
        InputStream s = new FileInputStream(f);
        loader.load(r, s, /* options */new HashMap());

I must miss some basic EMF knowledge ...