Statistics
| Revision:

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

History | View | Annotate | Download (14.3 KB)

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

    
3
import java.io.FileWriter;
4
import java.io.OutputStream;
5
import java.io.OutputStreamWriter;
6

    
7
import javax.xml.namespace.QName;
8
import javax.xml.stream.XMLOutputFactory;
9
import javax.xml.stream.XMLStreamException;
10
import javax.xml.stream.XMLStreamWriter;
11

    
12
import org.gvsig.gpe.xml.stream.EventType;
13
import org.gvsig.gpe.xml.stream.IXmlStreamWriter;
14
import org.gvsig.gpe.xml.stream.XmlStreamException;
15

    
16
import com.bea.xml.stream.XMLWriterBase;
17

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

    
59
/**
60
 * An XML stream writer that uses a StAX writer under the hood.
61
 */
62
public class StaxXmlStreamWriter implements IXmlStreamWriter {
63

    
64
    private XMLStreamWriter writer;
65

    
66
    private EventType lastTagEvent;
67
    private EventType lastEvent;
68

    
69
    private long valueLength;
70

    
71
    private long writtenValueLength;
72

    
73
    /**
74
     * nsuri for the attribute currently being processed. null if an attribute is not being
75
     * processed
76
     */
77
    private String attributeNamespace;
78

    
79
    /**
80
     * local name for the attribute currently being processed. null if an attribute is not being
81
     * processed
82
     */
83
    private String attributeLocalName;
84

    
85
    private StringBuffer attributeValue = new StringBuffer();
86

    
87
    private StringBuffer valueBuff = new StringBuffer();
88

    
89
    public StaxXmlStreamWriter(final OutputStream out) throws XmlStreamException {
90
        //final XMLOutputFactory staxFactory = XMLOutputFactory.newInstance();
91
        //TODO: Change the classloader of gvSIG to use SPI
92
        //try {
93
                this.writer = new XMLWriterBase(new OutputStreamWriter(out));          
94
        //} catch (XMLStreamException e) {
95
        //   throw new XmlStreamException(e);
96
        //}
97
        lastEvent = EventType.NONE;
98
    }
99

    
100
    /**
101
     * @see IXmlStreamWriter#close()
102
     */
103
    public void close() throws XmlStreamException {
104
        if (writer != null) {
105
            try {
106
                writer.close();
107
            } catch (XMLStreamException e) {
108
                throw new XmlStreamException(e);
109
            }
110
            writer = null;
111
        }
112
    }
113

    
114
    /**
115
     * @see IXmlStreamWriter#isOpen()
116
     */
117
    public boolean isOpen() {
118
        return writer != null;
119
    }
120

    
121
    /**
122
     * @throws XmlStreamException 
123
     * @see IXmlStreamWriter#setDefaultNamespace(java.lang.String)
124
     */
125
    public void setDefaultNamespace(String defaultNamespaceUri) throws XmlStreamException {
126
              try {
127
                          writer.setDefaultNamespace(defaultNamespaceUri);
128
                  } catch (XMLStreamException e) {
129
                           throw new XmlStreamException(e);
130
                  } 
131
    }
132

    
133
    /**
134
     * @throws XmlStreamException 
135
     * @see IXmlStreamWriter#setPrefix(java.lang.String, java.lang.String)
136
     */
137
    public void setPrefix(String prefix, String namespaceUri) throws XmlStreamException {
138
        try {
139
                        writer.setPrefix(prefix, namespaceUri);
140
                } catch (XMLStreamException e) {
141
                         throw new XmlStreamException(e);
142
                }            
143
    }
144

    
145
    /**
146
     * @see IXmlStreamWriter#setSchemaLocation(java.lang.String, java.lang.String)
147
     */
148
    public void setSchemaLocation(String namespaceUri, String schemaLocationUri) {
149
               throw new UnsupportedOperationException("Not yet implemented");
150
    }
151

    
152
    /**
153
     * @see IXmlStreamWriter#flush()
154
     */
155
    public void flush() throws XmlStreamException {
156
        try {
157
            writer.flush();
158
        } catch (XMLStreamException e) {
159
            throw new XmlStreamException(e);
160
        }
161
    }
162

    
163
    /**
164
     * @see IXmlStreamWriter#getLastEvent()
165
     */
166
    public EventType getLastEvent() {
167
        return lastEvent;
168
    }
169

    
170
    /**
171
     * @see IXmlStreamWriter#getLastTagEvent()
172
     */
173
    public EventType getLastTagEvent() {
174
        return lastTagEvent;
175
    }
176

    
177
    /**
178
     * @see IXmlStreamWriter#getTagDeep()
179
     */
180
    public int getTagDeep() {
181
        throw new UnsupportedOperationException("Not yet implemented");
182
    }
183

    
184
    /**
185
     * @see IXmlStreamWriter#getValueLength()
186
     */
187
    public long getValueLength() {
188
        return valueLength;
189
    }
190

    
191
    /**
192
     * @see IXmlStreamWriter#getWrittenValueCount()
193
     */
194
    public long getWrittenValueCount() {
195
        return writtenValueLength;
196
    }
197

    
198
    /**
199
     * @see IXmlStreamWriter#startArray(org.gvsig.gpe.xml.stream.EventType, int)
200
     */
201
    public void startArray(EventType valueType, int arrayLength) throws XmlStreamException {
202
        valueLength = arrayLength;
203
        lastEvent = valueType;
204
        writtenValueLength = 0;
205
    }
206

    
207
    /**
208
     * @see IXmlStreamWriter#endArray()
209
     */
210
    public void endArray() throws XmlStreamException {
211
        // nothing to do
212
    }
213

    
214
    /**
215
     * @see IXmlStreamWriter#writeComment(java.lang.String)
216
     */
217
    public void writeComment(String commentContent) throws XmlStreamException {
218
        try {
219
            writer.writeComment(commentContent);
220
        } catch (XMLStreamException e) {
221
            throw new XmlStreamException(e);
222
        }
223
        lastEvent = EventType.COMMENT;
224
    }
225

    
226
    /**
227
     * @see IXmlStreamWriter#writeEndAttributes()
228
     */
229
    public void writeEndAttributes() throws XmlStreamException {
230
        flushAttribute();
231
        lastEvent = EventType.ATTRIBUTES_END;
232
    }
233

    
234
    /**
235
     * @see IXmlStreamWriter#writeEndDocument()
236
     */
237
    public void writeEndDocument() throws XmlStreamException {
238
        try {
239
            writer.writeEndDocument();
240
        } catch (XMLStreamException e) {
241
            throw new XmlStreamException(e);
242
        }
243
        lastEvent = EventType.END_DOCUMENT;
244
    }
245

    
246
    /**
247
     * @see IXmlStreamWriter#writeEndElement()
248
     */
249
    public void writeEndElement() throws XmlStreamException {
250
        try {
251
            writer.writeEndElement();
252
        } catch (XMLStreamException e) {
253
            throw new XmlStreamException(e);
254
        }
255
        lastEvent = lastTagEvent = EventType.END_ELEMENT;
256
    }
257

    
258
    /**
259
     * @see IXmlStreamWriter#writeStartAttribute(java.lang.String, java.lang.String)
260
     */
261
    public void writeStartAttribute(String namespaceUri, String localName)
262
            throws XmlStreamException {
263
        // do we already have an attribute in course?
264
        flushAttribute();
265

    
266
        attributeNamespace = namespaceUri;
267
        attributeLocalName = localName;
268
        attributeValue.setLength(0);
269
        lastEvent = EventType.ATTRIBUTE;
270
    }
271

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

    
286
    /**
287
     * @see IXmlStreamWriter#writeStartAttribute(javax.xml.namespace.QName)
288
     */
289
    public void writeStartAttribute(QName qname) throws XmlStreamException {
290
        writeStartAttribute(qname.getNamespaceURI(), qname.getLocalPart());
291
    }
292

    
293
    /**
294
     * @see IXmlStreamWriter#writeStartDocument()
295
     */
296
    public void writeStartDocument() throws XmlStreamException {
297
        try {
298
            writer.writeStartDocument();
299
        } catch (XMLStreamException e) {
300
            throw new XmlStreamException(e);
301
        }
302
        lastEvent = EventType.START_DOCUMENT;
303
    }
304

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

    
318
    /**
319
     * @see IXmlStreamWriter#writeStartElement(javax.xml.namespace.QName)
320
     */
321
    public void writeStartElement(QName qname) throws XmlStreamException {
322
        writeStartElement(qname.getNamespaceURI(), qname.getLocalPart());
323
    }
324

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

    
350
    /**
351
     * @see IXmlStreamWriter#writeValue(java.lang.String)
352
     */
353
    public void writeValue(String value) throws XmlStreamException {
354
        characters(value);
355
        writtenValueLength++;
356
        lastEvent = EventType.VALUE_STRING;
357
    }
358

    
359
    /**
360
     * @see IXmlStreamWriter#writeValue(char[], int, int)
361
     */
362
    public void writeValue(char[] chars, int offset, int length) throws XmlStreamException {
363
        characters(new String(chars, offset, length));
364
        writtenValueLength++;
365
        lastEvent = EventType.VALUE_STRING;
366
    }
367

    
368
    /**
369
     * @see IXmlStreamWriter#writeValue(int)
370
     */
371
    public void writeValue(int value) throws XmlStreamException {
372
        characters(String.valueOf(value));
373
        writtenValueLength++;
374
        lastEvent = EventType.VALUE_INT;
375
    }
376

    
377
    /**
378
     * @see IXmlStreamWriter#writeValue(long)
379
     */
380
    public void writeValue(long value) throws XmlStreamException {
381
        characters(String.valueOf(value));
382
        writtenValueLength++;
383
        lastEvent = EventType.VALUE_LONG;
384
    }
385

    
386
    /**
387
     * @see IXmlStreamWriter#writeValue(float)
388
     */
389
    public void writeValue(float value) throws XmlStreamException {
390
        characters(String.valueOf(value));
391
        writtenValueLength++;
392
        lastEvent = EventType.VALUE_FLOAT;
393
    }
394

    
395
    /**
396
     * @see IXmlStreamWriter#writeValue(double)
397
     */
398
    public void writeValue(double value) throws XmlStreamException {
399
        characters(String.valueOf(value));
400
        writtenValueLength++;
401
        lastEvent = EventType.VALUE_DOUBLE;
402
    }
403

    
404
    /**
405
     * @see IXmlStreamWriter#writeValue(boolean)
406
     */
407
    public void writeValue(boolean value) throws XmlStreamException {
408
        characters(String.valueOf(value));
409
        writtenValueLength++;
410
        lastEvent = EventType.VALUE_BOOL;
411
    }
412

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

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

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

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

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

    
494
}