Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGPE-KML / src / org / gvsig / gpe / kml / parser / v21 / features / ElementBinding.java @ 19680

History | View | Annotate | Download (4.46 KB)

1
package org.gvsig.gpe.kml.parser.v21.features;
2

    
3
import java.io.IOException;
4

    
5
import org.gvsig.gpe.kml.parser.GPEDeafultKmlParser;
6
import org.gvsig.gpe.kml.utils.KMLUtilsParser;
7
import org.gvsig.gpe.xml.stream.IXmlStreamReader;
8
import org.gvsig.gpe.xml.stream.XmlStreamException;
9

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

    
91
                String type = getType(elementRootType);
92

    
93
                String tag = parser.getName();
94
                currentTag = parser.getEventType();
95

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

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

    
138
        /**
139
         * This method has to calculate the element type
140
         * @param name
141
         * Element name
142
         * @return
143
         * Element type
144
         */
145
        private  String getType(String name){
146
                return "xs:string";
147
        }
148

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

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

    
171
}