1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45: import ;
46:
47:
52: public class StAXWriter implements Writer
53: {
54: XMLStreamWriter writer;
55:
56: int indent = 0;
57:
58: public StAXWriter(OutputStream os)
59: {
60: try
61: {
62: XMLOutputFactory factory = XMLOutputFactory.newInstance();
63: writer = factory.createXMLStreamWriter(os);
64: }
65: catch (XMLStreamException se)
66: {
67: throw (InternalError)
68: new InternalError(
69: "Could not instantiate a streaming XML writer.")
70: .initCause(se);
71: }
72:
73: }
74:
75: public void flush()
76: {
77: if (writer != null)
78: {
79: try
80: {
81: writer.flush();
82: }
83: catch (XMLStreamException xse)
84: {
85:
86: }
87: }
88:
89: }
90:
91: public void close()
92: {
93: if (writer != null)
94: {
95: try
96: {
97: writer.close();
98: }
99: catch (XMLStreamException xse)
100: {
101:
102: }
103: writer = null;
104: }
105:
106: }
107:
108: public void writePreamble()
109: {
110: try
111: {
112: writer.writeStartDocument("UTF-8", "1.0");
113: }
114: catch (XMLStreamException xmlse)
115: {
116:
117: }
118: }
119:
120: public void writeEnd(boolean wasEmpty)
121: {
122: try
123: {
124: indent -= 2;
125:
126: if (wasEmpty)
127: return;
128:
129: for (int i = 0; i < indent; i++)
130: writer.writeCharacters(" ");
131:
132: writer.writeEndElement();
133:
134: writer.writeCharacters("\n");
135: }
136: catch (XMLStreamException xmlse)
137: {
138:
139: }
140: }
141:
142: public void writeEndNoChildren()
143: {
144: try
145: {
146: writer.writeEndElement();
147: writer.writeCharacters("\n");
148: }
149: catch (XMLStreamException xmlse)
150: {
151:
152: }
153: }
154:
155: public void write(String tagName, boolean empty)
156: {
157: write(tagName, null, null, null, empty);
158: }
159:
160: public void write(String tagName, String value)
161: {
162: write(tagName, value, null, null, value == null);
163: }
164:
165: public void writeNoChildren(String tagName, String value)
166: {
167: try
168: {
169: for (int i = 0; i < indent; i++)
170: writer.writeCharacters(" ");
171:
172: writer.writeStartElement(tagName);
173:
174: writer.writeCharacters(value);
175: }
176: catch (XMLStreamException xmlse)
177: {
178:
179: }
180: }
181:
182: public void write(String tagName, String attributeName,
183: String attributeValue, boolean empty)
184: {
185: write(tagName, null, new String[] { attributeName },
186: new String[] { attributeValue }, empty);
187: }
188:
189: public void write(String tagName, String value, String[] attributeNames,
190: String[] attributeValues, boolean empty)
191: {
192: try
193: {
194: for (int i = 0; i < indent; i++)
195:
196: writer.writeCharacters(" ");
197:
198: if (empty)
199: writer.writeEmptyElement(tagName);
200: else
201: writer.writeStartElement(tagName);
202:
203: if (attributeNames != null)
204: for (int i = 0; i < attributeNames.length; i++)
205: writer.writeAttribute(attributeNames[i], attributeValues[i]);
206:
207: writer.writeCharacters("\n");
208:
209: indent += 2;
210:
211: if (value != null)
212: {
213: for (int i = 0; i < indent; i++)
214: writer.writeCharacters(" ");
215:
216: writer.writeCharacters(value);
217:
218: writer.writeCharacters("\n");
219: }
220: }
221: catch (XMLStreamException xmlse)
222: {
223:
224: }
225: }
226:
227: public void write(String tagName, String[] attributeNames,
228: String[] attributeValues, boolean empty)
229: {
230: write(tagName, null, attributeNames, attributeValues, empty);
231: }
232:
233: }