Statistics
| Revision:

root / trunk / libraries / libGPE-GML / src / org / gvsig / gpe / gml / parser / v2 / features / ElementTypeBinding.java @ 38193

History | View | Annotate | Download (5.95 KB)

1
package org.gvsig.gpe.gml.parser.v2.features;
2

    
3
import java.io.IOException;
4

    
5
import javax.xml.namespace.QName;
6

    
7
import org.gvsig.gpe.gml.parser.GPEDefaultGmlParser;
8
import org.gvsig.gpe.gml.utils.GMLGeometries;
9
import org.gvsig.gpe.gml.utils.GMLUtilsParser;
10
import org.gvsig.gpe.xml.stream.IXmlStreamReader;
11
import org.gvsig.gpe.xml.stream.XmlStreamException;
12
import org.gvsig.gpe.xml.utils.CompareUtils;
13
import org.gvsig.gpe.xml.utils.XMLAttributesIterator;
14
import org.gvsig.xmlschema.som.IXSElementDeclaration;
15
import org.gvsig.xmlschema.utils.TypeUtils;
16

    
17
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
18
 *
19
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
20
 *
21
 * This program is free software; you can redistribute it and/or
22
 * modify it under the terms of the GNU General Public License
23
 * as published by the Free Software Foundation; either version 2
24
 * of the License, or (at your option) any later version.
25
 *
26
 * This program is distributed in the hope that it will be useful,
27
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29
 * GNU General Public License for more details.
30
 *
31
 * You should have received a copy of the GNU General Public License
32
 * along with this program; if not, write to the Free Software
33
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
34
 *
35
 * For more information, contact:
36
 *
37
 *  Generalitat Valenciana
38
 *   Conselleria d'Infraestructures i Transport
39
 *   Av. Blasco Ib??ez, 50
40
 *   46010 VALENCIA
41
 *   SPAIN
42
 *
43
 *      +34 963862235
44
 *   gvsig@gva.es
45
 *      www.gvsig.gva.es
46
 *
47
 *    or
48
 *
49
 *   IVER T.I. S.A
50
 *   Salamanca 50
51
 *   46005 Valencia
52
 *   Spain
53
 *
54
 *   +34 963163400
55
 *   dac@iver.es
56
 */
57
/* CVS MESSAGES:
58
 *
59
 * $Id: ElementTypeBinding.java 350 2008-01-09 12:53:07Z jpiera $
60
 * $Log$
61
 * Revision 1.8  2007/06/14 13:50:05  jorpiell
62
 * The schema jar name has been changed
63
 *
64
 * Revision 1.7  2007/06/07 14:53:30  jorpiell
65
 * Add the schema support
66
 *
67
 * Revision 1.6  2007/05/18 10:41:01  csanchez
68
 * Actualizaci?n libGPE-GML eliminaci?n de clases inecesarias
69
 *
70
 * Revision 1.5  2007/05/16 13:00:48  csanchez
71
 * Actualizaci?n de libGPE-GML
72
 *
73
 * Revision 1.4  2007/05/15 09:51:59  jorpiell
74
 * The namespace is deleted from the element name
75
 *
76
 * Revision 1.3  2007/05/15 09:35:09  jorpiell
77
 * the tag names cant have blanc spaces
78
 *
79
 * Revision 1.2  2007/05/15 08:04:16  jorpiell
80
 * If the element type is a geometry, the element value wiil be the toString geometry method
81
 *
82
 * Revision 1.1  2007/05/14 09:30:30  jorpiell
83
 * Add the FeatureMember tag
84
 *
85
 *
86
 */
87
/**
88
 * This class is used to parse some xml tags that
89
 * represents a xml element. 
90
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
91
 * @see http://www.w3.org/XML/Schema
92
 */
93
public class ElementTypeBinding {
94
        
95
        /**
96
         * It parses a xml element
97
         * @param parser
98
         * The XML parser
99
         * @param handler
100
         * The GPE parser that contains the content handler and
101
         * the error handler
102
         * @param feature
103
         * The feature is needed to add the geometry
104
         * @param parentElement
105
         * The parent element
106
         * @return
107
         * A Element
108
         * @throws XmlStreamException
109
         * @throws IOException
110
         */
111
        public Object parse(IXmlStreamReader parser,GPEDefaultGmlParser handler, Object feature, Object parentElement, IXSElementDeclaration parentType) throws XmlStreamException, IOException {
112
                boolean endFeature = false;
113
                boolean isGeometryTag = false;
114
                int currentTag;                
115
                Object element = null;        
116
                Object value = null;
117
                Object geometry = null;
118
                boolean isInitialized = false;
119
                //Used to finish to parse the current element
120
                QName elementRootType = parser.getName();
121
                String type = null;        
122
                
123
                XMLAttributesIterator attributesIterator = new XMLAttributesIterator(parser);
124
                
125
                //Find the element type
126
                IXSElementDeclaration elementType = null;
127
                if (parentType != null){
128
                        elementType = parentType.getSubElementByName(elementRootType.getLocalPart());
129
                        if (elementType == null){
130
                                type = TypeUtils.getXSType(String.class);
131
                        }else{
132
                                type = elementType.getTypeName();
133
                        }
134
                }else{
135
                        type = TypeUtils.getXSType(String.class);
136
                }
137
                
138
                QName tag = parser.getName();
139
                currentTag = parser.getEventType();
140

    
141
                while (!endFeature){
142
                        switch(currentTag){
143
                        case IXmlStreamReader.START_ELEMENT:
144
                                if (!(CompareUtils.compareWithNamespace(tag,elementRootType))){
145
                                        //If is a geometry
146
                                        if (GMLGeometries.isGML(tag)){
147
                                                geometry = handler.getProfile().getGeometryBinding().
148
                                                                parse(parser, handler);
149
                                                handler.getContentHandler().addGeometryToFeature(geometry, feature);
150
                                                if (geometry==null){
151
                                                        System.out.println("\t\t\tGEOMETRIA VACIA");
152
                                                        //Warning geometria vacia
153
                                                }
154
                                                isGeometryTag=true;
155
                                        }else {
156
                                                //If is not a geometry could be a complex feature
157
                                                if (!isInitialized){
158
                                                        element = handler.getContentHandler().startElement(elementRootType.getNamespaceURI(),
159
                                                                        GMLUtilsParser.removeBlancSymbol(elementRootType.getLocalPart()),
160
                                                                        null,
161
                                                                        attributesIterator,
162
                                                                        parentElement);
163
                                                        isInitialized = true;
164
                                                }
165
                                                handler.getProfile().getElementTypeBinding().
166
                                                        parse(parser, handler, feature, element, null);
167
                                        }                                
168
                                }
169
                                break;
170
                        case IXmlStreamReader.END_ELEMENT:
171
                                if (CompareUtils.compareWithNamespace(tag,elementRootType)){                                                
172
                                        endFeature = true;
173
                                        //If not is complex the element has not been created yet
174
                                        if (!isInitialized){
175
                                                element = handler.getContentHandler().startElement(
176
                                                                elementRootType.getNamespaceURI(),
177
                                                                elementRootType.getLocalPart(), 
178
                                                                value,        
179
                                                                attributesIterator,
180
                                                                parentElement);
181
                                                isInitialized = true;
182
                                        }
183
                                        handler.getContentHandler().endElement(element);
184
                                }
185
                                break;
186
                        case IXmlStreamReader.CHARACTERS:                                        
187
                                if (geometry == null){
188
                                        value = TypeUtils.getValue(elementType, parser.getText());
189
                                }
190
                                break;
191
                        }
192
                        if (!endFeature){                                        
193
                                currentTag = parser.next();
194
                                tag = parser.getName();
195
                        }
196
                }                        
197
                return element;                
198
        }
199
}