Statistics
| Revision:

root / trunk / libraries / libGPE-XML / src / org / gvsig / gpe / xml / stream / IXmlStreamWriter.java @ 21945

History | View | Annotate | Download (17.3 KB)

1
/* gvSIG. Sistem a de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 9638 62 495
28
 *      gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 */
31
package org.gvsig.gpe.xml.stream;
32

    
33
import java.io.IOException;
34

    
35
import javax.xml.namespace.QName;
36

    
37
/**
38
 * IXmlStreamWriter defines an "XML push" like streaming API for writing XML documents encoded as
39
 * per the OGC Binary XML Best Practices document.
40
 * <p>
41
 * This interface does not defines setter methods to control encoding behavior such as byte order to
42
 * use or character encoding. A IXmlStreamWriter instance is meant to be already configured with the
43
 * desired encoding options when returned from the {@link BxmlOutputFactory}.
44
 * </p>
45
 * <p>
46
 * Sample usage:
47
 * 
48
 * <pre>
49
 * <code>
50
 * BxmlOutputFactory factory = BxmlFactoryFinder.newOutputFactory();
51
 * IXmlStreamWriter writer = factory.createSerializer(inputStream);
52
 * 
53
 * writer.setSchemaLocation("http://www.example.com", "http://www.example.com/schema.xsd");
54
 * 
55
 * writer.writeStartDocument();
56
 * 
57
 * writer.setPrefix("ex", "http://www.example.com")
58
 * writer.setDefaultNamespace("http://www.anotherexample.com");
59
 * 
60
 * writer.writeStartElement("", "root");
61
 * 
62
 * writer.writeStartElement("http://www.example.com", "child");
63
 * 
64
 * //child attributes list...
65
 * writer.writeStartAttribute("http://www.example.com", "att");
66
 * writer.writeValue("attValue");
67
 * writer.writeStartAttribute("http://www.example.com", "emptyAtt");
68
 * writer.writeEndAttributes();
69
 * 
70
 * //child value as a purely streamed int array
71
 * writer.startArray(EventType.VALUE_INT, 10);
72
 * for(int i = 0; i < 10; i++){
73
 *   writer.writeValue(i);
74
 * }
75
 * writer.endArray();
76
 * 
77
 * writer.writeComment("interleaved value type follow");
78
 * 
79
 * writer.writeValue("10 11 12 13");
80
 * 
81
 * writer.writeEndElement(); // child
82
 * writer.writeEndElement(); // root
83
 * writer.writeEndDocument();
84
 * </code>
85
 * </pre>
86
 * 
87
 * </p>
88
 * 
89
 * @author Gabriel Roldan (TOPP)
90
 * @version $Id: IXmlStreamWriter.java 21945 2008-06-30 14:04:53Z jpiera $
91
 * @since 1.0
92
 */
93
public interface IXmlStreamWriter {
94

    
95
    /**
96
     * Returns a safe copy of the encoding options this writer is settled up with.
97
     * <p>
98
     * Modifications to the returned object state does not affect this writer behaviour.
99
     * </p>
100
     * 
101
     * @return an object representing the set of encoding options this writer operates with
102
     */
103
    //public EncodingOptions getEncodingOptions();
104

    
105
    /**
106
     * Returns the event corresponding to the last write call.
107
     * <p>
108
     * For example, if the last write call was
109
     * {@link #writeStartElement(String, String) writeStartElement} it shall return
110
     * {@link EventType#START_ELEMENT START_ELEMENT}, if it was
111
     * {@link #writeStartAttribute(String, String) writeStartAttribute},
112
     * {@link EventType#ATTRIBUTE ATTRIBUTE}, etc.
113
     * </p>
114
     * 
115
     * @post {$return != null}
116
     * @return {@link EventType#NONE} if writing has not yet started (ie, writeStartDocument has not
117
     *         been called), the event for the last write operation otherwise.
118
     */
119
    public EventType getLastEvent();
120

    
121
    /**
122
     * @return {@code null} if no tag event has been written yet, START_ELEMENT or END_ELEMENT
123
     *         otherwise, depending on which of them was lately called
124
     */
125
    public EventType getLastTagEvent();
126

    
127
    /**
128
     * @post {$return >= 0}
129
     * @return how deep is the current element tree
130
     */
131
    public int getTagDeep();
132

    
133
    /**
134
     * Closes this stream writer and releases any system resources associated with it, including the
135
     * underlying output stream or any other output source it may be operating upon.
136
     * <p>
137
     * A call to this method implies a flush of any possible buffered wtrite before closing the
138
     * stream.
139
     * </p>
140
     * <p>
141
     * The first call to this method closes and releases resources. Subsequent calls shall take no
142
     * effect and return quitely. After the first call to this method any non query method (defined
143
     * as metadata retrieval methods in the class' javadoc) shall fail with an io exception.
144
     * </p>
145
     * 
146
     * @throws IOException if an I/O error occurs.
147
     */
148
    public void close() throws IOException;
149

    
150
    /**
151
     * Returns whether this stream writer and its underlying output source are open and able to
152
     * receive more write calls.
153
     * <p>
154
     * A {@code IXmlStreamWriter} should be open since its creation until {@link #close()} is
155
     * explicitly called by client code.
156
     * </p>
157
     * 
158
     * @return {@code true} if this stream is still open, {@code false} otherwise
159
     */
160
    public boolean isOpen();
161

    
162
    /**
163
     * Write any cached data to the underlying output mechanism.
164
     * 
165
     * @throws IOException
166
     */
167
    public void flush() throws IOException;
168

    
169
    /**
170
     * @pre {getLastEvent() == NONE}
171
     * @pre {namespaceUri != null AND schemaLocationUri != null}
172
     * @pre {namespaceUri != ""}
173
     * @param namespaceUri
174
     * @param schemaLocationUri
175
     */
176
    public void setSchemaLocation(String namespaceUri, String schemaLocationUri);
177

    
178
    /**
179
     * Initiates the encoding of an XML formatted document in the BXML encoding.
180
     * <p>
181
     * This should be the first non query method to call after a stream writer is created. The
182
     * implementation will use the call to this method to write down the document header and xml
183
     * declaration.
184
     * </p>
185
     * 
186
     * @pre {getLastEvent() == NONE}
187
     * @post {getLastEvent() == START_DOCUMENT}
188
     * @throws IOException
189
     */
190
    public void writeStartDocument() throws IOException;
191

    
192
    /**
193
     * Finishes up encoding a BXML document.
194
     * <p>
195
     * Implementations shall use this method to write down the document's trailer token.
196
     * </p>
197
     * 
198
     * @pre {getTagDeep() == 0}
199
     * @post {getLastEvent() == END_DOCUMENT}
200
     * @throws IOException
201
     */
202
    public void writeEndDocument() throws IOException;
203

    
204
    /**
205
     * @pre {namespaceUri != null}
206
     * @pre {localName != null}
207
     * @post {getLastEvent() == START_ELEMENT}
208
     * @param namespaceUri the namespace uri for the element
209
     * @param localName the elements non qualified (local) name
210
     * @throws IOException
211
     */
212
    public void writeStartElement(final String namespaceUri, final String localName)
213
            throws IOException;
214

    
215
    /**
216
     * @pre {qname != null}
217
     * @post {getLastEvent() == START_ELEMENT}
218
     * @param qname qualified element name. Only namespace and localName are relevant. Prefix, if
219
     *            present, is ignored. The currently mapped prefix for the namespace will be used in
220
     *            any case.
221
     * @throws IOException
222
     * @see #setPrefix(String, String)
223
     */
224
    public void writeStartElement(final QName qname) throws IOException;
225

    
226
    /**
227
     * @pre {getTagDeep() > 0}
228
     * @pre { ( getLastEvent().isValue() AND getWrittenValueCount() == getValueLength() ) ||
229
     *      getLastEvent() IN ( START_ELEMENT, END_ELEMENT, ATTRIBUTES_END, COMMENT}
230
     * @post {getLastEvent() == END_ELEMENT}
231
     * @throws IOException
232
     */
233
    public void writeEndElement() throws IOException;
234

    
235
    /**
236
     * Starts writting an xml attribute.
237
     * <p>
238
     * Writting an attribute and its value is split in two parts. This method only stand for the
239
     * attribute declaration. Writting an attribute value is made after calling this method and is
240
     * optional. The attribute value can be done with any of the {@code writeValue} methods, even
241
     * using various {@code writeValue} calls for the same attribute.
242
     * </p>
243
     * <p>
244
     * After the last element attribute has been written, {@link #writeEndAttributes()} shall be
245
     * called to indicate the end of the attribute list.
246
     * </p>
247
     * Sample usage:
248
     * 
249
     * <pre>
250
     * <code>
251
     * <b>writer.startAttribute("", "att1");</b>
252
     * writer.writeValue("value1");
253
     * writer.writeValue(1);
254
     * writer.writeValue(2);
255
     * writer.writeValue(3);
256
     * <b>writer.startAttribute("", "att2");</b>
257
     * writer.writeValue(1.0L);
258
     * ...
259
     * writer.writeEndAttributes();
260
     * </code>
261
     * </pre>
262
     * 
263
     * @pre {getLastEvent() IN (START_ELEMENT, ATTRIBUTE) }
264
     * @pre {namespaceUri != null}
265
     * @pre {localName != null}
266
     * @post {getLastEvent() == ATTRIBUTE}
267
     * @param namespaceUri not null
268
     * @param localName not null
269
     * @throws IOException
270
     * @see #writeEndAttributes()
271
     */
272
    public void writeStartAttribute(final String namespaceUri, final String localName)
273
            throws IOException;
274

    
275
    /**
276
     * Starts writing an xml attribute for the given qualified name.
277
     * 
278
     * @pre {qname != null}
279
     * @param qname a QName where to get the namespace uri and local name for the attribute
280
     * @throws IOException
281
     * @see #writeStartAttribute(String, String)
282
     */
283
    public void writeStartAttribute(final QName qname) throws IOException;
284

    
285
    /**
286
     * Indicates there are no more element attributes to encode for the current element.
287
     * <p>
288
     * This method is useful for the encoder to recognize (and write down the appropriate tokens)
289
     * the following potential call to some writeValue method corresponds to the element's contents
290
     * and not to the current attribute value.
291
     * </p>
292
     * Sample usage:
293
     * 
294
     * <pre>
295
     * <code>
296
     * writer.startElement("", "element");
297
     * writer.startAttribute("", "att1");
298
     * writer.writeValue("value1");
299
     * writer.startAttribute("", "att2");
300
     * writer.writeValue(1.0L);
301
     * ...
302
     * <b>writer.writeEndAttributes();</b>
303
     * writer.value("element value");
304
     * ...
305
     * </code>
306
     * </pre>
307
     * 
308
     * @pre {getLastTagEvent() == START_ELEMENT}
309
     * @post {getLastEvent() == ATTRIBUTES_END}
310
     * @throws IOException
311
     */
312
    public void writeEndAttributes() throws IOException;
313

    
314
    /**
315
     * @pre {getLastEvent().isValue() == true || getLastEvent() IN (START_ELEMENT, ATTRIBUTE,
316
     *      COMMENT )}
317
     * @post {getLastEvent() == VALUE_STRING}
318
     * @post {getValueLength() == 1}
319
     * @post {getWrittenValueCount() == 1}
320
     * @param value
321
     * @throws IOException
322
     */
323
    public void writeValue(final String value) throws IOException;
324

    
325
    /**
326
     * @pre {getLastEvent().isValue() == true || getLastEvent() IN (START_ELEMENT, ATTRIBUTE,
327
     *      COMMENT )}
328
     * @post {getLastEvent() == VALUE_STRING}
329
     * @param chars
330
     * @param offset
331
     * @param length
332
     * @throws IOException
333
     */
334
    public void writeValue(final char[] chars, final int offset, final int length)
335
            throws IOException;
336

    
337
    /**
338
     * @pre {getLastEvent().isValue() == true || getLastEvent() IN (START_ELEMENT, ATTRIBUTE,
339
     *      COMMENT )}
340
     * @post {getLastEvent() == VALUE_INT}
341
     * @param value
342
     * @throws IOException
343
     */
344
    public void writeValue(final int value) throws IOException;
345

    
346
    /**
347
     * @pre {getLastEvent().isValue() == true || getLastEvent() IN (START_ELEMENT, ATTRIBUTE,
348
     *      COMMENT )}
349
     * @post {getLastEvent() == VALUE_LONG}
350
     * @param value
351
     * @throws IOException
352
     */
353
    public void writeValue(final long value) throws IOException;
354

    
355
    /**
356
     * @pre {getLastEvent().isValue() == true || getLastEvent() IN (START_ELEMENT, ATTRIBUTE,
357
     *      COMMENT )}
358
     * @post {getLastEvent() == VALUE_FLOAT}
359
     * @param value
360
     * @throws IOException
361
     */
362
    public void writeValue(final float value) throws IOException;
363

    
364
    /**
365
     * @pre {getLastEvent().isValue() == true || getLastEvent() IN (START_ELEMENT, ATTRIBUTE,
366
     *      COMMENT )}
367
     * @post {getLastEvent() == VALUE_DOUBLE}
368
     * @param value
369
     * @throws IOException
370
     */
371
    public void writeValue(final double value) throws IOException;
372

    
373
    /**
374
     * @pre {getLastEvent().isValue() == true || getLastEvent() IN (START_ELEMENT, ATTRIBUTE,
375
     *      COMMENT )}
376
     * @post {getLastEvent() == VALUE_BOOL}
377
     * @param value
378
     * @throws IOException
379
     */
380
    public void writeValue(final boolean value) throws IOException;
381

    
382
    /**
383
     * @pre {getLastEvent().isValue() == true || getLastEvent() IN (START_ELEMENT, ATTRIBUTE,
384
     *      COMMENT )}
385
     * @post {getLastEvent() == VALUE_BOOL}
386
     * @param value
387
     * @param offset
388
     * @param length
389
     * @throws IOException
390
     */
391
    public void writeValue(final boolean[] value, final int offset, final int length)
392
            throws IOException;
393

    
394
    /**
395
     * @pre {getLastEvent().isValue() == true || getLastEvent() IN (START_ELEMENT, ATTRIBUTE,
396
     *      COMMENT )}
397
     * @post {getLastEvent() == VALUE_INT}
398
     * @param value
399
     * @param offset
400
     * @param length
401
     * @throws IOException
402
     */
403
    public void writeValue(final int[] value, final int offset, final int length)
404
            throws IOException;
405

    
406
    /**
407
     * @pre {getLastEvent().isValue() == true || getLastEvent() IN (START_ELEMENT, ATTRIBUTE,
408
     *      COMMENT )}
409
     * @post {getLastEvent() == VALUE_LONG}
410
     * @param value
411
     * @param offset
412
     * @param length
413
     * @throws IOException
414
     */
415
    public void writeValue(final long[] value, final int offset, final int length)
416
            throws IOException;
417

    
418
    /**
419
     * @pre {getLastEvent().isValue() == true || getLastEvent() IN (START_ELEMENT, ATTRIBUTE,
420
     *      COMMENT )}
421
     * @post {getLastEvent() == VALUE_FLOAT}
422
     * @param value
423
     * @param offset
424
     * @param length
425
     * @throws IOException
426
     */
427
    public void writeValue(final float[] value, final int offset, final int length)
428
            throws IOException;
429

    
430
    /**
431
     * Writes a value section defined by the
432
     * 
433
     * @pre {getLastEvent() IN ( START_ELEMENT START_ELEMENT, ATTRIBUTE ATTRIBUTE )}
434
     * @post {getLastEvent() == VALUE_DOUBLE}
435
     * @param value
436
     * @param offset
437
     * @param length
438
     * @throws IOException
439
     */
440
    public void writeValue(final double[] value, int offset, int length) throws IOException;
441

    
442
    /**
443
     * Writes a comments block
444
     * <p>
445
     * The provided {@code commentContent} is the coalesced value of the equivalent XML comment
446
     * block (enclosed between {@code <!--} and {@code -->} tags.
447
     * </p>
448
     * 
449
     * @post {getLastEvent() == COMMENT}
450
     * @param commentContent the coallesced value of the comment section
451
     * @throws IOException
452
     */
453
    public void writeComment(String commentContent) throws IOException;
454

    
455
    /**
456
     * @pre {valueType != null}
457
     * @pre {valueType.isValue() == true}
458
     * @pre {valueType != VALUE_STRING}
459
     * @pre {arrayLength >= 0}
460
     * @post {getLastEvent() == valueType}
461
     * @post {getWrittenValueCount() == 0}
462
     * @post {getValueLength() == arrayLength}
463
     * @param valueType
464
     * @param arrayLength
465
     * @throws IOException
466
     */
467
    public void startArray(EventType valueType, int arrayLength) throws IOException;
468

    
469
    /**
470
     * Needs to be called when done with startArray
471
     * 
472
     * @pre {getWrittenValueCount() == getValueLength()}
473
     * @throws IOException
474
     */
475
    public void endArray() throws IOException;
476

    
477
    /**
478
     * Binds a URI to the default namespace for the current context.
479
     * <p>
480
     * This URI is bound in the scope of the current START_ELEMENT / END_ELEMENT pair. If this
481
     * method is called before a START_ELEMENT has been written the uri is bound in the root scope.
482
     * </p>
483
     * 
484
     * @pre {getLastEvent() == NONE || getLastEvent() == START_ELEMENT}
485
     * @param defaultNamespaceUri the uri to bind to the default namespace, may be {@code null}
486
     * @throws XmlStreamException 
487
     */
488
    public void setDefaultNamespace(String defaultNamespaceUri) throws XmlStreamException;
489

    
490
    /**
491
     * Sets the prefix the uri is bound to, for the current context.
492
     * <p>
493
     * This prefix is bound in the scope of the current START_ELEMENT / END_ELEMENT pair (current
494
     * context). If this method is called before a START_ELEMENT has been written the prefix is
495
     * bound in the root scope.
496
     * </p>
497
     * 
498
     * @pre {getLastEvent() == NONE || getLastEvent() == START_ELEMENT}
499
     * @param prefix
500
     * @param namespaceUri
501
     * @throws XmlStreamException 
502
     */
503
    public void setPrefix(final String prefix, final String namespaceUri) throws XmlStreamException;
504

    
505
    /**
506
     * @pre {getLastEvent().isValue() == true}
507
     * @post {$return >= 0}
508
     * @return the length of the current value being written.
509
     */
510
    public long getValueLength();
511

    
512
    /**
513
     * @pre {getLastEvent().isValue() == true}
514
     * @post {$return >= 0}
515
     * @post {$return <= getValueLength()}
516
     * @return how many elements has already being written for the current value
517
     */
518
    public long getWrittenValueCount();
519

    
520
}