|
Re: Converting relative URIs to absolute URIs? [message #791016 is a reply to message #790778] |
Sun, 05 February 2012 06:07 |
Ed Merks 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/
|
|
|
Powered by
FUDForum. Page generated in 0.02551 seconds