01: package org.vraptor.remote.xml;
02:
03: import org.vraptor.remote.OutjectionSerializer;
04:
05: import com.thoughtworks.xstream.XStream;
06: import com.thoughtworks.xstream.io.xml.DomDriver;
07: import com.thoughtworks.xstream.mapper.MapperWrapper;
08:
09: /**
10: * A basic object to xml serializer using xstream. It stripts out the package
11: * names for types which had no aliases.
12: *
13: * @author Guilherme Silveira
14: */
15: public class XMLSerializer implements OutjectionSerializer {
16:
17: private final XStream stream;
18:
19: public XMLSerializer() {
20: stream = new XStream(new DomDriver()) {
21: @Override
22: protected MapperWrapper wrapMapper(MapperWrapper next) {
23: return new MapperWrapper(next) {
24:
25: @Override
26: public String serializedClass(Class type) {
27: String value = super .serializedClass(type);
28: if (type.getName().replace('$', '-').equals(
29: value)) {
30: return type.getSimpleName();
31: }
32: return value;
33: }
34: };
35: }
36: };
37: stream.setMode(XStream.NO_REFERENCES);
38: }
39:
40: public CharSequence serialize(Object object) {
41: return stream.toXML(object);
42: }
43:
44: }
|