01: /*
02: * Copyright (C) 2004, 2005 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.xml;
13:
14: import org.dom4j.Branch;
15: import org.dom4j.DocumentFactory;
16: import org.dom4j.Element;
17:
18: public class Dom4JWriter extends AbstractDocumentWriter {
19:
20: private final DocumentFactory documentFactory;
21:
22: /**
23: * @since 1.2.1
24: */
25: public Dom4JWriter(final Branch root,
26: final DocumentFactory factory,
27: final XmlFriendlyReplacer replacer) {
28: super (root, replacer);
29: documentFactory = factory;
30: }
31:
32: /**
33: * @since 1.2.1
34: */
35: public Dom4JWriter(final DocumentFactory factory,
36: final XmlFriendlyReplacer replacer) {
37: this (null, factory, replacer);
38: }
39:
40: /**
41: * @since 1.2.1
42: */
43: public Dom4JWriter(final DocumentFactory documentFactory) {
44: this (documentFactory, new XmlFriendlyReplacer());
45: }
46:
47: /**
48: * @since 1.2.1
49: */
50: public Dom4JWriter(final Branch root,
51: final XmlFriendlyReplacer replacer) {
52: this (root, new DocumentFactory(), replacer);
53: }
54:
55: public Dom4JWriter(final Branch root) {
56: this (root, new DocumentFactory(), new XmlFriendlyReplacer());
57: }
58:
59: /**
60: * @since 1.2.1
61: */
62: public Dom4JWriter() {
63: this (new DocumentFactory(), new XmlFriendlyReplacer());
64: }
65:
66: protected Object createNode(final String name) {
67: final Element element = documentFactory
68: .createElement(escapeXmlName(name));
69: final Branch top = top();
70: if (top != null) {
71: top().add(element);
72: }
73: return element;
74: }
75:
76: public void setValue(final String text) {
77: top().setText(text);
78: }
79:
80: public void addAttribute(final String key, final String value) {
81: ((Element) top()).addAttribute(escapeXmlName(key), value);
82: }
83:
84: private Branch top() {
85: return (Branch) getCurrent();
86: }
87: }
|