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 07. March 2004 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.io.path;
13:
14: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
15: import com.thoughtworks.xstream.io.WriterWrapper;
16: import com.thoughtworks.xstream.io.xml.XmlFriendlyWriter;
17:
18: /**
19: * Wrapper for HierarchicalStreamWriter that tracks the path (a subset of XPath) of the current node that is being written.
20: *
21: * @see PathTracker
22: * @see Path
23: *
24: * @author Joe Walnes
25: */
26: public class PathTrackingWriter extends WriterWrapper {
27:
28: private final PathTracker pathTracker;
29: private final boolean isXmlFriendly;
30:
31: public PathTrackingWriter(HierarchicalStreamWriter writer,
32: PathTracker pathTracker) {
33: super (writer);
34: this .isXmlFriendly = writer.underlyingWriter() instanceof XmlFriendlyWriter;
35: this .pathTracker = pathTracker;
36: }
37:
38: public void startNode(String name) {
39: pathTracker
40: .pushElement(isXmlFriendly ? ((XmlFriendlyWriter) wrapped
41: .underlyingWriter()).escapeXmlName(name)
42: : name);
43: super .startNode(name);
44: }
45:
46: public void startNode(String name, Class clazz) {
47: pathTracker
48: .pushElement(isXmlFriendly ? ((XmlFriendlyWriter) wrapped
49: .underlyingWriter()).escapeXmlName(name)
50: : name);
51: super .startNode(name, clazz);
52: }
53:
54: public void endNode() {
55: super.endNode();
56: pathTracker.popElement();
57: }
58:
59: }
|