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 03. April 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.HierarchicalStreamReader;
17: import com.thoughtworks.xstream.io.path.Path;
18: import com.thoughtworks.xstream.io.path.PathTracker;
19: import com.thoughtworks.xstream.io.path.PathTrackingReader;
20: import com.thoughtworks.xstream.io.xml.XmlFriendlyReader;
21: import com.thoughtworks.xstream.mapper.Mapper;
22:
23: public class ReferenceByXPathUnmarshaller extends
24: AbstractReferenceUnmarshaller {
25:
26: private PathTracker pathTracker = new PathTracker();
27: protected boolean isXmlFriendly;
28:
29: public ReferenceByXPathUnmarshaller(Object root,
30: HierarchicalStreamReader reader,
31: ConverterLookup converterLookup, Mapper mapper) {
32: super (root, reader, converterLookup, mapper);
33: this .reader = new PathTrackingReader(reader, pathTracker);
34: isXmlFriendly = reader.underlyingReader() instanceof XmlFriendlyReader;
35: }
36:
37: /**
38: * @deprecated As of 1.2, use {@link #ReferenceByXPathUnmarshaller(Object, HierarchicalStreamReader, ConverterLookup, Mapper)}
39: */
40: public ReferenceByXPathUnmarshaller(Object root,
41: HierarchicalStreamReader reader,
42: ConverterLookup converterLookup, ClassMapper classMapper) {
43: this (root, reader, converterLookup, (Mapper) classMapper);
44: }
45:
46: protected Object getReferenceKey(String reference) {
47: final Path path = new Path(
48: isXmlFriendly ? ((XmlFriendlyReader) reader
49: .underlyingReader()).unescapeXmlName(reference)
50: : reference);
51: // We have absolute references, if path starts with '/'
52: return reference.charAt(0) != '/' ? pathTracker.getPath()
53: .apply(path) : path;
54: }
55:
56: protected Object getCurrentReferenceKey() {
57: return pathTracker.getPath();
58: }
59:
60: }
|