Wednesday, June 18, 2008

Shrink wurfl mobile database

WURFL is nice mobile database. The j2mepolish database is good too. Anyway I was trying to use wurfl.xml file(you can download it from wurfl homepage) while releasing my j2me application. But as the wurfl.xml file is too big(4.3M), I have removed some unused attributes and tags from it. And still I am able to use the parsers (I tested only the perl parser :-P ). Finally the size is reduced to (1.4M) .

My primary goal is to resize some of my background images appropriate to the the screen resolution for a specific mobile. So I filtered the resolution_width and resolution_height information.

Here is the small xslt code used to shrink the wurfl.xml .

<?xml version="1.0" ?>


This file part of MiniIM.

Copyright (C) 2007 Kamanashis Roy

MiniIM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

MiniIM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with MiniIM. If not, see <>.


<xsl:stylesheet xmlns:xsl="" version="1.0">

<xsl:output method="xml" indent="yes"/>

<!-- select the device tag -->
<xsl:template match="/wurfl">

<xsl:copy-of select="version"/>
<xsl:apply-templates select="devices/device"/>


<!-- process devices -->
<xsl:template match="device">

<device fall_back="{@fall_back}" id="{@id}" user_agent="{@user_agent}">
<!-- keep only display group -->

<xsl:apply-templates select="group[@id='display']"/>

<xsl:template match="group">

<!-- render non empty group -->
<xsl:if test="count(capability[@name='resolution_width'])=1">
<group id="{@id}">

<xsl:apply-templates select="capability[@name='resolution_width']"/>
<xsl:apply-templates select="capability[@name='resolution_height']"/>


<xsl:template match="capability">
<capability name="{@name}" value="{@value}"/>



Now let us do the transform, I have used xsltproc for the transform here, you may want to get xsltproc binary for your os or get source here. And you can apply transform like this,

shell> xsltproc your_shrinker.xslt wurfl.xml > wurfl_shrinked.xml

You can do some appropriate modifications to this code to keep some other attributes too. I prefer to write separate xsl code for each application. Note there are other alternatives, like database
usage available in wurfl site. So please read those docs before trying this :) .

Finally I used the cool perl module form imagemagick to resize my images :) .

Enjoy !


Jacques Koorts said...

You're going this totally the wrong way dude. Rather than spending hours on wurfl which is unmaintainable, you should rather write a function to resize your images auto to screensize of the phone. that way you write once, and have no maintenance problems. thats how most mobile developers tackle the problem.

wurlf is out of date by months anyway. jacques koorts

Kamanashis Roy said...

Hi, thanks for sharing the alternative idea.

But if I want to resize image then I have to keep those big images in my jar. And this will cause my jar get bigger when I could do it with small ones for a specific mobile.

The simple resizing code make the image deformed when the ratio is not right. I tested with different code with the result of DEFORMED image.

And NOT all mobiles are capable to load image that is bigger than the screen size !

You need a mobile database for many reasons than this simple one anyway.

I shall not argue about wurfl maintainability because some of the big companies are using this.

-- thanks

Anonymous said...

> wurlf is out of date by
> months anyway

Jaques, how can you say that?

I release a new WURFL monthly (last one today!) and, if you are a WURFL user, you will know that, thanks to the public interface, the community is constantly adding new devices and editing capabilities.

WURFL has been used by a major content provider for a Euro2008 site recently. Only a handful of unrecognised UAs were detected!

Luca Passani (WURFL Inventor and Maintainer)

Jacques Koorts said...

Luca Passani, my apologies. I don't know what i was thinking when writing that post :)

wurfl is great for what you're trying to do MiniIM, but if you can then rather attack the problem like this:

Put the following info in the jad:

1. url to pic
2. phone make/model

then do from phone: http://url_to_pic?make=nokia;model=n95

then on your website you use wurfl to resize the pic on the fly and send it back to the phone.

ofcourse this is only useful if you're already doing networking in your app, else you'll get that crappy security warning, which can make a standalone-non-networking app kinda ugly.

if you're not doing networking stuff, then yes you have to put it in the jar.

using j2me polish you then have to do a build for a few sets of phones (else you have to make 1000's of builds) like S40, S60, Samsung, Moto, etc

Another way to make builds is to make them per heap memory specified. So 10K JPG for phones with 500KB heap, 20K for 800 and so one.

J2ME Polish has information on the heap memory.

But there is really no need to compress wurfl, as Luca Passani has some nice and fast libs to access the xml file.