KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > io > xml > StaxWriter


1 package com.thoughtworks.xstream.io.xml;
2
3 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
4 import com.thoughtworks.xstream.io.StreamException;
5
6 import javax.xml.namespace.QName JavaDoc;
7 import javax.xml.stream.XMLStreamException;
8 import javax.xml.stream.XMLStreamWriter;
9
10 /**
11  * A stream writing that outputs to a StAX stream writer
12  *
13  * @author James Strachan
14  * @version $Revision: 585 $
15  */

16 public class StaxWriter implements HierarchicalStreamWriter {
17     private final QNameMap qnameMap;
18     private final XMLStreamWriter out;
19     private final boolean writeEnclosingDocument;
20     private boolean namespaceRepairingMode;
21
22     private int tagDepth;
23
24     public StaxWriter(QNameMap qnameMap, XMLStreamWriter out) throws XMLStreamException {
25         this(qnameMap, out, true, true);
26     }
27
28     /**
29      * Allows a StaxWriter to be created for partial XML output
30      *
31      * @param qnameMap is the mapper of Java class names to QNames
32      * @param out the stream to output to
33      * @param writeEnclosingDocument a flag to indicate whether or not the start/end document events should be written
34      * @throws XMLStreamException if the events could not be written to the output
35      */

36     public StaxWriter(QNameMap qnameMap, XMLStreamWriter out, boolean writeEnclosingDocument, boolean namespaceRepairingMode) throws XMLStreamException {
37         this.qnameMap = qnameMap;
38         this.out = out;
39         this.writeEnclosingDocument = writeEnclosingDocument;
40         this.namespaceRepairingMode = namespaceRepairingMode;
41         if (writeEnclosingDocument) {
42             out.writeStartDocument();
43         }
44     }
45
46     public void flush() {
47         try {
48             out.close();
49         }
50         catch (XMLStreamException e) {
51             throw new StreamException(e);
52         }
53     }
54
55     /**
56      * Call this method when you're finished with me
57      */

58     public void close() {
59         try {
60             out.close();
61         }
62         catch (XMLStreamException e) {
63             throw new StreamException(e);
64         }
65     }
66
67     public void addAttribute(String JavaDoc name, String JavaDoc value) {
68         try {
69             out.writeAttribute(name, value);
70         }
71         catch (XMLStreamException e) {
72             throw new StreamException(e);
73         }
74     }
75
76     public void endNode() {
77         try {
78             tagDepth--;
79             out.writeEndElement();
80             if (tagDepth == 0 && writeEnclosingDocument) {
81                 out.writeEndDocument();
82             }
83         }
84         catch (XMLStreamException e) {
85             throw new StreamException(e);
86         }
87     }
88
89     public void setValue(String JavaDoc text) {
90         try {
91             out.writeCharacters(text);
92         }
93         catch (XMLStreamException e) {
94             throw new StreamException(e);
95         }
96     }
97
98     public void startNode(String JavaDoc name) {
99         try {
100             QName JavaDoc qname = qnameMap.getQName(name);
101             String JavaDoc prefix = qname.getPrefix();
102             String JavaDoc uri = qname.getNamespaceURI();
103
104             // before you ask - yes it really is this complicated to output QNames to StAX
105
// handling both repair namespace modes :)
106

107             boolean hasPrefix = prefix != null && prefix.length() > 0;
108             boolean hasURI = uri != null && uri.length() > 0;
109             boolean writeNamespace = false;
110
111             if (hasURI) {
112                 if (hasPrefix) {
113                     String JavaDoc currentNamespace = out.getNamespaceContext().getNamespaceURI(prefix);
114                     if (currentNamespace == null || !currentNamespace.equals(uri)) {
115                         writeNamespace = true;
116                     }
117                 }
118                 else {
119                     String JavaDoc defaultNamespace = out.getNamespaceContext().getNamespaceURI("");
120                     if (defaultNamespace == null || !defaultNamespace.equals(uri)) {
121                         writeNamespace = true;
122                     }
123                 }
124             }
125
126             if (hasPrefix) {
127                 out.setPrefix(prefix, uri);
128             }
129             else if (hasURI) {
130                 if (writeNamespace) {
131                     out.setDefaultNamespace(uri);
132                 }
133             }
134             out.writeStartElement(prefix, qname.getLocalPart(), uri);
135             if (hasURI && writeNamespace && !isNamespaceRepairingMode()) {
136                 if (hasPrefix) {
137                     out.writeNamespace(prefix, uri);
138                 }
139                 else {
140                     out.writeDefaultNamespace(uri);
141                 }
142             }
143             tagDepth++;
144         }
145         catch (XMLStreamException e) {
146             throw new StreamException(e);
147         }
148     }
149
150     public HierarchicalStreamWriter underlyingWriter() {
151         return this;
152     }
153
154     /**
155      * Is StAX namespace repairing mode on or off?
156      *
157      * @return
158      */

159     public boolean isNamespaceRepairingMode() {
160         return namespaceRepairingMode;
161     }
162
163 }
164
Popular Tags