Statistics
| Revision:

root / trunk / libraries / libGPE / src / org / gvsig / gpe / writers / GPEWriterHandler.java @ 12418

History | View | Annotate | Download (18.1 KB)

1
package org.gvsig.gpe.writers;
2

    
3
import java.io.File;
4

    
5
import org.gvsig.gpe.GPEErrorHandler;
6
import org.gvsig.gpe.utils.GeographicalUtils;
7
import org.gvsig.gpe.utils.TypedObjectStack;
8
import org.gvsig.gpe.utils.TypedObjectStack.TypedObject;
9
import org.gvsig.gpe.warnings.NotSupportedElementWarning;
10
import org.gvsig.gpe.warnings.NotSupportedFeatureWarning;
11
import org.gvsig.gpe.warnings.NotSupportedLayerWarning;
12
import org.gvsig.gpe.warnings.PolygonAutomaticallyClosedWarning;
13
import org.gvsig.xmlschema.som.IXSElementDeclaration;
14
import org.gvsig.xmlschema.som.IXSSchemaDocument;
15

    
16
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
17
 *
18
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
19
 *
20
 * This program is free software; you can redistribute it and/or
21
 * modify it under the terms of the GNU General Public License
22
 * as published by the Free Software Foundation; either version 2
23
 * of the License, or (at your option) any later version.
24
 *
25
 * This program is distributed in the hope that it will be useful,
26
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
 * GNU General Public License for more details.
29
 *
30
 * You should have received a copy of the GNU General Public License
31
 * along with this program; if not, write to the Free Software
32
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
33
 *
34
 * For more information, contact:
35
 *
36
 *  Generalitat Valenciana
37
 *   Conselleria d'Infraestructures i Transport
38
 *   Av. Blasco Ib??ez, 50
39
 *   46010 VALENCIA
40
 *   SPAIN
41
 *
42
 *      +34 963862235
43
 *   gvsig@gva.es
44
 *      www.gvsig.gva.es
45
 *
46
 *    or
47
 *
48
 *   IVER T.I. S.A
49
 *   Salamanca 50
50
 *   46005 Valencia
51
 *   Spain
52
 *
53
 *   +34 963163400
54
 *   dac@iver.es
55
 */
56
/* CVS MESSAGES:
57
 *
58
 * $Id: GPEWriterHandler.java 12418 2007-06-29 12:19:48Z jorpiell $
59
 * $Log$
60
 * Revision 1.15  2007-06-29 12:19:14  jorpiell
61
 * The schema validation is made independently of the concrete writer
62
 *
63
 * Revision 1.14  2007/06/28 13:04:33  jorpiell
64
 * The Qname has been updated to the 1.5 JVM machine. The schema validation is made in the GPEWriterHandlerImplementor class
65
 *
66
 * Revision 1.13  2007/06/22 12:19:52  jorpiell
67
 * The targetNamespace is now writen fine
68
 *
69
 * Revision 1.12  2007/06/20 09:35:37  jorpiell
70
 * Add the javadoc comments
71
 *
72
 * Revision 1.11  2007/06/14 13:50:06  jorpiell
73
 * The schema jar name has been changed
74
 *
75
 * Revision 1.10  2007/06/07 14:52:28  jorpiell
76
 * Add the schema support
77
 *
78
 * Revision 1.9  2007/05/16 12:06:22  jorpiell
79
 * Add Deafult methods
80
 *
81
 * Revision 1.8  2007/05/07 12:57:55  jorpiell
82
 * Add some methods to manage the multigeometries
83
 *
84
 * Revision 1.7  2007/05/07 07:06:26  jorpiell
85
 * Add a constructor with the name and the description fields
86
 *
87
 * Revision 1.6  2007/04/26 14:29:15  jorpiell
88
 * Add a getStringProperty method to the GEPDeafults
89
 *
90
 * Revision 1.5  2007/04/19 11:50:20  csanchez
91
 * Actualizacion protoripo libGPE
92
 *
93
 * Revision 1.4  2007/04/19 07:23:20  jorpiell
94
 * Add the add methods to teh contenhandler and change the register mode
95
 *
96
 * Revision 1.3  2007/04/14 16:06:13  jorpiell
97
 * The writer handler has been updated
98
 *
99
 * Revision 1.2  2007/04/13 13:14:55  jorpiell
100
 * Created the base tests and add some methods to the content handler
101
 *
102
 * Revision 1.1  2007/04/13 07:17:54  jorpiell
103
 * Add the writting tests for the simple geometries
104
 *
105
 * Revision 1.2  2007/04/12 17:06:42  jorpiell
106
 * First GML writing tests
107
 *
108
 * Revision 1.1  2007/04/12 10:20:40  jorpiell
109
 * Add the writer
110
 *
111
 *
112
 */
113
/**
114
 * This class make the XML schema validation and call
115
 * to the concrete parser to write the output file.
116
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
117
 * @author Carlos S?nchez Peri??n (sanchez_carper@gva.es)
118
 */
119
public class GPEWriterHandler {
120
        private IGPEWriterHandlerImplementor writerImplementor = null;        
121
        //Used to know the current parsed elements
122
        private TypedObjectStack layerStack = null;
123
        private TypedObjectStack featureStack = null;
124
        private TypedObjectStack elementStack = null;
125
        //Boolean variable that indicates that an object has
126
        //to be removed because it is not valid
127
        private boolean isRemoved = false;
128
        
129
        /**
130
         * The constructor
131
         * @param writerImplementor
132
         * The concrete writer handler implementation
133
         */
134
        public GPEWriterHandler(IGPEWriterHandlerImplementor writerImplementor){
135
                this.writerImplementor = writerImplementor;
136
                layerStack = new TypedObjectStack();
137
                featureStack = new TypedObjectStack();
138
                elementStack = new TypedObjectStack();
139
        }        
140

    
141
        /**
142
         * @return the description
143
         */
144
        public String getDescription() {
145
                return writerImplementor.getDescription();
146
        }
147

    
148
        /**
149
         * @return the name
150
         */
151
        public String getName() {
152
                return writerImplementor.getName();
153
        }
154

    
155
        /**
156
         * @return the deafult writting version
157
         */
158
        public String getDefaultVersion(){
159
                return writerImplementor.getDefaultVersion();
160
        }
161

    
162
        /**
163
         * @return the deafult format
164
         */
165
        public String getDefaultFormat(){
166
                return writerImplementor.getDefaultFormat();
167
        }
168

    
169
        /**
170
         * Creates the output file and 
171
         * @return the file
172
         */
173
        public File getOutputFile() {
174
                return writerImplementor.getOutputFile();                
175
        }
176

    
177

    
178
        /**
179
         * @return the errorHandler
180
         */
181
        public GPEErrorHandler getErrorHandler() {
182
                return writerImplementor.getErrorHandler();
183
        }
184

    
185
        /**
186
         * @param errorHandler the errorHandler to set
187
         */
188
        public void setErrorHandler(GPEErrorHandler errorHandler) {
189
                writerImplementor.setErrorHandler(errorHandler);
190
        }
191

    
192
        /**
193
         * @return the fileName
194
         */
195
        public String getFileName() {
196
                return writerImplementor.getFileName();
197
        }
198

    
199
        /**
200
         * @param fileName the fileName to set
201
         */
202
        public void setFileName(String fileName) {
203
                writerImplementor.setFileName(fileName);
204
        }
205

    
206
        /**
207
         * @return the format
208
         */
209
        public String getFormat() {
210
                return writerImplementor.getFormat();
211
        }
212

    
213
        /**
214
         * @param format the format to set
215
         */
216
        public void setFormat(String format) {
217
                writerImplementor.setFormat(format);
218
        }
219

    
220
        /**
221
         * @return the version
222
         */
223
        public String getVersion() {
224
                return writerImplementor.getVersion();
225
        }
226

    
227
        /**
228
         * @param version the version to set
229
         */
230
        public void setVersion(String version) {
231
                writerImplementor.setVersion(version);
232
        }        
233

    
234
        /*
235
         * (non-Javadoc)
236
         * @see org.gvsig.gpe.writers.IGPEWriterHandler#startPoint(java.lang.String, double, double, double, java.lang.String)
237
         */
238
        public void startPoint(String id, double x, double y, double z, String srs) {
239
                if (isRemoved) return;
240
                writerImplementor.startPoint(id, x, y, z, srs);
241
        }
242

    
243
        /*
244
         * (non-Javadoc)
245
         * @see org.gvsig.gpe.writers.IGPEWriterHandler#endPoint()
246
         */
247
        public void endPoint() {
248
                if (isRemoved) return;
249
                writerImplementor.endPoint();
250
        }
251

    
252
        /*
253
         * (non-Javadoc)
254
         * @see org.gvsig.gpe.writers.IGPEWriterHandler#startLineString(java.lang.String, double[], double[], double[], java.lang.String)
255
         */
256
        public void startLineString(String id, double[] x, double[] y, double[] z, String srs) {
257
                if (isRemoved) return;
258
                writerImplementor.startLineString(id, x, y, z, srs);
259
        }
260

    
261
        /*
262
         * (non-Javadoc)
263
         * @see org.gvsig.gpe.writers.IGPEWriterHandler#endLineString()
264
         */        
265
        public void endLineString() {
266
                if (isRemoved) return;
267
                writerImplementor.endLineString();
268
        }
269

    
270
        /*
271
         * (non-Javadoc)
272
         * @see org.gvsig.gpe.writers.IGPEWriterHandler#startLinearRing(java.lang.String, double[], double[], double[], java.lang.String)
273
         */
274
        public void startLinearRing(String id, double[] x, double[] y, double[] z, String srs) {
275
                if (isRemoved) return;
276
                if (!GeographicalUtils.isClosed(x, y, z)){
277
                        x = GeographicalUtils.closePolygon(x);
278
                        y = GeographicalUtils.closePolygon(y);
279
                        z = GeographicalUtils.closePolygon(z);        
280
                        getErrorHandler().addWarning(new PolygonAutomaticallyClosedWarning(x,y,z));
281
                }
282
                writerImplementor.startLinearRing(id, x, y, z, srs);
283
        }        
284

    
285
        /*
286
         * (non-Javadoc)
287
         * @see org.gvsig.gpe.writers.IGPEWriterHandler#endLinearRing()
288
         */
289
        public void endLinearRing() {
290
                if (isRemoved) return;
291
                writerImplementor.endLinearRing();
292
        }
293
        
294
        /*
295
         * (non-Javadoc)
296
         * @see org.gvsig.gpe.writers.IGPEWriterHandler#startPolygon(java.lang.String, double[], double[], double[], java.lang.String)
297
         */
298
        public void startPolygon(String id, double[] x, double[] y, double[] z, String srs) {
299
                if (isRemoved) return;
300
                if (!GeographicalUtils.isClosed(x, y, z)){
301
                        x = GeographicalUtils.closePolygon(x);
302
                        y = GeographicalUtils.closePolygon(y);
303
                        z = GeographicalUtils.closePolygon(z);        
304
                        getErrorHandler().addWarning(new PolygonAutomaticallyClosedWarning(x,y,z));
305
                }
306
                writerImplementor.startPolygon(id, x, y, z, srs);
307
        }
308

    
309
        /*
310
         * (non-Javadoc)
311
         * @see org.gvsig.gpe.writers.IGPEWriterHandler#endPolygon()
312
         */
313
        public void endPolygon() {
314
                if (isRemoved) return;
315
                writerImplementor.endPolygon();
316
        }
317
        
318
        /*
319
         * (non-Javadoc)
320
         * @see org.gvsig.gpe.writers.IGPEWriterHandler#startInnerBoundary(java.lang.String, double[], double[], double[], java.lang.String)
321
         */
322
        public void startInnerBoundary(String id, double[] x, double[] y, double[] z, String srs) {
323
                if (isRemoved) return;
324
                if (!GeographicalUtils.isClosed(x, y, z)){
325
                        x = GeographicalUtils.closePolygon(x);
326
                        y = GeographicalUtils.closePolygon(y);
327
                        z = GeographicalUtils.closePolygon(z);        
328
                        getErrorHandler().addWarning(new PolygonAutomaticallyClosedWarning(x,y,z));
329
                }
330
                writerImplementor.startInnerBoundary(id, x, y, z, srs);
331
        }
332
        
333
/*
334
 * (non-Javadoc)
335
 * @see org.gvsig.gpe.writers.IGPEWriterHandler#endInnerBoundary()
336
 */
337
        public void endInnerBoundary() {
338
                if (isRemoved) return;
339
                writerImplementor.endInnerBoundary();
340
        }
341

    
342
        /*
343
         * (non-Javadoc)
344
         * @see org.gvsig.gpe.writers.IGPEWriterHandler#initialize()
345
         */
346
        public void initialize() {
347
                if (isRemoved) return;
348
                writerImplementor.initialize();
349
        }
350
        
351
        /*
352
         * (non-Javadoc)
353
         * @see org.gvsig.gpe.writers.IGPEWriterHandler#close()
354
         */
355
        public void close() {
356
                writerImplementor.close();
357
        }
358

    
359
        /*
360
         * (non-Javadoc)
361
         * @see org.gvsig.gpe.writers.IGPEWriterHandler#startBbox(java.lang.String, double[], double[], double[], java.lang.String)
362
         */
363
        public void startBbox(String id, double[] x, double[] y, double[] z, String srs) {
364
                if (isRemoved) return;
365
                writerImplementor.startBbox(id, x, y, z, srs);
366
        }
367
        
368
        /*
369
         * (non-Javadoc)
370
         * @see org.gvsig.gpe.writers.IGPEWriterHandler#endBbox()
371
         */
372
        public void endBbox() {
373
                if (isRemoved) return;
374
                writerImplementor.endBbox();
375
        }
376

    
377
        /*
378
         * (non-Javadoc)
379
         * @see org.gvsig.gpe.writers.IGPEWriterHandler#startElement(java.lang.String, java.lang.Object, java.lang.Object)
380
         */
381
        public void startElement(String name, Object value, String xsElementName) {
382
                if (isRemoved){
383
                        elementStack.push(name,xsElementName,isRemoved);
384
                        return;
385
                }
386
                TypedObject feature = featureStack.lastElement();
387
                //If is a simple element
388
                if (elementStack.size() == 0){
389
                        //If the feature has type
390
                        if (feature.getType() != null){
391
                                IXSElementDeclaration xsFeature = getSchemaDocument().getElementDeclarationByName(feature.getType());
392
                                //If the feature is null has been declarated inside the layer
393
                                if (xsFeature == null){
394
                                        TypedObject layer = layerStack.lastElement();
395
                                        //The layer has to have a type
396
                                        IXSElementDeclaration xsLayer = getSchemaDocument().getElementDeclarationByName(layer.getType());
397
                                        xsFeature = xsLayer.getSubElementByName(feature.getType());
398
                                }
399
                                //If the feature has a type it will be always on the schema
400
                                IXSElementDeclaration xsElement = xsFeature.getSubElementByName(name);
401
                                //If the element doesn't exist on the feature
402
                                if (xsElement == null){
403
                                        isRemoved = true;
404
                                        getErrorHandler().addWarning(new NotSupportedElementWarning(name,
405
                                                        xsElementName,
406
                                                        feature.getName(),
407
                                                        feature.getType()));
408
                                }                                                                
409
                        }
410
                }else{ //Is a complex element
411
                        TypedObject parentElement = elementStack.lastElement();
412
                        //TODO complex element validation
413
                }                                
414
                elementStack.push(name,xsElementName,isRemoved);
415
                if (!isRemoved){
416
                        writerImplementor.startElement(name, value, xsElementName);                        
417
                }
418
        }
419

    
420
/*+
421
 * (non-Javadoc)
422
 * @see org.gvsig.gpe.writers.IGPEWriterHandler#endElement()
423
 */
424
        public void endElement() {
425
                TypedObject element = elementStack.pop();
426
                if (!element.isRemoved()){
427
                        writerImplementor.endElement();
428
                }else{
429
                        if (!layerStack.lastElement().isRemoved()){
430
                                if (!featureStack.lastElement().isRemoved()){
431
                                        if (elementStack.size() == 0){
432
                                                isRemoved = false;
433
                                        }else{
434
                                                TypedObject parentElement = elementStack.lastElement();
435
                                                isRemoved = parentElement.isRemoved();
436
                                        }
437
                                }
438
                        }
439
                }                
440
        }
441

    
442
        /*
443
         * (non-Javadoc)
444
         * @see org.gvsig.gpe.writers.IGPEWriterHandler#startLayer(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
445
         */
446
        public void startLayer(String id, String name, String description, String srs, String xsElementName) {
447
                if (isRemoved){
448
                        layerStack.push(name,xsElementName,isRemoved);
449
                        return;
450
                }
451
                //If is a root layer
452
                if (layerStack.size() == 0){
453
                        //If it has a schema
454
                        if (xsElementName != null){
455
                                //If the schema doesn't exists the layer can't be added
456
                                if (getSchemaDocument().getElementDeclarationByName(xsElementName) == null){
457
                                        isRemoved = true;
458
                                        getErrorHandler().addWarning(new NotSupportedLayerWarning(name,        
459
                                                        xsElementName));                                                        
460
                                }
461
                        }
462
                }else{//It is a child layer
463
                        TypedObject parentLayer = layerStack.lastElement();
464
                        //If the parent layer has a XML schema type
465
                        if (parentLayer.getType() != null){
466
                                IXSElementDeclaration xsParentLayer = getSchemaDocument().getElementDeclarationByName(parentLayer.getType());
467
                                //If the parent layer has a schema
468
                                if (xsParentLayer != null){
469
                                        IXSElementDeclaration xsLayer = xsParentLayer.getSubElementByName(xsElementName);
470
                                        //If the layer name doesn't exist on the parent schema
471
                                        if (xsLayer == null){
472
                                                isRemoved = true;
473
                                                getErrorHandler().addWarning(new NotSupportedLayerWarning(name,
474
                                                                xsElementName,
475
                                                                parentLayer.getName(),
476
                                                                parentLayer.getType()));
477
                                        }
478
                                }
479
                        }
480
                }                                
481
                layerStack.push(name,xsElementName,isRemoved);
482
                if (!isRemoved){                        
483
                        writerImplementor.startLayer(id, name, description, srs, xsElementName);
484
                }
485
        }
486
        
487
/*
488
 * (non-Javadoc)
489
 * @see org.gvsig.gpe.writers.IGPEWriterHandler#endLayer()
490
 */
491
        public void endLayer() {
492
                TypedObject layer = layerStack.pop();
493
                if (!layer.isRemoved()){
494
                        writerImplementor.endLayer();
495
                }else{
496
                        if (layerStack.size() == 0){
497
                                isRemoved = false;
498
                        }else{
499
                                TypedObject parentLayer = layerStack.lastElement();
500
                                isRemoved = parentLayer.isRemoved();                                
501
                        }
502
                }
503
        }
504

    
505
        /*
506
         * (non-Javadoc)
507
         * @see org.gvsig.gpe.writers.IGPEWriterHandler#startFeature(java.lang.String, java.lang.String)
508
         */
509
        public void startFeature(String id, String name, String xsElementName) {
510
                if (isRemoved){
511
                        featureStack.push(name,xsElementName,isRemoved);
512
                        return;
513
                }
514
                TypedObject layer = layerStack.lastElement();
515
                //If the parent layer has type
516
                if (layer.getType() != null){
517
                        IXSElementDeclaration xsLayer = getSchemaDocument().getElementDeclarationByName(layer.getType());
518
                        //If the layer schema exists
519
                        if (xsLayer != null){
520
                                IXSElementDeclaration xsFeature = xsLayer.getSubElementByName(xsElementName);
521
                                //If the feature type doesn't exist on the layer
522
                                if (xsFeature == null){
523
                                        isRemoved = true;
524
                                        getErrorHandler().addWarning(new NotSupportedFeatureWarning(name,        
525
                                                        xsElementName,
526
                                                        layer.getName(),
527
                                                        layer.getType()));        
528
                                }                                
529
                        }
530
                }else{//The layer doesn't has type
531
                        //If the feature has type
532
                        if (xsElementName != null){
533
                                //If the feature type is not on the schema
534
                                if (getSchemaDocument().getElementDeclarationByName(xsElementName) == null){
535
                                        isRemoved = true;
536
                                        getErrorHandler().addWarning(new NotSupportedFeatureWarning(name,        
537
                                                        xsElementName));                                                                
538
                                }
539
                        }
540
                }
541
                featureStack.push(name,xsElementName,isRemoved);
542
                if (!isRemoved){                        
543
                        writerImplementor.startFeature(id, name, xsElementName);
544
                }
545
        }
546

    
547
        /*
548
         * (non-Javadoc)
549
         * @see org.gvsig.gpe.writers.IGPEWriterHandler#endFeature()
550
         */
551
        public void endFeature() {
552
                TypedObject feature = featureStack.pop();
553
                if (!feature.isRemoved()){
554
                        writerImplementor.endFeature();
555
                }else{
556
                        if (!layerStack.lastElement().isRemoved()){
557
                                if (featureStack.size() == 0){
558
                                        isRemoved = false;
559
                                }else{
560
                                        TypedObject parentFeature = featureStack.lastElement();
561
                                        isRemoved = parentFeature.isRemoved();
562
                                }
563
                        }
564
                }                
565
        }
566

    
567
        /*
568
         * (non-Javadoc)
569
         * @see org.gvsig.gpe.writers.IGPEWriterHandler#startMultiPoint(java.lang.String, java.lang.String)
570
         */
571
        public void startMultiPoint(String id, String srs) {
572
                if (isRemoved) return;
573
                writerImplementor.startMultiPoint(id, srs);
574
        }
575

    
576
        /*
577
         * (non-Javadoc)
578
         * @see org.gvsig.gpe.writers.IGPEWriterHandler#endMuliPoint()
579
         */
580
        public void endMultiPoint() {
581
                if (isRemoved) return;
582
                writerImplementor.endMultiPoint();
583
        }
584

    
585
        /*
586
         * (non-Javadoc)
587
         * @see org.gvsig.gpe.writers.IGPEWriterHandler#startMultiLineString(java.lang.String, java.lang.String)
588
         */
589
        public void startMultiLineString(String id, String srs) {
590
                if (isRemoved) return;
591
                writerImplementor.startMultiLineString(id, srs);
592
        }
593

    
594
        /*
595
         * (non-Javadoc)
596
         * @see org.gvsig.gpe.writers.IGPEWriterHandler#endMultiLineString()
597
         */
598
        public void endMultiLineString() {
599
                if (isRemoved) return;
600
                writerImplementor.endMultiLineString();
601
        }
602

    
603
        /*
604
         * (non-Javadoc)
605
         * @see org.gvsig.gpe.writers.IGPEWriterHandler#startMultiPolygon(java.lang.String, java.lang.String)
606
         */
607
        public void startMultiPolygon(String id, String srs) {
608
                if (isRemoved) return;
609
                writerImplementor.startMultiPolygon(id, srs);
610
        }
611

    
612
        /*
613
         * (non-Javadoc)
614
         * @see org.gvsig.gpe.writers.IGPEWriterHandler#endMultiPolygon()
615
         */
616
        public void endMultiPolygon() {
617
                if (isRemoved) return;
618
                writerImplementor.endMultiPolygon();
619
        }
620

    
621
        /*
622
         * (non-Javadoc)
623
         * @see org.gvsig.gpe.writers.IGPEWriterHandler#startMultiGeometry(java.lang.String, java.lang.String)
624
         */
625
        public void startMultiGeometry(String id, String srs) {
626
                if (isRemoved) return;
627
                writerImplementor.startMultiGeometry(id, srs);
628
        }
629

    
630
        /*
631
         * (non-Javadoc)
632
         * @see org.gvsig.gpe.writers.IGPEWriterHandler#endMultiGeometry()
633
         */
634
        public void endMultiGeometry() {
635
                if (isRemoved) return;
636
                writerImplementor.endMultiGeometry();
637
        }
638

    
639
        /**
640
         * @return the schemaDocument
641
         */
642
        public IXSSchemaDocument getSchemaDocument() {
643
                return writerImplementor.getSchemaDocument();
644
        }
645

    
646
        /**
647
         * @param schemaDocument the schemaDocument to set
648
         */
649
        public void setSchemaDocument(IXSSchemaDocument schemaDocument) {
650
                writerImplementor.setSchemaDocument(schemaDocument);
651
        }        
652
}