Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Converting relative URIs to absolute URIs?(org.eclipse.emf.common.util.URI)
Converting relative URIs to absolute URIs? [message #790778] Sat, 04 February 2012 21:04 Go to next message
js Missing name is currently offline js Missing nameFriend
Messages: 73
Registered: July 2009
Member
I have some resources in my ResourceSet with relative URIs (org.eclipse.emf.common.util.URI), eg. "images/image_a.png".

Now I need to be able to save these resources into an archive. I create an archive uri with an absolute path:

    final URI uri = URI.createFileURI(file.getAbsolutePath());
    String uriString = uri.toString();
    final URI zipUri = URI.createURI("archive:" + uriString + "!/");


But how do I update my existing resources that only have relative paths to have the absolute archive path as root path?

I have tried:


    // Create an archive uri from an absolute plain file
    final URI uri = URI.createFileURI(destFile.getAbsolutePath());
    String uriString = uri.toString();
    final URI zipUri = URI.createURI("archive:" + uriString + "!/");

    // Create a resource with a relative uri
    URI relativeUri = URI.createFileURI("folder/somefile.txt");
    Resource resource = rs.createResource(relativeUri);

    // Update the relative uri of the resource to have the absolute archive uri as base uri
    String zipUriAsString = zipUri.toString();
    final String itemPath = zipUriAsString + resource.getURI();
    URI absUri = URI.createURI(itemPath);
    resource.setURI(absUri);

    // Save the resource/archive
    resource.save(saveOptions);


which seems to work, but is that how emf resources are intended to be used or is there a better way to transform relative uris into absolute uris (considering that the base uri is an archive)?
Re: Converting relative URIs to absolute URIs? [message #791016 is a reply to message #790778] Sun, 05 February 2012 06:07 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Comments below.

On 04/02/2012 10:04 PM, js Mising name wrote:
> I have some resources in my ResourceSet with relative URIs
> (org.eclipse.emf.common.util.URI), eg. "images/image_a.png".
That's generally not a good idea. Relative URIs get their meaning only
relative to an absolute URI...
>
> Now I need to be able to save these resources into an archive. I
> create an archive uri with an absolute path:
>
>
> final URI uri = URI.createFileURI(file.getAbsolutePath());
> String uriString = uri.toString();
> final URI zipUri = URI.createURI("archive:" + uriString + "!/");
>
>
> But how do I update my existing resources that only have relative
> paths to have the absolute archive path as root path?
So they need a path that starts with zip's URI as you have above.
>
> I have tried:
>
>
>
> // Create an archive uri from an absolute plain file
> final URI uri = URI.createFileURI(destFile.getAbsolutePath());
> String uriString = uri.toString();
> final URI zipUri = URI.createURI("archive:" + uriString + "!/");
That's treating your destination file itself as the archive.
>
> // Create a resource with a relative uri
> URI relativeUri = URI.createFileURI("folder/somefile.txt");
No, this creates an absolute URI with a relative path; that's even a
worse idea than a relative path.
> Resource resource = rs.createResource(relativeUri);
>
> // Update the relative uri of the resource to have the absolute
> archive uri as base uri
> String zipUriAsString = zipUri.toString();
> final String itemPath = zipUriAsString + resource.getURI();
> URI absUri = URI.createURI(itemPath);
> resource.setURI(absUri);
That looks better.
>
> // Save the resource/archive
> resource.save(saveOptions);
>
>
> which seems to work,
Yep.
> but is that how emf resources are intended to be used or is there a
> better way to transform relative uris into absolute uris (considering
> that the base uri is an archive)?
The path within the archive is arbitrary. You can make it whatever you
want, keeping in mind that references between the resources within the
archive will generally use relative URIs that are relative to the
archive itself. I.e., they'll use .. and path segments that will
navigate within the path structure you've induced in the archive. For
references outside the archive, they'll use whatever URI is on the
actual resource, which generally should always be an absolute URI...


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Modeling Symposium
Next Topic:Why EObject NOT Concrete Type?
Goto Forum:
  


Current Time: Fri Apr 26 16:54:29 GMT 2024

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

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

Back to the top