|
Re: Converting URI to URL and plus (+) character [message #812102 is a reply to message #812086] |
Sat, 03 March 2012 08:00 |
Ed Merks Messages: 33141 Registered: July 2009 |
Senior Member |
|
|
Peter,
Comments below.
On 03/03/2012 8:23 AM, Peter Severin wrote:
> I have an issue with URI to URL conversion for resources that contain
> '+' character in their names. Here's how I do the conversion:
>
>
> URI uri = URI.createPlatformURI(resourceWithPlusChar, true);
This isn't commonly used. If you have a path in the workspace, you'd
use URI.createPlatformResourceURI.
> URL url = new URL(uri.toString());
> InputStream stream = url.openStream();
>
> The last line throws an FileNotFoundException like this:
> java.io.FileNotFoundException:
> /home/peter/work/sketcher/runtime-workspace35/wireframing-icons/assets/Folder
> .svg
This looks like an absolute path in the file system. You'd use
URI.createFileURI for that. Of course if you're really wanting to
properly access resources in the workspace you'd use
URI.createPlatformResourceURI("wireframing-icons/assets/Folder+.svg")
assuming that wireframing-icons is a project in your workspace.
>
> In the message it's clear that the '+' sign got converted to spaces. I
> tracked the conversion to
> org.eclipse.core.internal.resources.PlatformURLResourceConnection#resolve
> where it passes the string through URLDecoder#decode:
>
> String filePath = url.getFile().trim();
> filePath = URLDecoder.decode(filePath, "UTF-8"); //$NON-NLS-1$
>
>
> So what's the proper way to convert from EMF's URI to URL?
Use createPlatformResourceURI for accessing the workspace or
createFileURI for accessing the file system directly; don't bypass the
workspace with direct file system access because you'll bypass things
like keeping local history.
> Shouldn't EMF also encode plus characters?
It doesn't look like that's necessary. It can be used directly in file
system URIs and in platform resource URIs. I tried creating an EMF
resource with + in the name (an Ecore model named My+model.ecore) and
that worked fine. So, apparently, if you use
ResourceSet.getURIConverter or URIConverter.INSTANCE and call
createInputStream with your URI, that will work properly.
>
>
> Thanks in advance,
>
> Peter
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
|
|
Re: Converting URI to URL and plus (+) character [message #813478 is a reply to message #812840] |
Mon, 05 March 2012 10:16 |
Peter Severin Messages: 19 Registered: July 2009 |
Junior Member |
|
|
Ed, thanks for your reply. My workaround for now is to escape pluses
like this:
new URL(uri.toString().replace("+", "%2B"))
On 04/03/2012 13:43, Ed Merks wrote:
> Peter,
>
> The platform itself provides the URL handler for "platform" scheme so
> that's something you'll have to report to the platform. Another approach
> is to use createFromImageData and use the ImageData constructor that
> takes a stream which you can then use the URI converter to create. Of
> course if you're using IFile, you can get the input stream from that
> directly. Avoiding the use of + would be perhaps easier...
>
>
> On 04/03/2012 10:12 AM, Peter Severin wrote:
>> Ed, thanks for your reply. I do use URI.createPlatformResourceURI and
>> I don't bypass workspace access. I saw that EMF uses URIConverter to
>> get InputStream directly from an URI and you are right that this
>> handles '+' characters correctly.
>>
>> However there is still an issue with proper URI to URL conversion. In
>> many cases it's not possible to use URIConverter and one needs an URL.
>> For example if I want to use ImageDescriptor#createFromURL I can't use
>> an URI.
>>
>> I created a test case that can be ran as JUnit plugin test:
>>
>> public class URITest extends TestCase {
>> protected void setUp() throws Exception {
>> IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
>> IProject project = root.getProject("test");
>> project.create(null);
>> project.open(null);
>> IFile file = project.getFile("file+.txt");
>> file.create(new ByteArrayInputStream("test".getBytes()), true, null);
>> }
>>
>> public void testPlus() throws Exception {
>> URI uri = URI.createPlatformResourceURI("/test/file+.txt", true);
>> URL url = new URL(uri.toString());
>> assertTrue(url.toString().contains("+"));
>> url.openStream(); // IT FAILS HERE
>> }
>> }
>>
|
|
|
Powered by
FUDForum. Page generated in 0.03885 seconds