Resource Access in Bundles [message #76003] |
Wed, 01 November 2006 10:57  |
Eclipse User |
|
|
|
Originally posted by: user.domain.invalid
Hi,
I have a bundle which includes several resources files, e.g. *.csv files.
How can I access these files from within my bundle?
I tried:
Url u = myBundle.getEntry("name.csv");
But the given url has the form:
bundleentry://32/name.csv
32 is the bundle id.
But how can I access to the file with such an url?
Best regards,
Sabine
|
|
|
|
|
|
|
|
|
|
|
Re: Resource Access in Bundles [message #78501 is a reply to message #78486] |
Tue, 05 December 2006 12:42   |
Eclipse User |
|
|
|
Michael, I when through this once myself. The bundle apparatus wants to
prevent you from getting the files, I suppose because the designers
viewed "files" as concrete bits on a disk drive. So you can only get
stuff thru a different abstraction, a combination of Java streams, Web
URIs, and Eclipse Paths. This code may help you find more info, esp on
FileLocator and getEntryPaths; it serves content or directories
depending on the in coming url:
public Response openPluginStream(String uri) throws IOException {
Bundle bundle = FireclipsePlugin.getInstance().getBundle();
IPath inPlugin = new Path(uri);
if (!inPlugin.hasTrailingSeparator() && !uri.equals("/")) {
try {
return new Response(HTTP_OK, getMIME(uri), FileLocator
.openStream(bundle, inPlugin, false));
} catch (IOException e) {
// IOException if the given path cannot be found in this plug-in
// Fall through and try as dir
uri = uri + "/";
}
} // else ends in "/"
Enumeration e = bundle.getEntryPaths(uri);
if (e == null)
throw new IOException("Bundle from plugin "
+ FireclipsePlugin.PLUGIN_ID
+ " has no entry paths for uri=" + uri);
String title = "Contents of " + FireclipsePlugin.PLUGIN_ID;
StringBuffer buf = new StringBuffer("<html><head><title>" + title
+ "</title><BASE href=\"" + this.getPluginURL()
+ "\"></head><body><h1>" + title + "</h1>\n");
while (e.hasMoreElements()) {
String path = e.nextElement().toString();
buf.append("<a href=\"" + path.substring(1) + "\">"
+ path.substring(1) + "</a><br>\n");
}
buf.append("</body></html>\n");
return new Response(HTTP_OK, "text/html", new StringBufferInputStream(
buf.toString()));
}
michael cao wrote:
> Hi, Tom,
>
> If url bundleentry:://32/name.csv is not converted, can it be used to open
> that file directly ?
>
> Thanks,
> Michael Cao
>
> "Tom Watson" <tjwatson@us.ibm.com> wrote in message
> news:eigao0$uac$1@utils.eclipse.org...
>
>>Ronny F wrote:
>>
>>>Hi,
>>>
>>>how do you get your bundle for myBundle.getEntry("name.csv"); without
>>>platform?
>>>
>>>Thank you.
>>>Ronny
>>>
>>
>>The BundleContext has getBundle methods that allow you to get a Bundle
>>object.
>>
>>But I'm still confused by the original question in this thread.
>>
>>Url u = myBundle.getEntry("name.csv");
>>
>>gives url has the form:
>>bundleentry://32/name.csv
>>
>>Sabine, can you explain why this URL does not work for you. Do you really
>>need it to be a file: URL? If so the you will need to look for a
>>URLConverter service that has a protocol=bundleentry service property.
>>Then you can call the URLConverter.toFileURL method.
>>
>>Tom
>
>
>
|
|
|
Re: Resource Access in Bundles [message #78610 is a reply to message #78486] |
Wed, 06 December 2006 22:46  |
Eclipse User |
|
|
|
Originally posted by: jeff_mcaffer.REMOVE.ca.ibm.com
I think there is still some confusion here. can someone post a code
pattern that is not working for them? You can indeed call
getEntry("name.csv").openStream() and read the contents. So what is the
case for which this does not work?
Jeff
michael cao wrote:
> Hi, Tom,
>
> If url bundleentry:://32/name.csv is not converted, can it be used to open
> that file directly ?
>
> Thanks,
> Michael Cao
>
> "Tom Watson" <tjwatson@us.ibm.com> wrote in message
> news:eigao0$uac$1@utils.eclipse.org...
>> Ronny F wrote:
>>> Hi,
>>>
>>> how do you get your bundle for myBundle.getEntry("name.csv"); without
>>> platform?
>>>
>>> Thank you.
>>> Ronny
>>>
>> The BundleContext has getBundle methods that allow you to get a Bundle
>> object.
>>
>> But I'm still confused by the original question in this thread.
>>
>> Url u = myBundle.getEntry("name.csv");
>>
>> gives url has the form:
>> bundleentry://32/name.csv
>>
>> Sabine, can you explain why this URL does not work for you. Do you really
>> need it to be a file: URL? If so the you will need to look for a
>> URLConverter service that has a protocol=bundleentry service property.
>> Then you can call the URLConverter.toFileURL method.
>>
>> Tom
>
>
|
|
|
Powered by
FUDForum. Page generated in 0.04679 seconds