Archive for the 'F/OSS' Category

ClassNotFoundException using XMLUnit with JDK 1.4

I recently set up a testcase for validating generated XML and in doing so, decided to use the XMLUnit extensions to jUnit.

When running my testcase, I got the following stacktrace:

java.lang.ClassNotFoundException: org.w3c.dom.ranges.DocumentRange
	at java.net.URLClassLoader$1.run(URLClassLoader.java:199)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:141)
	at com.client.MapHandlerTest.testMarshal(MapHandlerTest.java:79)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:324)
	at junit.framework.TestCase.runTest(TestCase.java:154)
	at junit.framework.TestCase.runBare(TestCase.java:127)
	at junit.framework.TestResult$1.protect(TestResult.java:106)
	at junit.framework.TestResult.runProtected(TestResult.java:124)
	at junit.framework.TestResult.run(TestResult.java:109)
	at junit.framework.TestCase.run(TestCase.java:118)
	at junit.framework.TestSuite.runTest(TestSuite.java:208)
	at junit.framework.TestSuite.run(TestSuite.java:203)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

This client uses a fairly old release of Java 1.4, and it appears the

org.w3c.dom.ranges.DocumentRange

class (and family) hadn’t entered the JDK at that time.

No worries, just downloaded a more recent XML parser (Xerces 2.8.1), added xml-apis.jar to my classpath and the tests run smoothly again. :)

Formatting dates with displaytag

Stumbled upon this weird oddity/bug when using the excellent displaytag library. I need to output a date according to my own date pattern, and tried to apply a date format to my column like so:

This however doesnt work, and just returns the format. There’s a bug report on a somewhat similar problem, and Marco Papini states that he could get it to work by using escapeXml="false" but for myself, I just needed to pad my date format (yyyy-MM-dd) like this {0,date,yyyy-MM-dd} to make it a proper format for MessageFormat as per the docs.

So, if you get problems formatting dates with displaytag, make sure you use MessageFormat-approved syntax, and if that still fails, add escapeXml="false".

My code ended up:

PropertyEditor for joda-time

Hi there again.

Just thought I’d let you take a look at this very basic YearMonthDayEditor for Joda-Time. Joda-Time is an excellent replacement to fix some of the problems, bugs and weird things in the JDK Date and Calendar classes.

When using model object with Joda-Time objects, I wanted a property editor to use in my Spring-based controllers.

This class is of course based heavily on the ones in Spring, and is just a simple example that scratched my itch. You would probably also want at least the DateTimeEditor as well when you get heavily into joda-time. Enjoy.

Continue reading ‘PropertyEditor for joda-time’

New Maven book

Hi there.

Just wanna give y’all a pointer to a new, free Maven-book by Mergere.

Better Builds with Maven is written by Vincent Massol, Jason van Zyl and other key contributors of the Maven community and combines detailed explanations and code examples to walk you through improving your software development process with Maven 2.0.

I’m reading it as we speak, and it looks quite good!

Thanks guys!

Working with comma separated lists in XSL

Found this gem of knowledge for creating a comma separated list with XSLT without having to resort to the monstrous hacks I’ve used before.

My XML file is looking like this:

<message id="Login" initiator="client" system="lobby">
    <description>This messages is sent from the client when the client wants to login.</description>
    <request>
        <argument name="loginname" type="&string;" example="myUserName" maxsize="40"/>
        <argument name="password" type="&string;" example="myP4Ssw0rd!" maxsize="40">
            <description>Yes, this should be encrypted. Actual algorithm not yet
            decided.</description>
        </argument>
        <argument name="macaddress" type="&string;" example="09:00:07:A9:B2:EB">
            <description>We may wish to include even more information about the client PC at login,
                but for now see the CheckVersion message.</description>
        </argument>
    </request>
    <response> &frgUserId; <argument name="loginserverid" type="&integer;" example="12">
            <description>This is the id for the loginserver the client has been assigned
            to.</description>
        </argument>
        <argument name="token" type="&string;" example="33e24aeb44d25327" maxsize="2048"
            required="false">
            <description>This is a string for the unique token that identifies this particular
                client's login.</description>
        </argument>
        <argument name="errornumber" type="&integer;" example="0">
            <description>This should be >0 in case of an error. These error codes are
                described in another document.</description>
        </argument>
    </response>
</message>

Now, I want to generate source code from this using XSLT. In one instance I generate method signatures that need all request arguments as parameters for the method. I then have a snippet of XSL generate the code like this (simplified):

<xsl:for-each select="//message">
    <![CDATA[public void on]]><xsl:value-of select="@id"/>(<xsl:if test="request != ''">
        <xsl:for-each select="request/*">
            <xsl:value-of select="@type"/>
            <xsl:value-of select="concat(' ', @name)"/>
            <xsl:if test="position() != last()">, </xsl:if>
        </xsl:for-each>
    </xsl:if>);</xsl:for-each>

The beauty is of course in </xsl:if>);</xsl:for-each> which simply tests if this position is the same as the last position. Simple and elegant!

Now, if you are using XSLT 2.0 you could go one step further and skip the entire inner xsl:for-each and do it in one line. This solution is not quite as simple in this particular example considering that I need to concatenate several attributes for each item in the list, but it’s still doable.

<xsl:for-each select="//message">
    <![CDATA[public void on]]><xsl:value-of select="@id"/>(<xsl:if test="request != ''">
        <xsl:value-of select="for $s in (request/*) return (concat($s/attribute::type, ' ', $s/attribute::name))" separator=", "/>
    </xsl:if>);</xsl:for-each>

Lets say you wanted something simple, such as a list of all the message id’s defined in the XML file, this would be done like this in 1.0:

<xsl:for-each select="//message">
    <xsl:value-of select="@id"/>
    <xsl:if test="position() != last()">, </xsl:if>
</xsl:for-each>

And the 2.0 equivalent is:

<xsl:value-of select="//message/@id" separator=", "/>

In this example the 2.0 solution is much cleaner in my mind. But choose the one you like the most! :)

New look for sourceforge.net

Wow. It’s times like these that make it clear you’ve really been on a vacation. Sourceforge launched a redesign a week ago, and I noticed today.

First impression: nice. The graphic design of the site is better in my mind. But after about 10 minutes with the new design, I’m saddened. They removed what must be the two most common features from the start page: login and search. What the heck was they thinking?!

The project page had a overhaul as well, and even though I like the new download button, in all they seems to have missed a lot of opportunities to make the page clearer, and instead focusing on giving the ads more space. The menu is confusing at best, and the main stats are hard to see amongst all the text.

SF says they’re not done, so maybe it will get worked out, but for now I think it is a major step back…

Go have a look, and decide for yourself what you think about it

Unicode and language support in Linux/UNIX

Got the following juice link from my colleague Johan Ekblad:

A Quick Primer On Unicode and Software Internationalization Under Linux and UNIX

This page provides a quick summary of information with links to other URLs regarding using Unicode for multilingual internationalization projects on Linux and other UNIX-based operating systems. If you would like to be able to use more than one language on your Linux/UNIX computer but haven’t completely figured out how to do that yet, then you should read this page.

Go ahead and read it, has some good tips on what to do if you get in trouble with foreign characters, i18n, l10n or some other buzzword.

Using Automake and Autoconf with C++

Just a quick pointer to this excellent guide for using Automake and Autoconf with C++ .

Good, correct and concise writing coupled with an example of “just the right size”, make this a prime example of how good documentation can be.

Any more guides like this for popular F/OSS software will propel usage of these tools even further inside the proprietary world. Way to go!

Country and currency info in your application

In my current project, I do most of the database modelling as well as implementation of the database layer and since the project includes user registration of potential users from around the world we need some tables for keeping track of available countries.

While searching the web for previous work (don’t reinvent the wheel my friend) I came upon a nice resource by Bill Rhodes at http://27.org/isocountrylist/. This is a great resource for codes and names for both countries and states, complete with a perl-script to keep it synchronized with the ISO 3166 standard. Sweet!

That was a big help of course, now all I needed was a list of currency codes and names. I couldn’t locate a nicely formatted text file of ISO 4217, but found this web based list of currencies that I could build upon.

I only need two codes for now, but if I would’ve found the corresponding text file at ISO.org I would’ve/could’ve adapted Bill’s perl to also fix the currency table.

My solution was dead simple:
DROP TABLE IF EXISTS `currency`;
CREATE TABLE `currency` (
iso3 CHAR(3) NOT NULL PRIMARY KEY,
printable_name VARCHAR(80) NOT NULL,
numcode SMALLINT,
country_iso3 CHAR(3)
);
INSERT INTO currency VALUES ('USD','US Dollar','840', 'US');
INSERT INTO currency VALUES ('SEK','Swedish Krona','752','SE');

If I get around to do the script, I will send it to Bill so he may keep it there as well.

Wanted: C++ Connection Pool for MySQL++

Do any of you, in my hordes of faithful readers (hi mom!), know of a good, preferably free/open, connection pool to use when talking to MySQL through MySQL++ in a C++ program running under Solaris 9?

I’ve asked the maintainers of that package, but it seems they know of no such project. If I can’t find one, I will roll my own (and hopefully get OK to release it as F/OSS), but I would rather not reinvent the wheel if at all possible.

So, if you know of anything that would fit the bill, or a good, generic free/open source object pool for C++, please leave a comment or email me.

Thanks folks!