1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation. Sun designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Sun in the LICENSE file that accompanied this code.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
22 * have any questions.
23 */
24
25 /*
26 * Copyright (c) 2003 by BEA Systems, Inc. All Rights Reserved.
27 */
28
29 package javax.xml.stream;
30
31 import javax.xml.namespace.NamespaceContext;
32
33 /**
34 * The XMLStreamWriter interface specifies how to write XML. The XMLStreamWriter does
35 * not perform well formedness checking on its input. However
36 * the writeCharacters method is required to escape & , < and >
37 * For attribute values the writeAttribute method will escape the
38 * above characters plus " to ensure that all character content
39 * and attribute values are well formed.
40 *
41 * Each NAMESPACE
42 * and ATTRIBUTE must be individually written.
43 *
44 * <table border="1" cellpadding="2" cellspacing="0">
45 * <thead>
46 * <tr>
47 * <th colspan="5">XML Namespaces, <code>javax.xml.stream.isRepairingNamespaces</code> and write method behaviour</th>
48 * </tr>
49 * <tr>
50 * <th>Method</th> <!-- method -->
51 * <th colspan="2"><code>isRepairingNamespaces</code> == true</th>
52 * <th colspan="2"><code>isRepairingNamespaces</code> == false</th>
53 * </tr>
54 * <tr>
55 * <th></th> <!-- method -->
56 * <th>namespaceURI bound</th>
57 * <th>namespaceURI unbound</th>
58 * <th>namespaceURI bound</th>
59 * <th>namespaceURI unbound</th>
60 * </tr>
61 * </thead>
62 *
63 * <tbody>
64 * <tr>
65 * <th><code>writeAttribute(namespaceURI, localName, value)</code></th>
66 * <!-- isRepairingNamespaces == true -->
67 * <td>
68 * <!-- namespaceURI bound -->
69 * prefix:localName="value" <sup>[1]</sup>
70 * </td>
71 * <td>
72 * <!-- namespaceURI unbound -->
73 * xmlns:{generated}="namespaceURI" {generated}:localName="value"
74 * </td>
75 * <!-- isRepairingNamespaces == false -->
76 * <td>
77 * <!-- namespaceURI bound -->
78 * prefix:localName="value" <sup>[1]</sup>
79 * </td>
80 * <td>
81 * <!-- namespaceURI unbound -->
82 * <code>XMLStreamException</code>
83 * </td>
84 * </tr>
85 *
86 * <tr>
87 * <th><code>writeAttribute(prefix, namespaceURI, localName, value)</code></th>
88 * <!-- isRepairingNamespaces == true -->
89 * <td>
90 * <!-- namespaceURI bound -->
91 * bound to same prefix:<br />
92 * prefix:localName="value" <sup>[1]</sup><br />
93 * <br />
94 * bound to different prefix:<br />
95 * xmlns:{generated}="namespaceURI" {generated}:localName="value"
96 * </td>
97 * <td>
98 * <!-- namespaceURI unbound -->
99 * xmlns:prefix="namespaceURI" prefix:localName="value" <sup>[3]</sup>
100 * </td>
101 * <!-- isRepairingNamespaces == false -->
102 * <td>
103 * <!-- namespaceURI bound -->
104 * bound to same prefix:<br />
105 * prefix:localName="value" <sup>[1][2]</sup><br />
106 * <br />
107 * bound to different prefix:<br />
108 * <code>XMLStreamException</code><sup>[2]</sup>
109 * </td>
110 * <td>
111 * <!-- namespaceURI unbound -->
112 * xmlns:prefix="namespaceURI" prefix:localName="value" <sup>[2][5]</sup>
113 * </td>
114 * </tr>
115 *
116 * <tr>
117 * <th><code>writeStartElement(namespaceURI, localName)</code><br />
118 * <br />
119 * <code>writeEmptyElement(namespaceURI, localName)</code></th>
120 * <!-- isRepairingNamespaces == true -->
121 * <td >
122 * <!-- namespaceURI bound -->
123 * <prefix:localName> <sup>[1]</sup>
124 * </td>
125 * <td>
126 * <!-- namespaceURI unbound -->
127 * <{generated}:localName xmlns:{generated}="namespaceURI">
128 * </td>
129 * <!-- isRepairingNamespaces == false -->
130 * <td>
131 * <!-- namespaceURI bound -->
132 * <prefix:localName> <sup>[1]</sup>
133 * </td>
134 * <td>
135 * <!-- namespaceURI unbound -->
136 * <code>XMLStreamException</code>
137 * </td>
138 * </tr>
139 *
140 * <tr>
141 * <th><code>writeStartElement(prefix, localName, namespaceURI)</code><br />
142 * <br />
143 * <code>writeEmptyElement(prefix, localName, namespaceURI)</code></th>
144 * <!-- isRepairingNamespaces == true -->
145 * <td>
146 * <!-- namespaceURI bound -->
147 * bound to same prefix:<br />
148 * <prefix:localName> <sup>[1]</sup><br />
149 * <br />
150 * bound to different prefix:<br />
151 * <{generated}:localName xmlns:{generated}="namespaceURI">
152 * </td>
153 * <td>
154 * <!-- namespaceURI unbound -->
155 * <prefix:localName xmlns:prefix="namespaceURI"> <sup>[4]</sup>
156 * </td>
157 * <!-- isRepairingNamespaces == false -->
158 * <td>
159 * <!-- namespaceURI bound -->
160 * bound to same prefix:<br />
161 * <prefix:localName> <sup>[1]</sup><br />
162 * <br />
163 * bound to different prefix:<br />
164 * <code>XMLStreamException</code>
165 * </td>
166 * <td>
167 * <!-- namespaceURI unbound -->
168 * <prefix:localName>
169 * </td>
170 * </tr>
171 * </tbody>
172 * <tfoot>
173 * <tr>
174 * <td colspan="5">
175 * Notes:
176 * <ul>
177 * <li>[1] if namespaceURI == default Namespace URI, then no prefix is written</li>
178 * <li>[2] if prefix == "" || null && namespaceURI == "", then no prefix or Namespace declaration is generated or written</li>
179 * <li>[3] if prefix == "" || null, then a prefix is randomly generated</li>
180 * <li>[4] if prefix == "" || null, then it is treated as the default Namespace and no prefix is generated or written, an xmlns declaration is generated and written if the namespaceURI is unbound</li>
181 * <li>[5] if prefix == "" || null, then it is treated as an invalid attempt to define the default Namespace and an XMLStreamException is thrown</li>
182 * </ul>
183 * </td>
184 * </tr>
185 * </tfoot>
186 * </table>
187 *
188 * @author Copyright (c) 2003 by BEA Systems. All Rights Reserved.
189 * @see XMLOutputFactory
190 * @see XMLStreamReader
191 * @since 1.6
192 */
193 public interface XMLStreamWriter {
194
195 /**
196 * Writes a start tag to the output. All writeStartElement methods
197 * open a new scope in the internal namespace context. Writing the
198 * corresponding EndElement causes the scope to be closed.
199 * @param localName local name of the tag, may not be null
200 * @throws XMLStreamException
201 */
202 public void writeStartElement(String localName)
203 throws XMLStreamException;
204
205 /**
206 * Writes a start tag to the output
207 * @param namespaceURI the namespaceURI of the prefix to use, may not be null
208 * @param localName local name of the tag, may not be null
209 * @throws XMLStreamException if the namespace URI has not been bound to a prefix and
210 * javax.xml.stream.isRepairingNamespaces has not been set to true
211 */
212 public void writeStartElement(String namespaceURI, String localName)
213 throws XMLStreamException;
214
215 /**
216 * Writes a start tag to the output
217 * @param localName local name of the tag, may not be null
218 * @param prefix the prefix of the tag, may not be null
219 * @param namespaceURI the uri to bind the prefix to, may not be null
220 * @throws XMLStreamException
221 */
222 public void writeStartElement(String prefix,
223 String localName,
224 String namespaceURI)
225 throws XMLStreamException;
226
227 /**
228 * Writes an empty element tag to the output
229 * @param namespaceURI the uri to bind the tag to, may not be null
230 * @param localName local name of the tag, may not be null
231 * @throws XMLStreamException if the namespace URI has not been bound to a prefix and
232 * javax.xml.stream.isRepairingNamespaces has not been set to true
233 */
234 public void writeEmptyElement(String namespaceURI, String localName)
235 throws XMLStreamException;
236
237 /**
238 * Writes an empty element tag to the output
239 * @param prefix the prefix of the tag, may not be null
240 * @param localName local name of the tag, may not be null
241 * @param namespaceURI the uri to bind the tag to, may not be null
242 * @throws XMLStreamException
243 */
244 public void writeEmptyElement(String prefix, String localName, String namespaceURI)
245 throws XMLStreamException;
246
247 /**
248 * Writes an empty element tag to the output
249 * @param localName local name of the tag, may not be null
250 * @throws XMLStreamException
251 */
252 public void writeEmptyElement(String localName)
253 throws XMLStreamException;
254
255 /**
256 * Writes string data to the output without checking for well formedness.
257 * The data is opaque to the XMLStreamWriter, i.e. the characters are written
258 * blindly to the underlying output. If the method cannot be supported
259 * in the currrent writing context the implementation may throw a
260 * UnsupportedOperationException. For example note that any
261 * namespace declarations, end tags, etc. will be ignored and could
262 * interfere with proper maintanence of the writers internal state.
263 *
264 * @param data the data to write
265 */
266 // public void writeRaw(String data) throws XMLStreamException;
267
268 /**
269 * Writes an end tag to the output relying on the internal
270 * state of the writer to determine the prefix and local name
271 * of the event.
272 * @throws XMLStreamException
273 */
274 public void writeEndElement()
275 throws XMLStreamException;
276
277 /**
278 * Closes any start tags and writes corresponding end tags.
279 * @throws XMLStreamException
280 */
281 public void writeEndDocument()
282 throws XMLStreamException;
283
284 /**
285 * Close this writer and free any resources associated with the
286 * writer. This must not close the underlying output stream.
287 * @throws XMLStreamException
288 */
289 public void close()
290 throws XMLStreamException;
291
292 /**
293 * Write any cached data to the underlying output mechanism.
294 * @throws XMLStreamException
295 */
296 public void flush()
297 throws XMLStreamException;
298
299 /**
300 * Writes an attribute to the output stream without
301 * a prefix.
302 * @param localName the local name of the attribute
303 * @param value the value of the attribute
304 * @throws IllegalStateException if the current state does not allow Attribute writing
305 * @throws XMLStreamException
306 */
307 public void writeAttribute(String localName, String value)
308 throws XMLStreamException;
309
310 /**
311 * Writes an attribute to the output stream
312 * @param prefix the prefix for this attribute
313 * @param namespaceURI the uri of the prefix for this attribute
314 * @param localName the local name of the attribute
315 * @param value the value of the attribute
316 * @throws IllegalStateException if the current state does not allow Attribute writing
317 * @throws XMLStreamException if the namespace URI has not been bound to a prefix and
318 * javax.xml.stream.isRepairingNamespaces has not been set to true
319 */
320
321 public void writeAttribute(String prefix,
322 String namespaceURI,
323 String localName,
324 String value)
325 throws XMLStreamException;
326
327 /**
328 * Writes an attribute to the output stream
329 * @param namespaceURI the uri of the prefix for this attribute
330 * @param localName the local name of the attribute
331 * @param value the value of the attribute
332 * @throws IllegalStateException if the current state does not allow Attribute writing
333 * @throws XMLStreamException if the namespace URI has not been bound to a prefix and
334 * javax.xml.stream.isRepairingNamespaces has not been set to true
335 */
336 public void writeAttribute(String namespaceURI,
337 String localName,
338 String value)
339 throws XMLStreamException;
340
341 /**
342 * Writes a namespace to the output stream
343 * If the prefix argument to this method is the empty string,
344 * "xmlns", or null this method will delegate to writeDefaultNamespace
345 *
346 * @param prefix the prefix to bind this namespace to
347 * @param namespaceURI the uri to bind the prefix to
348 * @throws IllegalStateException if the current state does not allow Namespace writing
349 * @throws XMLStreamException
350 */
351 public void writeNamespace(String prefix, String namespaceURI)
352 throws XMLStreamException;
353
354 /**
355 * Writes the default namespace to the stream
356 * @param namespaceURI the uri to bind the default namespace to
357 * @throws IllegalStateException if the current state does not allow Namespace writing
358 * @throws XMLStreamException
359 */
360 public void writeDefaultNamespace(String namespaceURI)
361 throws XMLStreamException;
362
363 /**
364 * Writes an xml comment with the data enclosed
365 * @param data the data contained in the comment, may be null
366 * @throws XMLStreamException
367 */
368 public void writeComment(String data)
369 throws XMLStreamException;
370
371 /**
372 * Writes a processing instruction
373 * @param target the target of the processing instruction, may not be null
374 * @throws XMLStreamException
375 */
376 public void writeProcessingInstruction(String target)
377 throws XMLStreamException;
378
379 /**
380 * Writes a processing instruction
381 * @param target the target of the processing instruction, may not be null
382 * @param data the data contained in the processing instruction, may not be null
383 * @throws XMLStreamException
384 */
385 public void writeProcessingInstruction(String target,
386 String data)
387 throws XMLStreamException;
388
389 /**
390 * Writes a CData section
391 * @param data the data contained in the CData Section, may not be null
392 * @throws XMLStreamException
393 */
394 public void writeCData(String data)
395 throws XMLStreamException;
396
397 /**
398 * Write a DTD section. This string represents the entire doctypedecl production
399 * from the XML 1.0 specification.
400 *
401 * @param dtd the DTD to be written
402 * @throws XMLStreamException
403 */
404 public void writeDTD(String dtd)
405 throws XMLStreamException;
406
407 /**
408 * Writes an entity reference
409 * @param name the name of the entity
410 * @throws XMLStreamException
411 */
412 public void writeEntityRef(String name)
413 throws XMLStreamException;
414
415 /**
416 * Write the XML Declaration. Defaults the XML version to 1.0, and the encoding to utf-8
417 * @throws XMLStreamException
418 */
419 public void writeStartDocument()
420 throws XMLStreamException;
421
422 /**
423 * Write the XML Declaration. Defaults the XML version to 1.0
424 * @param version version of the xml document
425 * @throws XMLStreamException
426 */
427 public void writeStartDocument(String version)
428 throws XMLStreamException;
429
430 /**
431 * Write the XML Declaration. Note that the encoding parameter does
432 * not set the actual encoding of the underlying output. That must
433 * be set when the instance of the XMLStreamWriter is created using the
434 * XMLOutputFactory
435 * @param encoding encoding of the xml declaration
436 * @param version version of the xml document
437 * @throws XMLStreamException If given encoding does not match encoding
438 * of the underlying stream
439 */
440 public void writeStartDocument(String encoding,
441 String version)
442 throws XMLStreamException;
443
444 /**
445 * Write text to the output
446 * @param text the value to write
447 * @throws XMLStreamException
448 */
449 public void writeCharacters(String text)
450 throws XMLStreamException;
451
452 /**
453 * Write text to the output
454 * @param text the value to write
455 * @param start the starting position in the array
456 * @param len the number of characters to write
457 * @throws XMLStreamException
458 */
459 public void writeCharacters(char[] text, int start, int len)
460 throws XMLStreamException;
461
462 /**
463 * Gets the prefix the uri is bound to
464 * @return the prefix or null
465 * @throws XMLStreamException
466 */
467 public String getPrefix(String uri)
468 throws XMLStreamException;
469
470 /**
471 * Sets the prefix the uri is bound to. This prefix is bound
472 * in the scope of the current START_ELEMENT / END_ELEMENT pair.
473 * If this method is called before a START_ELEMENT has been written
474 * the prefix is bound in the root scope.
475 * @param prefix the prefix to bind to the uri, may not be null
476 * @param uri the uri to bind to the prefix, may be null
477 * @throws XMLStreamException
478 */
479 public void setPrefix(String prefix, String uri)
480 throws XMLStreamException;
481
482
483 /**
484 * Binds a URI to the default namespace
485 * This URI is bound
486 * in the scope of the current START_ELEMENT / END_ELEMENT pair.
487 * If this method is called before a START_ELEMENT has been written
488 * the uri is bound in the root scope.
489 * @param uri the uri to bind to the default namespace, may be null
490 * @throws XMLStreamException
491 */
492 public void setDefaultNamespace(String uri)
493 throws XMLStreamException;
494
495 /**
496 * Sets the current namespace context for prefix and uri bindings.
497 * This context becomes the root namespace context for writing and
498 * will replace the current root namespace context. Subsequent calls
499 * to setPrefix and setDefaultNamespace will bind namespaces using
500 * the context passed to the method as the root context for resolving
501 * namespaces. This method may only be called once at the start of
502 * the document. It does not cause the namespaces to be declared.
503 * If a namespace URI to prefix mapping is found in the namespace
504 * context it is treated as declared and the prefix may be used
505 * by the StreamWriter.
506 * @param context the namespace context to use for this writer, may not be null
507 * @throws XMLStreamException
508 */
509 public void setNamespaceContext(NamespaceContext context)
510 throws XMLStreamException;
511
512 /**
513 * Returns the current namespace context.
514 * @return the current NamespaceContext
515 */
516 public NamespaceContext getNamespaceContext();
517
518 /**
519 * Get the value of a feature/property from the underlying implementation
520 * @param name The name of the property, may not be null
521 * @return The value of the property
522 * @throws IllegalArgumentException if the property is not supported
523 * @throws NullPointerException if the name is null
524 */
525 public Object getProperty(java.lang.String name) throws IllegalArgumentException;
526
527 }