Hi

I've two questions about Castor XML.

1) I've noticed that the XML content marshalled for
maps losses the info of the keys in the maps. Only the
values are stored. And when I do a unmarshalling on
the content, the keys and the values are of the same
object type. This raises some issues. For example, I
use String type for the keys as it is a simpler data
type (i.e. faster sort and search). I will get a
ClassCastException when I try to do
map.containsKey(key) for a map that has been
unmarshalled from the XML content.

Did I miss out some settings or is this a feature of
Castor XML that only values from a map are marshalled.

Or is it possible to create some handling class on my
own to make sure both the value and key are stored?

2) The other question is I've tried keeping a
java.lang.String as the value but it refuses to
unmarshall from the XML content (that I've just
marshalled to a file). An exception is thrown stating
that the FieldDescriptor 'string' is not found:

org.xml.sax.SAXException: unable to find
FieldDescriptor for 'string' in ClassDescriptor of
containerElement
        at
org.exolab.castor.xml.UnmarshalHandler.startElement(Unknown
Source)
        at
org.apache.xerces.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:393)
        at
org.apache.xerces.impl.XMLNamespaceBinder.startElement(XMLNamespaceBinder.java:571)
        at
org.apache.xerces.impl.dtd.XMLDTDValidator.startElement(XMLDTDValidator.java:796)
        at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:752)
        at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(XMLDocumentFragmentScannerImpl.java:1454)
        at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:333)
        at
org.apache.xerces.parsers.StandardParserConfiguration.parse(StandardParserConfiguration.java:529)
        at
org.apache.xerces.parsers.StandardParserConfiguration.parse(StandardParserConfiguration.java:585)
        at
org.apache.xerces.parsers.XMLParser.parse(XMLParser.java:147)
        at
org.apache.xerces.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1148)
        at
org.exolab.castor.xml.Unmarshaller.unmarshal(Unknown
Source)
        at xml.testMyWrapper.main(Unknown Source)

How do I get it to keep a string in a collection
calss? (This also happens for List collection).

Your help is very much appreciated.

Here is the mapping file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Object
Mapping DTD Version 1.0//EN"
                        
"http://castor.exolab.org/mapping.dtd";>
<mapping>
    <description>Castor generated mapping
file</description>
    <class name="xml.castor.wrapper.TreeMapWrapper">
        <map-to xml="treeMapWrapper"/>
        <field name="name" type="string"
required="true">
            <bind-xml name="name" node="element"/>
        </field>
        <field name="map"
type="xml.castor.wrapper.WrapperObj" required="true"
               collection="map" container="false">
            <bind-xml auto-naming="deriveByClass"/>
        </field>
    </class>
</mapping>


Here are my classes:

a) TreeMapWrapper

import java.util.*;
public class TreeMapWrapper
        implements WrapperInt
{
    private String name_;
    private Map map_;
    public TreeMapWrapper ()
    {
        map_ = new TreeMap();
        name_ = this.getClass().getName();
    }
    public TreeMapWrapper (String name)
    {
        this();

        if (name != null)
            name_ = name;
    }
    public String getName ()
    {
        return name_;
    }
    public Map getMap ()
    {
        return map_;
    }
    public Set getKeys ()
    {
        return map_.keySet();
    }
    public Collection getValues ()
    {
        return map_.values();
    }
    public Object getValue (Object key)
    {
        return map_.get(key);
    }
    public boolean hasKey (Object key)
    {
        return (map_.containsKey(key));
    }
    public void setName (String name)
    {
        name_ = name;
    }
    public void setMap (Map aMap)
    {
        map_ = aMap;
    }
    public Object put (Object key, Object value)
    {
        Object obj = null;
        if ((key != null) &&
                value != null)
            obj = map_.put(key, value);

        return obj;
    }
    public String debugStr ()
    {
        StringBuffer aStrBuff = new StringBuffer(200);

        // Describe this graph
       
aStrBuff.append(this.getClass().getName()).append(":
").append(getName()).append('\n');

        // Describe all key and values of this map
wrapper
        Set entrySet = map_.entrySet();
        Iterator iter = entrySet.iterator();
        while (iter.hasNext()) {
            Map.Entry anEntry = (Map.Entry)
iter.next();
            aStrBuff.append("Key  :
").append(anEntry.getKey()).append('\t');
            aStrBuff.append("Value:
").append(anEntry.getValue()).append('\n');
        }

        return aStrBuff.toString();
    }
    public void debug ()
    {
        System.err.println(debugStr());
    }
}

b) WrapperObj

public class WrapperObj
        implements Comparable
{
    private String value_;
    public WrapperObj ()
    {
        value_ = new String();
    }
    public WrapperObj (String value)
    {
        value_ = value;
    }
    public String getValue()
    {
        return value_;
    }
    public void setValue (String value)
    {
        value_ = value;
    }
    public String toString ()
    {
        return value_;
    }
    public int compareTo(WrapperObj wObj)
    {
        return value_.compareTo(wObj.getValue());
    }
    public int compareTo(Object obj)
    {
        return compareTo((WrapperObj) obj);
    }
}

c) testWrapper

import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;
import org.xml.sax.InputSource;
import java.io.*;
public class testMyWrapper
{
    private static final String className_ =
"testMyWrapper";
    public static void main(String args[])
    {
        if (args.length < 1) {
            System.err.println("USAGE: " + className_
+ " <mapping file> <XML file>");
            System.exit(1);
        }
        try {
            // Initialise Java to XML mappings
            Mapping mapping = new Mapping();
            mapping.loadMapping(args[0]);

            // Create data for marshalling
            TreeMapWrapper outTree = new
TreeMapWrapper("TreeMapTest");
            String key = null;
            String value = null;
            for (int i = 1; i <= 10; i++) {
                key = "K" + Integer.toString(i);
                value = "V" + Integer.toString(i);
                outTree.put(key, new
WrapperObj(value));
            }

            // Marshal to file
            Marshaller mar = new Marshaller(new
BufferedWriter(new FileWriter(args[1])));
            mar.setMapping(mapping);
            mar.marshal(outTree);

            // Marshal from file
            Unmarshaller unmar = new
Unmarshaller(mapping);
            InputSource inSrc = new InputSource(new
BufferedReader(new FileReader(args[1])));
            TreeMapWrapper inTree = (TreeMapWrapper)
unmar.unmarshal(inSrc);
            inTree.debug();
        }
        catch (MappingException excp) {
            System.err.println(className_ + "::main()
-> MappingException encountered.");
            excp.printStackTrace();
        }
        catch (MarshalException excp) {
            System.err.println(className_ + "::main()
-> ValidationException encountered.");
            excp.printStackTrace();
        }
        catch (ValidationException excp) {
            System.err.println(className_ + "::main()
-> ValidationException encountered.");
            excp.printStackTrace();
        }
        catch (FileNotFoundException excp) {
            System.err.println(className_ + "::main()
-> Can't find Tli XML document file.");
            excp.printStackTrace();
        }
        catch (IOException excp) {
            System.err.println(className_ + "::main()
-> Error reading Tli object.");
            excp.printStackTrace();
        }
    }
}

Thanks a lot.

Cheers

Lawrence Teo



__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com

----------------------------------------------------------- 
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
        unsubscribe castor-dev

Reply via email to