package org.gvsig.remoteClient.gml; import java.awt.geom.Rectangle2D; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import org.gvsig.remoteClient.gml.exceptions.GMLException; import org.gvsig.remoteClient.gml.exceptions.GMLExceptionList; import org.gvsig.remoteClient.gml.exceptions.GMLParserException; import org.gvsig.remoteClient.gml.factories.FeaturesParserFactory; import org.gvsig.remoteClient.gml.factories.IGeometriesFactory; import org.gvsig.remoteClient.gml.factories.XMLParserFactory; import org.gvsig.remoteClient.gml.schemas.XMLSchemaManager; import org.gvsig.remoteClient.gml.schemas.XMLSchemaParser; import org.gvsig.remoteClient.gml.warnings.GMLWarningInfo; import org.xmlpull.v1.XmlPullParserException; /* gvSIG. Sistema de Información Geográfica de la Generalitat Valenciana * * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA. * * For more information, contact: * * Generalitat Valenciana * Conselleria d'Infraestructures i Transport * Av. Blasco Ibáñez, 50 * 46010 VALENCIA * SPAIN * * +34 963862235 * gvsig@gva.es * www.gvsig.gva.es * * or * * IVER T.I. S.A * Salamanca 50 * 46005 Valencia * Spain * * +34 963163400 * dac@iver.es */ /* CVS MESSAGES: * * $Id: GMLReader.java 18271 2008-01-24 09:06:43Z jpiera $ * $Log$ * Revision 1.4 2007-01-15 13:11:00 csanchez * Sistema de Warnings y Excepciones adaptado a BasicException * * Revision 1.3 2006/12/22 11:25:44 csanchez * Nuevo parser GML 2.x para gml's sin esquema * * Revision 1.2 2006/11/06 12:15:11 jorpiell * Añadido un constructor con un parámetro booleano que se usará para abrir gml's sin validar el esquema * * Revision 1.1 2006/08/10 12:00:49 jorpiell * Primer commit del driver de Gml * * */ /** * This class must be used to read any GML 2.x file (2.0, 2.1.1 * 2.1.2). It is able to parse GML features that have a xml complex * type with only one level of complexity. More levels and GML 3.x * will be supported in next versions. * * Name: OpenGIS® Geography Markup Language (GML) Implementation Specification, * Version: 2.x * Project Document: 01-029 (2.0), 02-099 (2.1.1) and 02-069 (v2.1.2) * * @author Jorge Piera Llodrá (piera_jor@gva.es) * @author Carlos Sánchez Periñán (sanchez_carper@gva.es) * */ public class GMLReader { private XMLSchemaManager schemaManager = null; private GMLFeaturesParser featuresParser = null; private FeaturesParserFactory featuresFactory = null; private GMLWarningInfo warnings = null; private File m_File = null; /** * Constructor * @param file * GML File to read * @param factory * Geometries factory that is used to create the parsed * GML geometries * @throws Exception * @throws FileNotFoundException * When the file is not found */ public GMLReader(File file,IGeometriesFactory factory) throws GMLException{ //file to parse... this.m_File = file; initialize(factory); } /** * Initializes the XMLSchemaParser, tries to parse the GML header * to obtain the GML version and parses all the needed schemas * @param factory * Geometries factory that is used to create the parsed * GML geometries * @throws Exception */ private void initialize(IGeometriesFactory factory) throws GMLException{ warnings = new GMLWarningInfo(); XMLSchemaParser parser = new XMLParserFactory().createSchemaParser(m_File); //Retrieve the header attributes and download and parse the schema schemaManager = new XMLSchemaManager(m_File); try { schemaManager.parse(parser); } catch (XmlPullParserException e) { // TODO Auto-generated catch block throw new GMLException(m_File.getName(),e); } catch (IOException e) { // TODO Auto-generated catch block throw new GMLException(m_File.getName(),e); } //Parse the GML global fields featuresFactory = new FeaturesParserFactory(); try { featuresParser = featuresFactory.createParser(getVersion(),factory,parser); } catch (GMLParserException e) { throw new GMLException(m_File.getName(),e); } } /** * Gets the GML iteartor used to retrieve all the * features * @param factory * Factory to create the geometries * @return */ public IGMLFeaturesIterator getFeaturesIterator() throws GMLException{ return featuresParser.getFeaturesReader(); } /** * @return Returns the version. */ public String getVersion(){ return schemaManager.getVersion(); } /** * @return Returns a list of codes with information to the user about GML parse. * @throws ListBaseException */ public GMLExceptionList getWarnings(){ if (schemaManager.warnings.areWarnings()) { warnings.setGMLWarningList(schemaManager.warnings.getGMLWarningList()); } if (featuresFactory.warnings.areWarnings()) { warnings.setGMLWarningList(featuresFactory.warnings.getGMLWarningList()); } //*********************************************************** // CUIDADO CON LANZAR LOS WARNINGS, YA VEREMOS COMO LANZARLOS //*********************************************************** return warnings.getGMLWarningList(); } // /** // * @return Returns true if there are warnings parsing the gml file. // */ // public boolean areWarnings(){ // this.getWarnings(); // if (warnings.areWarnings()) // { // return true; // } // return false; // } /** * @return Returns the target namespace. */ public String getTargetNamespace(){ return schemaManager.getTargetNamespace(); } /** * @return Returns the extent. */ public Rectangle2D getExtent(){ return featuresParser.getExtent().getExtent(); } /** * @return Returns the SRS */ public String getSRS(){ return featuresParser.getExtent().getSrs(); } }