Statistics
| Revision:

svn-gvsig-desktop / tags / v10_RC2c / libraries / libRemoteServices / src / org / gvsig / remoteClient / gml / schemas / XMLSchemaParser.java @ 8745

History | View | Annotate | Download (8.67 KB)

1
package org.gvsig.remoteClient.gml.schemas;
2

    
3
import java.io.BufferedReader;
4
import java.io.File;
5
import java.io.FileInputStream;
6
import java.io.FileNotFoundException;
7
import java.io.FileReader;
8
import java.io.IOException;
9
import java.util.Hashtable;
10

    
11
import org.gvsig.remoteClient.gml.GMLTags;
12
import org.gvsig.remoteClient.gml.factories.XMLElementsFactory;
13
import org.gvsig.remoteClient.gml.factories.XMLTypesFactory;
14
import org.gvsig.remoteClient.utils.CapabilitiesTags;
15
import org.gvsig.remoteClient.wfs.WFSAttribute;
16
import org.gvsig.remoteClient.wfs.WFSSchemaParser;
17
import org.kxml2.io.KXmlParser;
18
import org.xmlpull.v1.XmlPullParserException;
19

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

    
108
        /**
109
         * @return Returns the schema.
110
         */
111
        public String getSchema() {
112
                return schema;
113
        }
114

    
115
        /**
116
         * @param schema The schema to set.
117
         */
118
        public void setSchema(String schema) {
119
                this.schema = schema;
120
        }        
121
        
122
        /**
123
         * Returns a SCHEMA:TAG
124
         * @param tag
125
         * @return SCHEMA:TAG
126
         */
127
        private String getTag(String tag){
128
                if (tag == null){
129
                        return null;
130
                }
131
                if ((schema == null) || (schema.equals(""))){
132
                        return tag;
133
                }
134
                return schema + ":" + tag;
135
        }
136
        
137
        /*
138
         *  (non-Javadoc)
139
         * @see org.xmlpull.v1.XmlPullParser#require(int, java.lang.String, java.lang.String)
140
         */
141
        public void require(int type, String namespace, String name)
142
                throws XmlPullParserException, IOException{
143
                super.require(type,namespace,getTag(name));
144
        }
145
        
146
        /*
147
         *  (non-Javadoc)
148
         * @see org.xmlpull.v1.XmlPullParser#getName()
149
         */
150
        public String getName(){
151
                try{
152
                String name = super.getName();
153
                if ((schema != null) || (!(schema.equals("")))){
154
                        return name.substring(name.indexOf(":") + 1,name.length());
155
                }
156
                return name;
157
                }catch (NullPointerException e){
158
                        return "";
159
                }
160
        }
161
        
162
        public void parse(File f,String nameSpace) {
163
                FileReader reader = null;       
164
                try
165
                {
166
                        reader = new FileReader(f);
167
                        BufferedReader br = new BufferedReader(reader);
168
                        char[] buffer = new char[100];
169
                        br.read(buffer);
170
                        StringBuffer st = new StringBuffer(new String(buffer));
171
                        String searchText = "encoding=\"";
172
                        int index = st.indexOf(searchText);
173
                        if (index>-1) {
174
                                st.delete(0, index+searchText.length());
175
                                encoding = st.substring(0, st.indexOf("\""));
176
                        }
177
                } catch (FileNotFoundException ex) {
178
                        ex.printStackTrace();
179
                } catch (IOException e) {
180
                        e.printStackTrace();
181
                }
182
                
183
                int tag;
184
                
185
                try
186
                {
187
                        setInput(new FileInputStream(f), encoding);        
188
                        nextTag();
189
                        
190
                        if ( getEventType() != KXmlParser.END_DOCUMENT ) 
191
                        {     
192
                                setSchemaFromMainTag(getName());
193
                                     
194
                                require(KXmlParser.START_TAG, null, CapabilitiesTags.WFS_SCHEMAROOT); 
195
                                tag = nextTag();
196
                                while(tag != KXmlParser.END_DOCUMENT)
197
                                {
198
                                        switch(tag)
199
                                        {
200
                                        case KXmlParser.START_TAG:
201
                                                if (getName().compareTo(CapabilitiesTags.COMPLEXTYPE)==0){                                                        
202
                                                        for (int i=0 ; i<getAttributeCount() ; i++){
203
                                                            if (getAttributeName(i).compareTo(GMLTags.GML_NAME) == 0){
204
                                                                    XMLComplexType complexType = XMLTypesFactory.addCompleyType(nameSpace,getAttributeValue(i));
205
                                                                    parseComplexType(complexType);                                                                    
206
                                                            }
207
                                                            
208
                                                        }
209
                                                } else if (getName().compareTo(CapabilitiesTags.ELEMENT)==0){                                                        
210
                                                        XMLElementsFactory.addType(this);
211
                                                }
212
                                                break;
213
                                        case KXmlParser.END_TAG:                            
214
                                                break;
215
                                        case KXmlParser.TEXT:
216
                                                if (getName()!=null)
217
                                                        System.out.println("[TEXT]["+getText().trim()+"]");                         
218
                                                break;
219
                                        }
220
                                        tag = next();
221
                                }
222
                                require(KXmlParser.END_DOCUMENT, null, null);                
223
                        }
224
                }
225
                catch(XmlPullParserException parser_ex){
226
                        System.out.println(parser_ex.getMessage());
227
                        parser_ex.printStackTrace();
228
                        return;
229
                }
230
                catch (IOException ioe) {           
231
                        ioe.printStackTrace();
232
                        return;
233
                }                
234
        }
235
        
236
        private void parseComplexType(XMLComplexType complexType) throws IOException, XmlPullParserException{   
237
                int currentTag;
238
                boolean end = false;                
239
                
240
                require(KXmlParser.START_TAG, null, CapabilitiesTags.COMPLEXTYPE);
241
                currentTag = next();
242
                
243
                while (!end) 
244
                {
245
                        switch(currentTag)
246
                        {
247
                        case KXmlParser.START_TAG:
248
                                if (getName().compareTo(CapabilitiesTags.COMPLEXCONTENT)==0)
249
                                {
250
                                        parseComplexContent(complexType); 
251
                                }   
252
                                break;
253
                        case KXmlParser.END_TAG:
254
                                if (getName().compareTo(CapabilitiesTags.COMPLEXTYPE) == 0)
255
                                        end = true;
256
                                break;
257
                        case KXmlParser.TEXT:                   
258
                                break;
259
                        }
260
                        if (!end){
261
                                currentTag = next();
262
                        }        
263
                }
264
        }
265
        
266
        private void parseComplexContent(XMLComplexType complexType) throws IOException, XmlPullParserException
267
        {   
268
                int currentTag;
269
                boolean end = false;
270
                
271
                require(KXmlParser.START_TAG, null, CapabilitiesTags.COMPLEXCONTENT);
272
                currentTag = next();
273
                
274
                while (!end) 
275
                {
276
                        switch(currentTag)
277
                        {
278
                        case KXmlParser.START_TAG:
279
                                if (getName().compareTo(CapabilitiesTags.EXTENSION )==0)
280
                                {
281
                                        parseExtension(complexType); 
282
                                }   
283
                                break;
284
                        case KXmlParser.END_TAG:
285
                                if (getName().compareTo(CapabilitiesTags.COMPLEXCONTENT) == 0)
286
                                        end = true;
287
                                break;
288
                        case KXmlParser.TEXT:                   
289
                                break;
290
                        }
291
                        if (!end){
292
                                currentTag = next();
293
                        }        
294
                }
295
        }
296
        
297
        private void parseExtension(XMLComplexType complexType) throws IOException, XmlPullParserException
298
        {   
299
                int currentTag;
300
                boolean end = false;
301
                
302
                require(KXmlParser.START_TAG, null, CapabilitiesTags.EXTENSION);
303
                currentTag = next();
304
                
305
                while (!end) 
306
                {
307
                        switch(currentTag)
308
                        {
309
                        case KXmlParser.START_TAG:
310
                                if (getName().compareTo(CapabilitiesTags.SEQUENCE)==0)
311
                                {
312
                                        parseSequence(complexType); 
313
                                }   
314
                                break;
315
                        case KXmlParser.END_TAG:
316
                                if (getName().compareTo(CapabilitiesTags.EXTENSION) == 0)
317
                                        end = true;
318
                                break;
319
                        case KXmlParser.TEXT:                   
320
                                break;
321
                        }
322
                        if (!end){
323
                                currentTag = next();
324
                        }                        
325
                }
326
        }
327
        
328
        private void parseSequence(XMLComplexType complexType) throws IOException, XmlPullParserException
329
        {   
330
                int currentTag;
331
                boolean end = false;
332
                
333
                require(KXmlParser.START_TAG, null, CapabilitiesTags.SEQUENCE);
334
                currentTag = next();
335
                                
336
                while (!end) 
337
                {
338
                        switch(currentTag)
339
                        {
340
                        case KXmlParser.START_TAG:
341
                                if (getName().compareTo(CapabilitiesTags.ELEMENT)==0)
342
                                {
343
                                        XMLElement element = XMLElementsFactory.addType(this);
344
                                        if (element != null){
345
                                                complexType.addSubtypes(element);
346
                                        }
347
                                }   
348
                                break;
349
                        case KXmlParser.END_TAG:
350
                                if (getName().compareTo(CapabilitiesTags.SEQUENCE) == 0)
351
                                        end = true;
352
                                break;
353
                        case KXmlParser.TEXT:                   
354
                                break;
355
                        }
356
                        if (!end){
357
                                currentTag = next();
358
                        }
359
                }                
360
        }
361

    
362
        
363
}