Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGPE-KML / src / org / gvsig / gpe / kml / writer / GPEKmlWriterHandlerImplementor.java @ 37960

History | View | Annotate | Download (14.3 KB)

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

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

    
6
import javax.xml.namespace.QName;
7

    
8
import org.gvsig.gpe.kml.utils.Kml2_1_Tags;
9
import org.gvsig.gpe.kml.utils.KmlStyle;
10
import org.gvsig.gpe.kml.writer.profiles.IWriterProfile;
11
import org.gvsig.gpe.warnings.FeatureNotSupportedWarning;
12
import org.gvsig.gpe.writer.ICoordinateSequence;
13
import org.gvsig.gpe.xml.stream.IXmlStreamWriter;
14
import org.gvsig.gpe.xml.stream.stax.StaxXmlStreamWriter;
15
import org.gvsig.gpe.xml.utils.XMLTags;
16
import org.gvsig.gpe.xml.writer.GPEXmlWriterHandlerImplementor;
17

    
18

    
19
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
20
 *
21
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
22
 *
23
 * This program is free software; you can redistribute it and/or
24
 * modify it under the terms of the GNU General Public License
25
 * as published by the Free Software Foundation; either version 2
26
 * of the License, or (at your option) any later version.
27
 *
28
 * This program is distributed in the hope that it will be useful,
29
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31
 * GNU General Public License for more details.
32
 *
33
 * You should have received a copy of the GNU General Public License
34
 * along with this program; if not, write to the Free Software
35
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
36
 *
37
 * For more information, contact:
38
 *
39
 *  Generalitat Valenciana
40
 *   Conselleria d'Infraestructures i Transport
41
 *   Av. Blasco Ib??ez, 50
42
 *   46010 VALENCIA
43
 *   SPAIN
44
 *
45
 *      +34 963862235
46
 *   gvsig@gva.es
47
 *      www.gvsig.gva.es
48
 *
49
 *    or
50
 *
51
 *   IVER T.I. S.A
52
 *   Salamanca 50
53
 *   46005 Valencia
54
 *   Spain
55
 *
56
 *   +34 963163400
57
 *   dac@iver.es
58
 */
59
/* CVS MESSAGES:
60
 *
61
 * $Id: GPEKmlWriterHandlerImplementor.java 362 2008-01-10 08:41:41Z jpiera $
62
 * $Log$
63
 * Revision 1.1  2007/06/29 12:19:48  jorpiell
64
 * The schema validation is made independently of the concrete writer
65
 *
66
 * Revision 1.17  2007/06/11 06:41:41  jorpiell
67
 * Add a new Exception
68
 *
69
 * Revision 1.16  2007/06/07 14:53:59  jorpiell
70
 * Add the schema support
71
 *
72
 * Revision 1.15  2007/05/16 15:54:20  jorpiell
73
 * Add elements support
74
 *
75
 * Revision 1.14  2007/05/16 12:08:14  jorpiell
76
 * A multipoint layer is not supported
77
 *
78
 * Revision 1.13  2007/05/16 09:30:09  jorpiell
79
 * the writting methods has to have the errorHandler argument
80
 *
81
 * Revision 1.12  2007/05/15 12:37:45  jorpiell
82
 * Add multyGeometries
83
 *
84
 * Revision 1.11  2007/05/09 08:36:24  jorpiell
85
 * Add the bbox to the layer
86
 *
87
 * Revision 1.10  2007/05/08 09:28:17  jorpiell
88
 * Add comments to create javadocs
89
 *
90
 * Revision 1.9  2007/05/08 08:22:37  jorpiell
91
 * Add comments to create javadocs
92
 *
93
 * Revision 1.8  2007/05/07 07:07:18  jorpiell
94
 * Add a constructor with the name and the description fields
95
 *
96
 * Revision 1.7  2007/05/02 11:46:50  jorpiell
97
 * Writing tests updated
98
 *
99
 * Revision 1.6  2007/04/20 08:38:59  jorpiell
100
 * Tests updating
101
 *
102
 * Revision 1.5  2007/04/17 10:30:41  jorpiell
103
 * Add a method to compress a file
104
 *
105
 * Revision 1.4  2007/04/14 16:08:07  jorpiell
106
 * Kml writing support added
107
 *
108
 * Revision 1.3  2007/04/13 13:16:21  jorpiell
109
 * Add KML reading support
110
 *
111
 * Revision 1.2  2007/04/12 17:06:43  jorpiell
112
 * First GML writing tests
113
 *
114
 * Revision 1.1  2007/04/12 10:21:52  jorpiell
115
 * Add the writers
116
 *
117
 *
118
 */
119
/**
120
 * KML writer handler used to write KML/KMZ files
121
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
122
 */
123
public abstract class GPEKmlWriterHandlerImplementor extends GPEXmlWriterHandlerImplementor{
124
        private int layerLevel = 0;
125
//        private Stack currentElementStream = null;        
126
//        private Stack currentElementName = null;        
127
        private IWriterProfile profile = null;
128
        
129
        public GPEKmlWriterHandlerImplementor() {
130
                super();
131
//                currentElementStream = new Stack();
132
//                currentElementName = new Stack();
133
        }
134
        
135
        /*
136
         * (non-Javadoc)
137
         * @see org.gvsig.gpe.xml.writer.GPEXmlWriterHandler#createOutputStream()
138
         */
139
        protected IXmlStreamWriter createWriter() throws IOException {
140
                return new StaxXmlStreamWriter(getOutputStream());
141
        }
142
        
143
        /*
144
         * (non-Javadoc)
145
         * @see org.gvsig.gpe.writers.GPEWriterHandler#getDefaultFormat()
146
         */
147
        public String getDefaultFormat() {
148
                return "KML";
149
        }
150
        
151
        /*
152
         * (non-Javadoc)
153
         * @see org.gvsig.gpe.writer.IGPEWriterHandlerImplementor#getFormat()
154
         */
155
        public String getFormat() {
156
                return "text/xml; subtype=kml/2.1";                
157
        }
158

    
159
        /*
160
         * (non-Javadoc)
161
         * @see org.gvsig.gpe.writer.GPEWriterHandler#initialize()
162
         */
163
        public void initialize(){
164
                super.initialize();
165
                try {
166
                        writer.setDefaultNamespace(Kml2_1_Tags.NAMESPACE_21);
167
                        writer.writeStartElement(Kml2_1_Tags.ROOT);        
168
                        writer.writeStartAttribute(XMLTags.XML_NAMESPACE_URI, XMLTags.XML_NAMESPACE_PREFIX);
169
                        writer.writeValue(Kml2_1_Tags.NAMESPACE_21);
170
                        writer.writeEndAttributes();
171
                } catch (IOException e) {
172
                        getErrorHandler().addError(e);
173
                }
174
        }
175
        
176
        /*
177
         * (non-Javadoc)
178
         * @see org.gvsig.gpe.writer.GPEWriterHandler#close()
179
         */
180
        public void close(){
181
                try {
182
                        writer.writeEndElement();
183
                        writer.flush();
184
                        writer.close();
185
                } catch (IOException e) {
186
                        getErrorHandler().addError(e);
187
                }                
188
        }        
189
        
190
        /*
191
         * (non-Javadoc)
192
         * @see org.gvsig.gpe.writer.GPEWriterHandlerImplementor#startLayer(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
193
         */
194
        public void startLayer(String id, String namespace, String name, String description, String srs) {
195
                try{
196
                        if (layerLevel == 0){
197
                                getProfile().getDocumentWriter().start(writer, this, id, name, description);
198
                        }else{
199
                                getProfile().getFolderWriter().start(writer, this, id, name, description);
200
                        }                        
201
                } catch (IOException e) {
202
                        getErrorHandler().addError(e);
203
                }
204
                layerLevel++;
205
        }
206
        
207
        
208
        /**
209
         * This method is only used in order to save the legend for KML files
210
         * It should be called after startLayer method
211
         * @param styles
212
         */
213
        public void writeStyles(KmlStyle[] styles) {
214
                try {
215
                        getProfile().getStylesWriter().writeStyles(writer, this, styles);
216
                } catch (IOException e) {
217
                        e.printStackTrace();
218
                        getErrorHandler().addError(e);
219
                }
220
        }
221

    
222
        /*
223
         * (non-Javadoc)
224
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endLayer()
225
         */
226
        public void endLayer() {
227
                layerLevel--;
228
                try{
229
                        if (layerLevel == 0){
230
                                getProfile().getDocumentWriter().end(writer, this);
231
                        }else{
232
                                getProfile().getFolderWriter().end(writer, this);
233
                        }
234
                } catch (IOException e) {
235
                        getErrorHandler().addError(e);
236
                }                
237
        }
238
        
239
        /*
240
         * (non-Javadoc)
241
         * @see org.gvsig.gpe.writers.GPEWriterHandler#startFeature(java.lang.String, java.lang.String)
242
         */
243
        public void startFeature(String id, String namespace, String name) {
244
//                currentElementStream.push(new IXMLStreamWriter(new StringWriter()));
245
//                currentElementName.push(new Stack());
246
                try{
247
                        getProfile().getPlaceMarkWriter().start(writer, this, id, name);
248
                } catch (IOException e) {
249
                        getErrorHandler().addError(e);
250
                }        
251
        }
252
        
253
        /*
254
         * (non-Javadoc)
255
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endFeature()
256
         */
257
        public void endFeature() {
258
//                IXmlStreamWriter elementWriter = (IXmlStreamWriter)currentElementStream.pop();
259
//                currentElementName.pop();
260
                try{
261
                        getProfile().getMetadataWriter().start(writer, this);
262
//                        writer.write(elementWriter.toString());
263
                        getProfile().getMetadataWriter().end(writer, this);
264
                        getProfile().getPlaceMarkWriter().end(writer, this);
265
                } catch (IOException e) {
266
                        getErrorHandler().addError(e);
267
                }                        
268
        }
269
        
270
        /*
271
         * (non-Javadoc)
272
         * @see org.gvsig.gpe.writer.GPEWriterHandler#startElement(java.lang.String, java.lang.Object, java.lang.Object)
273
         */
274
        public void startElement(String namespace, String name, Object value) {                
275
                try {
276
                        QName aux = new QName(namespace, name);
277
                        getProfile().getElementWriter().start(writer, this, aux, value);
278
                } catch (IOException e) {
279
                        getErrorHandler().addError(e);
280
                }
281
                
282
        }
283
        
284
        /*
285
         * (non-Javadoc)
286
         * @see org.gvsig.gpe.writer.GPEWriterHandler#endElement()
287
         */
288
        public void endElement() {
289
                try {
290
                        getProfile().getElementWriter().end(writer, this);                   
291
                } catch (IOException e) {
292
                        getErrorHandler().addError(e);
293
                }                
294
        }
295
        
296
        /*
297
         * (non-Javadoc)
298
         * @see org.gvsig.gpe.writer.IGPEWriterHandlerImplementor#startPoint(java.lang.String, org.gvsig.gpe.writer.ICoordinateSequence, java.lang.String)
299
         */
300
        public void startPoint(String id, ICoordinateSequence coords, String srs) {
301
                try {
302
                        getProfile().getPointTypeWriter().start(writer, this, id, coords);
303
                } catch (IOException e) {
304
                        getErrorHandler().addError(e);
305
                }                
306
        }
307
        
308
        /*
309
         * (non-Javadoc)
310
         * @see org.gvsig.gpe.writer.GPEWriterHandler#endPoint()
311
         */
312
        public void endPoint() {
313
                try {
314
                        getProfile().getPointTypeWriter().end(writer, this);                        
315
                } catch (IOException e) {
316
                        getErrorHandler().addError(e);
317
                }                
318
        }
319
        
320
        /*
321
         * (non-Javadoc)
322
         * @see org.gvsig.gpe.writer.IGPEWriterHandlerImplementor#startLineString(java.lang.String, org.gvsig.gpe.writer.ICoordinateSequence, java.lang.String)
323
         */
324
        public void startLineString(String id, ICoordinateSequence coords, String srs) {
325
                try {
326
                        getProfile().getLineStringTypeWriter().start(writer, this, id, coords);                        
327
                } catch (IOException e) {
328
                        getErrorHandler().addError(e);
329
                }
330
        }
331
        
332
        /*
333
         * (non-Javadoc)
334
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endLineString()
335
         */
336
        public void endLineString() {
337
                try {
338
                        getProfile().getLineStringTypeWriter().end(writer, this);                        
339
                } catch (IOException e) {
340
                        getErrorHandler().addError(e);
341
                }
342
        }
343
        
344
        /*
345
         * (non-Javadoc)
346
         * @see org.gvsig.gpe.writer.IGPEWriterHandlerImplementor#startLinearRing(java.lang.String, org.gvsig.gpe.writer.ICoordinateSequence, java.lang.String)
347
         */
348
        public void startLinearRing(String id, ICoordinateSequence coords, String srs) {
349
                try {
350
                        getProfile().getLinearRingWriter().start(writer, this, coords);                        
351
                } catch (IOException e) {
352
                        getErrorHandler().addError(e);
353
                }
354
        }
355
        
356
        /*
357
         * (non-Javadoc)
358
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endLinearRing()
359
         */
360
        public void endLinearRing() {
361
                try {
362
                        getProfile().getLinearRingWriter().end(writer, this);                        
363
                } catch (IOException e) {
364
                        getErrorHandler().addError(e);
365
                }
366
        }
367
        
368
        /*
369
         * (non-Javadoc)
370
         * @see org.gvsig.gpe.writer.IGPEWriterHandlerImplementor#startPolygon(java.lang.String, org.gvsig.gpe.writer.ICoordinateSequence, java.lang.String)
371
         */
372
        public void startPolygon(String id, ICoordinateSequence coords, String srs) {
373
                try {
374
                        getProfile().getPolygonTypeWriter().start(writer, this, id, coords);                        
375
                } catch (IOException e) {
376
                        getErrorHandler().addError(e);
377
                }
378
        }
379
        
380
        /*
381
         * (non-Javadoc)
382
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endPolygon()
383
         */
384
        public void endPolygon() {
385
                try {
386
                        getProfile().getPolygonTypeWriter().end(writer, this);                        
387
                } catch (IOException e) {
388
                        getErrorHandler().addError(e);
389
                }
390
        }
391
        
392
        /*
393
         * (non-Javadoc)
394
         * @see org.gvsig.gpe.writer.IGPEWriterHandlerImplementor#startInnerBoundary(java.lang.String, org.gvsig.gpe.writer.ICoordinateSequence, java.lang.String)
395
         */
396
        public void startInnerBoundary(String id, ICoordinateSequence coords, String srs) {
397
                try {
398
                        getProfile().getInnerBoundaryIsWriter().start(writer, this, coords);                        
399
                } catch (IOException e) {
400
                        getErrorHandler().addError(e);
401
                }
402
        }
403
        
404
        /*
405
         * (non-Javadoc)
406
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endInnerBoundary()
407
         */
408
        public void endInnerBoundary() {
409
                try {
410
                        getProfile().getInnerBoundaryIsWriter().end(writer, this);                        
411
                } catch (IOException e) {
412
                        getErrorHandler().addError(e);
413
                }
414
        }
415
        
416
        /*
417
         * (non-Javadoc)
418
         * @see org.gvsig.gpe.writer.IGPEWriterHandlerImplementor#startBbox(java.lang.String, org.gvsig.gpe.writer.ICoordinateSequence, java.lang.String)
419
         */
420
        public void startBbox(String id, ICoordinateSequence coords, String srs) {
421
                try {
422
                        getProfile().getRegionWriter().start(writer, this, coords);                        
423
                } catch (IOException e) {
424
                        getErrorHandler().addError(e);
425
                }        
426
        }
427
        
428
        /*
429
         * (non-Javadoc)
430
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endBbox()
431
         */
432
        public void endBbox() {
433
                try {
434
                        getProfile().getRegionWriter().end(writer, this);                        
435
                } catch (IOException e) {
436
                        getErrorHandler().addError(e);
437
                }                
438
        }
439
        
440
        /*
441
         * (non-Javadoc)
442
         * @see org.gvsig.gpe.writers.GPEWriterHandler#startMultiPoint(java.lang.String, java.lang.String)
443
         */
444
        public void startMultiGeometry(String id, String srs) {
445
                try {
446
                        getProfile().getMultiGeometryWriter().start(writer, this, id);
447
                } catch (IOException e) {
448
                        getErrorHandler().addError(e);
449
                }
450
        }
451
        
452
        /*
453
         * (non-Javadoc)
454
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endMuliPoint()
455
         */
456
        public void endMultiGeometry() {
457
                try {
458
                        getProfile().getMultiGeometryWriter().end(writer, this);
459
                } catch (IOException e) {
460
                        getErrorHandler().addError(e);
461
                }
462
        }
463
        
464
        /*
465
         * (non-Javadoc)
466
         * @see org.gvsig.gpe.writers.GPEWriterHandler#startMultiPoint(java.lang.String, java.lang.String)
467
         */
468
        public void startMultiPoint(String id, String srs) {
469
                getErrorHandler().addWarning(
470
                                new FeatureNotSupportedWarning(FeatureNotSupportedWarning.MULTIPOINTCREATION,getName()));
471
                startMultiGeometry(id, srs);
472
        }
473
        
474
        /*
475
         * (non-Javadoc)
476
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endMuliPoint()
477
         */
478
        public void endMultiPoint() {
479
                super.endMultiPoint();
480
                endMultiGeometry();
481
        }
482
        
483
        /*
484
         * (non-Javadoc)
485
         * @see org.gvsig.gpe.writers.GPEWriterHandler#startMultiPoint(java.lang.String, java.lang.String)
486
         */
487
        public void startMultiLineString(String id, String srs) {
488
                getErrorHandler().addWarning(
489
                                new FeatureNotSupportedWarning(FeatureNotSupportedWarning.MULTILINESTRINGCREATION,getName()));
490
                startMultiGeometry(id, srs);
491
        }
492
        
493
        /*
494
         * (non-Javadoc)
495
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endMuliPoint()
496
         */
497
        public void endMultiLineString() {
498
                endMultiGeometry();
499
        }
500
        
501
        /*
502
         * (non-Javadoc)
503
         * @see org.gvsig.gpe.writers.GPEWriterHandler#startMultiPoint(java.lang.String, java.lang.String)
504
         */
505
        public void startMultiPolygon(String id, String srs) {
506
                getErrorHandler().addWarning(
507
                                new FeatureNotSupportedWarning(FeatureNotSupportedWarning.MULTIPOLYGONCREATION,getName()));
508
                startMultiGeometry(id, srs);
509
        }
510
        
511
        /*
512
         * (non-Javadoc)
513
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endMuliPoint()
514
         */
515
        public void endMultiPolygon() {
516
                endMultiGeometry();
517
        }
518

    
519
        /**
520
         * @return the profile
521
         */
522
        public IWriterProfile getProfile() {
523
                return profile;
524
        }
525

    
526
        /**
527
         * @param profile the profile to set
528
         */
529
        public void setProfile(IWriterProfile profile) {
530
                this.profile = profile;
531
        }
532
        
533
        /* (non-Javadoc)
534
         * @see org.gvsig.gpe.writer.IGPEWriterHandlerImplementor#getFileExtension()
535
         */
536
        public String getFileExtension() {
537
                return "kml";
538
        }
539
}