Saving correctly a model with references [message #487059] |
Mon, 21 September 2009 17:18 |
No real name 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 |
Ed Merks Messages: 33142 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/
|
|
|
|
Powered by
FUDForum. Page generated in 0.03377 seconds