01: /*
02: * Copyright (C) 2003, 2004 Joe Walnes.
03: * Copyright (C) 2006, 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 26. September 2003 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.converters.basic;
13:
14: import com.thoughtworks.xstream.converters.Converter;
15: import com.thoughtworks.xstream.converters.MarshallingContext;
16: import com.thoughtworks.xstream.converters.UnmarshallingContext;
17: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
18: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
19:
20: /**
21: * Base helper class for converters that can store the
22: * state of an object as a single String.
23: * <p/>
24: * <p>Subclasses should implement the toString(Object) and
25: * fromString(String) methods for the conversion.</p>
26: *
27: * @author Joe Walnes
28: * @deprecated Since 1.2 use {@link com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter}
29: */
30: public abstract class AbstractBasicConverter implements Converter {
31:
32: protected abstract Object fromString(String str);
33:
34: public abstract boolean canConvert(Class type);
35:
36: protected String toString(Object obj) {
37: return obj.toString();
38: }
39:
40: public void marshal(Object source, HierarchicalStreamWriter writer,
41: MarshallingContext context) {
42: writer.setValue(toString(source));
43: }
44:
45: public Object unmarshal(HierarchicalStreamReader reader,
46: UnmarshallingContext context) {
47: return fromString(reader.getValue());
48: }
49:
50: }
|