Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGPE-KML / src / org / gvsig / gpe / kml / reader / bindings / ElementBinding.java @ 11700

History | View | Annotate | Download (4.34 KB)

1
package org.gvsig.gpe.kml.reader.bindings;
2

    
3
import java.io.IOException;
4

    
5
import org.gvsig.gpe.gml.utils.GMLUtilsParser;
6
import org.gvsig.gpe.kml.GPEKmlParser;
7
import org.gvsig.gpe.kml.reader.bindings.geometries.GeometryBinding;
8
import org.xmlpull.v1.XmlPullParser;
9
import org.xmlpull.v1.XmlPullParserException;
10

    
11
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
12
 *
13
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
14
 *
15
 * This program is free software; you can redistribute it and/or
16
 * modify it under the terms of the GNU General Public License
17
 * as published by the Free Software Foundation; either version 2
18
 * of the License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU General Public License
26
 * along with this program; if not, write to the Free Software
27
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
28
 *
29
 * For more information, contact:
30
 *
31
 *  Generalitat Valenciana
32
 *   Conselleria d'Infraestructures i Transport
33
 *   Av. Blasco Ib??ez, 50
34
 *   46010 VALENCIA
35
 *   SPAIN
36
 *
37
 *      +34 963862235
38
 *   gvsig@gva.es
39
 *      www.gvsig.gva.es
40
 *
41
 *    or
42
 *
43
 *   IVER T.I. S.A
44
 *   Salamanca 50
45
 *   46005 Valencia
46
 *   Spain
47
 *
48
 *   +34 963163400
49
 *   dac@iver.es
50
 */
51
/* CVS MESSAGES:
52
 *
53
 * $Id: ElementBinding.java 11700 2007-05-16 15:54:36Z jorpiell $
54
 * $Log$
55
 * Revision 1.1  2007-05-16 15:54:20  jorpiell
56
 * Add elements support
57
 *
58
 *
59
 */
60
/**
61
 * It parses an element. An element is a tag inside of a 
62
 * metadata tag
63
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
64
 */
65
public class ElementBinding {
66
        /**
67
         * It parses a xml element
68
         * @param parser
69
         * The XML parser
70
         * @param handler
71
         * The GPE parser that contains the content handler and
72
         * the error handler
73
         * @param parentElement
74
         * The parent element
75
         * @return
76
         * A Element
77
         * @throws XmlPullParserException
78
         * @throws IOException
79
         */
80
        public static Object parse(XmlPullParser parser,GPEKmlParser handler, Object feature, Object parentElement) throws XmlPullParserException, IOException {
81
                boolean endFeature = false;
82
                int currentTag;                
83
                Object element = null;        
84
                Object value = null;
85
                boolean isInitialized = false;
86
                //Used to finish to parse the current element
87
                String elementRootType = parser.getName();
88

    
89
                Class type = getType(elementRootType);
90

    
91
                String tag = parser.getName();
92
                currentTag = parser.getEventType();
93

    
94
                while (!endFeature){
95
                        switch(currentTag){
96
                        case XmlPullParser.START_TAG:
97
                                if (!(tag.compareTo(elementRootType)==0)){
98
                                        if (!isInitialized){
99
                                                String elementName = getElementName(elementRootType);
100
                                                element = handler.getContentHandler().startElement(GMLUtilsParser.removeBlancSymbol(elementName), 
101
                                                                null,
102
                                                                type,
103
                                                                parentElement);
104
                                                isInitialized = true;
105
                                        }
106
                                        ElementBinding.parse(parser, handler, feature, element);
107
                                }
108

    
109
                                break;
110
                        case XmlPullParser.END_TAG:
111
                                if (tag.compareTo(elementRootType) == 0){                                                
112
                                        endFeature = true;
113
                                        if (!isInitialized){
114
                                                String elementName = getElementName(elementRootType);
115
                                                element = handler.getContentHandler().startElement(GMLUtilsParser.removeBlancSymbol(elementName), 
116
                                                                value,
117
                                                                type,
118
                                                                parentElement);
119
                                                isInitialized = true;
120
                                        }
121
                                        handler.getContentHandler().endElement(element);
122
                                }
123
                                break;
124
                        case XmlPullParser.TEXT:                                        
125
                                value = getValue(type, parser.getText());
126
                                break;
127
                        }
128
                        if (!endFeature){                                        
129
                                currentTag = parser.next();
130
                                tag = parser.getName();
131
                        }
132
                }
133
                return element;                
134
        }
135

    
136
        /**
137
         * This method has to calculate the element type
138
         * @param name
139
         * Element name
140
         * @return
141
         * Element type
142
         */
143
        private static Class getType(String name){
144
                return String.class;
145
        }
146

    
147
        /**
148
         * Removes the namespace from a element name
149
         * @return
150
         * The element name without namespace
151
         */
152
        private static String getElementName(String element){
153
                return element.substring(element.indexOf(":") + 1,element.length());
154
        }
155

    
156
        /**
157
         * This method must return the element value
158
         * @param type
159
         * Element type
160
         * @param value
161
         * Element value like a String
162
         * @return
163
         * Element value
164
         */
165
        private static Object getValue(Class type, String value){
166
                return value;
167
        }
168

    
169
}