Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Saving correctly a model with references(saving the references with the full URI)
Saving correctly a model with references [message #487059] Mon, 21 September 2009 17:18 Go to next message
No real name is currently offline No real nameFriend
Messages: 61
Registered: July 2009
Member
hi,

I have a test-plugin, which creates an uml2-model "TestModel.uml". That uml2-model references another uml2-model "BlaKeks.profile.uml" (which is located within plugin).

Now when I save the model, I want the reference to look correctly like this:
<appliedProfile href="file:/D:/Programme/Master/eclipses/eclipseMaster/workspace/org.objectteams.obtero/Blakeks.profile.uml#_qW8D4KaQEd6gIa6UvKJtLw"/>


But what I get instead, is this:
<appliedProfile href="../../../workspace/org.objectteams.obtero/Blakeks.profile.uml#_qW8D4KaQEd6gIa6UvKJtLw"/>


What am I doing wrong? How can I make EMF save the model with the absolute URI?

This is what I am doing:

			ResourceSet resourceSet = new ResourceSetImpl();
			UMLFactory umlFac = UMLFactory.eINSTANCE;
			IProject project = selectedFolder.getCorrespondingResource().getProject();
			
			// create model
			
			Model model = umlFac.createModel();
			model.setName("TestModel");
			model.createNestedPackage("fooPackage");

			// apply obtero-profile

			URL modelURL = FileLocator.find(Activator.getDefault().getBundle(), new Path("Blakeks.profile.uml"), null);
			modelURL = FileLocator.toFileURL(modelURL);
			String absolutePath = modelURL.getPath();
			URI uri = URI.createFileURI(absolutePath);

			Resource resource = resourceSet.getResource(uri, true);
			Profile obteroProfile = (Profile) EcoreUtil.getObjectByType(resource.getContents(), UMLPackage.Literals.PACKAGE);
			model.applyProfile(obteroProfile);
			
			// save model
			
			IPath sourcePath = Path.fromPortableString("model");
			if( !project.getFolder(sourcePath).exists() )
			{
				project.getFolder(sourcePath).create(true, true, null);
			}
			IFolder modelFolder = project.getFolder(sourcePath);
			
			String fileName = model.getName() + ".uml";
			IFile file = modelFolder.getFile(fileName);
			String path = file.getLocation().toFile().getAbsolutePath();
			URI fileURI = URI.createFileURI(path);
			Resource aResource = resourceSet.createResource(fileURI);
			aResource.getContents().add(model);
			aResource.save(null);



thank you

[Updated on: Mon, 21 September 2009 17:19]

Report message to a moderator

Re: Saving correctly a model with references [message #487277 is a reply to message #487059] Tue, 22 September 2009 16:15 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Comments below.

skobilke@gmx.de wrote:
> hi,
>
> I have a test-plugin, which creates an uml2-model "TestModel.uml".
> That uml2-model references another uml2-model "BlaKeks.profile.uml"
> (which is located within plugin).
>
> Now when I save the model, I want the reference to look correctly like
> this:
> <appliedProfile
> href=" file:/D:/Programme/Master/eclipses/eclipseMaster/workspace/o rg.objectteams.obtero/Blakeks.profile.uml#_qW8D4KaQEd6gIa6Uv KJtLw "/>
>
Absolute references are typically not a great thing because it means
your resources won't work on someone else's machine.
>
> But what I get instead, is this:
> <appliedProfile
> href=" ../../../workspace/org.objectteams.obtero/Blakeks.profile.um l#_qW8D4KaQEd6gIa6UvKJtLw "/>
>
That looks better to me. Why would you want to other thing? I wonder
if you should be using platform:/resource URIs. Is this really
something in the Eclipse workspace?
>
> What am I doing wrong? How can I make EMF save the modle with the
> absolute URI?
Not without using this option:

/**
* A {@link URIHandler} value that will be used to control how URIs
are {@link URI#resolve(URI) resolved} during load
* and {@link URI#deresolve(URI) deresolved} during save.
* @see URI
* @see URIHandler
* @see XMLHelper#resolve(URI, URI)
* @see XMLHelper#deresolve(URI)
* @see URIHandlerImpl
*/
String OPTION_URI_HANDLER = "URI_HANDLER";

>
> This is what I am doing:
>
> ResourceSet resourceSet = new ResourceSetImpl();
> UMLFactory umlFac = UMLFactory.eINSTANCE;
> IProject project =
> selectedFolder.getCorrespondingResource().getProject();
>
> // create model
>
> Model model = umlFac.createModel();
> model.setName("TestModel");
> model.createNestedPackage("fooPackage");
>
> // apply obtero-profile
>
> URL modelURL =
> FileLocator.find(Activator.getDefault().getBundle(), new
> Path("Blakeks.profile.uml"), null);
> modelURL = FileLocator.toFileURL(modelURL);
This seems like a bad idea. It would seem better to use
platform:/plugin/<plugin-id>/<path-in-plugin>/Blakeks.profile.uml. Then
your saved resource could be loaded into any Eclipse environment where
profile's plugin is installed, rather than being bound to your physical
machine.
> String absolutePath = modelURL.getPath();
> URI uri = URI.createFileURI(absolutePath);
>
> Resource resource = resourceSet.getResource(uri, true);
> Profile obteroProfile = (Profile)
> EcoreUtil.getObjectByType(resource.getContents(),
> UMLPackage.Literals.PACKAGE);
> model.applyProfile(obteroProfile);
>
> // save model
>
> IPath sourcePath = Path.fromPortableString("model");
> if( !project.getFolder(sourcePath).exists() )
> {
> project.getFolder(sourcePath).create(true, true, null);
> }
> IFolder modelFolder = project.getFolder(sourcePath);
>
> String fileName = model.getName() + ".uml";
> IFile file = modelFolder.getFile(fileName);
> String path = file.getLocation().toFile().getAbsolutePath();
This is a bad idea too. You should be using platform resource URIs.
Look at the FAQ for converting an IFile to a URI...
> URI fileURI = URI.createFileURI(path);
> Resource aResource = resourceSet.createResource(fileURI);
> aResource.getContents().add(model);
> // EcoreUtil.resolveAll(obteroProfile);
> // EcoreUtil.resolveAll(model);
> // EcoreUtil.resolveAll(aResource);
> // EcoreUtil.resolveAll(resourceSet);
>
> // 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);
> aResource.save(null);
>
>
> thank you


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Saving correctly a model with references [message #487355 is a reply to message #487277] Tue, 22 September 2009 20:41 Go to previous message
No real name is currently offline No real nameFriend
Messages: 61
Registered: July 2009
Member
Thank you a lot. I didn't pay any attention to these URIs Embarrassed . But now I did. You are right. Solved.
Previous Topic:Teneo custom type with parameters
Next Topic:[CDO] Revision caches
Goto Forum:
  


Current Time: Thu Mar 28 21:13:27 GMT 2024

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

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

Back to the top