KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > converters > basic > AbstractBasicConverter


1 package com.thoughtworks.xstream.converters.basic;
2
3 import com.thoughtworks.xstream.converters.Converter;
4 import com.thoughtworks.xstream.converters.MarshallingContext;
5 import com.thoughtworks.xstream.converters.UnmarshallingContext;
6 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
7 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
8
9 /**
10  * Base helper class for converters that can store the
11  * state of an object as a single String.
12  * <p/>
13  * <p>Subclasses should implement the toString(Object) and
14  * fromString(String) methods for the conversion.</p>
15  *
16  * @author Joe Walnes
17  */

18 public abstract class AbstractBasicConverter implements Converter {
19
20     protected abstract Object JavaDoc fromString(String JavaDoc str);
21
22     public abstract boolean canConvert(Class JavaDoc type);
23
24     protected String JavaDoc toString(Object JavaDoc obj) {
25         return obj.toString();
26     }
27
28     public void marshal(Object JavaDoc source, HierarchicalStreamWriter writer, MarshallingContext context) {
29         writer.setValue(toString(source));
30     }
31
32     public Object JavaDoc unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
33         return fromString(reader.getValue());
34     }
35
36 }
37
Popular Tags