1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package org.marre.xml;
36
37 import java.io.IOException;
38 import java.io.Writer;
39 import java.util.Stack;
40
41 /***
42 * XmlWriter that creates text xml documents.
43 *
44 * NOTE! Not completed yet.
45 *
46 * @author Markus Eriksson
47 * @version $Id: TextXmlWriter.java,v 1.2 2004/12/07 20:03:21 c95men Exp $
48 */
49 public class TextXmlWriter implements XmlWriter
50 {
51 /***
52 * The writer that is used internally to store the xml document.
53 */
54 protected Writer myWriter;
55
56 /***
57 * Stack of tags. Used by addEndElement to know what tag to add.
58 */
59 protected Stack myTagStack;
60
61 /***
62 * Used by addElement to insert a \r\n.
63 */
64 protected boolean myCharsAddedBetweenTags = true;
65
66 /***
67 * Constructor.
68 *
69 * @param writer The writer to write to.
70 */
71 public TextXmlWriter(Writer writer)
72 {
73 myWriter = writer;
74 }
75
76 /***
77 * Sets the doctype.
78 *
79 * @see org.marre.xml.XmlWriter#setDoctype(java.lang.String)
80 */
81 public void setDoctype(String publicID)
82 {
83
84 }
85
86 /***
87 * Sets the doctype.
88 *
89 * @see org.marre.xml.XmlWriter#setDoctype(java.lang.String, java.lang.String)
90 */
91 public void setDoctype(String name, String systemURI)
92 {
93
94 }
95
96 /***
97 * Sets the doctype.
98 *
99 * @see org.marre.xml.XmlWriter#setDoctype(java.lang.String, java.lang.String, java.lang.String)
100 */
101 public void setDoctype(String name, String publicID, String publicURI)
102 {
103
104 }
105
106 /***
107 * Adds a start element tag.
108 *
109 * @see org.marre.xml.XmlWriter#addStartElement(java.lang.String)
110 */
111 public void addStartElement(String tag) throws IOException
112 {
113 if (!myCharsAddedBetweenTags)
114 {
115 myWriter.write("\r\n");
116 }
117 myCharsAddedBetweenTags = false;
118
119 myWriter.write("<" + tag + ">");
120 myTagStack.push(tag);
121 }
122
123 /***
124 * Adds a start element tag.
125 *
126 * @see org.marre.xml.XmlWriter#addStartElement(java.lang.String, org.marre.xml.XmlAttribute[])
127 */
128 public void addStartElement(String tag, XmlAttribute[] attribs) throws IOException
129 {
130 throw new IOException("Not implemented");
131 }
132
133 /***
134 * Adds an empty start element tag.
135 *
136 * @see org.marre.xml.XmlWriter#addEmptyElement(java.lang.String)
137 */
138 public void addEmptyElement(String tag) throws IOException
139 {
140 if (!myCharsAddedBetweenTags)
141 {
142 myWriter.write("\r\n");
143 }
144 myCharsAddedBetweenTags = false;
145
146 myWriter.write("<" + tag + "/>\r\n");
147 }
148
149 /***
150 * Adds an empty start element tag with attributes.
151 *
152 * @see org.marre.xml.XmlWriter#addEmptyElement(java.lang.String, org.marre.xml.XmlAttribute[])
153 */
154 public void addEmptyElement(String tag, XmlAttribute[] attribs) throws IOException
155 {
156 throw new IOException("Not implemented");
157 }
158
159 /***
160 * Adds an end tag.
161 *
162 * @see org.marre.xml.XmlWriter#addEndElement()
163 */
164 public void addEndElement() throws IOException
165 {
166 String tag = (String) myTagStack.pop();
167 myWriter.write("</" + tag + ">\r\n");
168 }
169
170 /***
171 * Adds characters to the xml document.
172 *
173 * @see org.marre.xml.XmlWriter#addCharacters(char[], int, int)
174 */
175 public void addCharacters(char[] ch, int start, int length) throws IOException
176 {
177 myCharsAddedBetweenTags = true;
178 myWriter.write(ch, start, length);
179 }
180
181 /***
182 * Adds a string to the xml document.
183 *
184 * @see org.marre.xml.XmlWriter#addCharacters(java.lang.String)
185 */
186 public void addCharacters(String str) throws IOException
187 {
188 myCharsAddedBetweenTags = true;
189 myWriter.write(str);
190 }
191
192 /***
193 * Flushes the writer.
194 *
195 * @see org.marre.xml.XmlWriter#flush()
196 */
197 public void flush() throws IOException
198 {
199 myWriter.flush();
200 }
201 }