Snua - Create objects from XML

Hello!

Snua is a tiny little library that I MP’ed yesterday night and finished today. It’s very young, and incomplete. Anyway, it uses DOM to parse XML and returns Java objects from that XML. Everything is built around using a generic Decoder interface (with inbuilt implementations of boolean, byte, char, double, float, int, long, short and String decoders). It takes an org.w3c.dom.Node argument and produces a generic object from it. Here is a tiny bit of code for showing off.

SnuaTest.java


import java.io.FileInputStream;
import java.io.FileNotFoundException;

import se.sickan.snua.Resource;
import se.sickan.snua.ResourceException;
import se.sickan.snua.Resources;

public class SnuaTest implements Resource {
	public boolean booleanTest;
    public byte byteTest;
    public char charTest;
    public double doubleTest;
    public float floatTest;
    public int intTest;
    public long longTest;
    public short shortTest;
    public String stringTest;
    public static void main(String[] args) throws ResourceException, FileNotFoundException {
        Resources resources = new Resources(new FileInputStream("test.xml"));
        resources.loadResources();
        SnuaTest snuaTest = resources.getResource("test");
        System.out.println(snuaTest.booleanTest);
        System.out.println(snuaTest.byteTest);
        System.out.println(snuaTest.charTest);
        System.out.println(snuaTest.doubleTest);
        System.out.println(snuaTest.floatTest);
        System.out.println(snuaTest.intTest);
        System.out.println(snuaTest.longTest);
        System.out.println(snuaTest.shortTest);
        System.out.println(snuaTest.stringTest);
    }
}

And the complementary test.xml


<resources>
	<define name="SnuaTest" class="SnuaTest"/>
	<SnuaTest name="test">
		<booleanTest>false</booleanTest>
        	<byteTest>0</byteTest>
        	<charTest>p</charTest>
        	<doubleTest>1d</doubleTest>
        	<floatTest>2f</floatTest>
        	<intTest>3</intTest>
        	<longTest>4</longTest>
        	<shortTest>5</shortTest>
        	<stringTest>6</stringTest>
	</SnuaTest>
</resources>

And lastly, the glorious output:


false
0
p
1.0
2.0
3
4
5
6

To download a baby, goto https://dl.dropbox.com/u/42258713/snua.zip!

Nice library and good idea, need find time and try :wink:

P.s in source code so many cool things, and many of them even without feedback ;(

Nice library and good idea, need find time and try :wink:

P.s in source code so many cool things, and many of them even without feedback ;(