Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Saving a resource to a new uri
Saving a resource to a new uri [message #805942] Fri, 24 February 2012 05:21 Go to next message
Klaus S. is currently offline Klaus S.
Messages: 10
Registered: January 2011
Junior Member
Hi all,

I'd like to save a model resource along with its referenced resources to a new uri. My trouble is, the references of the model are pointing after saving still to the original resource files. What I'm doing wrong?

The code is posted below:


public boolean saveResource(Resource res, String path) {
    res.setURI(URI.createFileURI(path));
    Map<Object, Object> defaultSaveOptions = getSaveOptions();
    // save additional resources (profiles, ...)
    if (res.getResourceSet() != null) {
        for (Resource resTmp : res.getResourceSet().getResources()) {
	    if (!resTmp.getURI().path().equals(res.getURI().path())) {
               resTmp.setURI(res.getURI().trimSegments(1)
                .appendSegment(resTmp.getURI().lastSegment()));

		try {
		   resTmp.save(Collections.EMPTY_MAP);
		} catch (IOException e) {
			e.printStackTrace();
		}
	    }
        }
    }
    try {
        res.save(defaultSaveOptions);
    } catch (IOException e) {
	e.printStackTrace();
	return false;
    }
    return true;
}
Re: Saving a resource to a new uri [message #805962 is a reply to message #805942] Fri, 24 February 2012 05:49 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 25746
Registered: July 2009
Senior Member
Christian,

Comments below.

On 24/02/2012 11:21 AM, Christian S. wrote:
> Hi all,
>
> I'd like to save a model resource along with its referenced resources
> to a new uri. My trouble is, the references of the model are pointing
> after saving still to the original resource files. What I'm doing wrong?
>
> The code is posted below:
>
>
>
> public boolean saveResource(Resource res, String path) {
> res.setURI(URI.createFileURI(path));
Have all the proxies to this resource been resolved? If there are any
unresolved proxies they will resolve to the old name. You can use
EcoreUtil.resolveAll to ensure they're all resolved.
> Map<Object, Object> defaultSaveOptions = getSaveOptions();
> // save additional resources (profiles, ...)
> if (res.getResourceSet() != null) {
> for (Resource resTmp : res.getResourceSet().getResources()) {
> if (!resTmp.getURI().path().equals(res.getURI().path())) {
Is this a very expensive way of saying resTemp != res?
> resTmp.setURI(res.getURI().trimSegments(1)
> .appendSegment(resTmp.getURI().lastSegment()));
These resources don't have references to each other, right? Only to the
resource you renamed earlier?
>
> try {
> resTmp.save(Collections.EMPTY_MAP);
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
> }
> }
> try {
> res.save(defaultSaveOptions);
> } catch (IOException e) {
> e.printStackTrace();
> return false;
> }
> return true;
> }
>
Re: Saving a resource to a new uri [message #806878 is a reply to message #805962] Sat, 25 February 2012 11:49 Go to previous message
Klaus S. is currently offline Klaus S.
Messages: 10
Registered: January 2011
Junior Member
Ed Merks wrote on Fri, 24 February 2012 05:49

>
> public boolean saveResource(Resource res, String path) {
> res.setURI(URI.createFileURI(path));
Have all the proxies to this resource been resolved? If there are any
unresolved proxies they will resolve to the old name. You can use
EcoreUtil.resolveAll to ensure they're all resolved.
>


EcoreUtil.resolveAll ... thats it. thanks!
Previous Topic:[EMF.Edit] How to delete a reference/object using RemoveCommand, my approach correct?
Next Topic:EMF Compare and OPTION_ZIP ?
Goto Forum:
  


Current Time: Sat Apr 19 03:06:56 EDT 2014

Powered by FUDForum. Page generated in 0.02211 seconds