Statistics
| Revision:

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

History | View | Annotate | Download (4.83 KB)

1
package org.gvsig.gpe.gml.parser.v2.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.gml.parser.GPEDefaultGmlParser;
9
import org.gvsig.gpe.gml.parser.v2.geometries.DoubleTypeBinding;
10
import org.gvsig.gpe.gml.utils.GMLTags;
11
import org.gvsig.gpe.utils.StringUtils;
12
import org.gvsig.gpe.xml.stream.EventType;
13
import org.gvsig.gpe.xml.stream.IXmlStreamReader;
14
import org.gvsig.gpe.xml.stream.XmlStreamException;
15
import org.gvsig.gpe.xml.utils.CompareUtils;
16

    
17
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
18
 *
19
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
20
 *
21
 * This program is free software; you can redistribute it and/or
22
 * modify it under the terms of the GNU General Public License
23
 * as published by the Free Software Foundation; either version 2
24
 * of the License, or (at your option) any later version.
25
 *
26
 * This program is distributed in the hope that it will be useful,
27
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29
 * GNU General Public License for more details.
30
 *
31
 * You should have received a copy of the GNU General Public License
32
 * along with this program; if not, write to the Free Software
33
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
34
 *
35
 * For more information, contact:
36
 *
37
 *  Generalitat Valenciana
38
 *   Conselleria d'Infraestructures i Transport
39
 *   Av. Blasco Ib??ez, 50
40
 *   46010 VALENCIA
41
 *   SPAIN
42
 *
43
 *      +34 963862235
44
 *   gvsig@gva.es
45
 *      www.gvsig.gva.es
46
 *
47
 *    or
48
 *
49
 *   IVER T.I. S.A
50
 *   Salamanca 50
51
 *   46005 Valencia
52
 *   Spain
53
 *
54
 *   +34 963163400
55
 *   dac@iver.es
56
 */
57
/* CVS MESSAGES:
58
*
59
* $Id:CoordinatesTypeBinding.java 371 2008-01-10 09:30:19Z jpiera $
60
* $Log$
61
* Revision 1.3  2007/05/14 09:52:53  jorpiell
62
* Nullpointer exception fixed
63
*
64
* Revision 1.2  2007/05/14 09:31:06  jorpiell
65
* Add the a new class to compare tags
66
*
67
* Revision 1.1  2007/05/07 12:58:42  jorpiell
68
* Add some methods to manage the multigeometries
69
*
70
* Revision 1.1  2007/05/07 07:06:46  jorpiell
71
* Add a constructor with the name and the description fields
72
*
73
*
74
*/
75
/**
76
 * It parses a gml:CoordinatesType object. Example:
77
 * <p>
78
 * <pre>
79
 * <code>
80
 * &lt;coordinates&gt;60.0,60.0 60.0,90.0 90.0,90.0&lt;/coordinates&gt;
81
 * </code>
82
 * </pre>
83
 * </p> 
84
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
85
 */
86
public class CoordinatesTypeIterator extends GmlCoodinatesIterator{
87
        private StringTokenizer coordinatesString = null;        
88
        
89
        /**
90
         * It parses the gml:coordinates tag
91
         * @param parser
92
         * The XML parser
93
         * @param handler
94
         * The GPE parser that contains the content handler and
95
         * the error handler
96
         * @return
97
         * It retuns a matrix of doubles with 3 columns (x,y,z) and
98
         * one row for each coordinate.
99
         * @throws XmlStreamException
100
         * @throws IOException
101
         */
102
        /* (non-Javadoc)
103
         * @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)
104
         */
105
    public void initialize(IXmlStreamReader parser, GPEDefaultGmlParser handler, QName lastTag)
106
            throws XmlStreamException, IOException {
107
        parser.require(EventType.START_ELEMENT, GMLTags.GML_COORDINATES);
108
        super.initialize(parser, handler, lastTag);
109

    
110
        final EventType valueEvent = parser.next();
111
        if(valueEvent.isValue()){
112
            String coordinatesTag = parser.getText().trim();
113
            if (dimension == -1) {
114
                dimension = caculateDimension(coordinatesTag);
115
            }
116
            coordinatesString = new StringTokenizer(coordinatesTag, TUPLES_SEPARATOR);
117
            parser.nextTag();
118
        }
119
        parser.require(EventType.END_ELEMENT, GMLTags.GML_COORDINATES);
120
    }
121
        
122
        /**
123
         * Calculates the dimension from a array of coordinates
124
         * @param coordinatesTag
125
         * @return
126
         */
127
        private int caculateDimension(String coordinatesTag) {
128
                String firstPair;
129
                StringTokenizer st = new StringTokenizer(coordinatesTag,TUPLES_SEPARATOR);
130
                if (st.hasMoreTokens()){
131
                        firstPair = st.nextToken();
132
                }else{                        
133
                        firstPair = coordinatesTag;
134
                }                
135
                return StringUtils.splitString(firstPair, COORDINATES_SEPARATOR).length;                
136
        }
137

    
138
        /*
139
         * (non-Javadoc)
140
         * @see org.gvsig.gpe.parser.ICoordinateIterator#hasNext()
141
         */
142
        public boolean hasNext() throws IOException {
143
                return coordinatesString.hasMoreTokens();        
144
        }
145

    
146
        /*
147
         * (non-Javadoc)
148
         * @see org.gvsig.gpe.parser.ICoordinateIterator#next(double[])
149
         */
150
        public void next(double[] buffer) throws IOException {
151
                String next = coordinatesString.nextToken();
152
                String[] coordinates = org.gvsig.gpe.utils.StringUtils.splitString(next.trim(),COORDINATES_SEPARATOR);
153
                for (int i=0 ; i<coordinates.length ; i++){
154
                        buffer[i] = DoubleTypeBinding.parse(coordinates[i],COORDINATES_DECIMAL);
155
                }        
156
        }
157

    
158

    
159
}