WURFL Java API
by Luca Passani
passani at eunet dot no
The API is simple. You have basically three objects
which give you all the methods to obtain the info you need:
CapabilityMatrix ,
UAManager and ListManager.
- CapabilityMatrix:
getCapabilityForDevice() requires a DeviceID and a
capability name and it will return
the value of the capability.
- UAManager:
getDeviceIDFromUA() and getDeviceIDFromUALoose()
require a User-Agent string and return the WURFL device ID.
It comes with two flavors: strict matching and loose matching.
If in doubt, use the loose matching.
- ListManager:
- contains method that return lists of WURFL related data, such as list
of capabilities, list of devices, etc.
To add to that, you have a singleton (ObjectsManager) that
makes sure that all the WURFL data is initialized at least once
and only once. The three objects above are returned by
the ObjectsManager() with calls like:
CapabilityMatrix cm = ObjectsManager.getCapabilityMatrixInstance();
One important aspect of this API is that you need *no* XML
knowledge whatsoever to use it. You just deal with strings and lists
of different kinds (ArrayLists and HashMaps).
The complete JavaDocs are available here
Here is a simple example of how to use the library (this is actually
from a servlet, but if you replace out with System.out,
you can use this code for a command line application. (click here to see the complete servlet)
import net.sourceforge.wurfl.wurflapi.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
:
UAManager uam = ObjectsManager.getUAManagerInstance();
CapabilityMatrix cm = ObjectsManager.getCapabilityMatrixInstance();
out.println(
uam.getDeviceIDFromUA("MOT-T720/05.08.41R MIB/2.0 Profile/MIDP-1.0 Configuration/CLDC-1.0"));
out.println(
uam.getDeviceIDFromUA("MOT-T720/05.08.41R MIB/2.0 Profile/MIDP-1.0 Configuration/CLDC-1.0"));
out.println("loose matching MOT-T720/G_05.01.43R");
out.println(uam.getDeviceIDFromUALoose("MOT-T720/G_05.01.43R"));
out.println(uam.getDeviceIDFromUALoose("MOT-T720/G_05.01.43R"));
out.println(uam.getDeviceIDFromUALoose("UP.Browser"));
out.println(uam.getDeviceIDFromUALoose("Nokia7650"));
out.println("Capability Matrix:");
out.println("query mms_vbxml capa for some Telit phone...");
out.println(cm.getCapabilityForDevice("telit_gm822_ver1_sub302_5017","mms_wbxml"));
This will produce:
mot_t720_ver1_sub050841r
mot_t720_ver1_sub050841r
loose matching MOT-T720/G_05.01.43R
mot_t720_ver1_subg050143r
mot_t720_ver1_subg050143r
samsung_r200s_ver1_sub4119kxxxx
nokia_7650_ver1_subnover
Capability Matrix:
query mms_vbxml capa for some Telit phone...
false
One important aspect to note is that the results of your queries
are cached. This speeds up WURFL access enormously. The cache
is the same for all of the servlet/JSP pages running inside the same
webapplication.
Of course, any WURFL-based application will need to be initialized with a
wurfl.xml file. This may happen in two different ways, depending on
wether you are deploying in a web (Servlet/JSP) environment or
it's a stand-alone application.
Initializing stand-alone application
You can explicitly initialize the WURFL by telling the system where
to find the file:
ObjectsManager.getWurflInstance("C:\wurfl_stuff\wurfl.xml");
if you don't tell the library where to find the wurfl programmatically
using the method above, the API will initially look for a file called
wurfl.properties to see if the location of the wurfl is specified there:
sample wurfl.properties:
wurflpath = file://C:\\projects\\wurfl\\antbuild\\resources\\wurfl.xml
If the property file is not found, the API will look for
"C:\temp\wurfl.xml"
on Windows and "/tmp/wurl.xml" on UNIX.
If nothing is found, an exception is thrown and a message is printed.
If you want, you can try to initialize the WURFL directly off the wurfl website:
ObjectsManager.wurflWebInit();
The WURFL file location is http://wurfl.sourceforge.net/wurfl.xml .
Please note that this feature is experimental,probably not reliable and takes
a lot of time to initialize (needs to pull in 900+ kb of Wurfl down before
anything happens). Wurfl parsing itself takes around 3 seconds on my
256 Meg RAM IBM Thinkpad laptop.
Initializing web-apps
If you are using the API from inside a servlet (or if you
just use the JSP taglibs), you only need to place
the wurfl.xml file into the "/WEB-INF/" directory under
your /webapps/<webapp-name>. The same is valid
for the wurfl_patch.xml patch file).
The webapplications are shipped with an initialization (Init)
servlet which will load the wurfl and patch files when your application
server starts up.
This is done by using the following API method.
ObjectsManager.initFromWebApplication(config.getServletContext());
One little disadvantage of this is that I had to introduce a dependency
on 'servlet.jar' even for application that have nothing to do with
servlets. Given the use that most people will do of the API, producing
a dual version did not really make sense. If you have good reasons to
remove this dependency, let me know and I have some ideas on how
this can be changed.
JSP WALL tag-lib
The WURFL API package now also contains the WALL tag-lib.
The files are still small enough that there is no need to unbundle
the functionalities and produce a different package.
If you just want to use the library, you can the wurfl.zip file,
place it in your webapps directory under tomcat and you are ready to rock & roll.
Just refer to the download page
Note: if you application server is not Tomcat,
you may have to provide the xerces libs too. Please post on the WMLProgramming
mailing list if you have problems with this.
For my own installation of tomcat, webapps is located at:
C:\Program Files\Apache Group\Tomcat 4.1\webapps
but of course this may be different on your system.
Once the webapp is installed, mind the following URLs:
http://localhost:8080/wurfl/test
Test Servlet
http://localhost:8080/wurfl/wurfl.html
WURFL browser
http://localhost:8080/wurfl/
JSP tag-lib demo
If you want to see the code, you are more than welcome to do that and give
ideas and/or contribute code. You can use
the CVS client of your choice to sync with the Sourceforge CVS
repository for WURFL:
http://wurfl.cvs.sourceforge.net/wurfl/tools/java/wurflapi-xom/antbuild/
This antbuild distribution relies on
a ANT build file.
This should make it much easier to compile the WURFL API from source.
ANT is not included though. You'll have to download and install it separately
(unless you already have it), but IMO it's worth the effort.
To build the library, just jump into the antbuild directory and type ant
after you have installed ant. As an aside, if you happen to be a Ant expert and
have suggestions about how to optimize the build.xml script, just go ahead
and drop me (passani at eunet dot no) an email.
Here are the commands currently available in build.xml
C:\projects\wurfl\antbuild>ant help
Buildfile: build.xml
help:
[echo] WURFL API Build file
[echo] ------------------------------------------------------
[echo] available targets are:
[echo] buildlib (default) --> build the wurfltags.jar file
[echo] help --> print this message
[echo] docs --> build javadocs for the library
[echo] runtest --> build the test and run JUnit testing
[echo] clean --> delete everything and get ready to start again
[echo] ------------------------------------------------------
You may also want to add a line in your global server.xml TOMCAT config file
and add the following line to make your servlets and JSP reloadable during development:
TOMCAT_HOME/conf/server.xml
<Context path="/wurfl" docBase="wurfl" debug="0" reloadable="true" />
WURFL XML parsing
The Java API relies on a nice open-source API
to parse XML. XOM is a small and elegant Java library to parse XML:
http://cafeconleche.org/XOM/
The libraries are redistributable under LGPL.
XOM requires you to have some XML parser installed (like Crimson or Xerces).
If you are using a recent enough version of tomcat, you are in luck:
Tomcat already comes with the xerces library (used to parse its own
config files). The implication of this is that this version of
the WURFL Java API is tiny to deploy (less then 300 kb totally
for XOM and wurfltags.jar) on tomcat.
If you use the WURFL api for command-line or GUI applications (or
if you deploy on other application servers), you may use the Apache Xerces
library (
http://xml.apache.org/xerces2-j/) or a variety of other parsers.
|
|