Revision 183

View differences:

org.gvsig.xmlpull/library/trunk/org.gvsig.xmlpull/org.gvsig.xmlpull.lib/org.gvsig.xml.lib.api/src/main/java/org/gvsig/xml/lib/api/XMLManager.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2010 {Iver T.I.}   {Task}
26
*/
27
 
28
package org.gvsig.xml.lib.api;
29

  
30
import java.io.InputStream;
31
import java.io.OutputStream;
32

  
33
import org.gvsig.xml.lib.api.stream.IXmlStreamReader;
34
import org.gvsig.xml.lib.api.stream.IXmlStreamWriter;
35
import org.gvsig.xml.lib.api.stream.XmlStreamException;
36

  
37
/**
38
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
39
 */
40
public interface XMLManager {
41

  
42
	public IXmlStreamReader createStreamReader(String mimeType, InputStream is) throws XmlStreamException, IllegalArgumentException;
43
	
44
	public IXmlStreamWriter createStreamWriter(String mimeType, OutputStream os) throws XmlStreamException, IllegalArgumentException;
45

  
46
}
47

  
0 48

  
org.gvsig.xmlpull/library/trunk/org.gvsig.xmlpull/org.gvsig.xmlpull.lib/org.gvsig.xml.lib.api/src/main/java/org/gvsig/xml/lib/api/stream/IXmlStreamWriter.java
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.xml.lib.api.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
}
0 521

  
org.gvsig.xmlpull/library/trunk/org.gvsig.xmlpull/org.gvsig.xmlpull.lib/org.gvsig.xml.lib.api/src/main/java/org/gvsig/xml/lib/api/stream/EventType.java
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.xml.lib.api.stream;
32

  
33

  
34
/**
35
 * The EventType enum defines the possible parse events a {@link IXmlStreamReader} may return during
36
 * each call to {@code next()}.
37
 * <p>
38
 * These are high level events and may not map one to one to the low level Binary XML format tokens.
39
 * This is so to better reflect the problem domain for this high level API, letting the low level
40
 * treatment of specific bxml tokens to the implementation criteria.
41
 * </p>
42
 * <p>
43
 * In general lines, it could be said that the following EventType to TokenType mappings are
44
 * defined: <table>
45
 * <tr>
46
 * <th>EventType</th>
47
 * <th>TokenType</th>
48
 * </tr>
49
 * <tr>
50
 * <td>NONE</td>
51
 * <td>Header</td>
52
 * </tr>
53
 * <tr>
54
 * <td>START_DOCUMENT</td>
55
 * <td>XmlDeclaration, if present, forced otherwise in order to always return a START_DOCUMENT
56
 * event</td>
57
 * </tr>
58
 * <tr>
59
 * <td>END_DOCUMENT</td>
60
 * <td>Trailer</td>
61
 * </tr>
62
 * <tr>
63
 * <td>START_ELEMENT</td>
64
 * <td>EmptyElement, EmptyAttrElement, ContentElement, ContentAttrElement</td>
65
 * </tr>
66
 * <tr>
67
 * <td>END_ELEMENT</td>
68
 * <td>ElementEnd, or forced when EmptyElement or EmptyAttrElement</td>
69
 * </tr>
70
 * <tr>
71
 * <td>ATTRIBUTE</td>
72
 * <td>AttributeStart, only used for writing</td>
73
 * </tr>
74
 * <tr>
75
 * <td>ATTRIBUTES_END</td>
76
 * <td>AttributeListEnd, only used for writing</td>
77
 * </tr>
78
 * <tr>
79
 * <td>COMMENT</td>
80
 * <td>Comment</td>
81
 * </tr>
82
 * <tr>
83
 * <td>SPACE</td>
84
 * <td>WhiteSpace</td>
85
 * </tr>
86
 * <tr>
87
 * <td>VALUE_STRING, VALUE_BOOL, VALUE_BYTE, VALUE_INT, VALUE_LONG, VALUE_FLOAT, VALUE_DOUBLE</td>
88
 * <td>CharContent token, followed by the specific value type</td>
89
 * </tr>
90
 * </table>
91
 * </p>
92
 * 
93
 * @author Gabriel Roldan (TOPP)
94
 * @version $Id: EventType.java 21945 2008-06-30 14:04:53Z jpiera $
95
 * @since 1.0
96
 */
97
public final class EventType {
98

  
99
    private boolean isValueEvent;
100

  
101
    private int code;
102

  
103
    /**
104
     * Private default constructor inforces the use of this class by its public static members only
105
     */
106
    private EventType(final int code) {
107
        this(code, false);
108
    }
109

  
110
    private EventType(final int code, boolean isValue) {
111
        this.code = code;
112
        this.isValueEvent = isValue;
113
    }
114

  
115
    public static final int NONE_CODE = 0;
116

  
117
    /**
118
     * Null object to indicate either a document has not started being parsed yet, or a document has
119
     * not started being written yet.
120
     */
121
    public static final EventType NONE = new EventType(NONE_CODE);
122

  
123
    public static final int START_DOCUMENT_CODE = 1;
124
    /**
125
     * Indicates the parser is positioned at the start of the document. The next call to
126
     * {@link IXmlStreamReader#next()} shall return either {@link #START_ELEMENT}, {@link #COMMENT}
127
     * or {@link #END_DOCUMENT} if the document is empty.
128
     */
129
    public static final EventType START_DOCUMENT = new EventType(START_DOCUMENT_CODE);
130

  
131
    public static final int END_DOCUMENT_CODE = 2;
132

  
133
    /**
134
     * Indicates the parser has reached the end of the bxml document
135
     */
136
    public static final EventType END_DOCUMENT = new EventType(END_DOCUMENT_CODE);
137

  
138
    public static final int START_ELEMENT_CODE = 3;
139

  
140
    /**
141
     * Signals the opening of an element tag.
142
     */
143
    public static final EventType START_ELEMENT = new EventType(START_ELEMENT_CODE);
144

  
145
    public static final int END_ELEMENT_CODE = 4;
146

  
147
    /**
148
     * Signals the end of the current element. No content elements such as {@code <element/>} and
149
     * {@code <element att="attval"/>} will be notified by an START_ELEMENT and END_ELEMENT event
150
     * with no value event in between, to preserve semantic equivalence with the
151
     * {@code <element></element>} tag sequence.
152
     */
153
    public static final EventType END_ELEMENT = new EventType(END_ELEMENT_CODE);
154

  
155
    public static final int VALUE_BOOL_CODE = 5;
156

  
157
    /**
158
     * Content event that indicates the parser is at a value token whose content is either a Java
159
     * boolean or an array of booleans.
160
     */
161
    public static final EventType VALUE_BOOL = new EventType(VALUE_BOOL_CODE, true);
162

  
163
    public static final int VALUE_BYTE_CODE = 6;
164

  
165
    /**
166
     * Content event that indicates the parser is at a value token whose content is either a Java
167
     * byte or an array of bytes.
168
     */
169
    public static final EventType VALUE_BYTE = new EventType(VALUE_BYTE_CODE, true);
170

  
171
    public static final int VALUE_INT_CODE = 7;
172

  
173
    /**
174
     * Content event that indicates the parser is at a value token whose content is either a Java
175
     * integer or an array of integers. For the sake of simplicity, the low level {@code SmallNum},
176
     * {@code Short}, {@code UShort}, and {@code Int} value types are mapped to this event type.
177
     */
178
    public static final EventType VALUE_INT = new EventType(VALUE_INT_CODE, true);
179

  
180
    public static final int VALUE_LONG_CODE = 8;
181

  
182
    /**
183
     * Content event that indicates the parser is at a value token whose content is either a Java
184
     * long or an array of longs.
185
     */
186
    public static final EventType VALUE_LONG = new EventType(VALUE_LONG_CODE, true);
187

  
188
    public static final int VALUE_FLOAT_CODE = 9;
189

  
190
    /**
191
     * Content event that indicates the parser is at a value token whose content is either a Java
192
     * float or an array of floats.
193
     */
194
    public static final EventType VALUE_FLOAT = new EventType(VALUE_FLOAT_CODE, true);
195

  
196
    public static final int VALUE_DOUBLE_CODE = 10;
197

  
198
    /**
199
     * Content event that indicates the parser is at a value token whose content is either a Java
200
     * double or an array of double.
201
     */
202
    public static final EventType VALUE_DOUBLE = new EventType(VALUE_DOUBLE_CODE, true);
203

  
204
    public static final int VALUE_STRING_CODE = 11;
205
    /**
206
     * Content event that indicates the parser is at a value token whose content is a Java String.
207
     * There are no such a structure as an array of Strings in the BXML spec.
208
     */
209
    public static final EventType VALUE_STRING = new EventType(VALUE_STRING_CODE, true);
210

  
211
    public static final int VALUE_CDATA_CODE = 12;
212

  
213
    /**
214
     * This is event equivalent to the {@code "<![CDATA[content]]>"} structure in textual XML,
215
     * signaled by the CDataSectionToken. This token is essentially equivalent to the
216
     * CharContentToken, except that its use may be regarded as a hint to a translator to regenerate
217
     * a CDATA section in textual XML.
218
     */
219
    public static final EventType VALUE_CDATA = new EventType(VALUE_CDATA_CODE, true);
220

  
221
    public static final int VALUE_SPACE_CODE = 13;
222

  
223
    /**
224
     * Content event that signals the precense of a WhiteSpace token. That is, a potentially
225
     * insignificant sequence of white space or newline characters
226
     */
227
    public static final EventType SPACE = new EventType(VALUE_SPACE_CODE, true);
228

  
229
    public static final int COMMENT_CODE = 14;
230

  
231
    /**
232
     * Indicates the parser is at a XML comment token
233
     */
234
    public static final EventType COMMENT = new EventType(COMMENT_CODE);
235

  
236
    public static final int ATTRIBUTE_CODE = 15;
237

  
238
    /**
239
     * Used only for writing attributes.
240
     * 
241
     * @see IXmlStreamWriter#writeStartAttribute(String, String)
242
     */
243
    public static final EventType ATTRIBUTE = new EventType(ATTRIBUTE_CODE);
244

  
245
    public static final int ATTRIBUTES_END_CODE = 16;
246

  
247
    /**
248
     * Used only for writing attributes.
249
     * 
250
     * @see IXmlStreamWriter#writeEndAttributes()
251
     */
252
    public static final EventType ATTRIBUTES_END = new EventType(ATTRIBUTES_END_CODE);
253

  
254
    public int getCode() {
255
        return code;
256
    }
257

  
258
    /**
259
     * Convenience method that returns whether this EventType represents a value token
260
     * 
261
     * @return {@code true} if {@code eventType} is one of
262
     *         {@code VALUE_BOOL, VALUE_BYTE, VALUE_INT, VALUE_LONG, VALUE_FLOAT, VALUE_DOUBLE,
263
     *         VALUE_STRING, VALUE_CDATA, SPACE}
264
     */
265
    public boolean isValue() {
266
        return isValueEvent;
267
    }
268

  
269
    /**
270
     * Convenience method that returns whether this EventType represents a tag element
271
     * 
272
     * @return {@code true} if {@code eventType} is one of {@code START_ELEMENT, END_ELEMENT}
273
     */
274
    public boolean isTag() {
275
        return START_ELEMENT == this || END_ELEMENT == this;
276
    }
277
}
0 278

  
org.gvsig.xmlpull/library/trunk/org.gvsig.xmlpull/org.gvsig.xmlpull.lib/org.gvsig.xml.lib.api/src/main/java/org/gvsig/xml/lib/api/stream/AbstractXmlStreamReaderWrapper.java
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.xml.lib.api.stream;
32

  
33
import javax.xml.namespace.QName;
34

  
35
/**
36
 * Plain wrapper for an {@link IXmlStreamReader} aimed at easying the creation of IXmlStreamReader
37
 * decorators.
38
 * <p>
39
 * This class is a pure abstract wrapper, so all methods delegate to the wrapped object.
40
 * </p>
41
 * 
42
 * @author Gabriel Roldan (TOPP)
43
 * @version $Id: AbstractXmlStreamReaderWrapper.java 20425 2008-05-05 14:24:58Z groldan $
44
 */
45
public abstract class AbstractXmlStreamReaderWrapper implements IXmlStreamReader {
46

  
47
    private IXmlStreamReader wrapped;
48

  
49
    protected AbstractXmlStreamReaderWrapper() {
50
        // do nothing
51
    }
52

  
53
    protected AbstractXmlStreamReaderWrapper(IXmlStreamReader wrapped) {
54
        setWrapped(wrapped);
55
    }
56

  
57
    public void setWrapped(IXmlStreamReader wrapped) {
58
        this.wrapped = wrapped;
59
    }
60
    
61
    /**
62
     * @see org.gvsig.gpe.xml.stream.IXmlStreamReader#getAttributeCount()
63
     */
64
    public int getAttributeCount() throws XmlStreamException {
65
        return wrapped.getAttributeCount();
66
    }
67

  
68
    /**
69
     * @see org.gvsig.gpe.xml.stream.IXmlStreamReader#getAttributeName(int)
70
     */
71
    public QName getAttributeName(int i) throws XmlStreamException {
72
        return wrapped.getAttributeName(i);
73
    }
74

  
75
    /**
76
     * @param i
77
     * @see org.gvsig.gpe.xml.stream.IXmlStreamReader#getAttributeValue(int)
78
     */
79
    public String getAttributeValue(int i) throws XmlStreamException {
80
        return wrapped.getAttributeValue(i);
81
    }
82

  
83
    /**
84
     * @see org.gvsig.gpe.xml.stream.IXmlStreamReader#getEventType()
85
     */
86
    public int getEventType() throws XmlStreamException {
87
        return wrapped.getEventType();
88
    }
89

  
90
    /**
91
     * @see org.gvsig.gpe.xml.stream.IXmlStreamReader#getName()
92
     */
93
    public QName getName() throws XmlStreamException {
94
        return wrapped.getName();
95
    }
96

  
97
    /**
98
     * @see org.gvsig.gpe.xml.stream.IXmlStreamReader#getText()
99
     */
100
    public String getText() throws XmlStreamException {
101
        return wrapped.getText();
102
    }
103

  
104
    /**
105
     * @see org.gvsig.gpe.xml.stream.IXmlStreamReader#isWhitespace()
106
     */
107
    public boolean isWhitespace() throws XmlStreamException {
108
        return wrapped.isWhitespace();
109
    }
110

  
111
    /**
112
     * @see org.gvsig.gpe.xml.stream.IXmlStreamReader#next()
113
     */
114
    public int next() throws XmlStreamException {
115
        return wrapped.next();
116
    }
117

  
118
    /**
119
     * @see org.gvsig.gpe.xml.stream.IXmlStreamReader#nextTag()
120
     */
121
    public int nextTag() throws XmlStreamException {
122
        return wrapped.nextTag();
123
    }
124

  
125
}
0 126

  
org.gvsig.xmlpull/library/trunk/org.gvsig.xmlpull/org.gvsig.xmlpull.lib/org.gvsig.xml.lib.api/src/main/java/org/gvsig/xml/lib/api/stream/IXmlStreamReader.java
1
package org.gvsig.xml.lib.api.stream;
2

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

  
49
import javax.xml.namespace.QName;
50

  
51
/**
52
 * Represents an abstraction layer over a concrete XML parsing technology.
53
 * <p>
54
 * This interface provides a pull like approach to parsing xml formatted documents for the libGPE
55
 * library, yet allowing to interchange implementations which can be based, for example, in XML SAX,
56
 * XML Pull, Binary XML, etc.
57
 * </p>
58
 * <p>
59
 * Note at this time this interface contract is made just of the methods from XmlPullParser that
60
 * where already used so far in the libGPE-GML and libGPE-KML modules. The intention is to first
61
 * introduce this abstraction layer and then evolve it as needed to support an intermediate level of
62
 * abstraction between actual xml parsing and {@link IGPEContentHandler}, while keeping the unit
63
 * tests passing all the time. So this is work in progress and expected to change dramatically,
64
 * though ensuring no functional breackages in any time.
65
 * </p>
66
 * 
67
 * @author Gabriel Roldan (TOPP)
68
 * @version $Id: IXmlStreamReader.java 19593 2008-03-12 17:23:30Z groldan $
69
 */
70
public interface IXmlStreamReader {
71

  
72
    /**
73
     * Indicates an event is a start element
74
     * 
75
     * @see javax.xml.stream.events.StartElement
76
     */
77
    public static final int START_ELEMENT = 1;
78
    /**
79
     * Indicates an event is an end element
80
     * 
81
     * @see javax.xml.stream.events.EndElement
82
     */
83
    public static final int END_ELEMENT = 2;
84
    /**
85
     * Indicates an event is a processing instruction
86
     * 
87
     * @see javax.xml.stream.events.ProcessingInstruction
88
     */
89
    public static final int PROCESSING_INSTRUCTION = 3;
90

  
91
    /**
92
     * Indicates an event is characters
93
     * 
94
     * @see javax.xml.stream.events.Characters
95
     */
96
    public static final int CHARACTERS = 4;
97

  
98
    /**
99
     * Indicates an event is a comment
100
     * 
101
     * @see javax.xml.stream.events.Comment
102
     */
103
    public static final int COMMENT = 5;
104

  
105
    /**
106
     * The characters are white space (see [XML], 2.10 "White Space Handling"). Events are only
107
     * reported as SPACE if they are ignorable white space. Otherwise they are reported as
108
     * CHARACTERS.
109
     * 
110
     * @see javax.xml.stream.events.Characters
111
     */
112
    public static final int SPACE = 6;
113

  
114
    /**
115
     * Indicates an event is a start document
116
     * 
117
     * @see javax.xml.stream.events.StartDocument
118
     */
119
    public static final int START_DOCUMENT = 7;
120

  
121
    /**
122
     * Indicates an event is an end document
123
     * 
124
     * @see javax.xml.stream.events.EndDocument
125
     */
126
    public static final int END_DOCUMENT = 8;
127

  
128
    /**
129
     * Indicates an event is an entity reference
130
     * 
131
     * @see javax.xml.stream.events.EntityReference
132
     */
133
    public static final int ENTITY_REFERENCE = 9;
134

  
135
    /**
136
     * Indicates an event is an attribute
137
     * 
138
     * @see javax.xml.stream.events.Attribute
139
     */
140
    //public static final int ATTRIBUTE = 10;
141

  
142
    /**
143
     * Indicates an event is a DTD
144
     * 
145
     * @see javax.xml.stream.events.DTD
146
     */
147
    public static final int DTD = 11;
148

  
149
    /**
150
     * Indicates an event is a CDATA section
151
     * 
152
     * @see javax.xml.stream.events.Characters
153
     */
154
    public static final int CDATA = 12;
155

  
156
    /**
157
     * Indicates the event is a namespace declaration
158
     * 
159
     * @see javax.xml.stream.events.Namespace
160
     */
161
    public static final int NAMESPACE = 13;
162

  
163
    /**
164
     * Indicates a Notation
165
     * 
166
     * @see javax.xml.stream.events.NotationDeclaration
167
     */
168
    public static final int NOTATION_DECLARATION = 14;
169

  
170
    /**
171
     * Indicates a Entity Declaration
172
     * 
173
     * @see javax.xml.stream.events.NotationDeclaration
174
     */
175
    public static final int ENTITY_DECLARATION = 15;
176

  
177
    /**
178
     * @return
179
     * @throws IllegalStateException
180
     */
181
    int getAttributeCount() throws XmlStreamException;
182

  
183
    QName getAttributeName(int i) throws XmlStreamException;
184

  
185
    /**
186
     * Returns the complete (coalesced) value for the attribute at index {@code i}
187
     * @param i
188
     * @return
189
     * @throws XmlStreamException
190
     */
191
    String getAttributeValue(int i) throws XmlStreamException;
192

  
193
    /**
194
     * Returns the qualified name (with prefix if applicable) for the current element.
195
     * 
196
     * @return the qualified name for the current START_ELEMENT or END_ELEMENT event, or
197
     *         <code>null</code> if the current event is not a START_ELEMENT or END_ELEMENT.
198
     * @throws XmlStreamException
199
     */
200
    QName getName() throws XmlStreamException;
201

  
202
    int getEventType() throws XmlStreamException;
203

  
204
    String getText() throws XmlStreamException;
205

  
206
    int next() throws XmlStreamException;
207

  
208
    int nextTag() throws XmlStreamException;
209

  
210
    boolean isWhitespace() throws XmlStreamException;
211
}
0 212

  
org.gvsig.xmlpull/library/trunk/org.gvsig.xmlpull/org.gvsig.xmlpull.lib/org.gvsig.xml.lib.api/src/main/java/org/gvsig/xml/lib/api/stream/IXmlStreamWriterFactory.java
1
package org.gvsig.xml.lib.api.stream;
2

  
3
import java.io.OutputStream;
4

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

  
46
/**
47
 * 
48
 */
49
public interface IXmlStreamWriterFactory {
50

  
51
    boolean canWrite(final String mimeType);
52

  
53
    IXmlStreamWriter createWriter(final String mimeType, final OutputStream out)
54
            throws XmlStreamException, IllegalArgumentException;
55
}
0 56

  
org.gvsig.xmlpull/library/trunk/org.gvsig.xmlpull/org.gvsig.xmlpull.lib/org.gvsig.xml.lib.api/src/main/java/org/gvsig/xml/lib/api/stream/XmlStreamException.java
1
package org.gvsig.xml.lib.api.stream;
2

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

  
49
import java.io.IOException;
50

  
51
/**
52
 * Signals either a parsing or io error ocurred while scanning or writing an xml formatted document.
53
 * 
54
 * @author Gabriel Roldan (TOPP)
55
 * @version $Id: XmlStreamException.java 19593 2008-03-12 17:23:30Z groldan $
56
 */
57
public class XmlStreamException extends IOException {
58

  
59
    public XmlStreamException(String message) {
60
        this(message, null);
61
    }
62

  
63
    public XmlStreamException(String message, Throwable cause) {
64
        super(message);
65
        super.initCause(cause);
66
    }
67

  
68
    public XmlStreamException(Throwable cause) {
69
        this(null, cause);
70
    }
71

  
72
}
0 73

  
org.gvsig.xmlpull/library/trunk/org.gvsig.xmlpull/org.gvsig.xmlpull.lib/org.gvsig.xml.lib.api/src/main/java/org/gvsig/xml/lib/api/stream/StreamUtils.java
1
package org.gvsig.xml.lib.api.stream;
2

  
3
import java.io.IOException;
4
import java.io.InputStream;
5

  
6
class StreamUtils {
7

  
8
    private StreamUtils() {
9
        // do nothing
10
    }
11

  
12
    /**
13
     * @param _is
14
     * @return Returns the encoding or UTF-8 encoding by default.
15
     * @throws IOException
16
     */
17
    public static String getEncoding(final InputStream _is) throws IOException {
18
        String encoding = "UTF-8";
19
        int srcCount = 0;
20
        char[] srcBuf = new char[128];
21

  
22
        // read four bytes
23
        int chk = 0;
24
        try {
25
            while (srcCount < 4) {
26
                int i = _is.read();
27
                if (i == -1)
28
                    break;
29
                chk = (chk << 8) | i;
30
                srcBuf[srcCount++] = (char) i;
31
            }
32

  
33
            if (srcCount == 4) {
34
                switch (chk) {
35
                case 0x00000FEFF:
36
                    encoding = "UTF-32BE";
37
                    srcCount = 0;
38
                    break;
39

  
40
                case 0x0FFFE0000:
41
                    encoding = "UTF-32LE";
42
                    srcCount = 0;
43
                    break;
44

  
45
                case 0x03c:
46
                    encoding = "UTF-32BE";
47
                    srcBuf[0] = '<';
48
                    srcCount = 1;
49
                    break;
50

  
51
                case 0x03c000000:
52
                    encoding = "UTF-32LE";
53
                    srcBuf[0] = '<';
54
                    srcCount = 1;
55
                    break;
56

  
57
                case 0x0003c003f:
58
                    encoding = "UTF-16BE";
59
                    srcBuf[0] = '<';
60
                    srcBuf[1] = '?';
61
                    srcCount = 2;
62
                    break;
63

  
64
                case 0x03c003f00:
65
                    encoding = "UTF-16LE";
66
                    srcBuf[0] = '<';
67
                    srcBuf[1] = '?';
68
                    srcCount = 2;
69
                    break;
70

  
71
                case 0x03c3f786d:
72
                    while (true) {
73
                        int i = _is.read();
74
                        if (i == -1)
75
                            break;
76
                        srcBuf[srcCount++] = (char) i;
77
                        if (i == '>') {
78
                            String s = new String(srcBuf, 0, srcCount);
79
                            int i0 = s.indexOf("encoding");
80
                            if (i0 != -1) {
81
                                while (s.charAt(i0) != '"' && s.charAt(i0) != '\'')
82
                                    i0++;
83
                                char deli = s.charAt(i0++);
84
                                int i1 = s.indexOf(deli, i0);
85
                                encoding = s.substring(i0, i1);
86
                            }
87
                            break;
88
                        }
89
                    }
90

  
91
                default:
92
                    if ((chk & 0x0ffff0000) == 0x0FEFF0000) {
93
                        encoding = "UTF-16BE";
94
                        srcBuf[0] = (char) ((srcBuf[2] << 8) | srcBuf[3]);
95
                        srcCount = 1;
96
                    } else if ((chk & 0x0ffff0000) == 0x0fffe0000) {
97
                        encoding = "UTF-16LE";
98
                        srcBuf[0] = (char) ((srcBuf[3] << 8) | srcBuf[2]);
99
                        srcCount = 1;
100
                    } else if ((chk & 0x0ffffff00) == 0x0EFBBBF00) {
101
                        encoding = "UTF-8";
102
                        srcBuf[0] = srcBuf[3];
103
                        srcCount = 1;
104
                    }
105
                }
106
            }
107
        } catch (IOException ex) {
108
            return null;
109
        }
110
        return encoding;
111
    }
112

  
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff