[jdom-interest] A utility Element subclass and a request for API extension

Les Hill leh at pobox.com
Wed Mar 20 13:34:02 PST 2002


Jason, Brett,

Recently I downloaded and used JDOM 1b8 for what I imagine to be a common
use, persisting some data in XML form.  The change from using the W3 DOM API
was remarkable, thanks guys!  Constructs like:

element.addContent(new Element("foo").setAttribute("bar", "true"));

made what had been a tedious and error prone process, fairly painless.
However, I found myself continually having to convert Java primitive types
when using setAttribute(String, String, Namespace).  Eventually I wrote a
simple subclass which introduced 18 new setAttribute() signatures (9 each
for namespace specific and non).  This was much better.  One big problem
remained.  I could no longer use the above construct, I had to go to the
more long winded:

UtilityElement ue = new UtilityElement("foo");
ue.setAttribute("bar", true);
element.addContent(ue);

This problem could be solved by simply taking the 18 new setAttribute()
methods from the utility subclass and adding them directly to Element.  This
change is very much in line with the JDOM mission statement, and would
certainly make life easier for Java developers who are using JDOM to add XML
features and functionality to applications which are not directly targeted
at manipulating XML -- for example, using XML as a configuration language
for a new mail processor or using XML as an intermediate save form in a
long-running analyzer program.

The utility subclass is directly included below.

Thanks again,

Les Hill
leh at galaxynine.com

----cut here
// $Id$
package com.galaxynine.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import org.jdom.Element;
import org.jdom.Namespace;

public class UtilityElement extends Element
{
    // Use the ISO-8601 extended format used by XML Schema for dateTime
    private SimpleDateFormat dateFormat = new
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

    public UtilityElement(String name)
    {
        super(name);
    }

    public Element setAttribute(String name, boolean value)
    {
        return setAttribute(name, Boolean.toString(value));
    }

    public Element setAttribute(String name, byte value)
    {
        return setAttribute(name, Byte.toString(value));
    }

    public Element setAttribute(String name, char value)
    {
        return setAttribute(name, Character.toString(value));
    }

    public Element setAttribute(String name, Date value)
    {
        return setAttribute(name, dateFormat.format(value));
    }

    public Element setAttribute(String name, double value)
    {
        return setAttribute(name, Double.toString(value));
    }

    public Element setAttribute(String name, float value)
    {
        return setAttribute(name, Float.toString(value));
    }

    public Element setAttribute(String name, int value)
    {
        return setAttribute(name, Integer.toString(value));
    }

    public Element setAttribute(String name, long value)
    {
        return setAttribute(name, Long.toString(value));
    }

    public Element setAttribute(String name, short value)
    {
        return setAttribute(name, Short.toString(value));
    }

    public Element setAttribute(String name, boolean value, Namespace ns)
    {
        return setAttribute(name, Boolean.toString(value), ns);
    }

    public Element setAttribute(String name, byte value, Namespace ns)
    {
        return setAttribute(name, Byte.toString(value), ns);
    }

    public Element setAttribute(String name, char value, Namespace ns)
    {
        return setAttribute(name, Character.toString(value), ns);
    }

    public Element setAttribute(String name, Date value, Namespace ns)
    {
        return setAttribute(name, dateFormat.format(value), ns);
    }

    public Element setAttribute(String name, double value, Namespace ns)
    {
        return setAttribute(name, Double.toString(value), ns);
    }

    public Element setAttribute(String name, float value, Namespace ns)
    {
        return setAttribute(name, Float.toString(value), ns);
    }

    public Element setAttribute(String name, int value, Namespace ns)
    {
        return setAttribute(name, Integer.toString(value), ns);
    }

    public Element setAttribute(String name, long value, Namespace ns)
    {
        return setAttribute(name, Long.toString(value), ns);
    }

    public Element setAttribute(String name, short value, Namespace ns)
    {
        return setAttribute(name, Short.toString(value), ns);
    }
}
----cut here





More information about the jdom-interest mailing list