Sylvain,
using the following Comparator implementation
package xml.treeMap;
import java.util.Comparator;
public class RootComparator implements Comparator {
public int compare(final Object arg0, final Object arg1) {
if (arg0 == null || arg1 == null) {
throw new NullPointerException("Objects to compare cannot be null");
}
if (!(arg0 instanceof java.lang.String)) {
throw new ClassCastException(
"Problem converting objects to compare to java.lang.String");
}
if (!(arg0 instanceof java.lang.String)) {
throw new ClassCastException(
"Problem converting objects to compare to java.lang.String");
}
return -1* ((java.lang.String) arg0).compareTo(
((java.lang.String) arg1));
}
}
using a sortedMap collection type actually produces the expected
results.
I think that your Comparator implementation does not meet the contract
as specified, and causes side-effects.
I hope this helps
Werner
________________________________
From: castor sylvain [mailto:[EMAIL PROTECTED]
Sent: Dienstag, 28. November 2006 19:34
To: [email protected]
Subject: Re: [castor-user] Howto map a TreeMap ?
Thanks Werner for your attention,
Here the very code.
==> The symptom is that, in case of a TreeMap based on a
Comparator, the values of the map are not outputed !
The main (JUnit test) is :
SerializeTreeMap.java
package castor.test;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.TreeMap;
import junit.framework.TestCase;
public class SerializeTreeMap extends TestCase {
private String mapping =
// "<!DOCTYPE databases PUBLIC \"-//EXOLAB/Castor
Mapping DTD Version 1.0//EN\" \" http://castor.org/mapping.dtd\
<http://castor.org/mapping.dtd\> ">"
// + "<mapping>"
"<mapping>"
+ ""
+ " <class name=\"castor.test.FormRadio\" >"
+ " <field name=\"radios\" collection=\"map\">"
+ " <bind-xml name=\"radio\">"
+ " <class
name=\"org.exolab.castor.mapping.MapItem\">"
+ " <field name=\"key\"
type=\"java.lang.String\">"
+ " <bind-xml name=\"type\"
node=\"attribute\" />"
+ " </field>"
+ " <field name=\"value\" type=\"
java.lang.String\">"
+ " <bind-xml name=\"label\"
node=\"attribute\"/>"
+ " </field>"
+ " </class>"
+ " </bind-xml>"
+ " </field>"
+ " </class>"
+ "</mapping>"
;
/**
* Testing Comparator
*
* @throws IOException
*/
public void testComparator() throws IOException {
Map _appliTypes = new TreeMap(new
AppliTypeComparator());
_appliTypes.put("Type 3","Application of type 3");
_appliTypes.put("Type 2","Application of type 2");
_appliTypes.put("Type 5","Application of type 5");
_appliTypes.put("Type 1","Application of type 1");
_appliTypes.put("Type 6","Application of type 6");
_appliTypes.put("Type 4","Application of type 4");
Object[] keys = _appliTypes.keySet().toArray();
assertTrue("Type 1".equals((String)keys[0]));
assertTrue("Type 2".equals((String)keys[1]));
assertTrue("Type 3".equals((String)keys[2]));
assertTrue("Type 4".equals((String)keys[3]));
assertTrue("Type 5".equals((String)keys[4]));
assertTrue("Type 6".equals((String)keys[5]));
}
/**
* Testing a Simple TreeMap (natural ordered on keys)
serialization
*
*
----------------------------------------------------------
* ==> OK : values of map are outputed in attribute "label"
*
----------------------------------------------------------
*
* @throws IOException
*/
public void testSerializeSimpleTreeMap() throws IOException
{
FormRadio _radio = new FormRadio();
Map _appliTypes = new TreeMap();
_appliTypes.put("Type 3","Application of type 3");
_appliTypes.put("Type 2","Application of type 2");
_appliTypes.put("Type 5","Application of type 5");
_appliTypes.put("Type 1","Application of type 1");
_appliTypes.put("Type 6","Application of type 6");
_appliTypes.put("Type 4","Application of type 4");
_radio.setRadios(_appliTypes);
String xmlflow = serialize(_radio, mapping);
logWithOutputFile(xmlflow,"TreeMapSimpleTest.xml");
}
/**
* Testing a TreeMap (ordered by a Comparator) serialization
*
*
----------------------------------------------------------
* ==> NOK !! values of map are not outputed !....
*
----------------------------------------------------------
*
* @throws IOException
*/
public void testSerializeTreeMapWithComparator() throws
IOException {
FormRadio _radio = new FormRadio();
Map _appliTypes = new TreeMap(new
AppliTypeComparator());
_appliTypes.put("Type 3","Application of type 3");
_appliTypes.put("Type 2","Application of type 2");
_appliTypes.put("Type 5","Application of type 5");
_appliTypes.put("Type 1","Application of type 1");
_appliTypes.put("Type 6","Application of type 6");
_appliTypes.put("Type 4","Application of type 4");
_radio.setRadios(_appliTypes);
String xmlflow = serialize(_radio, mapping);
logWithOutputFile(xmlflow,"TreeMapComparatorTest.xml");
}
/**
* Serializing an object
*
* @param toSerialize
* @param mapping
* @return
*/
private String serialize(Object toSerialize, String mapping)
{
String xmlflow = null;
HomeSerializer serializer = new HomeSerializer();
InputStream is = new
ByteArrayInputStream(mapping.getBytes());
serializer.loadMapping(is);
try {
xmlflow = serializer.marshall(toSerialize);
} catch (IOException e) {
e.printStackTrace();
}
return xmlflow;
}
/**
*
* @param toWrite
*/
private void logWithOutputFile(String toWrite, String
toFile) {
System.out.println(toWrite);
File dumpFile = new File("/tmp/"+toFile);
try {
FileWriter out = new FileWriter(dumpFile);
out.write(toWrite);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
HomeSerializer.java
package castor.test;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
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.ValidationException;
import org.xml.sax.InputSource;
/**
* Serializer JAVA <==> XML
*
* - based on Castor services
*
* @author sylvain
*
*/
public class HomeSerializer {
private Mapping mapping = null;
private Marshaller marshaller = null;
public HomeSerializer() {
mapping = new Mapping();
marshaller = new Marshaller();
marshaller.setSuppressXSIType (true);
}
/**
* Load mapping file to be holded by Marshaller/Unmarshaller
*
* @param mappings
*/
public void loadMapping(InputStream mappings) {
mapping = new Mapping();
InputSource is = new InputSource(mappings);
mapping.loadMapping(is);
try {
marshaller.setMapping(mapping);
} catch (MappingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Marshalling JAVA object to XML flow with given root
element
*
* @param toSerialize
* @param rootElement
* @return
* @throws IOException
* @throws ValidationException
* @throws MarshalException
*/
public String marshall(Object toSerialize, String
rootElement) {
marshaller.setRootElement(rootElement);
try{
return marshall(toSerialize);
}catch(IOException ioe) {
ioe.printStackTrace();
return null;
}
}
/**
* Marshalling JAVA object to XML flow
*
* @param toSerialize
* @return
* @throws IOException
* @throws ValidationException
* @throws
*/
public String marshall(Object toSerialize) throws
IOException {
StringWriter output = new StringWriter();
marshaller.setWriter (output);
try
{
marshaller.marshal(toSerialize);
}
catch (MarshalException me) {
throw new IOException (me.getMessage());
}
catch (ValidationException ve) {
throw new IOException (ve.getMessage());
}
return output.toString();
}
/**
* Marshalling JAVA object to XML flow
*
* @param toSerialize
* @param mapping
* @param rootElement
* @return xml flow
* @throws ValidationException
* @throws MarshalException
*/
public String marshall(Object o, String filemap, String
rootElement) throws IOException
{
InputStream is = new FileInputStream(filemap);
loadMapping(is);
is.close();
return marshall(o, rootElement);
}
public void setDebug(boolean debug) {
marshaller.setDebug(debug);
}
}
AppliTypeComparator.java
package castor.test;
import java.util.Comparator;
import java.util.HashMap;
/**
* Comparator for UserTO fields in Admin GUI
*
* @author sylvain
*/
public class AppliTypeComparator implements Comparator {
private HashMap sorter = null;
public AppliTypeComparator() {
sorter = new HashMap();
sorter.put("Type 1", new Integer(1));
sorter.put("Type 2", new Integer(2));
sorter.put("Type 3", new Integer(3));
sorter.put ("Type 4", new Integer(4));
sorter.put("Type 5", new Integer(5));
sorter.put("Type 6", new Integer(6));
}
public int compare(Object o1, Object o2) {
try {
if (sorter.get(o1) == null) return 1;
if (sorter.get(o2) == null) return -1;
int order1 = ((Integer)sorter.get(o1)).intValue();
int order2 = ((Integer)sorter.get(o2)).intValue();
if (order2>order1) {
return -1;
} else {
return 1;
}
} catch (Exception ex) {
System.out.println("[AppliTypeComparator::compare]
Exception thrown whuile comparing elements, Exception: " + ex);
System.out.println("[AppliTypeComparator::compare]
... element 1 : "+o1+ " / element 2 : "+o2);
return 0;
}
}
}
FormRadio.java
package castor.test;
import java.util.Map;
import java.util.TreeMap;
public class FormRadio {
private Map radios = new TreeMap();
public FormRadio() {
super();
}
public Map getRadios() {
return radios;
}
public void setRadios(Map radios) {
this.radios = radios;
}
}
---------------------------------------------------------------------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email