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. September 2004 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.io.xml;
13:
14: import org.jdom.DefaultJDOMFactory;
15: import org.jdom.Element;
16: import org.jdom.JDOMFactory;
17:
18: import java.util.List;
19:
20: /**
21: * @author Laurent Bihanic
22: */
23: public class JDomWriter extends AbstractDocumentWriter {
24:
25: private final JDOMFactory documentFactory;
26:
27: /**
28: * @since 1.2
29: */
30: public JDomWriter(final Element container,
31: final JDOMFactory factory,
32: final XmlFriendlyReplacer replacer) {
33: super (container, replacer);
34: documentFactory = factory;
35: }
36:
37: public JDomWriter(final Element container, final JDOMFactory factory) {
38: this (container, factory, new XmlFriendlyReplacer());
39: }
40:
41: /**
42: * @since 1.2.1
43: */
44: public JDomWriter(final JDOMFactory factory,
45: final XmlFriendlyReplacer replacer) {
46: this (null, factory, replacer);
47: }
48:
49: public JDomWriter(final JDOMFactory factory) {
50: this (null, factory);
51: }
52:
53: /**
54: * @since 1.2.1
55: */
56: public JDomWriter(final Element container,
57: final XmlFriendlyReplacer replacer) {
58: this (container, new DefaultJDOMFactory(), replacer);
59: }
60:
61: public JDomWriter(final Element container) {
62: this (container, new DefaultJDOMFactory());
63: }
64:
65: public JDomWriter() {
66: this (new DefaultJDOMFactory());
67: }
68:
69: protected Object createNode(final String name) {
70: final Element element = documentFactory
71: .element(escapeXmlName(name));
72: final Element parent = top();
73: if (parent != null) {
74: parent.addContent(element);
75: }
76: return element;
77: }
78:
79: public void setValue(final String text) {
80: top().addContent(documentFactory.text(text));
81: }
82:
83: public void addAttribute(final String key, final String value) {
84: top().setAttribute(
85: documentFactory.attribute(escapeXmlName(key), value));
86: }
87:
88: private Element top() {
89: return (Element) getCurrent();
90: }
91:
92: /**
93: * @deprecated since 1.2.1, use {@link #getTopLevelNodes()} instead
94: */
95: public List getResult() {
96: return getTopLevelNodes();
97: }
98: }
|