Statistics
| Revision:

root / org.gvsig.gpe / library / trunk / org.gvsig.gpe / org.gvsig.gpe.prov / org.gvsig.gpe.prov.kml / src / main / java / org / gvsig / gpe / prov / kml / writer / GPEKmlWriterHandlerImplementor.java @ 261

History | View | Annotate | Download (13.6 KB)

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

    
3
import java.io.IOException;
4
import java.util.ArrayList;
5
import java.util.List;
6

    
7
import org.gvsig.gpe.lib.api.writer.ICoordinateSequence;
8
import org.gvsig.gpe.lib.impl.warnings.FeatureNotSupportedWarning;
9
import org.gvsig.gpe.prov.kml.utils.Kml2_1_Tags;
10
import org.gvsig.gpe.prov.kml.writer.profiles.IWriterProfile;
11
import org.gvsig.gpe.prov.xml.utils.XMLTags;
12
import org.gvsig.gpe.prov.xml.writer.GPEXmlWriterHandlerImplementor;
13

    
14

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

    
129
        /*
130
         * (non-Javadoc)
131
         * @see org.gvsig.gpe.writers.GPEWriterHandler#getDefaultFormat()
132
         */
133
        public String getDefaultFormat() {
134
                return "KML";
135
        }
136
        
137
        /*
138
         * (non-Javadoc)
139
         * @see org.gvsig.gpe.writer.IGPEWriterHandlerImplementor#getFormat()
140
         */
141
        public String getFormat() {
142
                return "text/xml; subtype=kml/2.1";                
143
        }
144

    
145
        /*
146
         * (non-Javadoc)
147
         * @see org.gvsig.gpe.writer.GPEWriterHandler#initialize()
148
         */
149
        public void initialize(){
150
                super.initialize();
151
                try {
152
                        writer.setDefaultNamespace(Kml2_1_Tags.NAMESPACE_21);
153
                        writer.writeStartElement(Kml2_1_Tags.ROOT);        
154
                        writer.writeStartAttribute(XMLTags.XML_NAMESPACE_URI, XMLTags.XML_NAMESPACE_PREFIX);
155
                        writer.writeValue(Kml2_1_Tags.NAMESPACE_21);
156
                        writer.writeEndAttributes();
157
                } catch (IOException e) {
158
                        getErrorHandler().addError(e);
159
                }
160
        }
161
        
162
        /*
163
         * (non-Javadoc)
164
         * @see org.gvsig.gpe.writer.GPEWriterHandler#close()
165
         */
166
        public void close(){
167
                try {
168
                        writer.writeEndElement();
169
                        writer.flush();
170
                        writer.close();
171
                } catch (IOException e) {
172
                        getErrorHandler().addError(e);
173
                }                
174
        }        
175
        
176
        /*
177
         * (non-Javadoc)
178
         * @see org.gvsig.gpe.writer.GPEWriterHandlerImplementor#startLayer(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
179
         */
180
        public void startLayer(String id, String namespace, String name, String description, String srs) {
181
                try{
182
                        if (layerLevel == 0){
183
                                getProfile().getDocumentWriter().start(writer, this, id, name, description);
184
                        }else{
185
                                getProfile().getFolderWriter().start(writer, this, id, name, description);
186
                        }                        
187
                } catch (IOException e) {
188
                        getErrorHandler().addError(e);
189
                }
190
                layerLevel++;
191
        }
192
        
193
        /*
194
         * (non-Javadoc)
195
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endLayer()
196
         */
197
        public void endLayer() {
198
                layerLevel--;
199
                try{
200
                        if (layerLevel == 0){
201
                                getProfile().getDocumentWriter().end(writer, this);
202
                        }else{
203
                                getProfile().getFolderWriter().end(writer, this);
204
                        }
205
                } catch (IOException e) {
206
                        getErrorHandler().addError(e);
207
                }                
208
        }
209

    
210
    public void startFeature(String id, String namespace, String name) {
211
            try{
212
                metadataElements = new ArrayList(); 
213
                currentMetadataElement = null;
214
                        getProfile().getPlaceMarkWriter().start(writer, this, id, name);
215
                } catch (IOException e) {
216
                        getErrorHandler().addError(e);
217
                }        
218
        }
219
        
220
        /*
221
         * (non-Javadoc)
222
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endFeature()
223
         */
224
        public void endFeature() {                        
225
                try{
226
                        getProfile().getMetadataWriter().start(writer, this);
227
                        for (int i=0 ; i<metadataElements.size() ; i++){
228
                            writeMetadatadElement((MetadataElement)metadataElements.get(i));
229
                        }
230
                        getProfile().getMetadataWriter().end(writer, this);
231
                        getProfile().getPlaceMarkWriter().end(writer, this);
232
                } catch (IOException e) {
233
                        getErrorHandler().addError(e);
234
                }                        
235
        }
236
        
237
        private void writeMetadatadElement(MetadataElement metadataElement) throws IOException{
238
            getProfile().getElementWriter().start(writer, this, metadataElement);
239
            for (int i=0 ; i<metadataElement.getElementsSize() ; i++){
240
                writeMetadatadElement(metadataElement.getElementAt(i));
241
            }
242
            getProfile().getElementWriter().end(writer, this); 
243
        }
244

    
245
        
246
    public void startElement(String namespace, String name, Object value) {
247
        if (currentMetadataElement == null){
248
            currentMetadataElement = new MetadataElement(namespace, name, value);
249
            metadataElements.add(currentMetadataElement);
250
        }else{
251
            currentMetadataElement = currentMetadataElement.addChildElement(namespace, name, value);
252
        }                
253
        }
254
        
255
    public void endElement() {                        
256
        currentMetadataElement = currentMetadataElement.getParentElement();
257
        }
258
        
259
        /*
260
         * (non-Javadoc)
261
         * @see org.gvsig.gpe.writer.IGPEWriterHandlerImplementor#startPoint(java.lang.String, org.gvsig.gpe.writer.ICoordinateSequence, java.lang.String)
262
         */
263
        public void startPoint(String id, ICoordinateSequence coords, String srs) {
264
                try {
265
                        getProfile().getPointTypeWriter().start(writer, this, id, coords);
266
                } catch (IOException e) {
267
                        getErrorHandler().addError(e);
268
                }                
269
        }
270
        
271
        /*
272
         * (non-Javadoc)
273
         * @see org.gvsig.gpe.writer.GPEWriterHandler#endPoint()
274
         */
275
        public void endPoint() {
276
                try {
277
                        getProfile().getPointTypeWriter().end(writer, this);                        
278
                } catch (IOException e) {
279
                        getErrorHandler().addError(e);
280
                }                
281
        }
282
        
283
        /*
284
         * (non-Javadoc)
285
         * @see org.gvsig.gpe.writer.IGPEWriterHandlerImplementor#startLineString(java.lang.String, org.gvsig.gpe.writer.ICoordinateSequence, java.lang.String)
286
         */
287
        public void startLineString(String id, ICoordinateSequence coords, String srs) {
288
                try {
289
                        getProfile().getLineStringTypeWriter().start(writer, this, id, coords);                        
290
                } catch (IOException e) {
291
                        getErrorHandler().addError(e);
292
                }
293
        }
294
        
295
        /*
296
         * (non-Javadoc)
297
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endLineString()
298
         */
299
        public void endLineString() {
300
                try {
301
                        getProfile().getLineStringTypeWriter().end(writer, this);                        
302
                } catch (IOException e) {
303
                        getErrorHandler().addError(e);
304
                }
305
        }
306
        
307
        /*
308
         * (non-Javadoc)
309
         * @see org.gvsig.gpe.writer.IGPEWriterHandlerImplementor#startLinearRing(java.lang.String, org.gvsig.gpe.writer.ICoordinateSequence, java.lang.String)
310
         */
311
        public void startLinearRing(String id, ICoordinateSequence coords, String srs) {
312
                try {
313
                        getProfile().getLinearRingWriter().start(writer, this, coords);                        
314
                } catch (IOException e) {
315
                        getErrorHandler().addError(e);
316
                }
317
        }
318
        
319
        /*
320
         * (non-Javadoc)
321
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endLinearRing()
322
         */
323
        public void endLinearRing() {
324
                try {
325
                        getProfile().getLinearRingWriter().end(writer, this);                        
326
                } catch (IOException e) {
327
                        getErrorHandler().addError(e);
328
                }
329
        }
330
        
331
        /*
332
         * (non-Javadoc)
333
         * @see org.gvsig.gpe.writer.IGPEWriterHandlerImplementor#startPolygon(java.lang.String, org.gvsig.gpe.writer.ICoordinateSequence, java.lang.String)
334
         */
335
        public void startPolygon(String id, ICoordinateSequence coords, String srs) {
336
                try {
337
                        getProfile().getPolygonTypeWriter().start(writer, this, id, coords);                        
338
                } catch (IOException e) {
339
                        getErrorHandler().addError(e);
340
                }
341
        }
342
        
343
        /*
344
         * (non-Javadoc)
345
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endPolygon()
346
         */
347
        public void endPolygon() {
348
                try {
349
                        getProfile().getPolygonTypeWriter().end(writer, this);                        
350
                } catch (IOException e) {
351
                        getErrorHandler().addError(e);
352
                }
353
        }
354
        
355
        /*
356
         * (non-Javadoc)
357
         * @see org.gvsig.gpe.writer.IGPEWriterHandlerImplementor#startInnerBoundary(java.lang.String, org.gvsig.gpe.writer.ICoordinateSequence, java.lang.String)
358
         */
359
        public void startInnerBoundary(String id, ICoordinateSequence coords, String srs) {
360
                try {
361
                        getProfile().getInnerBoundaryIsWriter().start(writer, this, coords);                        
362
                } catch (IOException e) {
363
                        getErrorHandler().addError(e);
364
                }
365
        }
366
        
367
        /*
368
         * (non-Javadoc)
369
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endInnerBoundary()
370
         */
371
        public void endInnerBoundary() {
372
                try {
373
                        getProfile().getInnerBoundaryIsWriter().end(writer, this);                        
374
                } catch (IOException e) {
375
                        getErrorHandler().addError(e);
376
                }
377
        }
378
        
379
        /*
380
         * (non-Javadoc)
381
         * @see org.gvsig.gpe.writer.IGPEWriterHandlerImplementor#startBbox(java.lang.String, org.gvsig.gpe.writer.ICoordinateSequence, java.lang.String)
382
         */
383
        public void startBbox(String id, ICoordinateSequence coords, String srs) {
384
                try {
385
                        getProfile().getRegionWriter().start(writer, this, coords);                        
386
                } catch (IOException e) {
387
                        getErrorHandler().addError(e);
388
                }        
389
        }
390
        
391
        /*
392
         * (non-Javadoc)
393
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endBbox()
394
         */
395
        public void endBbox() {
396
                try {
397
                        getProfile().getRegionWriter().end(writer, this);                        
398
                } catch (IOException e) {
399
                        getErrorHandler().addError(e);
400
                }                
401
        }
402
        
403
        /*
404
         * (non-Javadoc)
405
         * @see org.gvsig.gpe.writers.GPEWriterHandler#startMultiPoint(java.lang.String, java.lang.String)
406
         */
407
        public void startMultiGeometry(String id, String srs) {
408
                try {
409
                        getProfile().getMultiGeometryWriter().start(writer, this, id);
410
                } catch (IOException e) {
411
                        getErrorHandler().addError(e);
412
                }
413
        }
414
        
415
        /*
416
         * (non-Javadoc)
417
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endMuliPoint()
418
         */
419
        public void endMultiGeometry() {
420
                try {
421
                        getProfile().getMultiGeometryWriter().end(writer, this);
422
                } catch (IOException e) {
423
                        getErrorHandler().addError(e);
424
                }
425
        }
426
        
427
        /*
428
         * (non-Javadoc)
429
         * @see org.gvsig.gpe.writers.GPEWriterHandler#startMultiPoint(java.lang.String, java.lang.String)
430
         */
431
        public void startMultiPoint(String id, String srs) {
432
                getErrorHandler().addWarning(
433
                                new FeatureNotSupportedWarning(FeatureNotSupportedWarning.MULTIPOINTCREATION,getName()));
434
                startMultiGeometry(id, srs);
435
        }
436
        
437
        /*
438
         * (non-Javadoc)
439
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endMuliPoint()
440
         */
441
        public void endMultiPoint() {
442
                super.endMultiPoint();
443
                endMultiGeometry();
444
        }
445
        
446
        /*
447
         * (non-Javadoc)
448
         * @see org.gvsig.gpe.writers.GPEWriterHandler#startMultiPoint(java.lang.String, java.lang.String)
449
         */
450
        public void startMultiLineString(String id, String srs) {
451
                getErrorHandler().addWarning(
452
                                new FeatureNotSupportedWarning(FeatureNotSupportedWarning.MULTILINESTRINGCREATION,getName()));
453
                startMultiGeometry(id, srs);
454
        }
455
        
456
        /*
457
         * (non-Javadoc)
458
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endMuliPoint()
459
         */
460
        public void endMultiLineString() {
461
                endMultiGeometry();
462
        }
463
        
464
        /*
465
         * (non-Javadoc)
466
         * @see org.gvsig.gpe.writers.GPEWriterHandler#startMultiPoint(java.lang.String, java.lang.String)
467
         */
468
        public void startMultiPolygon(String id, String srs) {
469
                getErrorHandler().addWarning(
470
                                new FeatureNotSupportedWarning(FeatureNotSupportedWarning.MULTIPOLYGONCREATION,getName()));
471
                startMultiGeometry(id, srs);
472
        }
473
        
474
        /*
475
         * (non-Javadoc)
476
         * @see org.gvsig.gpe.writers.GPEWriterHandler#endMuliPoint()
477
         */
478
        public void endMultiPolygon() {
479
                endMultiGeometry();
480
        }
481

    
482
        /**
483
         * @return the profile
484
         */
485
        public IWriterProfile getProfile() {
486
                return profile;
487
        }
488

    
489
        /**
490
         * @param profile the profile to set
491
         */
492
        public void setProfile(IWriterProfile profile) {
493
                this.profile = profile;
494
        }
495
        
496
        /* (non-Javadoc)
497
         * @see org.gvsig.gpe.writer.IGPEWriterHandlerImplementor#getFileExtension()
498
         */
499
        public String getFileExtension() {
500
                return "kml";
501
        }
502
}