01: /*
02: * Copyright (C) 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 06. March 2004 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.converters.collections;
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: * Converts a char[] to XML, storing the contents as a single
22: * String.
23: *
24: * @author Joe Walnes
25: */
26: public class CharArrayConverter implements Converter {
27:
28: public boolean canConvert(Class type) {
29: return type.isArray()
30: && type.getComponentType().equals(char.class);
31: }
32:
33: public void marshal(Object source, HierarchicalStreamWriter writer,
34: MarshallingContext context) {
35: char[] chars = (char[]) source;
36: writer.setValue(new String(chars));
37: }
38:
39: public Object unmarshal(HierarchicalStreamReader reader,
40: UnmarshallingContext context) {
41: return reader.getValue().toCharArray();
42: }
43: }
|