01: /*
02: * Copyright (C) 2004, 2005, 2006 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 15. March 2004 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.core;
13:
14: import com.thoughtworks.xstream.alias.ClassMapper;
15: import com.thoughtworks.xstream.converters.ConverterLookup;
16: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
17: import com.thoughtworks.xstream.io.path.Path;
18: import com.thoughtworks.xstream.mapper.Mapper;
19:
20: public class ReferenceByIdMarshaller extends
21: AbstractReferenceMarshaller {
22:
23: private final IDGenerator idGenerator;
24:
25: public static interface IDGenerator {
26: String next();
27: }
28:
29: public ReferenceByIdMarshaller(HierarchicalStreamWriter writer,
30: ConverterLookup converterLookup, Mapper mapper,
31: IDGenerator idGenerator) {
32: super (writer, converterLookup, mapper);
33: this .idGenerator = idGenerator;
34: }
35:
36: public ReferenceByIdMarshaller(HierarchicalStreamWriter writer,
37: ConverterLookup converterLookup, Mapper mapper) {
38: this (writer, converterLookup, mapper, new SequenceGenerator(1));
39: }
40:
41: /**
42: * @deprecated As of 1.2, use {@link #ReferenceByIdMarshaller(HierarchicalStreamWriter, ConverterLookup, Mapper, IDGenerator)}
43: */
44: public ReferenceByIdMarshaller(HierarchicalStreamWriter writer,
45: ConverterLookup converterLookup, ClassMapper classMapper,
46: IDGenerator idGenerator) {
47: this (writer, converterLookup, (Mapper) classMapper, idGenerator);
48: }
49:
50: /**
51: * @deprecated As of 1.2, use {@link #ReferenceByIdMarshaller(HierarchicalStreamWriter, ConverterLookup, Mapper)}
52: */
53: public ReferenceByIdMarshaller(HierarchicalStreamWriter writer,
54: ConverterLookup converterLookup, ClassMapper classMapper) {
55: this (writer, converterLookup, (Mapper) classMapper);
56: }
57:
58: protected String createReference(Path currentPath,
59: Object existingReferenceKey) {
60: return existingReferenceKey.toString();
61: }
62:
63: protected Object createReferenceKey(Path currentPath) {
64: return idGenerator.next();
65: }
66:
67: protected void fireValidReference(Object referenceKey) {
68: writer.addAttribute(getMapper().aliasForAttribute("id"),
69: referenceKey.toString());
70: }
71: }
|