Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » EMF resource save with relative Paths and a different format
EMF resource save with relative Paths and a different format [message #1766987] Thu, 29 June 2017 10:03 Go to next message
Simon BBBBBBB is currently offline Simon BBBBBBBFriend
Messages: 54
Registered: March 2015
Member
Hello,

i am trying to write an EMF Editor that merges two models. This works very well until i try to save the model. after that, my two files are different but thats only because the way it saves the resource is different between my sample ecore model editor - file and the one i created.

This is what i expect/want:

<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="foo" nsURI="http://foo" nsPrefix="foo">
  <eClassifiers xsi:type="ecore:EClass" name="Bar">
    <eGenericSuperTypes eClassifier="ecore:EClass platform:/plugin/mymodell/model/somemodel.ecore#//MapEntry">
      <eTypeArguments eClassifier="#//Kirikaese"/>
      <eTypeArguments eClassifier="#//Kirikaese"/>
    </eGenericSuperTypes>
  </eClassifiers>
  <eClassifiers xsi:type="ecore:EClass" name="Kirikaese" eSuperTypes="platform:/plugin/mymodell/model/somemodel.ecore#//Nameable platform:/plugin/mymodell/model/somemodel.ecore#//MapEntryKey"/>
</ecore:EPackage>


this is what i get

<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="foo" nsURI="http://foo" nsPrefix="foo">
  <eClassifiers xsi:type="ecore:EClass" name="Bar">
    <eGenericSuperTypes>
      <eTypeArguments eClassifier="//Kirikaese"/>
      <eTypeArguments eClassifier="//Kirikaese"/>
      <eClassifier xsi:type="ecore:EClass" href="../../../plugin/mymodell/model/somemodel.ecore#//MapEntry"/>
    </eGenericSuperTypes>
  </eClassifiers>
  <eClassifiers xsi:type="ecore:EClass" name="Kirikaese">
    <eSuperTypes href="../../../plugin/mymodell/model/somemodel.ecore#//Nameable"/>
    <eSuperTypes href="../../../plugin/mymodell/model/somemodel.ecore#//MapEntryKey"/>
  </eClassifiers>
</ecore:EPackage>


What is it that i am doing wrong?

i am working within an eclipse-instance.

This is how i load my resource:
URI originResourceURI = URI.createPlatformResourceURI("originResourceURI", true);
originResource = editingDomain.getResourceSet().getResource(originResourceURI, true);

This is how i save them:

        final Map<Object, Object> saveOptions = new HashMap<Object, Object>();
        saveOptions.put(Resource.OPTION_SAVE_ONLY_IF_CHANGED, Resource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER);
        saveOptions.put(Resource.OPTION_LINE_DELIMITER, Resource.OPTION_LINE_DELIMITER_UNSPECIFIED);

        try {
            originResource .save(saveOptions);
        } catch (IOException e) {
            e.printStackTrace();
        }


Thank you for your time and help
Re: EMF resource save with relative Paths and a different format [message #1766989 is a reply to message #1766987] Thu, 29 June 2017 10:54 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 29545
Registered: July 2009
Senior Member
Be sure that your URI uses the .ecore extension so that org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl.createResource(URI) is actually used to create the resource. That method is implemented like this:
  public Resource createResource(URI uri)
  {
    XMLResource result = 
      new XMIResourceImpl(uri)
      {
        @Override
        protected boolean useIDs()
        {
          return eObjectToIDMap != null || idToEObjectMap != null;
        }
      };
    result.setEncoding("UTF-8");

    result.getDefaultSaveOptions().put(XMLResource.OPTION_USE_ENCODED_ATTRIBUTE_STYLE, Boolean.TRUE);
    result.getDefaultSaveOptions().put(XMLResource.OPTION_LINE_WIDTH, 80);
    result.getDefaultSaveOptions().put(XMLResource.OPTION_URI_HANDLER, new URIHandlerImpl.PlatformSchemeAware());
    return result;
  }
All these options have an impact on the serialization where the URIHandlerImpl.PlatformSchemeAware will avoid creating a relative URI between platform:/resource/... and platform:/plugin/... and the OPTION_USE_ENCODED_ATTRIBUTE_STYLE set to TRUE will avoid the nested elements with the hrefs...

When you create a platform resource URI for the result, it should be of the form "/project{/folder}*/filename.ecore".
Re: EMF resource save with relative Paths and a different format [message #1766995 is a reply to message #1766989] Thu, 29 June 2017 11:59 Go to previous message
Simon BBBBBBB is currently offline Simon BBBBBBBFriend
Messages: 54
Registered: March 2015
Member
Thank you so much ed. This is the perfect answer and it works very well.

the problem was that one of my files had a different file ending. i used the saveOptions you listed to save my file and the result is exactly what i need.
Previous Topic:Remove dangling references
Next Topic:How to know the name of a class (metadata) in a composition relationhip?
Goto Forum:
  


Current Time: Sat Sep 22 11:56:50 GMT 2018

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

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

Back to the top