API classes and interfaces that are used for import/export of XMI documents into/from a JMI-compliant repository. Below is a description of the most common use-cases.

Reading XMI

XMIReader instances can be used for reading XMI documents. XMIReader class extends the standard javax.jmi.xmi.XmiReader interface and is a little more flexible (allows plugging of custom XMIReferenceResolver implementations via XMIInputConfig for custom handling of references (href's) in the XMI documents being read).

Here is a sample code that creates an empty instance of MOF and reads in an XMI containing some MOF metamodel:

    // get the default repository
    MDRepository repository = MDRManager.getDefault().getDefaultRepository();
    
    // start a write transaction
    boolean fail = true;
    repository.beginTrans(true);
    try {
        // create a new MOF extent
        RefPackage mof = repository.createExtent("MyMOFExtent");
        // create an XMIReader
        XMIReader reader = XMIReaderFactory.getDefault().createXMIReader();
        // read the document
        reader.read(new File("myxmifile.xmi").toURL().toString(), mof);
        // everything succeeded => set fail flag to false
        fail = false;
    } catch (Exception e) {
        System.out.println("Fatal error reading XMI.");
        e.printStackTrace();
    } finally {
        // commit/rollback the transaction
        repository.endTrans(fail);
    }

Writing XMI

Writing is similar to reading. The following code writes content of "MyMOFExtent" to an XMI file:

    // get the default repository
    MDRepository repository = MDRManager.getDefault().getDefaultRepository();
    
    // declare an output stream
    FileOutputStream out = null;
    
    // start a read-only transaction
    repository.beginTrans(false);
    try {
        // get the extent
        RefPackage mof = repository.getExtent("MyMOFExtent");
        // create an XMIWriter
        XMIWriter writer = XMIWriterFactory.getDefault().createXMIWriter();
        // open a stream
        out = new FileOutputStream("myxmifile.xml");
        // export the document (default version of XMI = 1.2)
        writer.write(out, mof, null);
    } catch (Exception e) {
        System.out.println("Fatal error reading XMI.");
        e.printStackTrace();
    } finally {
        // release the transaction
        repository.endTrans();
        // close the stream if necessary
        if (out != null) {
            try {
               out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }