Friday, December 24, 2010

Saving the xsi:schemaLocation into EMF XMI file

Previously I've shared how to fix loading an EMF XMI file within Eclipse IDE that has no xsi:schemaLocation.

The reason why it happened to me was everytime XMIResourceImpl.save() is called, it removes the xsi:schemaLocation from the saved XMI file. Which made the file not openable from Eclipse IDE again. I want to get rid of this problem once and for all!

Thanks to Ed Merks, there are three ways to solve this problem:

  1. Install the plugin containing your EMF-generated package to the Eclipse IDE. This seems to be the proper way to do it. Better yet, you can install the generated EMF.Edit and EMF.Editor plugins so you can experience the real editor with all the features (including icons) you've prepared.
  2. Use XMIResource.OPTION_SCHEMA_LOCATION to serialize the xsi:schemaLocation. I'll detail this below.
  3. Change the package's nsURI so that it points to the real, absolute path of the Ecore file (or the metamodel, I think you can use an XML Schema/XSD or UML file as well). This can be a file:// URI or a http:// URI (I guess platform:/ URIs would work as well).

Since the first and third option is self-explanatory, here's how to do the second option.

Using XMIResource.OPTION_SCHEMA_LOCATION


Open your *PackageImpl class and override the createResource() method to specify the actual schema (Ecore metamodel) location:

@Override
protected Resource createResource(String uri) {
return super.createResource("file:/home/ceefour/project/AbisPulsa/workspace/com.abispulsa.model/src/model/abispulsa.ecore");
}

The method is not there so you have to create it (press Ctrl+Space on an empty space to bring up the code assist).

Now when doing resource.save(), you need to set the XMIResource.OPTION_SCHEMA_LOCATION to true :

log.info(String.format("Saving resource: %s", getCatalog().eResource().getURI()));
HashMap<String, Object> opts = new HashMap<String, Object>();
opts.put(XMIResource.OPTION_SCHEMA_LOCATION, true);
getCatalog().eResource().save(opts);

Now it saves the xsi:schemaLocation to the XMI file. :-)

Also sourced from EMF Wiki: http://wiki.eclipse.org/index.php/EMF/FAQ#How_can_I_ensure_that_an_xsi:schemaLocation_is_serialized_for_my_packages.3F

6 comments:

  1. Blizzard tried to solve the long wait problem of people in Wow Gold Account restoration queues. The number of compromised accounts under the mandatory authenticator plan should be greatly reduced, if not be completely eliminated, so that Cheap WOW Gold players can enjoy a much more secure game environment. Blizzard can make it providing all of its 11.5 million players with higher speed even though some probably have a hard time with the transition.Buy wow Gold

    ReplyDelete
  2. Apache Ivy may simply Cheap RS Goldbecause they to help users keep with the information they do know as opposed to forcing people to improve their own resources. Many of us tried Maven after and yes it was obviously a waste all items have to become the best way Expert desires so that it is normally this doesn't work. Apache Ant/Ivy it can be Buy WOW Goldmuch more adaptable.

    ReplyDelete
  3. Hi, good summary! One thing you want to change is to use absolute URI (file:///home/ ....) instead of (file:/home). The latter will become messy in case when you have projects in eclipse physically located in different directories. For example you might have:

    project1
    model
    my.ecore
    project2
    my.xmi

    but if project1 is in /home/user/projects/project1 and project2 is in /home/user/projects2/project2 the relative URI set in schemaLocation of mu.xmi will be ../../projects/project1/model/my.ecore. This will not work - the relative URI are resolved using eclipse platform rather than physical location (in this example it will be ../project1/model/my.ecore).


    ReplyDelete
  4. finally i found my bug :) ty for this helpfull thread.

    You might consider to put the annotation

    /**
    * @generated not
    */

    to ur method that it won't be overriden when u need to generate ur classes again

    ReplyDelete
  5. Hey guys, what are you busy in recently? Past can not be changed but the future is still in your power. No matter what happens in your life, make it either a good memory or a good lesson.Just put forward. Do what you like and be happy. Enjoy everyday as it is the last minute of the world. I always play d3 to have fun. But I need to farm Diablo 3 gold which is a liitle boring. Do you guys know any place to buy cheap diablo 3 gold? Be happy.

    ReplyDelete
  6. Past can not be changed but the future is still in your power. No matter what happens in your life, make it either a good memory or a good lesson.Just put forward.
    xiaomi mi 4i review
    buy Meizu Metal
    meizu mx5

    ReplyDelete