Statistics
| Revision:

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

History | View | Annotate | Download (14.1 KB)

1
package org.gvsig.gpe.xml.stream.stax;
2

    
3
import java.io.OutputStream;
4

    
5
import javax.xml.namespace.QName;
6
import javax.xml.stream.XMLOutputFactory;
7
import javax.xml.stream.XMLStreamException;
8
import javax.xml.stream.XMLStreamWriter;
9

    
10
import org.gvsig.gpe.xml.stream.EventType;
11
import org.gvsig.gpe.xml.stream.IXmlStreamWriter;
12
import org.gvsig.gpe.xml.stream.XmlStreamException;
13

    
14
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
15
 *
16
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
17
 *
18
 * This program is free software; you can redistribute it and/or
19
 * modify it under the terms of the GNU General Public License
20
 * as published by the Free Software Foundation; either version 2
21
 * of the License, or (at your option) any later version.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 * GNU General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU General Public License
29
 * along with this program; if not, write to the Free Software
30
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
31
 *
32
 * For more information, contact:
33
 *
34
 *  Generalitat Valenciana
35
 *   Conselleria d'Infraestructures i Transport
36
 *   Av. Blasco Ib??ez, 50
37
 *   46010 VALENCIA
38
 *   SPAIN
39
 *
40
 *      +34 963862235
41
 *   gvsig@gva.es
42
 *      www.gvsig.gva.es
43
 *
44
 *    or
45
 *
46
 *   IVER T.I. S.A
47
 *   Salamanca 50
48
 *   46005 Valencia
49
 *   Spain
50
 *
51
 *   +34 963163400
52
 *   dac@iver.es
53
 */
54

    
55
/**
56
 * An XML stream writer that uses a StAX writer under the hood.
57
 */
58
public class StaxXmlStreamWriter implements IXmlStreamWriter {
59

    
60
    private XMLStreamWriter writer;
61

    
62
    private EventType lastTagEvent;
63
    private EventType lastEvent;
64

    
65
    private long valueLength;
66

    
67
    private long writtenValueLength;
68

    
69
    /**
70
     * nsuri for the attribute currently being processed. null if an attribute is not being
71
     * processed
72
     */
73
    private String attributeNamespace;
74

    
75
    /**
76
     * local name for the attribute currently being processed. null if an attribute is not being
77
     * processed
78
     */
79
    private String attributeLocalName;
80

    
81
    private StringBuffer attributeValue = new StringBuffer();
82

    
83
    private StringBuffer valueBuff = new StringBuffer();
84

    
85
    public StaxXmlStreamWriter(final OutputStream out) throws XmlStreamException {
86
        final XMLOutputFactory staxFactory = XMLOutputFactory.newInstance();
87
        try {
88
            this.writer = staxFactory.createXMLStreamWriter(out);
89
        } catch (XMLStreamException e) {
90
            throw new XmlStreamException(e);
91
        }
92
        lastEvent = EventType.NONE;
93
    }
94

    
95
    /**
96
     * @see IXmlStreamWriter#close()
97
     */
98
    public void close() throws XmlStreamException {
99
        if (writer != null) {
100
            try {
101
                writer.close();
102
            } catch (XMLStreamException e) {
103
                throw new XmlStreamException(e);
104
            }
105
            writer = null;
106
        }
107
    }
108

    
109
    /**
110
     * @see IXmlStreamWriter#isOpen()
111
     */
112
    public boolean isOpen() {
113
        return writer != null;
114
    }
115

    
116
    /**
117
     * @throws XmlStreamException 
118
     * @see IXmlStreamWriter#setDefaultNamespace(java.lang.String)
119
     */
120
    public void setDefaultNamespace(String defaultNamespaceUri) throws XmlStreamException {
121
              try {
122
                          writer.setDefaultNamespace(defaultNamespaceUri);
123
                  } catch (XMLStreamException e) {
124
                           throw new XmlStreamException(e);
125
                  } 
126
    }
127

    
128
    /**
129
     * @throws XmlStreamException 
130
     * @see IXmlStreamWriter#setPrefix(java.lang.String, java.lang.String)
131
     */
132
    public void setPrefix(String prefix, String namespaceUri) throws XmlStreamException {
133
        try {
134
                        writer.setPrefix(prefix, namespaceUri);
135
                } catch (XMLStreamException e) {
136
                         throw new XmlStreamException(e);
137
                }            
138
    }
139

    
140
    /**
141
     * @see IXmlStreamWriter#setSchemaLocation(java.lang.String, java.lang.String)
142
     */
143
    public void setSchemaLocation(String namespaceUri, String schemaLocationUri) {
144
               throw new UnsupportedOperationException("Not yet implemented");
145
    }
146

    
147
    /**
148
     * @see IXmlStreamWriter#flush()
149
     */
150
    public void flush() throws XmlStreamException {
151
        try {
152
            writer.flush();
153
        } catch (XMLStreamException e) {
154
            throw new XmlStreamException(e);
155
        }
156
    }
157

    
158
    /**
159
     * @see IXmlStreamWriter#getLastEvent()
160
     */
161
    public EventType getLastEvent() {
162
        return lastEvent;
163
    }
164

    
165
    /**
166
     * @see IXmlStreamWriter#getLastTagEvent()
167
     */
168
    public EventType getLastTagEvent() {
169
        return lastTagEvent;
170
    }
171

    
172
    /**
173
     * @see IXmlStreamWriter#getTagDeep()
174
     */
175
    public int getTagDeep() {
176
        throw new UnsupportedOperationException("Not yet implemented");
177
    }
178

    
179
    /**
180
     * @see IXmlStreamWriter#getValueLength()
181
     */
182
    public long getValueLength() {
183
        return valueLength;
184
    }
185

    
186
    /**
187
     * @see IXmlStreamWriter#getWrittenValueCount()
188
     */
189
    public long getWrittenValueCount() {
190
        return writtenValueLength;
191
    }
192

    
193
    /**
194
     * @see IXmlStreamWriter#startArray(org.gvsig.gpe.xml.stream.EventType, int)
195
     */
196
    public void startArray(EventType valueType, int arrayLength) throws XmlStreamException {
197
        valueLength = arrayLength;
198
        lastEvent = valueType;
199
        writtenValueLength = 0;
200
    }
201

    
202
    /**
203
     * @see IXmlStreamWriter#endArray()
204
     */
205
    public void endArray() throws XmlStreamException {
206
        // nothing to do
207
    }
208

    
209
    /**
210
     * @see IXmlStreamWriter#writeComment(java.lang.String)
211
     */
212
    public void writeComment(String commentContent) throws XmlStreamException {
213
        try {
214
            writer.writeComment(commentContent);
215
        } catch (XMLStreamException e) {
216
            throw new XmlStreamException(e);
217
        }
218
        lastEvent = EventType.COMMENT;
219
    }
220

    
221
    /**
222
     * @see IXmlStreamWriter#writeEndAttributes()
223
     */
224
    public void writeEndAttributes() throws XmlStreamException {
225
        flushAttribute();
226
        lastEvent = EventType.ATTRIBUTES_END;
227
    }
228

    
229
    /**
230
     * @see IXmlStreamWriter#writeEndDocument()
231
     */
232
    public void writeEndDocument() throws XmlStreamException {
233
        try {
234
            writer.writeEndDocument();
235
        } catch (XMLStreamException e) {
236
            throw new XmlStreamException(e);
237
        }
238
        lastEvent = EventType.END_DOCUMENT;
239
    }
240

    
241
    /**
242
     * @see IXmlStreamWriter#writeEndElement()
243
     */
244
    public void writeEndElement() throws XmlStreamException {
245
        try {
246
            writer.writeEndElement();
247
        } catch (XMLStreamException e) {
248
            throw new XmlStreamException(e);
249
        }
250
        lastEvent = lastTagEvent = EventType.END_ELEMENT;
251
    }
252

    
253
    /**
254
     * @see IXmlStreamWriter#writeStartAttribute(java.lang.String, java.lang.String)
255
     */
256
    public void writeStartAttribute(String namespaceUri, String localName)
257
            throws XmlStreamException {
258
        // do we already have an attribute in course?
259
        flushAttribute();
260

    
261
        attributeNamespace = namespaceUri;
262
        attributeLocalName = localName;
263
        attributeValue.setLength(0);
264
        lastEvent = EventType.ATTRIBUTE;
265
    }
266

    
267
    private void flushAttribute() throws XmlStreamException {
268
        if (attributeLocalName != null) {
269
            String value = attributeValue.toString();
270
            try {
271
                writer.writeAttribute(attributeNamespace, attributeLocalName, value);
272
            } catch (XMLStreamException e) {
273
                throw new XmlStreamException(e);
274
            }
275
            attributeNamespace = null;
276
            attributeLocalName = null;
277
            attributeValue.setLength(0);
278
        }
279
    }
280

    
281
    /**
282
     * @see IXmlStreamWriter#writeStartAttribute(javax.xml.namespace.QName)
283
     */
284
    public void writeStartAttribute(QName qname) throws XmlStreamException {
285
        writeStartAttribute(qname.getNamespaceURI(), qname.getLocalPart());
286
    }
287

    
288
    /**
289
     * @see IXmlStreamWriter#writeStartDocument()
290
     */
291
    public void writeStartDocument() throws XmlStreamException {
292
        try {
293
            writer.writeStartDocument();
294
        } catch (XMLStreamException e) {
295
            throw new XmlStreamException(e);
296
        }
297
        lastEvent = EventType.START_DOCUMENT;
298
    }
299

    
300
    /**
301
     * @see IXmlStreamWriter#writeStartElement(java.lang.String, java.lang.String)
302
     */
303
    public void writeStartElement(String namespaceUri, String localName) throws XmlStreamException {
304
        try {
305
            writer.writeStartElement(namespaceUri, localName);
306
        } catch (XMLStreamException e) {
307
            throw new XmlStreamException(e);
308
        }
309
        attributeNamespace = attributeLocalName = null;
310
        lastEvent = lastTagEvent = EventType.START_ELEMENT;
311
    }
312

    
313
    /**
314
     * @see IXmlStreamWriter#writeStartElement(javax.xml.namespace.QName)
315
     */
316
    public void writeStartElement(QName qname) throws XmlStreamException {
317
        writeStartElement(qname.getNamespaceURI(), qname.getLocalPart());
318
    }
319

    
320
    /**
321
     * All writeValue methods delegate to this one, which will call a writeCharacters if its an
322
     * element's value or an attribute if its an attribute value.
323
     * <p>
324
     * Whether the value corresponds to an attribute or an element's one is determined by the
325
     * nullity of the attributeLocalName field.
326
     * </p>
327
     * 
328
     * @param value
329
     * @throws XmlStreamException
330
     */
331
    private void characters(String value) throws XmlStreamException {
332
        if (attributeLocalName == null) {
333
            // an element's value
334
            try {
335
                writer.writeCharacters(value);
336
            } catch (XMLStreamException e) {
337
                throw new XmlStreamException(e);
338
            }
339
        } else {
340
            // got a component of the current attribute value
341
            attributeValue.append(value);
342
        }
343
    }
344

    
345
    /**
346
     * @see IXmlStreamWriter#writeValue(java.lang.String)
347
     */
348
    public void writeValue(String value) throws XmlStreamException {
349
        characters(value);
350
        writtenValueLength++;
351
        lastEvent = EventType.VALUE_STRING;
352
    }
353

    
354
    /**
355
     * @see IXmlStreamWriter#writeValue(char[], int, int)
356
     */
357
    public void writeValue(char[] chars, int offset, int length) throws XmlStreamException {
358
        characters(new String(chars, offset, length));
359
        writtenValueLength++;
360
        lastEvent = EventType.VALUE_STRING;
361
    }
362

    
363
    /**
364
     * @see IXmlStreamWriter#writeValue(int)
365
     */
366
    public void writeValue(int value) throws XmlStreamException {
367
        characters(String.valueOf(value));
368
        writtenValueLength++;
369
        lastEvent = EventType.VALUE_INT;
370
    }
371

    
372
    /**
373
     * @see IXmlStreamWriter#writeValue(long)
374
     */
375
    public void writeValue(long value) throws XmlStreamException {
376
        characters(String.valueOf(value));
377
        writtenValueLength++;
378
        lastEvent = EventType.VALUE_LONG;
379
    }
380

    
381
    /**
382
     * @see IXmlStreamWriter#writeValue(float)
383
     */
384
    public void writeValue(float value) throws XmlStreamException {
385
        characters(String.valueOf(value));
386
        writtenValueLength++;
387
        lastEvent = EventType.VALUE_FLOAT;
388
    }
389

    
390
    /**
391
     * @see IXmlStreamWriter#writeValue(double)
392
     */
393
    public void writeValue(double value) throws XmlStreamException {
394
        characters(String.valueOf(value));
395
        writtenValueLength++;
396
        lastEvent = EventType.VALUE_DOUBLE;
397
    }
398

    
399
    /**
400
     * @see IXmlStreamWriter#writeValue(boolean)
401
     */
402
    public void writeValue(boolean value) throws XmlStreamException {
403
        characters(String.valueOf(value));
404
        writtenValueLength++;
405
        lastEvent = EventType.VALUE_BOOL;
406
    }
407

    
408
    /**
409
     * @see IXmlStreamWriter#writeValue(boolean[], int, int)
410
     */
411
    public void writeValue(boolean[] value, int offset, int length) throws XmlStreamException {
412
        valueBuff.setLength(0);
413
        for (int i = 0; i < length; i++) {
414
            valueBuff.append(value[offset + i]);
415
            if (i < length - 1) {
416
                valueBuff.append(' ');
417
            }
418
        }
419
        characters(valueBuff.toString());
420
        writtenValueLength += length;
421
        lastEvent = EventType.VALUE_BOOL;
422
    }
423

    
424
    /**
425
     * @see IXmlStreamWriter#writeValue(int[], int, int)
426
     */
427
    public void writeValue(int[] value, int offset, int length) throws XmlStreamException {
428
        valueBuff.setLength(0);
429
        for (int i = 0; i < length; i++) {
430
            valueBuff.append(value[offset + i]);
431
            if (i < length - 1) {
432
                valueBuff.append(' ');
433
            }
434
        }
435
        characters(valueBuff.toString());
436
        writtenValueLength += length;
437
        lastEvent = EventType.VALUE_INT;
438
    }
439

    
440
    /**
441
     * @see IXmlStreamWriter#writeValue(long[], int, int)
442
     */
443
    public void writeValue(long[] value, int offset, int length) throws XmlStreamException {
444
        valueBuff.setLength(0);
445
        for (int i = 0; i < length; i++) {
446
            valueBuff.append(value[offset + i]);
447
            if (i < length - 1) {
448
                valueBuff.append(' ');
449
            }
450
        }
451
        characters(valueBuff.toString());
452
        writtenValueLength += length;
453
        lastEvent = EventType.VALUE_LONG;
454
    }
455

    
456
    /**
457
     * @see IXmlStreamWriter#writeValue(float[], int, int)
458
     */
459
    public void writeValue(float[] value, int offset, int length) throws XmlStreamException {
460
        valueBuff.setLength(0);
461
        for (int i = 0; i < length; i++) {
462
            valueBuff.append(value[offset + i]);
463
            if (i < length - 1) {
464
                valueBuff.append(' ');
465
            }
466
        }
467
        characters(valueBuff.toString());
468
        writtenValueLength += length;
469
        lastEvent = EventType.VALUE_FLOAT;
470
    }
471

    
472
    /**
473
     * @see IXmlStreamWriter#writeValue(double[], int, int)
474
     */
475
    public void writeValue(double[] value, int offset, int length) throws XmlStreamException {
476
        valueBuff.setLength(0);
477
        for (int i = 0; i < length; i++) {
478
            valueBuff.append(value[offset + i]);
479
            if (i < length - 1) {
480
                valueBuff.append(',');
481
            }
482
        }
483
        valueBuff.append(' ');
484
        characters(valueBuff.toString());
485
        writtenValueLength += length;
486
        lastEvent = EventType.VALUE_DOUBLE;
487
    }
488

    
489
}