Statistics
| Revision:

root / branches / v10 / libraries / libRemoteServices / src / org / gvsig / remoteClient / gml / GMLReader.java @ 10876

History | View | Annotate | Download (6.54 KB)

1
package org.gvsig.remoteClient.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.remoteClient.gml.exceptions.BaseException;
9
import org.gvsig.remoteClient.gml.exceptions.GMLException;
10
import org.gvsig.remoteClient.gml.exceptions.GMLParserException;
11
import org.gvsig.remoteClient.gml.exceptions.ListBaseException;
12
import org.gvsig.remoteClient.gml.factories.FeaturesParserFactory;
13
import org.gvsig.remoteClient.gml.factories.IGeometriesFactory;
14
import org.gvsig.remoteClient.gml.factories.XMLParserFactory;
15
import org.gvsig.remoteClient.gml.schemas.XMLSchemaManager;
16
import org.gvsig.remoteClient.gml.schemas.XMLSchemaParser;
17
import org.gvsig.remoteClient.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: GMLReader.java 9917 2007-01-25 16:13:00Z jorpiell $
63
 * $Log$
64
 * Revision 1.1.2.3  2007-01-25 16:12:59  jorpiell
65
 * Se han sustituido las clases por las que hay en el nuevo driver de GML.
66
 *
67
 * Revision 1.4  2007/01/15 13:11:00  csanchez
68
 * Sistema de Warnings y Excepciones adaptado a BasicException
69
 *
70
 * Revision 1.3  2006/12/22 11:25:44  csanchez
71
 * Nuevo parser GML 2.x para gml's sin esquema
72
 *
73
 * Revision 1.2  2006/11/06 12:15:11  jorpiell
74
 * A?adido un constructor con un par?metro booleano que se usar? para abrir gml's sin validar el esquema
75
 *
76
 * Revision 1.1  2006/08/10 12:00:49  jorpiell
77
 * Primer commit del driver de Gml
78
 *
79
 *
80
 */
81
/**
82
 * This class must be used to read any GML 2.x file (2.0, 2.1.1 
83
 * 2.1.2). It is able to parse GML features that have a xml complex
84
 * type with only one level of complexity. More levels and GML 3.x 
85
 * will be supported in next versions.
86
 * 
87
 * Name: OpenGIS? Geography Markup Language (GML) Implementation Specification,
88
 * Version: 2.x
89
 * Project Document: 01-029 (2.0), 02-099 (2.1.1) and 02-069 (v2.1.2)
90
 * 
91
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
92
 * @author Carlos S?nchez Peri??n (sanchez_carper@gva.es)
93
 * 
94
 */
95
public class GMLReader {
96
        private XMLSchemaManager schemaManager = null;
97
        private GMLFeaturesParser featuresParser = null;
98
        private FeaturesParserFactory featuresFactory = null;
99
        private GMLWarningInfo warnings = null;
100
        private File m_File = null;
101
        
102
        
103
        /**
104
         * Constructor
105
         * @param file
106
         * GML File to read
107
         * @param factory
108
         * Geometries factory that is used to create the parsed 
109
         * GML geometries
110
         * @throws Exception 
111
         * @throws FileNotFoundException
112
         * When the file is not found
113
         */
114
        public GMLReader(File file,IGeometriesFactory factory) throws GMLException{
115
                
116
                //file to parse...
117
                this.m_File = file;
118
                initialize(factory);
119
        }
120
                
121
        /**
122
         * Initializes the XMLSchemaParser, tries to parse  the GML header 
123
         * to obtain the GML version and parses all the needed schemas
124
         * @param factory
125
         * Geometries factory that is used to create the parsed 
126
         * GML geometries
127
         * @throws Exception 
128
         */
129
        private void initialize(IGeometriesFactory factory) throws GMLException{
130
                
131
                warnings = new GMLWarningInfo(); 
132
                XMLSchemaParser parser = new XMLParserFactory().createSchemaParser(m_File);
133
                
134
                //Retrieve the header attributes and download and parse the schema
135
                schemaManager = new XMLSchemaManager(m_File);
136
                try {
137
                        schemaManager.parse(parser);
138
                } catch (XmlPullParserException e) {
139
                        // TODO Auto-generated catch block
140
                        throw new GMLException(m_File.getName(),e);
141
                } catch (IOException e) {
142
                        // TODO Auto-generated catch block
143
                        throw new GMLException(m_File.getName(),e);
144
                }
145
                
146
                //Parse the GML global fields
147
                featuresFactory = new FeaturesParserFactory();
148
                try {
149
                        featuresParser = featuresFactory.createParser(getVersion(),factory,parser);
150
                } catch (GMLParserException e) {
151
                        throw new GMLException(m_File.getName(),e);
152
                }
153
                
154
        }
155
        
156
        /**
157
         * Gets the GML iteartor used to retrieve all the 
158
         * features
159
         * @param factory
160
         * Factory to create the geometries
161
         * @return
162
         */
163
        public IGMLFeaturesIterator getFeaturesIterator() throws GMLException{
164
                try {
165
                        return featuresParser.getFeaturesReader();
166
                } catch (BaseException e) {
167
                        throw new GMLException(e);
168
                }
169
        }                
170

    
171
        /**
172
         * @return Returns the version.
173
         */
174
        public String getVersion(){
175
                return schemaManager.getVersion();
176
        }
177
        
178
        /**
179
         * @return Returns a list of codes with information to the user about GML parse.
180
         * @throws ListBaseException 
181
         */
182
        public  ListBaseException getWarnings(){
183
                if (schemaManager.warnings.areWarnings())
184
                {
185
                        warnings.setGMLWarningList(schemaManager.warnings.getGMLWarningList());
186
                }
187
                if (featuresFactory.warnings.areWarnings())
188
                {
189
                        warnings.setGMLWarningList(featuresFactory.warnings.getGMLWarningList());
190
                }
191
                //***********************************************************
192
                // CUIDADO CON LANZAR LOS WARNINGS, YA VEREMOS COMO LANZARLOS
193
                //***********************************************************
194
                return warnings.getGMLWarningList();
195
                
196
        }
197
        
198
//        /**
199
//         * @return Returns true if there are warnings parsing the gml file.
200
//         */
201
//        public boolean areWarnings(){
202
//                this.getWarnings();
203
//                if (warnings.areWarnings())
204
//                {
205
//                        return true;
206
//                }
207
//                return false;
208
//        }
209
        
210
        /**
211
         * @return Returns the target namespace.
212
         */
213
        public String getTargetNamespace(){
214
                return schemaManager.getTargetNamespace();
215
        }
216

    
217
        /**
218
         * @return Returns the extent.
219
         */        
220
        public Rectangle2D getExtent(){
221
                return featuresParser.getExtent().getExtent();
222
        }
223
        
224
        /**
225
         * @return Returns the SRS
226
         */        
227
        public String getSRS(){
228
                return featuresParser.getExtent().getSrs();
229
        }
230
}