Statistics
| Revision:

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

History | View | Annotate | Download (5.65 KB)

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

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

    
6
import org.gvsig.gpe.kml.parser.GPEDeafultKmlParser;
7
import org.gvsig.gpe.kml.parser.v21.geometries.DoubleBinding;
8
import org.gvsig.gpe.kml.utils.KmlTags;
9
import org.gvsig.gpe.utils.StringUtils;
10
import org.gvsig.gpe.xml.stream.XmlStreamException;
11
import org.gvsig.gpe.xml.stream.IXmlStreamReader;
12
import org.gvsig.gpe.xml.utils.CompareUtils;
13

    
14
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
15
 *
16
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
17
 *
18
 * This program is free software; you can redistribute it and/or
19
 * modify it under the terms of the GNU General Public License
20
 * as published by the Free Software Foundation; either version 2
21
 * of the License, or (at your option) any later version.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 * GNU General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU General Public License
29
 * along with this program; if not, write to the Free Software
30
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
31
 *
32
 * For more information, contact:
33
 *
34
 *  Generalitat Valenciana
35
 *   Conselleria d'Infraestructures i Transport
36
 *   Av. Blasco Ib??ez, 50
37
 *   46010 VALENCIA
38
 *   SPAIN
39
 *
40
 *      +34 963862235
41
 *   gvsig@gva.es
42
 *      www.gvsig.gva.es
43
 *
44
 *    or
45
 *
46
 *   IVER T.I. S.A
47
 *   Salamanca 50
48
 *   46005 Valencia
49
 *   Spain
50
 *
51
 *   +34 963163400
52
 *   dac@iver.es
53
 */
54
/* CVS MESSAGES:
55
 *
56
 * $Id:CoordinatesTypeBinding.java 357 2008-01-09 17:50:08Z jpiera $
57
 * $Log$
58
 * Revision 1.2  2007/06/07 14:53:59  jorpiell
59
 * Add the schema support
60
 *
61
 * Revision 1.1  2007/05/11 07:06:29  jorpiell
62
 * Refactoring of some package names
63
 *
64
 * Revision 1.4  2007/05/09 08:36:24  jorpiell
65
 * Add the bbox to the layer
66
 *
67
 * Revision 1.3  2007/05/08 08:22:37  jorpiell
68
 * Add comments to create javadocs
69
 *
70
 * Revision 1.2  2007/04/14 16:08:07  jorpiell
71
 * Kml writing support added
72
 *
73
 * Revision 1.1  2007/04/13 13:16:21  jorpiell
74
 * Add KML reading support
75
 *
76
 * Revision 1.1  2007/03/07 08:19:10  jorpiell
77
 * Pasadas las clases de KML de libGPE-GML a libGPE-KML
78
 *
79
 * Revision 1.1  2007/02/28 11:48:31  csanchez
80
 * *** empty log message ***
81
 *
82
 * Revision 1.1  2007/02/20 10:53:20  jorpiell
83
 * A?adidos los proyectos de kml y gml antiguos
84
 *
85
 * Revision 1.1  2007/02/12 13:49:18  jorpiell
86
 * A?adido el driver de KML
87
 *
88
 *
89
 */
90
/**
91
 * This class parses a coordinates tag. Example:
92
 * <p>
93
 * <pre>
94
 * <code>
95
 * &lt;coordinates&gt;60.0,60.0 60.0,90.0 90.0,90.0&lt;/coordinates&gt;
96
 * </code>
97
 * </pre>
98
 * </p> 
99
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
100
 * @see http://code.google.com/apis/kml/documentation/kml_tags_21.html#coordinates
101
 */
102
public class CoordinatesTypeIterator extends KmlCoodinatesIterator{
103
        private StringTokenizer coordinatesString = null;        
104
        
105
        /**
106
         * It parses the gml:coordinates tag
107
         * @param parser
108
         * The XML parser
109
         * @param handler
110
         * The GPE parser that contains the content handler and
111
         * the error handler
112
         * @return
113
         * It retuns a matrix of doubles with 3 columns (x,y,z) and
114
         * one row for each coordinate.
115
         * @throws XmlStreamException
116
         * @throws IOException
117
         */
118
        /* (non-Javadoc)
119
         * @see org.gvsig.gpe.gml.parser.v2.coordinates.GmlCoodinatesIterator#initialize(org.gvsig.gpe.xml.stream.IXmlStreamReader, org.gvsig.gpe.gml.parser.GPEDefaultGmlParser, java.lang.String)
120
         */
121
        public void initialize(IXmlStreamReader parser,
122
                        GPEDeafultKmlParser handler, String lastTag)
123
                        throws XmlStreamException, IOException {
124
                super.initialize(parser, handler, lastTag);
125
                
126
                boolean endFeature = false;
127
                int currentTag;                                
128

    
129
                String tag = parser.getName();
130
                currentTag = parser.getEventType();
131

    
132
                while (!endFeature){
133
                        switch(currentTag){
134
                        case IXmlStreamReader.START_TAG:
135
                                if (CompareUtils.compareWithOutNamespace(tag,KmlTags.COORDINATES)){
136
                                        parser.next();                                                
137
                                        String coordinatesTag = parser.getText().trim();
138
                                        if (dimension == -1){
139
                                                dimension = caculateDimension(coordinatesTag);
140
                                        }                                                
141
                                        coordinatesString = new StringTokenizer(coordinatesTag);
142
                                        //Parses until the last tag...
143
                                        parseAll();
144
                                        return;
145
                                }
146
                                break;
147
                        case IXmlStreamReader.END_TAG:
148
                                if (CompareUtils.compareWithOutNamespace(tag,KmlTags.COORDINATES)){                                                
149
                                        endFeature = true;
150
                                }
151
                                break;
152
                        case IXmlStreamReader.TEXT:                                        
153

    
154
                                break;
155
                        }
156
                        if (!endFeature){                                        
157
                                currentTag = parser.next();
158
                                tag = parser.getName();
159
                        }
160
                }                        
161
        }
162
        
163
        /**
164
         * Calculates the dimension from a array of coordinates
165
         * @param coordinatesTag
166
         * @return
167
         */
168
        private int caculateDimension(String coordinatesTag) {
169
                String firstPair;
170
                StringTokenizer st = new StringTokenizer(coordinatesTag);
171
                if (st.hasMoreTokens()){
172
                        firstPair = st.nextToken();
173
                }else{                        
174
                        firstPair = coordinatesTag;
175
                }                
176
                return StringUtils.splitString(firstPair, KmlTags.COORDINATES_SEPARATOR).length;                
177
        }
178

    
179
        /*
180
         * (non-Javadoc)
181
         * @see org.gvsig.gpe.parser.ICoordinateIterator#hasNext()
182
         */
183
        public boolean hasNext() throws IOException {
184
                return coordinatesString.hasMoreTokens();        
185
        }
186

    
187
        /*
188
         * (non-Javadoc)
189
         * @see org.gvsig.gpe.parser.ICoordinateIterator#next(double[])
190
         */
191
        public void next(double[] buffer) throws IOException {
192
                String next = coordinatesString.nextToken();
193
                String[] coordinates = org.gvsig.gpe.utils.StringUtils.splitString(next.trim(),KmlTags.COORDINATES_SEPARATOR);
194
                for (int i=0 ; i<coordinates.length ; i++){
195
                        buffer[i] = handler.getProfile().getDoubleBinding().parse(coordinates[i]);
196
                }        
197
        }
198

    
199

    
200
}