01: /*
02: * Copyright (C) 2006, 2007 XStream Committers.
03: * All rights reserved.
04: *
05: * The software in this package is published under the terms of the BSD
06: * style license a copy of which has been included with this distribution in
07: * the LICENSE.txt file.
08: *
09: * Created on 03. April 2006 by Joerg Schaible
10: */
11: package com.thoughtworks.xstream.converters.reflection;
12:
13: import com.thoughtworks.xstream.converters.ConversionException;
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: * A special converter that prevents self-serialization. The serializing XStream instance
22: * adds a converter of this type to prevent self-serialization and will throw an
23: * exception instead.
24: *
25: * @author Jörg Schaible
26: * @since 1.2
27: */
28: public class SelfStreamingInstanceChecker implements Converter {
29:
30: private final Object self;
31: private Converter defaultConverter;
32:
33: public SelfStreamingInstanceChecker(Converter defaultConverter,
34: Object xstream) {
35: this .defaultConverter = defaultConverter;
36: this .self = xstream;
37: }
38:
39: public boolean canConvert(Class type) {
40: return type == self.getClass();
41: }
42:
43: public void marshal(Object source, HierarchicalStreamWriter writer,
44: MarshallingContext context) {
45: if (source == self) {
46: throw new ConversionException(
47: "Cannot marshal the XStream instance in action");
48: }
49: defaultConverter.marshal(source, writer, context);
50: }
51:
52: public Object unmarshal(HierarchicalStreamReader reader,
53: UnmarshallingContext context) {
54: return defaultConverter.unmarshal(reader, context);
55: }
56:
57: }
|