Statistics
| Revision:

root / tags / v2_0_0_Build_2020 / libraries / libRemoteServices / src / org / gvsig / remoteclient / wfs / schema / XMLSchemaParser.java @ 33963

History | View | Annotate | Download (18.1 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.kxml2.io.KXmlParser;
8
import org.xmlpull.v1.XmlPullParserException;
9

    
10
import org.gvsig.compat.CompatLocator;
11
import org.gvsig.compat.lang.StringUtils;
12
import org.gvsig.remoteclient.utils.CapabilitiesTags;
13
import org.gvsig.remoteclient.utils.EncodingXMLParser;
14
import org.gvsig.remoteclient.wfs.schema.type.XMLComplexType;
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: XMLSchemaParser.java 18271 2008-01-24 09:06:43Z jpiera $
59
 * $Log$
60
 * Revision 1.7  2007-01-15 13:11:00  csanchez
61
 * Sistema de Warnings y Excepciones adaptado a BasicException
62
 *
63
 * Revision 1.6  2006/12/29 17:15:48  jorpiell
64
 * Se tienen en cuenta los simpleTypes y los choices, adem?s de los atributos multiples
65
 *
66
 * Revision 1.5  2006/12/22 11:25:44  csanchez
67
 * Nuevo parser GML 2.x para gml's sin esquema
68
 *
69
 * Revision 1.4  2006/10/10 12:52:28  jorpiell
70
 * Soporte para features complejas.
71
 *
72
 * Revision 1.3  2006/10/02 08:33:49  jorpiell
73
 * Cambios del 10 copiados al head
74
 *
75
 * Revision 1.1.2.1  2006/09/19 12:23:15  jorpiell
76
 * Ya no se depende de geotools
77
 *
78
 * Revision 1.2  2006/09/18 12:08:55  jorpiell
79
 * Se han hecho algunas modificaciones que necesitaba el WFS
80
 *
81
 * Revision 1.1  2006/08/10 12:00:49  jorpiell
82
 * Primer commit del driver de Gml
83
 *
84
 * Revision 1.1  2006/05/16 14:12:56  jorpiell
85
 * A?adido el parseador de Esquemas
86
 * 
87
 * 
88
 */
89
/**
90
 * Thas class is used to parse a schema XSD
91
 * 
92
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
93
 * @author Carlos S?nchez Peri??n (sanchez_carper@gva.es)
94
 * 
95
 */
96
public class XMLSchemaParser extends EncodingXMLParser {
97
        private String targetNameSpace = null;
98
        private String schema = "";
99
        private String encoding = "UTF-8";        
100
        private String nameSpace = "";
101
        private String version = null;
102
        
103
        private static final StringUtils stringUtils = CompatLocator.getStringUtils();
104
        
105
        public XMLSchemaParser(){
106
                super();                
107
        }
108
        
109
        public XMLSchemaParser(String schema){
110
                super();
111
                //schema instace is named with the string in "schema"
112
                this.schema = schema;                
113
        }
114
        
115
        /**
116
         * It gets the schema from a tag. The schema is separated
117
         * of the tag name by ":".
118
         * @param tag
119
         */
120
        public void setSchemaFromMainTag(String tag){
121
                //set the name string of the namespace.
122
                int pos = tag.indexOf(":");
123
                if (pos > 0){
124
                        this.schema = tag.substring(0,pos);
125
                }else{
126
                        this.schema = "";
127
                }
128
        }        
129

    
130
        /**
131
         * @return Returns the schema.
132
         */
133
        public String getSchema() {
134
                return schema;
135
        }
136

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

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

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

    
643
        public String getversion() {
644
                if (version == null){
645
                        //return the default GML version
646
                        return "99.99.99";
647
                }
648
                return version;                
649
        }
650
        public String getTargetNamespace() {
651
                return targetNameSpace;                
652
        }
653
}