Results 1 to 3 of 3
- 02-11-2009, 07:19 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 2
- Rep Power
- 0
Protocol for loading resources from jar
Hi all,
I'm writing a program that, among other things, displays html documents. These documents are often quite image-heavy, and downloading all the resources can take some time. I'm now planning on bundling some of the most commonly used resources directly into my JAR file, and want it to be easy to reference these from the external documents: e.g. instead of
I could useJava Code:<img scr="http:/example.com/logo.jpg"/>
or whatever. (note: only one slash shown in examples because forum doesn't like me posting links).Java Code:<img src="resource:/org/company/logo.jpg"/>
Note that I may not know the location of the jar file, or maybe not even the jar file it's in. I think this should be ok, though -- I'd want to do essentially the same thing that SomeClass.getClassLoader().getResource("/org/company/...") would do.
Apparently there's some way of creating a custom protocol, but I haven't really understood the documentation on this. Can anyone provide an example of this in action? Or, if that's not the right line of thought, some other solution?
Thanks so much,
Sam
- 02-12-2009, 07:57 AM #2
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Here's an outline of the necessary steps:
1. Create a class that implementsURLStreamHandlerFactory for your new custom protocol
2. Register that class globally, by calling
URL.setStreamHandlerFactory({an instance of your handler});
Now, anytime the HTMLKit sees a URL with your custom protocol, it will call your stream handler to get a URLStreamHandler. You will need to return a customized URLStreamHandler that extends URLStreamHandler, so that you can override the openConnection methods to return a customized version of a URLConnection you've overwritten.
Finally, you're customized URLConnection should override getInputStream to return the results of ClassLoader.getResourceAsStream.
Painful, but doable.
- 02-12-2009, 07:14 PM #3
Look at
URL url = ClassLoader.getSystemClassLoader.getResource(name) ;
This searches the classpath and returns a URL to the resource. ImageIO and File can both use URL (or URL.toURI()) to retrieve the contents of the resource.
Since your JAR is on the classpath, your resource will be found.
ClassLoader also has a getResourceAsStream() method.
Note that name must use "/" to define the classpath to the resource.
Similar Threads
-
x modem protocol
By jithan in forum New To JavaReplies: 0Last Post: 08-21-2008, 10:43 AM -
Loading resources generates warning messages
By Namita Patil in forum New To JavaReplies: 6Last Post: 07-03-2008, 02:04 PM -
Problem loading resources.
By jimm1 in forum Advanced JavaReplies: 6Last Post: 06-23-2008, 07:31 PM -
New Protocol?
By lada.r in forum NetworkingReplies: 0Last Post: 11-06-2007, 09:13 PM -
how to use SIP protocol
By katie in forum Advanced JavaReplies: 1Last Post: 08-06-2007, 10:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks