Statistics
| Revision:

root / branches / v1_9_0 / libGPE-GML / src / org / gvsig / gpe / gml / parser / v2 / features / ElementTypeBinding.java @ 53

History | View | Annotate | Download (7.99 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.parser.profiles.IBindingProfile;
9
import org.gvsig.gpe.gml.utils.GMLGeometries;
10
import org.gvsig.gpe.gml.utils.GMLTags;
11
import org.gvsig.gpe.gml.utils.GMLUtilsParser;
12
import org.gvsig.gpe.parser.IGPEContentHandler;
13
import org.gvsig.gpe.xml.stream.EventType;
14
import org.gvsig.gpe.xml.stream.IXmlStreamReader;
15
import org.gvsig.gpe.xml.stream.XmlStreamException;
16
import org.gvsig.gpe.xml.utils.CompareUtils;
17
import org.gvsig.gpe.xml.utils.XMLAttributesIterator;
18
import org.gvsig.xmlschema.som.IXSElementDeclaration;
19
import org.gvsig.xmlschema.utils.TypeUtils;
20

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

    
117
        parser.require(EventType.START_ELEMENT, null, null);
118
        
119
                boolean endFeature = false;
120
                boolean isGeometryTag = false;
121
                EventType currentEvent;                
122
                Object element = null;        
123
                Object value = null;
124
                Object geometry = null;
125
                boolean isInitialized = false;
126
                //Used to finish to parse the current element
127
                final QName elementRootType = parser.getElementName();
128
                String type = null;        
129
                
130
                XMLAttributesIterator attributesIterator = new XMLAttributesIterator(parser);
131
                
132
                //Find the element type
133
                IXSElementDeclaration elementType = null;
134
                if (parentType != null){
135
                        elementType = parentType.getSubElementByName(elementRootType.getLocalPart());
136
                        if (elementType == null){
137
                                type = TypeUtils.getXSType(String.class);
138
                        }else{
139
                                type = elementType.getTypeName();
140
                        }
141
                }else{
142
                        type = TypeUtils.getXSType(String.class);
143
                }
144
                
145
                QName tag = parser.getElementName();
146
                currentEvent = parser.getEventType();
147

    
148
        final IGPEContentHandler contentHandler = handler.getContentHandler();
149
        final IBindingProfile profile = handler.getProfile();
150
        final String namespaceURI = elementRootType.getNamespaceURI();
151
        final String localName = GMLUtilsParser.removeBlancSymbol(elementRootType.getLocalPart());
152
                while (!endFeature){
153
            switch(currentEvent.getCode()){
154
                        case EventType.START_ELEMENT_CODE:
155
                                if (!(CompareUtils.compareWithNamespace(tag,elementRootType))){
156
                                        //If is a geometry
157
                    if (GMLGeometries.isGML(tag)){
158
                        geometry = profile.getGeometryBinding().parse(parser, handler);
159
                        contentHandler.addGeometryToFeature(geometry, feature);
160
                        if (geometry == null) {
161
                            System.out.println("\t\t\tGEOMETRIA VACIA");
162
                            // Warning geometria vacia
163
                        }
164
                        isGeometryTag = true;
165
                                        }else {
166
                                                //If is not a geometry could be a complex feature
167
                                                if (!isInitialized){
168
                            element = contentHandler.startElement(namespaceURI, localName, null,
169
                                    attributesIterator, parentElement);
170
                            isInitialized = true;
171
                                                }
172
                        final ElementTypeBinding elementTypeBinding = profile
173
                                .getElementTypeBinding();
174
                        elementTypeBinding.parse(parser, handler, feature, element, null);
175
                                        }                                
176
                                }
177
                                break;
178
                        case EventType.END_ELEMENT_CODE:
179
                                if (CompareUtils.compareWithNamespace(tag,elementRootType)){                                                
180
                                        endFeature = true;
181
                                        //If not is complex the element has not been created yet
182
                                        if (!isInitialized){
183
                        element = contentHandler.startElement(namespaceURI, localName, value,
184
                                attributesIterator, parentElement);
185
                                                isInitialized = true;
186
                                        }
187
                                        contentHandler.endElement(element);
188
                                }
189
                                break;
190
                        default:
191
                if (currentEvent.isValue()) {
192
                    if (geometry == null) {
193
                        switch (currentEvent.getCode()) {
194
                        case EventType.VALUE_BOOL_CODE:
195
                            value = Boolean.valueOf(parser.getBooleanValue());
196
                            break;
197
                        case EventType.VALUE_BYTE_CODE:
198
                            value = new Integer(parser.getByteValue());
199
                            break;
200
                        case EventType.VALUE_DOUBLE_CODE:
201
                            value = new Double(parser.getDoubleValue());
202
                            break;
203
                        case EventType.VALUE_FLOAT_CODE:
204
                            value = new Float(parser.getFloatValue());
205
                            break;
206
                        case EventType.VALUE_INT_CODE:
207
                            value = new Integer(parser.getIntValue());
208
                            break;
209
                        case EventType.VALUE_LONG_CODE:
210
                            value = new Long(parser.getLongValue());
211
                            break;
212
                        default:
213
                            value = TypeUtils.getValue(elementType, parser.getText());
214
                            break;
215
                        }
216
                    }
217
                }
218
                                break;
219
                        }
220
                        if (!endFeature){                                        
221
                                currentEvent = parser.next();
222
                                if(currentEvent.isTag()){
223
                                    tag = parser.getElementName();
224
                                }
225
                        }
226
                }
227
                
228
                parser.require(EventType.END_ELEMENT, elementRootType);
229
                return element;                
230
        }
231
}