Statistics
| Revision:

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

History | View | Annotate | Download (5.67 KB)

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

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

    
6
import javax.xml.namespace.QName;
7

    
8
import org.gvsig.gpe.kml.parser.GPEDefaultKmlParser;
9
import org.gvsig.gpe.kml.utils.Kml2_1_Tags;
10
import org.gvsig.gpe.utils.StringUtils;
11
import org.gvsig.gpe.xml.stream.IXmlStreamReader;
12
import org.gvsig.gpe.xml.stream.XmlStreamException;
13
import org.gvsig.gpe.xml.utils.CompareUtils;
14

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

    
130
                QName tag = parser.getName();
131
                currentTag = parser.getEventType();
132

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

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

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

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

    
200

    
201
}