Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libRemoteServices / src / org / gvsig / remoteClient / wfs / schema / XMLSchemaParser.java @ 28543

History | View | Annotate | Download (17.9 KB)

1
package org.gvsig.remoteClient.wfs.schema;
2

    
3
import java.io.File;
4
import java.io.FileReader;
5
import java.io.IOException;
6

    
7
import org.gvsig.remoteClient.utils.CapabilitiesTags;
8
import org.gvsig.remoteClient.utils.EncodingXMLParser;
9
import org.gvsig.remoteClient.wfs.schema.type.XMLComplexType;
10
import org.kxml2.io.KXmlParser;
11
import org.xmlpull.v1.XmlPullParserException;
12

    
13
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
14
 *
15
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
16
 *
17
 * This program is free software; you can redistribute it and/or
18
 * modify it under the terms of the GNU General Public License
19
 * as published by the Free Software Foundation; either version 2
20
 * of the License, or (at your option) any later version.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 * GNU General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU General Public License
28
 * along with this program; if not, write to the Free Software
29
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
30
 *
31
 * For more information, contact:
32
 *
33
 *  Generalitat Valenciana
34
 *   Conselleria d'Infraestructures i Transport
35
 *   Av. Blasco Ib??ez, 50
36
 *   46010 VALENCIA
37
 *   SPAIN
38
 *
39
 *      +34 963862235
40
 *   gvsig@gva.es
41
 *      www.gvsig.gva.es
42
 *
43
 *    or
44
 *
45
 *   IVER T.I. S.A
46
 *   Salamanca 50
47
 *   46005 Valencia
48
 *   Spain
49
 *
50
 *   +34 963163400
51
 *   dac@iver.es
52
 */
53
/* CVS MESSAGES:
54
 *
55
 * $Id: XMLSchemaParser.java 18271 2008-01-24 09:06:43Z jpiera $
56
 * $Log$
57
 * Revision 1.7  2007-01-15 13:11:00  csanchez
58
 * Sistema de Warnings y Excepciones adaptado a BasicException
59
 *
60
 * Revision 1.6  2006/12/29 17:15:48  jorpiell
61
 * Se tienen en cuenta los simpleTypes y los choices, adem?s de los atributos multiples
62
 *
63
 * Revision 1.5  2006/12/22 11:25:44  csanchez
64
 * Nuevo parser GML 2.x para gml's sin esquema
65
 *
66
 * Revision 1.4  2006/10/10 12:52:28  jorpiell
67
 * Soporte para features complejas.
68
 *
69
 * Revision 1.3  2006/10/02 08:33:49  jorpiell
70
 * Cambios del 10 copiados al head
71
 *
72
 * Revision 1.1.2.1  2006/09/19 12:23:15  jorpiell
73
 * Ya no se depende de geotools
74
 *
75
 * Revision 1.2  2006/09/18 12:08:55  jorpiell
76
 * Se han hecho algunas modificaciones que necesitaba el WFS
77
 *
78
 * Revision 1.1  2006/08/10 12:00:49  jorpiell
79
 * Primer commit del driver de Gml
80
 *
81
 * Revision 1.1  2006/05/16 14:12:56  jorpiell
82
 * A?adido el parseador de Esquemas
83
 * 
84
 * 
85
 */
86
/**
87
 * Thas class is used to parse a schema XSD
88
 * 
89
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
90
 * @author Carlos S?nchez Peri??n (sanchez_carper@gva.es)
91
 * 
92
 */
93
public class XMLSchemaParser extends EncodingXMLParser {
94
        private String targetNameSpace = null;
95
        private String schema = "";
96
        private String encoding = "UTF-8";        
97
        private String nameSpace = "";
98
        private String version = null;
99
        
100
        
101
        public XMLSchemaParser(){
102
                super();                
103
        }
104
        
105
        public XMLSchemaParser(String schema){
106
                super();
107
                //schema instace is named with the string in "schema"
108
                this.schema = schema;                
109
        }
110
        
111
        /**
112
         * It gets the schema from a tag. The schema is separated
113
         * of the tag name by ":".
114
         * @param tag
115
         */
116
        public void setSchemaFromMainTag(String tag){
117
                //set the name string of the namespace.
118
                int pos = tag.indexOf(":");
119
                if (pos > 0){
120
                        this.schema = tag.substring(0,pos);
121
                }else{
122
                        this.schema = "";
123
                }
124
        }        
125

    
126
        /**
127
         * @return Returns the schema.
128
         */
129
        public String getSchema() {
130
                return schema;
131
        }
132

    
133
        /**
134
         * @param schema The schema to set.
135
         */
136
        public void setSchema(String schema) {
137
                this.schema = schema;
138
        }        
139
        
140
        /**
141
         * Returns a SCHEMA:TAG
142
         * @param tag
143
         * @return SCHEMA:TAG
144
         */
145
        private String getTag(String tag){
146
                //get the tag without the namespace
147
                if (tag == null){
148
                        return null;
149
                }
150
                if ((schema == null) || (schema.equals(""))){
151
                        return tag;
152
                }
153
                return schema + ":" + tag;
154
        }
155
        
156
        /*
157
         *  (non-Javadoc)
158
         * @see org.xmlpull.v1.XmlPullParser#require(int, java.lang.String, java.lang.String)
159
         */
160
        public void require(int type, String namespace, String name)
161
                throws XmlPullParserException, IOException{
162
                super.require(type,namespace,getTag(name));
163
        }
164
        
165
        /*
166
         *  (non-Javadoc)
167
         * @see org.xmlpull.v1.XmlPullParser#getName()
168
         */
169
        public String getName(){
170
                try{
171
                String name = super.getName();
172
                if ((schema != null) || (!(schema.equals("")))){
173
                        return name.substring(name.indexOf(":") + 1,name.length());
174
                }
175
                return name;
176
                }catch (NullPointerException e){
177
                        return "";
178
                }
179
        }
180
        
181
        /*
182
         *  (non-Javadoc)
183
         * @see org.xmlpull.v1.XmlPullParser#getName()
184
         */
185
        public String getNameSpace(){
186
                try{
187
                String name = super.getName();
188
                if ((name!=null)&&(name.split(":").length > 1)){
189
                        return name.split(":")[0];
190
                }
191
                return "";
192
                }catch (NullPointerException e){
193
                        return "";
194
                }
195
        }
196
        
197
        /********************************************************************************
198
         *  FUNCTION PARSE(FILE,NAMESPACE) PARSING THE XML SCHEMA ".xsd"                                   *
199
         *                                                                                                                                                                    *
200
         *          -gml version                                                                                                                         *
201
         *          -elementFormDefault {qualified,non-qualified} (not implemented yet)                *
202
         *          -namespaces "xmlns" (nor implemented,only in gml file)                                         *
203
         *          -targetNamespace (the name of this schema)                                                                 *
204
         *                -imports (not implemented)                                                                                                   *  
205
         *                -features -> "elements"="types"                                                                                          *
206
         *                                                                                                                                                                *        
207
         * @param f (file ".xsd" to parse)                                                                                                *
208
         * @param nameSpace (Schemas name)                                                                                                *
209
         * @throws IOException                                                                                                                         *
210
         * @throws XmlPullParserException                                                                                                 *
211
         *                                                                                                                                                                *
212
         ********************************************************************************/
213
        public void parse(File f,String nameSpace) throws XmlPullParserException, IOException{
214
                this.nameSpace = nameSpace;
215
                FileReader reader = null;       
216
                int tag;
217
                
218
                setInput(f);
219
                nextTag();
220
                        
221
                //Parsing attributes from the head tag...
222
                if ( getEventType() != KXmlParser.END_DOCUMENT ) 
223
                {     
224
                        /************************
225
                         * Etiqueta <schema>        *
226
                         ************************/
227
                        setSchemaFromMainTag(getName());
228
                        //Searching the init tag "schema"
229
                        require(KXmlParser.START_TAG, null, CapabilitiesTags.WFS_SCHEMAROOT); 
230
                        for (int i=0 ; i<getAttributeCount() ; i++){
231
                                //getting attributes and values
232
                                String attName = getAttributeName(i);
233
                                String attValue = getAttributeValue(i);
234
                                
235
                                /********************************
236
                                 * Atributo <targetNamespace>        *
237
                                 ********************************/
238
                                //Target Namespace (URI)
239
                                //this is the namespace of all components in the schema
240
                                //setTargetNamespace(); 
241
                                if (attName.compareTo(GMLTags.XML_TARGET_NAMESPACE)==0){
242
                                        targetNameSpace = attValue;
243
                                }
244
                                
245
                                /************************************
246
                                 * Atributo <elementFormDefault>        *
247
                                 ************************************/
248
                                //Qualified--> Los elementos del espacio de nombres de destino deben cualificarse 
249
                                //con el prefijo del espacio de nombres.
250
                                //Unqualified--> No es necesario que los elementos del 
251
                                //espacio de nombres de destino est?n cualificados con el prefijo del espacio de nombres.
252
                                //(Espacion_de_Nombres:Elemento)
253
                                //elementFormDefault(); 
254
                                if (attName.compareTo(GMLTags.XML_ELEMENT_FORM_DEFAULT)==0){
255
                                }
256
                                
257
                                /************************************
258
                                 * Atributo <attributeFormDefault>        *
259
                                 ************************************/
260
                                //Lo mismo que el anterior pero con los atributos...
261
                                //(Espacio_de_Nombres:Atributo)
262
                                //attributeFormDefault();
263
                                if (attName.compareTo(GMLTags.XML_ATTRIBUTE_FORM_DEFAULT)==0){
264
                                }
265
                                
266
                                /************************
267
                                 * Atributo <Version>        *
268
                                 ************************/
269
                                //Gets gml version to parse by the right parser.
270
                                //getversion();
271
                                if (attName.compareTo(GMLTags.VERSION)==0){
272
                                        version=attValue;
273
                                }
274
                        }
275
                        tag = nextTag();
276
                        
277
                        while(tag != KXmlParser.END_DOCUMENT)
278
                        {
279
                                switch(tag)
280
                                {
281
                                case KXmlParser.START_TAG:
282
                                        
283
                                        /************************
284
                                         * Etiqueta <import>        *
285
                                         ************************/
286
                                        //imports elements from other schemas (other schema  ".xsd" files)
287
                                        
288
                                        /****************************
289
                                         * Etiqueta <complexType>        *
290
                                         ****************************/
291
                                        if (getName().compareTo(CapabilitiesTags.COMPLEXTYPE)==0){                                                        
292
                                                for (int i=0 ; i<getAttributeCount() ; i++){
293
                                                        /********************
294
                                                         * Atributo <name>        *
295
                                                         ********************/
296
                                                        if (getAttributeName(i).compareTo(GMLTags.GML_NAME) == 0){
297
                                                                // inserts a new complex type inside the namespace
298
                                                                XMLComplexType complexType = XMLTypesFactory.addComplexType(nameSpace,getAttributeValue(i));
299
                                                            parseComplexType(complexType);                                                                    
300
                                                    }
301
                                                    
302
                                                }
303
                                        }
304
                                        /****************************
305
                                         * Etiqueta <simpleType>        *
306
                                         ****************************/
307
                                        // SIMPLE TYPE elements like enumerations not implemented 
308
                                        else if (getName().compareTo(CapabilitiesTags.SIMPLETYPE)==0){
309
                                                parseSimpleType();
310
                                        }
311
                                        /************************
312
                                         * Etiqueta <element>        *
313
                                         ************************/
314
                                        else if (getName().compareTo(CapabilitiesTags.ELEMENT)==0){                                                        
315
                                                XMLElement entity = XMLElementsFactory.addType(this);                                                
316
                                        }
317
                                        break;
318
                                        case KXmlParser.END_TAG:                            
319
                                                break;
320
                                        //Show the Text on the screen
321
                                        case KXmlParser.TEXT:
322
                                                                
323
                                                break;
324
                                }
325
                                tag = next();
326
                        }
327
                        require(KXmlParser.END_DOCUMENT, null, null);
328
                }
329
        }
330
        
331
        /****************************************************
332
         *  FUNCTION PARSE SIMPLE TYPE()                                            *
333
         *                                                                                                     *
334
         *  Parse simple types and its restrictions                         *
335
         *                                                                                                        *        
336
         ****************************************************/
337
    private void parseSimpleType() throws IOException, XmlPullParserException{   
338
                int currentTag;
339
                boolean end = false;                
340
                currentTag = getEventType();
341
                
342
                String typeName = null;
343
                String typeValue = null;
344

    
345
                for (int i=0 ; i<getAttributeCount() ; i++){                        
346
                        if (getAttributeName(i).compareTo(CapabilitiesTags.ELEMENT_NAME) == 0){
347
                                typeName = getAttributeValue(i);
348
                        }
349
                }
350
                
351
                while (!end){
352
                        switch(currentTag){
353
                        case KXmlParser.START_TAG:
354
                                if (getName().compareTo(CapabilitiesTags.RESTRICTION)==0){
355
                                        for (int i=0 ; i<getAttributeCount() ; i++){
356
                                                if (getAttributeName(i).compareTo(CapabilitiesTags.BASE) == 0){
357
                                                        typeValue = getAttributeValue(i);
358
                                                }
359
                                        }                                        
360
                                }   
361
                                //Falta parsear los tipos enumerados
362
                                break;
363
                        case KXmlParser.END_TAG:
364
                                if (getName().compareTo(CapabilitiesTags.SIMPLETYPE) == 0)
365
                                        end = true;
366
                                break;
367
                        case KXmlParser.TEXT:                   
368
                                break;
369
                        }
370
                        if (!end){
371
                                currentTag = next();
372
                        }                        
373
                }
374
                if ((typeName != null) && (typeValue != null)){
375
                        XMLTypesFactory.addSimpleType(typeName,typeValue);
376
                }
377
        }
378
    
379
    /****************************************************
380
         *  FUNCTION PARSE COMPLEX TYPE(COMPLEX TYPE)                    *
381
         *                                                                                                            *
382
         *  Parse the attributes from a complex type                *
383
         *                                                                                                        *        
384
         * @param complexType                                                                *
385
         *                                                                                                        *
386
         ****************************************************/
387
        
388
        private void parseComplexType(XMLComplexType complexType) throws IOException, XmlPullParserException{   
389
                int currentTag;
390
                boolean end = false;                
391
                
392
                require(KXmlParser.START_TAG, null, CapabilitiesTags.COMPLEXTYPE);
393
                currentTag = next();
394
                
395
                while (!end) 
396
                {
397
                        switch(currentTag)
398
                        {
399
                        case KXmlParser.START_TAG:
400
                                /********************************
401
                                 * Etiqueta <complexContent>        *
402
                                 ********************************/
403
                                if (getName().compareTo(CapabilitiesTags.COMPLEXCONTENT)==0){
404
                                        parseComplexContent(complexType); 
405
                                }
406
                                /************************
407
                                 * Etiqueta <sequence>        *
408
                                 ************************/
409
                                else if(getName().compareTo(CapabilitiesTags.SEQUENCE)==0){
410
                                        parseSequence(complexType);
411
                                }
412
                                /************************
413
                                 * Etiqueta <choice>        *
414
                                 ************************/
415
                                else if(getName().compareTo(CapabilitiesTags.CHOICE)==0){
416
                                        parseChoice(complexType);
417
                                }
418
                                break;
419
                        case KXmlParser.END_TAG:
420
                                if (getName().compareTo(CapabilitiesTags.COMPLEXTYPE) == 0)
421
                                        end = true;
422
                                break;
423
                        case KXmlParser.TEXT:                   
424
                                break;
425
                        }
426
                        if (!end){
427
                                currentTag = next();
428
                        }        
429
                }                
430
        }
431
        
432
        /************************************************************************************
433
         *  FUNCION PARSE COMPLEX CONTENT(COMPLEX TYPE)                                                                                   *
434
         *                                                                                                                                                                            *
435
         *  Parsea los atributos que componen un contenido complejo                                                  *
436
         *                                                                                                                                                                        *        
437
         * @param complexType (elemento complejo a parsear)                                                                        *
438
         *                                                                                                                                                                        *
439
         ************************************************************************************/
440
        
441
        private void parseComplexContent(XMLComplexType complexType) throws IOException, XmlPullParserException
442
        {   
443
                int currentTag;
444
                boolean end = false;
445
                
446
                //Establece como requisito abrir una etiqueta <complexContent>
447
                require(KXmlParser.START_TAG, null, CapabilitiesTags.COMPLEXCONTENT);
448
                currentTag = next();
449
                
450
                while (!end) 
451
                {
452
                        switch(currentTag)
453
                        {
454
                        //Comparo las posibles etiquetas de apertura, las cuales solo nos interesa <extension>
455
                        case KXmlParser.START_TAG:
456
                                if (getName().compareTo(CapabilitiesTags.EXTENSION )==0){
457
                                        parseExtension(complexType); 
458
                                }else if (getName().compareTo(CapabilitiesTags.RESTRICTION)==0){
459
                                        parseRestriction(complexType); 
460
                                } 
461
                                break;
462
                        //encuentro una etiqueta de cierre de complexContent con lo cual cierro el tipo complejo
463
                        case KXmlParser.END_TAG:
464
                                if (getName().compareTo(CapabilitiesTags.COMPLEXCONTENT) == 0)
465
                                        end = true;
466
                                break;
467
                        //Siempre puede existir texto por el medio que ignoro
468
                        case KXmlParser.TEXT:                   
469
                                break;
470
                        }
471
                        if (!end){
472
                                currentTag = next();
473
                        }        
474
                }
475
        }
476
        
477
        //Comprueba que las etiquetas extension se abren y se cierran
478
        private void parseExtension(XMLComplexType complexType) throws IOException, XmlPullParserException
479
        {   
480
                int currentTag;
481
                boolean end = false;
482
                                
483
                require(KXmlParser.START_TAG, null, CapabilitiesTags.EXTENSION);
484
                for (int i=0 ; i<getAttributeCount() ; i++){
485
                        String attName = getAttributeName(i);
486
                        String attValue = getAttributeValue(i);
487
                        if (CapabilitiesTags.BASE.equals(attName)){
488
                                complexType.setBaseType(attValue);                                
489
                        }
490
                }
491
                
492
                currentTag = next();                
493
                while (!end) 
494
                {
495
                        switch(currentTag)
496
                        {
497
                        case KXmlParser.START_TAG:
498
                                if (getName().compareTo(CapabilitiesTags.SEQUENCE)==0)
499
                                {
500
                                        parseSequence(complexType); 
501
                                }   
502
                                break;
503
                        case KXmlParser.END_TAG:
504
                                if (getName().compareTo(CapabilitiesTags.EXTENSION) == 0)
505
                                        end = true;
506
                                break;
507
                        case KXmlParser.TEXT:                   
508
                                break;
509
                        }
510
                        if (!end){
511
                                currentTag = next();
512
                        }                        
513
                }
514
        }
515
        
516
        //Comprueba que las etiquetas extension se abren y se cierran
517
        private void parseRestriction(XMLComplexType complexType) throws IOException, XmlPullParserException
518
        {   
519
                int currentTag;
520
                boolean end = false;
521
                
522
                
523
                require(KXmlParser.START_TAG, null, CapabilitiesTags.RESTRICTION);
524
                currentTag = next();
525
                
526
                while (!end) 
527
                {
528
                        switch(currentTag)
529
                        {
530
                        case KXmlParser.START_TAG:
531
                                if (getName().compareTo(CapabilitiesTags.SEQUENCE)==0)
532
                                {
533
                                        parseSequence(complexType); 
534
                                }   
535
                                break;
536
                        case KXmlParser.END_TAG:
537
                                if (getName().compareTo(CapabilitiesTags.RESTRICTION) == 0)
538
                                        end = true;
539
                                break;
540
                        case KXmlParser.TEXT:                   
541
                                break;
542
                        }
543
                        if (!end){
544
                                currentTag = next();
545
                        }                        
546
                }
547
        }
548
        
549
        private void parseSequence(XMLComplexType complexType) throws IOException, XmlPullParserException
550
        {   
551
                int currentTag;
552
                boolean end = false;
553
                XMLElement elemento_previo=null;
554
                
555
                require(KXmlParser.START_TAG, null, CapabilitiesTags.SEQUENCE);
556
                currentTag = next();
557
                
558
                while (!end) 
559
                {
560
                        switch(currentTag)
561
                        {
562
                        case KXmlParser.START_TAG:
563
                                /************************
564
                                 * Etiqueta <Element>        *
565
                                 ************************/
566
                                if (getName().compareTo(CapabilitiesTags.ELEMENT)==0){
567
                                        XMLElement element = new XMLElement(this);
568
                                        if (element != null){
569
                                                complexType.addElements(element);
570
                                        }
571
                                        elemento_previo=element;
572
                                }
573
                                /****************************
574
                                 * Etiqueta <ComplexType>        *
575
                                 ****************************/
576
                                else if(getName().compareTo(CapabilitiesTags.COMPLEXTYPE)==0){
577
                                        for (int i=0 ; i<getAttributeCount() ; i++){
578
                                            if (getAttributeName(i).compareTo(GMLTags.GML_NAME) == 0){
579
                                                    XMLComplexType complexTypeChild = XMLTypesFactory.addComplexType(nameSpace,getAttributeValue(i));
580
                                                    parseComplexType(complexTypeChild);                                                                    
581
                                            }                                            
582
                                        }
583
                                }
584
                                else if(getName().compareTo(CapabilitiesTags.SIMPLETYPE)==0){
585
                                        elemento_previo.parseSimpleType(this);
586
                                }
587
                                break;
588
                        case KXmlParser.END_TAG:
589
                                if (getName().compareTo(CapabilitiesTags.SEQUENCE) == 0)
590
                                        end = true;
591
                                break;
592
                        case KXmlParser.TEXT:                   
593
                                break;
594
                        }
595
                        if (!end){
596
                                currentTag = next();
597
                        }
598
                }                
599
        }
600

    
601
        private void parseChoice(XMLComplexType complexType) throws IOException, XmlPullParserException
602
        {   
603
                int currentTag;
604
                boolean end = false;
605
                        
606
                require(KXmlParser.START_TAG, null, CapabilitiesTags.CHOICE);
607
                currentTag = next();
608
                
609
                complexType.setAttributesType(XMLComplexType.CHOICE_TYPE);
610
                
611
                while (!end) 
612
                {
613
                        switch(currentTag)
614
                        {
615
                        case KXmlParser.START_TAG:
616
                                /************************
617
                                 * Etiqueta <Element>        *
618
                                 ************************/
619
                                if (getName().compareTo(CapabilitiesTags.ELEMENT)==0){
620
                                        XMLElement element = new XMLElement(this);
621
                                        if (element != null){
622
                                                complexType.addElements(element);
623
                                        }                                        
624
                                }                                
625
                                break;
626
                        case KXmlParser.END_TAG:
627
                                if (getName().compareTo(CapabilitiesTags.CHOICE) == 0)
628
                                        end = true;
629
                                break;
630
                        case KXmlParser.TEXT:                   
631
                                break;
632
                        }
633
                        if (!end){
634
                                currentTag = next();
635
                        }
636
                }                
637
        }
638

    
639
        public String getversion() {
640
                if (version == null){
641
                        //return the default GML version
642
                        return "99.99.99";
643
                }
644
                return version;                
645
        }
646
        public String getTargetNamespace() {
647
                return targetNameSpace;                
648
        }
649
}