KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > converters > collections > AbstractCollectionConverter


1 package com.thoughtworks.xstream.converters.collections;
2
3 import com.thoughtworks.xstream.alias.ClassMapper;
4 import com.thoughtworks.xstream.converters.ConversionException;
5 import com.thoughtworks.xstream.converters.Converter;
6 import com.thoughtworks.xstream.converters.MarshallingContext;
7 import com.thoughtworks.xstream.converters.UnmarshallingContext;
8 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
9 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
10
11 /**
12  * Base helper class for converters that need to handle
13  * collections of items (arrays, Lists, Maps, etc).
14  * <p/>
15  * <p>Typically, subclasses of this will converter the outer
16  * structure of the collection, loop through the contents and
17  * call readItem() or writeItem() for each item.</p>
18  *
19  * @author Joe Walnes
20  */

21 public abstract class AbstractCollectionConverter implements Converter {
22     protected ClassMapper classMapper;
23     protected String JavaDoc classAttributeIdentifier;
24
25     public abstract boolean canConvert(Class JavaDoc type);
26
27     public AbstractCollectionConverter(ClassMapper classMapper, String JavaDoc classAttributeIdentifier) {
28         this.classMapper = classMapper;
29         this.classAttributeIdentifier = classAttributeIdentifier;
30     }
31
32     public abstract void marshal(Object JavaDoc source, HierarchicalStreamWriter writer, MarshallingContext context);
33
34     public abstract Object JavaDoc unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context);
35
36     protected void writeItem(Object JavaDoc item, MarshallingContext context, HierarchicalStreamWriter writer) {
37         if (item == null) {
38             // todo: this is duplicated in TreeMarshaller.start()
39
writer.startNode(classMapper.lookupName(ClassMapper.Null.class));
40             writer.endNode();
41         } else {
42             writer.startNode(classMapper.lookupName(item.getClass()));
43             context.convertAnother(item);
44             writer.endNode();
45         }
46     }
47
48     protected Object JavaDoc readItem(HierarchicalStreamReader reader, UnmarshallingContext context, Object JavaDoc current) {
49         String JavaDoc classAttribute = reader.getAttribute(classAttributeIdentifier);
50         Class JavaDoc type;
51         if (classAttribute == null) {
52             type = classMapper.lookupType(reader.getNodeName());
53         } else {
54             type = classMapper.lookupType(classAttribute);
55         }
56         return context.convertAnother(current, type);
57     }
58
59     protected Object JavaDoc createCollection(Class JavaDoc type) {
60         Class JavaDoc defaultType = classMapper.lookupDefaultType(type);
61         try {
62             return defaultType.newInstance();
63         } catch (InstantiationException JavaDoc e) {
64             throw new ConversionException("Cannot instantiate " + defaultType.getName(), e);
65         } catch (IllegalAccessException JavaDoc e) {
66             throw new ConversionException("Cannot instantiate " + defaultType.getName(), e);
67         }
68     }
69 }
70
Popular Tags