Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libRemoteServices / src / org / gvsig / remoteclient / wfs / schema / XMLElement.java @ 33738

History | View | Annotate | Download (11.6 KB)

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

    
3
import java.io.IOException;
4
import java.util.Vector;
5

    
6
import org.kxml2.io.KXmlParser;
7
import org.xmlpull.v1.XmlPullParserException;
8

    
9
import org.gvsig.compat.CompatLocator;
10
import org.gvsig.compat.lang.StringUtils;
11
import org.gvsig.remoteclient.utils.CapabilitiesTags;
12
import org.gvsig.remoteclient.wfs.schema.type.IXMLType;
13
import org.gvsig.remoteclient.wfs.schema.type.XMLComplexType;
14

    
15
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
16
 *
17
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
18
 *
19
 * This program is free software; you can redistribute it and/or
20
 * modify it under the terms of the GNU General Public License
21
 * as published by the Free Software Foundation; either version 2
22
 * of the License, or (at your option) any later version.
23
 *
24
 * This program is distributed in the hope that it will be useful,
25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27
 * GNU General Public License for more details.
28
 *
29
 * You should have received a copy of the GNU General Public License
30
 * along with this program; if not, write to the Free Software
31
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
32
 *
33
 * For more information, contact:
34
 *
35
 *  Generalitat Valenciana
36
 *   Conselleria d'Infraestructures i Transport
37
 *   Av. Blasco Ib??ez, 50
38
 *   46010 VALENCIA
39
 *   SPAIN
40
 *
41
 *      +34 963862235
42
 *   gvsig@gva.es
43
 *      www.gvsig.gva.es
44
 *
45
 *    or
46
 *
47
 *   IVER T.I. S.A
48
 *   Salamanca 50
49
 *   46005 Valencia
50
 *   Spain
51
 *
52
 *   +34 963163400
53
 *   dac@iver.es
54
 */
55
/* CVS MESSAGES:
56
 *
57
 * $Id: XMLElement.java 18271 2008-01-24 09:06:43Z jpiera $
58
 * $Log$
59
 * Revision 1.10  2006-12-29 17:15:48  jorpiell
60
 * Se tienen en cuenta los simpleTypes y los choices, adem?s de los atributos multiples
61
 *
62
 * Revision 1.9  2006/12/22 11:25:44  csanchez
63
 * Nuevo parser GML 2.x para gml's sin esquema
64
 *
65
 * Revision 1.8  2006/11/16 13:27:42  jorpiell
66
 * El XML Element tiene un m?todo para recuperar a los hijos de un elemento determinado
67
 *
68
 * Revision 1.7  2006/11/16 09:45:10  jorpiell
69
 * A?adido un m?todo que te devuelve un XMLElement a partir del nombre de un element
70
 *
71
 * Revision 1.6  2006/11/14 12:45:30  jorpiell
72
 * Se ha a?adido un m?todo al XMLEntity que devuelve el nombre completo desde el nodo apdre al nodo hijo separados por barras
73
 *
74
 * Revision 1.5  2006/10/31 13:52:37  ppiqueras
75
 * Mejoras para uso de features complejas
76
 *
77
 * Revision 1.4  2006/10/31 12:24:33  jorpiell
78
 * En caso de que el elemento forme parte de un tipo complejo tiene que tener un enlace al tipo del objeto padre
79
 *
80
 * Revision 1.3  2006/10/10 12:52:28  jorpiell
81
 * Soporte para features complejas.
82
 *
83
 * Revision 1.2  2006/08/29 08:43:13  jorpiell
84
 * Dos peque?os cambios para tener en cuenta las referencias
85
 *
86
 * Revision 1.1  2006/08/10 12:00:49  jorpiell
87
 * Primer commit del driver de Gml
88
 *
89
 *
90
 */
91
/**
92
 * A simple XSD element that represent an object
93
 * with a type of data.
94
 * 
95
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
96
 * @author Carlos S?nchez Peri??n (sanchez_carper@gva.es)
97
 * 
98
 */
99
public class XMLElement {
100
        private String name = null;
101
        private String reference = null;
102
        private IXMLType type = null;
103
        private String typeUnknown = null;
104
        private int minOccurs = -1;
105
        private int maxOccurs = -1;
106
        private int totalDigits = 0;
107
        private int fractionDigits = 0;        
108
        //Only used if is a part of a complex type
109
        private XMLElement parentElement = null;
110
        
111
        private static final StringUtils stringUtils = CompatLocator.getStringUtils();
112
        
113
        public XMLElement(XMLSchemaParser parser) throws XmlPullParserException, IOException {
114
                super();        
115
                parse(parser);
116
        }        
117
        
118
        public XMLElement(String name) {
119
                super();                
120
                this.name = name;
121
        }        
122
        
123
        /**
124
         * @return Returns the maxOccurs.
125
         */
126
        public int getMaxOccurs() {
127
                return maxOccurs;
128
        }
129
        /**
130
         * @param maxOccurs The maxOccurs to set.
131
         */
132
        public void setMaxOccurs(int maxOccurs) {
133
                this.maxOccurs = maxOccurs;
134
        }
135
        /**
136
         * @return Returns the minOccurs.
137
         */
138
        public int getMinOccurs() {
139
                return minOccurs;
140
        }
141
        /**
142
         * @param minOccurs The minOccurs to set.
143
         */
144
        public void setMinOccurs(int minOccurs) {
145
                this.minOccurs = minOccurs;
146
        }
147
        /**
148
         * @return Returns the name.
149
         */
150
        public String getName() {
151
                return name;
152
        }
153
        /**
154
         * @param name The name to set.
155
         */
156
        public void setName(String name) {
157
                this.name = name;
158
        }
159
        /**
160
         * @return Returns the type.
161
         */
162
        public IXMLType getEntityType() {
163
                if (type != null){
164
                        return type;
165
                }
166
                if (typeUnknown != null){                        
167
                        this.type = XMLTypesFactory.getType(typeUnknown);
168
                }
169
                if ((type == null) && (typeUnknown != null)){
170
                        if (stringUtils.split(typeUnknown, ":").length > 1){
171
                                this.type = XMLTypesFactory.getType(null + ":" + stringUtils.split(typeUnknown, ":")[1]);
172
                                if (type == null){
173
                                        this.type = XMLTypesFactory.getType(stringUtils.split(typeUnknown, ":")[1]);
174
                                }
175
                        }
176
                }
177
                return type;
178
                
179
        }
180
        /**
181
         * @param type The type to set.
182
         */
183
        public void setEntityType(String type) {
184
                IXMLType xmlType = XMLTypesFactory.getType(type);
185
                if (xmlType == null){
186
                        String[] types = stringUtils.split(type, ":");
187
                        if (types.length == 1){
188
                                xmlType = XMLTypesFactory.getType("XS:" + type);
189
                        }
190
                        if (xmlType == null){
191
                                xmlType = XMLTypesFactory.getType("GML:" + type);
192
                        }        
193
                        if (xmlType == null){
194
                                typeUnknown = type;
195
                        }
196
                }
197
                this.type = xmlType;
198
        }
199
        
200
        /*
201
         *  (non-Javadoc)
202
         * @see java.lang.Object#toString()
203
         */
204
        public String toString(){
205
                return name;
206
        }
207
        
208
        /**
209
     * Parses the contents of the parser  to extract the information
210
     * about a XSD element
211
         * @throws IOException 
212
         * @throws XmlPullParserException 
213
     * 
214
     */
215
    private void parse(XMLSchemaParser parser) throws XmlPullParserException, IOException{
216
            //con kxml contamos cuantos argumentos tiene la etiqueta gml para parsear
217
            for (int i=0 ; i<parser.getAttributeCount() ; i++){
218
                    //detecta los atributos del elemento y los salva
219
                    //puede ser que se sepa antes el atributo que el tipo al que pertenece o viceversa
220
                    if (parser.getAttributeName(i).compareTo(CapabilitiesTags.ELEMENT_NAME) == 0){
221
                            setName(parser.getAttributeValue(i));
222
                    }else if (parser.getAttributeName(i).compareTo(CapabilitiesTags.ELEMENT_TYPE) == 0){
223
                            setEntityType(parser.getAttributeValue(i));
224
                    }else if (parser.getAttributeName(i).compareTo(CapabilitiesTags.ELEMENT_MAXOCCURS) == 0){
225
                            try{
226
                                    setMaxOccurs(Integer.parseInt(parser.getAttributeValue(i)));
227
                            }catch(NumberFormatException e){
228
                                    setMaxOccurs(-1);
229
                            }
230
                    }else if (parser.getAttributeName(i).compareTo(CapabilitiesTags.ELEMENT_MINOCCURS) == 0){
231
                            try{
232
                                    setMinOccurs(Integer.parseInt(parser.getAttributeValue(i)));
233
                            }catch(NumberFormatException e){
234
                                    setMinOccurs(-1);
235
                            }
236
                    }else if (parser.getAttributeName(i).compareTo(CapabilitiesTags.ELEMENT_REF) == 0){
237
                            setReference(parser.getAttributeValue(i));
238
                    }
239
            }  
240
            
241
        }
242
    
243
    public void parseSimpleType(XMLSchemaParser parser) throws IOException, XmlPullParserException
244
        {   
245
                int currentTag;
246
                boolean end = false;                
247
                currentTag = parser.getEventType();
248
                
249
                while (!end) 
250
                {
251
                        switch(currentTag)
252
                        {
253
                        case KXmlParser.START_TAG:
254
                                if (parser.getName().compareTo(CapabilitiesTags.RESTRICTION)==0){
255
                                        for (int i=0 ; i<parser.getAttributeCount() ; i++){
256
                                                if (parser.getAttributeName(i).compareTo(CapabilitiesTags.BASE) == 0){
257
                                                        setEntityType(parser.getAttributeValue(i));
258
                                                }
259
                                        }
260
                                        parseRestriction(parser);
261
                                }   
262
                                break;
263
                        case KXmlParser.END_TAG:
264
                                if (parser.getName().compareTo(CapabilitiesTags.SIMPLETYPE) == 0)
265
                                        end = true;
266
                                break;
267
                        case KXmlParser.TEXT:                   
268
                                break;
269
                        }
270
                        if (!end){
271
                                currentTag = parser.next();
272
                        }        
273
                }
274
        }
275
        
276
        private void parseRestriction(XMLSchemaParser parser) throws IOException, XmlPullParserException
277
        {   
278
                int currentTag;
279
                boolean end = false;
280
                
281
                parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.RESTRICTION);
282
                currentTag = parser.next();
283
                
284
                while (!end) 
285
                {
286
                        switch(currentTag)
287
                        {
288
                        case KXmlParser.START_TAG:
289
                                if (parser.getName().compareTo(CapabilitiesTags.TOTAL_DIGITS)==0)
290
                                {
291
                                        for (int i=0 ; i<parser.getAttributeCount() ; i++){
292
                                                if (parser.getAttributeName(i).compareTo(CapabilitiesTags.VALUE) == 0){
293
                                                        setTotalDigits(Integer.parseInt(parser.getAttributeValue(i)));
294
                                                }
295
                                        }
296
                                }else if (parser.getName().compareTo(CapabilitiesTags.FRACTION_DIGITS)==0){
297
                                        for (int i=0 ; i<parser.getAttributeCount() ; i++){
298
                                                if (parser.getAttributeName(i).compareTo(CapabilitiesTags.VALUE) == 0){
299
                                                        setFractionDigits(Integer.parseInt(parser.getAttributeValue(i)));
300
                                                }
301
                                        }
302
                                }
303
                                break;
304
                        case KXmlParser.END_TAG:
305
                                if (parser.getName().compareTo(CapabilitiesTags.RESTRICTION) == 0)
306
                                        end = true;
307
                                break;
308
                        case KXmlParser.TEXT:                   
309
                                break;
310
                        }
311
                        if (!end){
312
                                currentTag = parser.next();
313
                        }        
314
                }
315
        }
316

    
317
        /**
318
         * @return Returns the reference.
319
         */
320
        public String getReference() {
321
                return reference;
322
        }
323

    
324
        /**
325
         * @param reference The reference to set.
326
         */
327
        public void setReference(String reference) {
328
                this.reference = reference;
329
        }
330

    
331
        /**
332
         * @return Returns the fractionDigits.
333
         */
334
        public int getFractionDigits() {
335
                return fractionDigits;
336
        }
337

    
338
        /**
339
         * @param fractionDigits The fractionDigits to set.
340
         */
341
        public void setFractionDigits(int fractionDigits) {
342
                this.fractionDigits = fractionDigits;
343
        }
344

    
345
        /**
346
         * @return Returns the totalDigits.
347
         */
348
        public int getTotalDigits() {
349
                return totalDigits;
350
        }
351

    
352
        /**
353
         * @param totalDigits The totalDigits to set.
354
         */
355
        public void setTotalDigits(int totalDigits) {
356
                this.totalDigits = totalDigits;
357
        }
358

    
359
        /**
360
         * @return Returns the parentType.
361
         */
362
        public XMLElement getParentElement() {
363
                return parentElement;
364
        }
365

    
366
        /**
367
         * @param parent The parentTypeparentType to set.
368
         */
369
        public void setParentElement(XMLElement parentElement) {
370
                this.parentElement = parentElement;
371
        }        
372
        
373
        /**
374
         * This method returns the full element name. The full name
375
         * is composed by all the parent names. E.g: ParentName/ChildName 
376
         * @return
377
         */
378
        public String getFullName(){
379
                String path = getName();
380
                XMLElement parent = this.getParentElement();
381
                while (parent != null){
382
                        if (parent.getParentElement() != null){
383
                                path = parent.getName() + "/" + path;                                
384
                        }
385
                        parent = parent.getParentElement();
386
                }
387
                return path;
388
        }
389
        
390
        /**
391
         * Search a attribute by name. If the attribute is found, the
392
         * XMLElement is returned
393
         * @param attName
394
         * Attribute to search
395
         * @return xMLElement
396
         */
397
        public XMLElement searchAttribute(String attName){
398
                return searchAttribute(attName,getName());
399
        }
400
        
401
        private XMLElement searchAttribute(String attName,String fullName){
402
                if (fullName.equals(attName)){
403
                        return this;
404
                }
405
                if (getEntityType().getType() == IXMLType.COMPLEX){
406
                        Vector attributes = ((XMLComplexType)getEntityType()).getAttributes();
407
                        XMLElement child = null;
408
                        for (int i=0 ; i<attributes.size() ; i++){
409
                                child = (XMLElement)attributes.get(i);
410
                                XMLElement element = child.searchAttribute(attName,fullName + "/" + child.getName());
411
                                if (element != null){
412
                                        return element;
413
                                }
414
                        }
415
                }
416
                return null;                
417
        }
418
        
419
        /**
420
         * Return the element chidren (if the element is a complex type)
421
         * @return
422
         */
423
        public Vector getChildren(){
424
                if ((getEntityType() != null) &&
425
                                (getEntityType().getType() == IXMLType.COMPLEX)){
426
                        return ((XMLComplexType)getEntityType()).getAttributes();
427
                }
428
                return new Vector();
429
        }
430
        
431
        /**
432
         * Returns if the attribute is multiple or not
433
         * @return
434
         */
435
        public boolean isMultiple(){
436
                if (maxOccurs == 1){
437
                        return false;
438
                }
439
                return true;
440
        }
441
}