Statistics
| Revision:

root / trunk / libraries / libGPE-GML / src / org / gvsig / gpe / gml / GMLReader.java @ 11171

History | View | Annotate | Download (6.48 KB)

1 10550 csanchez
package org.gvsig.gpe.gml;
2
3
import java.awt.geom.Rectangle2D;
4
import java.io.File;
5
import java.io.FileNotFoundException;
6
import java.io.IOException;
7
8
import org.gvsig.exceptions.BaseException;
9
import org.gvsig.exceptions.ListBaseException;
10
import org.gvsig.gpe.gml.exceptions.GMLException;
11
import org.gvsig.gpe.gml.exceptions.GMLParserException;
12
import org.gvsig.gpe.gml.factories.FeaturesParserFactory;
13
import org.gvsig.gpe.gml.factories.IGeometriesFactory;
14
import org.gvsig.gpe.gml.factories.XMLParserFactory;
15
import org.gvsig.gpe.gml.schemas.XMLSchemaManager;
16
import org.gvsig.gpe.gml.schemas.XMLSchemaParser;
17
import org.gvsig.gpe.gml.warnings.GMLWarningInfo;
18
import org.xmlpull.v1.XmlPullParserException;
19
20
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
21
 *
22
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
23
 *
24
 * This program is free software; you can redistribute it and/or
25
 * modify it under the terms of the GNU General Public License
26
 * as published by the Free Software Foundation; either version 2
27
 * of the License, or (at your option) any later version.
28
 *
29
 * This program is distributed in the hope that it will be useful,
30
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32
 * GNU General Public License for more details.
33
 *
34
 * You should have received a copy of the GNU General Public License
35
 * along with this program; if not, write to the Free Software
36
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
37
 *
38
 * For more information, contact:
39
 *
40
 *  Generalitat Valenciana
41
 *   Conselleria d'Infraestructures i Transport
42
 *   Av. Blasco Ib??ez, 50
43
 *   46010 VALENCIA
44
 *   SPAIN
45
 *
46
 *      +34 963862235
47
 *   gvsig@gva.es
48
 *      www.gvsig.gva.es
49
 *
50
 *    or
51
 *
52
 *   IVER T.I. S.A
53
 *   Salamanca 50
54
 *   46005 Valencia
55
 *   Spain
56
 *
57
 *   +34 963163400
58
 *   dac@iver.es
59
 */
60
/* CVS MESSAGES:
61
 *
62
 * $Id$
63
 * $Log$
64
 * Revision 1.1  2007-02-28 11:48:31  csanchez
65
 * *** empty log message ***
66
 *
67
 * Revision 1.1  2007/02/20 10:53:20  jorpiell
68
 * A?adidos los proyectos de kml y gml antiguos
69
 *
70
 * Revision 1.4  2007/01/15 13:11:00  csanchez
71
 * Sistema de Warnings y Excepciones adaptado a BasicException
72
 *
73
 * Revision 1.3  2006/12/22 11:25:44  csanchez
74
 * Nuevo parser GML 2.x para gml's sin esquema
75
 *
76
 * Revision 1.2  2006/11/06 12:15:11  jorpiell
77
 * A?adido un constructor con un par?metro booleano que se usar? para abrir gml's sin validar el esquema
78
 *
79
 * Revision 1.1  2006/08/10 12:00:49  jorpiell
80
 * Primer commit del driver de Gml
81
 *
82
 *
83
 */
84
/**
85
 * This class must be used to read any GML 2.x file (2.0, 2.1.1
86
 * 2.1.2). It is able to parse GML features that have a xml complex
87
 * type with only one level of complexity. More levels and GML 3.x
88
 * will be supported in next versions.
89
 *
90
 * Name: OpenGIS? Geography Markup Language (GML) Implementation Specification,
91
 * Version: 2.x
92
 * Project Document: 01-029 (2.0), 02-099 (2.1.1) and 02-069 (v2.1.2)
93
 *
94
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
95
 * @author Carlos S?nchez Peri??n (sanchez_carper@gva.es)
96
 *
97
 */
98
public class GMLReader {
99
        private XMLSchemaManager schemaManager = null;
100
        private GMLFeaturesParser featuresParser = null;
101
        private FeaturesParserFactory featuresFactory = null;
102
        private GMLWarningInfo warnings = null;
103
        private File m_File = null;
104
105
106
        /**
107
         * Constructor
108
         * @param file
109
         * GML File to read
110
         * @param factory
111
         * Geometries factory that is used to create the parsed
112
         * GML geometries
113
         * @throws Exception
114
         * @throws FileNotFoundException
115
         * When the file is not found
116
         */
117
        public GMLReader(File file,IGeometriesFactory factory) throws GMLException{
118
119
                //file to parse...
120
                this.m_File = file;
121
                initialize(factory);
122
        }
123
124
        /**
125
         * Initializes the XMLSchemaParser, tries to parse  the GML header
126
         * to obtain the GML version and parses all the needed schemas
127
         * @param factory
128
         * Geometries factory that is used to create the parsed
129
         * GML geometries
130
         * @throws Exception
131
         */
132
        private void initialize(IGeometriesFactory factory) throws GMLException{
133
134
                warnings = new GMLWarningInfo();
135
                XMLSchemaParser parser = new XMLParserFactory().createSchemaParser(m_File);
136
137
                //Retrieve the header attributes and download and parse the schema
138
                schemaManager = new XMLSchemaManager(m_File);
139
                try {
140
                        schemaManager.parse(parser);
141
                } catch (XmlPullParserException e) {
142
                        // TODO Auto-generated catch block
143
                        throw new GMLException(m_File.getName(),e);
144
                } catch (IOException e) {
145
                        // TODO Auto-generated catch block
146
                        throw new GMLException(m_File.getName(),e);
147
                }
148
149
                //Parse the GML global fields
150
                featuresFactory = new FeaturesParserFactory();
151
                try {
152
                        featuresParser = featuresFactory.createParser(getVersion(),factory,parser);
153
                } catch (GMLParserException e) {
154
                        throw new GMLException(m_File.getName(),e);
155
                }
156
157
        }
158
159
        /**
160
         * Gets the GML iteartor used to retrieve all the
161
         * features
162
         * @param factory
163
         * Factory to create the geometries
164
         * @return
165
         */
166
        public IGMLFeaturesIterator getFeaturesIterator() throws GMLException{
167
                try {
168
                        return featuresParser.getFeaturesReader();
169
                } catch (BaseException e) {
170
                        throw new GMLException(e);
171
                }
172
        }
173
174
        /**
175
         * @return Returns the version.
176
         */
177
        public String getVersion(){
178
                return schemaManager.getVersion();
179
        }
180
181
        /**
182
         * @return Returns a list of codes with information to the user about GML parse.
183
         * @throws ListBaseException
184
         */
185
        public  ListBaseException getWarnings(){
186
                if (schemaManager.warnings.areWarnings())
187
                {
188
                        warnings.setGMLWarningList(schemaManager.warnings.getGMLWarningList());
189
                }
190
                if (featuresFactory.warnings.areWarnings())
191
                {
192
                        warnings.setGMLWarningList(featuresFactory.warnings.getGMLWarningList());
193
                }
194
                //***********************************************************
195
                // CUIDADO CON LANZAR LOS WARNINGS, YA VEREMOS COMO LANZARLOS
196
                //***********************************************************
197
                return warnings.getGMLWarningList();
198
199
        }
200
201
//        /**
202
//         * @return Returns true if there are warnings parsing the gml file.
203
//         */
204
//        public boolean areWarnings(){
205
//                this.getWarnings();
206
//                if (warnings.areWarnings())
207
//                {
208
//                        return true;
209
//                }
210
//                return false;
211
//        }
212
213
        /**
214
         * @return Returns the target namespace.
215
         */
216
        public String getTargetNamespace(){
217
                return schemaManager.getTargetNamespace();
218
        }
219
220
        /**
221
         * @return Returns the extent.
222
         */
223
        public Rectangle2D getExtent(){
224
                return featuresParser.getExtent().getExtent();
225
        }
226
227
        /**
228
         * @return Returns the SRS
229
         */
230
        public String getSRS(){
231
                return featuresParser.getExtent().getSrs();
232
        }
233
}