Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libGPE-GML / src / org / gvsig / gpe / gml / writer / GPEGmlWriterHandlerImplementor.java @ 28113

History | View | Annotate | Download (16.4 KB)

1
package org.gvsig.gpe.gml.writer;
2

    
3
import java.io.IOException;
4
import java.util.Stack;
5

    
6
import org.gvsig.gpe.gml.utils.GMLTags;
7
import org.gvsig.gpe.gml.writer.profiles.IWriterProfile;
8
import org.gvsig.gpe.writer.ICoordinateSequence;
9
import org.gvsig.gpe.xml.XmlProperties;
10
import org.gvsig.gpe.xml.stream.XmlStreamException;
11
import org.gvsig.gpe.xml.utils.XMLTags;
12
import org.gvsig.gpe.xml.writer.GPEXmlWriterHandlerImplementor;
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
/* CVS MESSAGES:
55
 *
56
 * $Id: GPEGmlWriterHandlerImplementor.java 350 2008-01-09 12:53:07Z jpiera $
57
 * $Log$
58
 * Revision 1.1  2007/06/29 12:19:34  jorpiell
59
 * The schema validation is made independently of the concrete writer
60
 *
61
 * Revision 1.21  2007/06/28 13:05:09  jorpiell
62
 * The Qname has been updated to the 1.5 JVM machine. The schema validation is made in the GPEWriterHandlerImplementor class
63
 *
64
 * Revision 1.20  2007/06/22 12:39:28  jorpiell
65
 * Bug adding with a element without feature name fixed
66
 *
67
 * Revision 1.19  2007/06/22 12:22:40  jorpiell
68
 * The typeNotFoundException has been deleted. It never was thrown
69
 *
70
 * Revision 1.18  2007/06/14 13:50:05  jorpiell
71
 * The schema jar name has been changed
72
 *
73
 * Revision 1.17  2007/06/07 14:53:30  jorpiell
74
 * Add the schema support
75
 *
76
 * Revision 1.16  2007/05/17 14:33:51  jorpiell
77
 * The fid attribute is written
78
 *
79
 * Revision 1.15  2007/05/16 12:07:47  jorpiell
80
 * Some warnings throwed
81
 *
82
 * Revision 1.14  2007/05/15 11:55:11  jorpiell
83
 * MultiGeometry is now supported
84
 *
85
 * Revision 1.13  2007/05/15 10:14:45  jorpiell
86
 * The element and the feature is managed like a Stack
87
 *
88
 * Revision 1.12  2007/05/14 11:18:12  jorpiell
89
 * Add the ErrorHandler to all the methods
90
 *
91
 * Revision 1.11  2007/05/08 10:24:16  jorpiell
92
 * Add comments to create javadocs
93
 *
94
 * Revision 1.10  2007/05/07 07:08:02  jorpiell
95
 * Add a constructor with the name and the description fields
96
 *
97
 * Revision 1.9  2007/04/26 14:40:03  jorpiell
98
 * Some writer handler methods updated
99
 *
100
 * Revision 1.8  2007/04/19 07:25:49  jorpiell
101
 * Add the add methods to teh contenhandler and change the register mode
102
 *
103
 * Revision 1.7  2007/04/18 11:03:55  jorpiell
104
 * Add the default schema property
105
 *
106
 * Revision 1.6  2007/04/17 07:00:35  jorpiell
107
 * GML name, descripction and Id tags separated
108
 *
109
 * Revision 1.5  2007/04/14 16:07:30  jorpiell
110
 * The writer has been created
111
 *
112
 * Revision 1.4  2007/04/13 13:16:00  jorpiell
113
 * Add the multiple geometries
114
 *
115
 * Revision 1.3  2007/04/13 07:17:57  jorpiell
116
 * Add the writting tests for the simple geometries
117
 *
118
 * Revision 1.2  2007/04/12 17:06:44  jorpiell
119
 * First GML writing tests
120
 *
121
 * Revision 1.1  2007/04/12 10:23:41  jorpiell
122
 * Add some writers and the GPEXml parser
123
 *
124
 *
125
 */
126
/**
127
 * GPE writer handler for the GML format. It writes a 
128
 * GML file.
129
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
130
 */
131
public abstract class GPEGmlWriterHandlerImplementor extends GPEXmlWriterHandlerImplementor{
132
        //In GML there is only one label. It the consumer
133
        //try to add more labels the driver will be report
134
        //one exception
135
        private boolean oneLayer = false;
136

    
137
        //Used for the multi-geometries
138
        private boolean isMultiple = false;
139

    
140
        //Used to know if the multiple geometries has type
141
        private boolean hasMultipleType = false;
142

    
143
        //Used to close the labels
144
        private Stack currentFeature = null;
145
        private Stack currentElement = null;        
146

    
147
        //To manage the end of file
148
        private boolean isInitialized = false;
149

    
150
        //Profile to write a concrete version
151
        private IWriterProfile profile = null;
152

    
153
        public GPEGmlWriterHandlerImplementor() {
154
                super();        
155
                currentFeature = new Stack();
156
                currentElement = new Stack();
157
        }
158
        
159
        /* (non-Javadoc)
160
         * @see org.gvsig.gpe.gml.writer.GPEGmlWriterHandlerImplementor#initialize()
161
         */
162
        public void initialize() {
163
                super.initialize();
164
                try {
165
                        //Initializes the namespaces
166
                        initPrefixes();
167
                } catch (XmlStreamException e) {
168
                        getErrorHandler().addError(e);
169
                }        
170
        }
171
        
172
        /*
173
         * (non-Javadoc)
174
         * @see org.gvsig.gpe.writer.GPEWriterHandler#close()
175
         */
176
        public void close(){
177
                try {
178
                        writer.flush();
179
                        writer.close();
180
                } catch (IOException e) {
181
                        getErrorHandler().addError(e);
182
                }
183
        }
184

    
185
        /*
186
         * (non-Javadoc)
187
         * @see org.gvsig.gpe.writer.GPEWriterHandlerImplementor#startLayer(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
188
         */
189
        public void startLayer(String id, String namespace, String name, String description, String srs) {
190
                try {
191
                        getProfile().getFeatureCollectionWriter().start(writer,
192
                                        this, id, namespace, name);                
193
                        writer.writeEndAttributes();
194
                        getProfile().getNameWriter().write(writer, this, name);
195
                        getProfile().getDescriptionWriter().write(writer, this, description);
196
                } catch (IOException e) {
197
                        getErrorHandler().addError(e);
198
                }        
199
        }        
200

    
201
        /**
202
         * Initializes the prefixes that will be used
203
         * @throws XmlStreamException
204
         */
205
        protected void initPrefixes() throws XmlStreamException{
206
                writer.setPrefix(XMLTags.XML_NAMESPACE_URI, XMLTags.XML_NAMESPACE_PREFIX);
207
                writer.setPrefix(XMLTags.XML_SCHEMA_INSTANCE_NAMESPACE_URI, XMLTags.XML_SCHEMA_INSTANCE_NAMESPACE_PREFIX);
208
                writer.setPrefix(GMLTags.GML_NAMESPACE_PREFIX, GMLTags.GML_NAMESPACE_URI);                
209
                writer.setPrefix(getGpeManager().getStringProperty(XmlProperties.DEFAULT_NAMESPACE_PREFIX),
210
                                getGpeManager().getStringProperty(XmlProperties.DEFAULT_NAMESPACE_URI));
211
        }
212

    
213
        /*
214
         * (non-Javadoc)
215
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endLayer()
216
         */
217
        public void endLayer() {
218
                try {
219
                        writer.writeEndElement();
220
                } catch (IOException e) {
221
                        getErrorHandler().addError(e);
222
                }
223
        }
224

    
225
        /*
226
         * (non-Javadoc)
227
         * @see org.gvsig.gpe.writer.GPEWriterHandlerImplementor#startFeature(java.lang.String, java.lang.String, java.lang.String)
228
         */
229
        public void startFeature(String id, String namespace, String name) {
230
                currentFeature.push(name);
231
                try {
232
                        profile.getFeatureMemberWriter().start(writer, this, id, namespace, name);
233
                } catch (IOException e) {
234
                        getErrorHandler().addError(e);
235
                }
236
        }
237

    
238
        /*
239
         * (non-Javadoc)
240
         * @see org.gvsig.gpe.writer.GPEWriterHandler#endFeature()
241
         */
242
        public void endFeature() {
243
                try {
244
                        profile.getFeatureMemberWriter().end(writer, this, (String)currentFeature.pop(), getTargetNamespacePrefix());
245
                } catch (IOException e) {
246
                        getErrorHandler().addError(e);
247
                }                
248
        }
249

    
250
        /*
251
         * (non-Javadoc)
252
         * @see org.gvsig.gpe.writer.IGPEWriterHandlerImplementor#startElement(java.lang.String, java.lang.String, java.lang.Object)
253
         */
254
        public void startElement(String namespace, String name, Object value) {
255
                currentElement.push(name);
256
                try {
257
                        profile.getElementWriter().start(writer, this, namespace, name, value);
258
                } catch (IOException e) {
259
                        getErrorHandler().addError(e);
260
                }                
261
        }
262

    
263
        /*
264
         * (non-Javadoc)
265
         * @see org.gvsig.gpe.writer.GPEWriterHandler#endElement()
266
         */
267
        public void endElement() {
268
                try {
269
                        profile.getElementWriter().end(writer, this, (String)currentElement.pop(), getTargetNamespacePrefix());                   
270
                } catch (IOException e) {
271
                        getErrorHandler().addError(e);
272
                }        
273
        }
274

    
275
        /*
276
         * (non-Javadoc)
277
         * @see org.gvsig.gpe.writer.IGPEWriterHandlerImplementor#startPoint(java.lang.String, org.gvsig.gpe.writer.ICoordinateSequence, java.lang.String)
278
         */
279
        public void startPoint(String id, ICoordinateSequence coords, String srs) {
280
                try {
281
                        if (isMultiple){
282
                                if (hasMultipleType){
283
                                        profile.getGeometryMemberWriter().startPoint(writer, this, id, srs);
284
                                        profile.getPointWriter().start(writer, this, id, coords, srs);
285
                                }else{
286
                                        profile.getPointMemberWriter().start(writer, this, id, coords, srs);
287
                                }
288
                        }else{
289
                                profile.getPointWriter().start(writer, this, id, coords, srs);
290
                        }
291
                } catch (IOException e) {
292
                        getErrorHandler().addError(e);
293
                }                
294
        }
295

    
296
        /*
297
         * (non-Javadoc)
298
         * @see org.gvsig.gpe.writer.GPEWriterHandler#endPoint()
299
         */
300
        public void endPoint() {
301
                try {
302
                        if (isMultiple){
303
                                if (hasMultipleType){
304
                                        profile.getPointWriter().end(writer, this);
305
                                        profile.getGeometryMemberWriter().end(writer, this);
306
                                }else{
307
                                        profile.getPointMemberWriter().end(writer, this);
308
                                }
309
                        }else{
310
                                profile.getPointWriter().end(writer, this);
311
                        }
312
                } catch (IOException e) {
313
                        getErrorHandler().addError(e);
314
                }
315

    
316
        }
317

    
318
        /*
319
         * (non-Javadoc)
320
         * @see org.gvsig.gpe.writer.IGPEWriterHandlerImplementor#startLineString(java.lang.String, org.gvsig.gpe.writer.ICoordinateSequence, java.lang.String)
321
         */
322
        public void startLineString(String id, ICoordinateSequence coords, String srs) {
323
                try {
324
                        if (isMultiple){
325
                                if (hasMultipleType){
326
                                        profile.getGeometryMemberWriter().startPoint(writer, this, id, srs);
327
                                        profile.getLineStringWriter().start(writer, this, id, coords, srs);
328
                                }else{
329
                                        profile.getLineStringMemeberWriter().start(writer, this, id, coords, srs);
330
                                }
331
                        }else{
332
                                profile.getLineStringWriter().start(writer, this, id, coords, srs);
333
                        }
334
                } catch (IOException e) {
335
                        getErrorHandler().addError(e);
336
                }                
337
        }
338

    
339
        /*
340
         * (non-Javadoc)
341
         * @see org.gvsig.gpe.writer.GPEWriterHandler#endLineString()
342
         */
343
        public void endLineString() {
344
                try {
345
                        if (isMultiple){
346
                                if (hasMultipleType){
347
                                        profile.getLineStringWriter().end(writer, this);
348
                                        profile.getGeometryMemberWriter().end(writer, this);
349
                                }else{
350
                                        profile.getLineStringMemeberWriter().end(writer, this);
351
                                }
352
                        }else{
353
                                profile.getLineStringWriter().end(writer, this);
354
                        }
355
                } catch (IOException e) {
356
                        getErrorHandler().addError(e);
357
                }                
358
        }
359

    
360
        /*
361
         * (non-Javadoc)
362
         * @see org.gvsig.gpe.writer.IGPEWriterHandlerImplementor#startBbox(java.lang.String, org.gvsig.gpe.writer.ICoordinateSequence, java.lang.String)
363
         */
364
        public void startBbox(String id, ICoordinateSequence coords, String srs) {
365
                try {
366
                        profile.getBoundedByWriter().start(writer, this, id, coords, srs);
367
                } catch (IOException e) {
368
                        getErrorHandler().addError(e);
369
                }                
370
        }
371

    
372
        /*
373
         * (non-Javadoc)
374
         * @see org.gvsig.gpe.writer.GPEWriterHandler#endBbox()
375
         */
376
        public void endBbox() {
377
                try {
378
                        profile.getBoundedByWriter().end(writer, this);
379
                } catch (IOException e) {
380
                        getErrorHandler().addError(e);
381
                }                        
382
        }
383

    
384
        /*
385
         * (non-Javadoc)
386
         * @see org.gvsig.gpe.writer.IGPEWriterHandlerImplementor#startLinearRing(java.lang.String, org.gvsig.gpe.writer.ICoordinateSequence, java.lang.String)
387
         */
388
        public void startLinearRing(String id, ICoordinateSequence coords, String srs) {
389
                try {
390
                        profile.getLinearRingWriter().start(writer, this, id, coords, srs);
391
                } catch (IOException e) {
392
                        getErrorHandler().addError(e);
393
                }                        
394
        }
395

    
396
        /*
397
         * (non-Javadoc)
398
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endLinearRing()
399
         */
400
        public void endLinearRing() {
401
                try {
402
                        profile.getLinearRingWriter().end(writer, this);
403
                } catch (IOException e) {
404
                        getErrorHandler().addError(e);
405
                }                
406
        }
407

    
408
        /*
409
         * (non-Javadoc)
410
         * @see org.gvsig.gpe.writer.IGPEWriterHandlerImplementor#startPolygon(java.lang.String, org.gvsig.gpe.writer.ICoordinateSequence, java.lang.String)
411
         */
412
        public void startPolygon(String id, ICoordinateSequence coords, String srs) {
413
                try {
414
                        if (isMultiple){
415
                                if (hasMultipleType){
416
                                        profile.getGeometryMemberWriter().startPoint(writer, this, id, srs);
417
                                        profile.getPolygonWriter().start(writer, this, id, coords, srs);
418
                                }else{
419
                                        profile.getPolygonMemberWriter().start(writer, this, id, coords, srs);
420
                                }
421
                        }else{
422
                                profile.getPolygonWriter().start(writer, this, id, coords, srs);
423
                        }
424
                } catch (IOException e) {
425
                        getErrorHandler().addError(e);
426
                }
427
        }
428

    
429
        /*
430
         * (non-Javadoc)
431
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endPolygon()
432
         */
433
        public void endPolygon() {
434
                try {
435
                        if (isMultiple){
436
                                if (hasMultipleType){
437
                                        profile.getPolygonWriter().end(writer, this);
438
                                        profile.getGeometryMemberWriter().end(writer, this);
439
                                }else{
440
                                        profile.getPolygonMemberWriter().end(writer, this);
441
                                }
442
                        }else{
443
                                profile.getPolygonWriter().end(writer, this);
444
                        }
445
                } catch (IOException e) {
446
                        getErrorHandler().addError(e);
447
                }        
448
        }
449

    
450
        /*
451
         * (non-Javadoc)
452
         * @see org.gvsig.gpe.writer.IGPEWriterHandlerImplementor#startInnerBoundary(java.lang.String, org.gvsig.gpe.writer.ICoordinateSequence, java.lang.String)
453
         */
454
        public void startInnerBoundary(String id, ICoordinateSequence coords, String srs) {
455
                try {
456
                        profile.getInnerBoundaryIsWriter().start(writer, this, coords);
457
                } catch (IOException e) {
458
                        getErrorHandler().addError(e);
459
                }
460
        }
461

    
462
        /*
463
         * (non-Javadoc)
464
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endInnerBoundary()
465
         */
466
        public void endInnerBoundary() {
467
                try {
468
                        profile.getInnerBoundaryIsWriter().end(writer, this);
469
                } catch (IOException e) {
470
                        getErrorHandler().addError(e);
471
                }        
472
        }
473

    
474
        /*
475
         * (non-Javadoc)
476
         * @see org.gvsig.gpe.writers.GPEWriterHandler#startMultiPoint(java.lang.String, java.lang.String)
477
         */
478
        public void startMultiPoint(String id, String srs) {
479
                isMultiple = true;
480
                try {
481
                        profile.getMultiPointWriter().start(writer, this, id, srs);
482
                } catch (IOException e) {
483
                        getErrorHandler().addError(e);
484
                }
485
        }
486

    
487
        /*
488
         * (non-Javadoc)
489
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endMuliPoint()
490
         */
491
        public void endMultiPoint() {
492
                isMultiple = false;
493
                try {
494
                        profile.getMultiPointWriter().end(writer, this);
495
                } catch (IOException e) {
496
                        getErrorHandler().addError(e);
497
                }
498
        }
499

    
500
        /*
501
         * (non-Javadoc)
502
         * @see org.gvsig.gpe.writers.GPEWriterHandler#startMultiPoint(java.lang.String, java.lang.String)
503
         */
504
        public void startMultiGeometry(String id, String srs) {
505
                isMultiple = true;
506
                hasMultipleType = true;
507
                try {
508
                        profile.getMultiGeometryWriter().start(writer, this, id, srs);
509
                } catch (IOException e) {
510
                        getErrorHandler().addError(e);
511
                }
512
        }
513

    
514
        /*
515
         * (non-Javadoc)
516
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endMuliPoint()
517
         */
518
        public void endMultiGeometry() {
519
                isMultiple = false;
520
                hasMultipleType = false;
521
                try {
522
                        profile.getMultiGeometryWriter().end(writer, this);
523
                } catch (IOException e) {
524
                        getErrorHandler().addError(e);
525
                }
526
        }
527

    
528
        /*
529
         * (non-Javadoc)
530
         * @see org.gvsig.gpe.writers.GPEWriterHandler#startMultiLineString(java.lang.String, java.lang.String)
531
         */
532
        public void startMultiLineString(String id, String srs) {
533
                isMultiple = true;
534
                try {
535
                        profile.getMultiLineStringWriter().start(writer, this, id, srs);
536
                } catch (IOException e) {
537
                        getErrorHandler().addError(e);
538
                }
539
        }
540

    
541
        /*
542
         * (non-Javadoc)
543
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endMultiLineString()
544
         */
545
        public void endMultiLineString() {
546
                isMultiple = false;
547
                try {
548
                        profile.getMultiLineStringWriter().end(writer, this);
549
                } catch (IOException e) {
550
                        getErrorHandler().addError(e);
551
                }
552
        }
553

    
554
        /*
555
         * (non-Javadoc)
556
         * @see org.gvsig.gpe.writers.GPEWriterHandler#startMultiPolygon(java.lang.String, java.lang.String)
557
         */
558
        public void startMultiPolygon(String id, String srs) {
559
                isMultiple = true;
560
                try {
561
                        profile.getMultiPolygonWriter().start(writer, this, id, srs);
562
                } catch (IOException e) {
563
                        getErrorHandler().addError(e);
564
                }                
565
        }
566

    
567
        /*
568
         * (non-Javadoc)
569
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endMultiPolygon()
570
         */
571
        public void endMultiPolygon() {
572
                isMultiple = false;
573
                try {
574
                        profile.getMultiPolygonWriter().end(writer, this);
575
                } catch (IOException e) {
576
                        getErrorHandler().addError(e);
577
                }                
578
        }
579

    
580
        /**
581
         * @return the profile
582
         */
583
        public IWriterProfile getProfile() {
584
                return profile;
585
        }
586

    
587
        /**
588
         * @param profile the profile to set
589
         */
590
        public void setProfile(IWriterProfile profile) {
591
                this.profile = profile;
592
        }
593
        
594
        /* (non-Javadoc)
595
         * @see org.gvsig.gpe.writer.IGPEWriterHandlerImplementor#getFileExtension()
596
         */
597
        public String getFileExtension() {
598
                return "gml";
599
        }
600
        
601
}