WURFL Parser

WURFL is a Device Description Repository, a catalogue of mobile device information and a framework for adaptation of mobile applications.

This is a SAX (Simple API for XML) parser, which can search WURFL XML & return all the device details matching the keyword given.

package subin.rnd.xml;

import java.util.Hashtable;
import java.util.Vector;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

public class WurflParser extends DefaultHandler
{
    public  Vector<Device> deviceList;
    private Device currentDevice;
    private Group  currentGroup;
    private String keyword;

    public static void main(String[] args)
    {
        WurflParser wp = new WurflParser("Nokia6600");

        System.out.println("Total Matches for \"" + wp.keyword + "\": " + wp.deviceList.size() + "\n");
        for(int i = 0; i < wp.deviceList.size(); i++)
            System.out.println(wp.deviceList.elementAt(i));
    }

    public WurflParser(String keyword)
    {
        this.keyword = keyword.toLowerCase().trim();
        this.deviceList = new Vector<Device>();

        try
        {
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            sp.parse("res/wurfl.xml", this);
        }
        catch (Exception e)
        {
            System.out.println("Exception while parsing the file. " + e);
        }
    }

    public void startElement(String uri, String localName, String qName,
            Attributes attributes)
    {
        if (qName.equalsIgnoreCase("device") && attributes.getLength() > 0)
        {
            if (!isValidDevice(attributes.getValue("user_agent")))
                return;
            this.currentDevice = new Device();
            this.currentDevice.id = attributes.getValue("id");
            this.currentDevice.fallBack = attributes.getValue("fall_back");
            this.currentDevice.actualDeviceRoot = Boolean.parseBoolean(attributes.getValue("actual_device_root"));
            this.currentDevice.userAgent = attributes.getValue("user_agent");
        }

        else if (qName.equalsIgnoreCase("group") && (attributes.getLength() > 0)
                && (this.currentDevice != null))
        {
            this.currentGroup = new Group();
            this.currentGroup.id = attributes.getValue("id");
        }

        else if (qName.equalsIgnoreCase("capability") && (attributes.getLength() > 0)
                && (this.currentGroup != null))
        {
            this.currentGroup.addAttribute(attributes.getValue("name"),
                    attributes.getValue("value"));
        }
    }

    public void endElement(String uri, String localName, String qName)
    {
        if (qName.equalsIgnoreCase("device") && (this.currentDevice != null))
        {
            this.deviceList.add(this.currentDevice);
            this.currentDevice = null;
        }

        else if (qName.equalsIgnoreCase("group") && (this.currentGroup != null))
        {
            this.currentDevice.addGroup(this.currentGroup);
            this.currentGroup = null;
        }
    }

    private boolean isValidDevice(String userAgent)
    {
        boolean status = (userAgent != null) && (userAgent.toLowerCase().indexOf(this.keyword) > -1);
        if (status)
            System.out.println("Comparing [" + status + "] " + userAgent);
        return (status);
    }

    public static class Device
    {
        public String id;
        public String userAgent;
        public String fallBack;
        public boolean actualDeviceRoot;
        public Vector<Group> group;

        public void addGroup(Group group)
        {
            this.group = (this.group == null)
                ? new Vector<Group>()
                : this.group;
            this.group.add(group);
        }

        public String toString()
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.append("Id: " + this.id + "\n");
            stringBuilder.append("UserAgent: " + this.userAgent + "\n");
            stringBuilder.append("FallBack: " + this.fallBack + "\n");
            stringBuilder.append("Actual Device Root: " + this.actualDeviceRoot + "\n");

            if (this.group != null)
            {
                for(int i = 0; i < this.group.size(); i++)
                    stringBuilder.append(this.group.elementAt(i));
            }

            return (stringBuilder.toString());
        }
    }

    public static class Group
    {
        public String id;
        public Hashtable<String, String> attributes = null;

        public void addAttribute(String key, String value)
        {
            this.attributes = (this.attributes == null)
                ? new Hashtable<String, String>()
                : this.attributes;
            this.attributes.put(key, value);
        }

        public String toString()
        {
            StringBuilder stringBuilder = new StringBuilder();

            if (this.attributes != null)
            {
                stringBuilder.append("  [" + this.id + "] \n");
                for (String key : this.attributes.keySet())
                {
                    stringBuilder.append("  " + key + ": "
                            + this.attributes.get(key) + "\n");
                }
            }

            stringBuilder.append("\n");
            return (stringBuilder.toString());
        }

    }
}

Check this SAX (Wikipedia) link to know more about SAX.

Advertisements