|
|
||
|
In this tutorial I create a
real-world example app that drills for resources
(e.g. properties, icons, images, xml documents, xsl stylesheets, etc.)
in the app's jar using Rachel's high-level
resource manager
getIcon
, getImage
,
getText
, getProperties
power getter methods
hiding the low-level, close-to-the-metal
getResource
and getResourceAsStream
class loader wizardy.
Here is a first impression of the ResMan resource loading example app.
Behind the scene
ResMan retrieves menu.properties
from its jar
and builds the help menu on-the-fly;
runs the overview.xml
xml document through an xslt engine using the
doc2html.xls
stylesheet to
show off the resulting hmtl page in the built-in browser as seen above;
retrieves Vanessa's portrait vanessa.jpg
and hangs it up in tabbed panel; and
retrieves Strunk's Style Rule Five marked-up in HTML in
strunk.html
in a single stroke and much more.
Retrieving resources from jars is easy
if you use Rachel's ResMan
's power getters
such as
ResMan.getText
,
ResMan.getIcon
,
ResMan.getImage
,
ResMan.getXmlDocument
,
ResMan.getProperties
,
or ResMan.getInputStream
.
Before you can drill for bits you need to tell Rachel all jars holding your treasures (that is, resources such as images, properties, xsl stylesheets, etc.). In the ResMan example all resources reside in the same jar along-side the app's binary byte-code classes.
Example: resman.jar
table of contents
doc2html.xsl menu.properties overview.xml strunk.html CmdShowDocumentBuiltIn.class demo/common/html/ CmdShowDocumentHousehold.class demo/common/html/ WebBrowser$1.class demo/common/html/ WebBrowser.class demo/common/html/ ImagePanel.class demo/common/image/ XmlTreeModel.class demo/common/tree/ XmlTreeNodeRenderer.class demo/common/tree/ AppFrame$1.class demo/resman/ AppFrame.class demo/resman/ Tool.class demo/resman/ inform.gif images/ vanessa.jpg images/ world2.gif images/ MANIFEST.MF META-INF/
The rachel.loader.ClassResourceLoader
class allows you to identify a jar using a single class
(usually dubbed resouche anchor)
and spares you from combing
your user's Web Start cache to find
out the jar's mangled filename.
Instead of creating a pure, single-purpose, dummy resource anchor class,
abuse the example's demo.resman.Tool
class
holding the main
method
and pass it on to ClassResourceLoader
.
Kick off your bit drilling extravaganza with
the one-line magic spell below.
ResMan.setResourceLoader( new ClassResourceLoader( Tool.class ) );
Icons. Use ResMan.getIcon()
to get icons from your app's jars. Example:
ImageIcon appIcon = ResMan.getIcon( "images/inform.gif" );
Compare this to the low-level, multi-line, heavy-lifting class loader workout below. Use ResMan's power getters and kiss the ClassLoader goodbye.
ClassLoader cl = Tool.class.getClassLoader(); URL appIconUrl = cl.getResource( "images/inform.gif" ); ImageIcon appIcon = new ImageIcon( appIconUrl );
Images. Use the ResMan.getImage
one-liner
below to get images (aka graphic pictures) from your app's jars.
Example:
BufferedImage vanessaImg = ResMan.getImage( "images/vanessa.jpg" );
Rachel uses Java's 1.4 new plugable image i/o toolkit. Out-of-the-box it can read gif, jpeg and png graphics formats. If you need other formats such as bmp or tiff, for example, ship a image i/o plug-in with your app.
Text. Use the ResMan.getText
power getter
below to get a text file's content stored in your app's jars
packed up in a string. Example:
String strunkHtmlSnippet = ResMan.getText( "strunk.html" );
Properties. Use ResMan.getProperties
to get a property file stored in your app's jars
delivered in a java.util.Properties table to your doorstep. Example:
Properties props = ResMan.getProperties( "menu.properties" );
Xml Documents. Use ResMan.getXmlDocument
to get an xml document stored in your app's jar
pre-parsed and stored in a JDOM tree ready-to-use. Example:
Document doc = ResMan.getXmlDocument( "overview.xml" );
Input Streams.
If ResMan
doesn't sport
a power getter for your desired format yet
(e.g. mp3 audio, mpeg video, etc.)
or if you want to pass on the raw InputStream
use ResMan.getInputStream
.
Example:
InputStream inStream = ResMan.getInputStream( "overview.xml" ); InputStream xslStream = ResMan.getInputStream( "doc2html.xsl" );
That's it. That's all there is to it.
|
Send your comments, suggestions, praise or poems to webmistress@vamphq.com | Copyright © 2001, 2002 Gerald Bauer |