Revision 11347

View differences:

trunk/libraries/libGPE-GML/src-test/org/gvsig/gpe/gml/readers/GMLPointsReader.java
1
package org.gvsig.gpe.gml.readers;
2

  
3
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
4
 *
5
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
20
 *
21
 * For more information, contact:
22
 *
23
 *  Generalitat Valenciana
24
 *   Conselleria d'Infraestructures i Transport
25
 *   Av. Blasco Ib??ez, 50
26
 *   46010 VALENCIA
27
 *   SPAIN
28
 *
29
 *      +34 963862235
30
 *   gvsig@gva.es
31
 *      www.gvsig.gva.es
32
 *
33
 *    or
34
 *
35
 *   IVER T.I. S.A
36
 *   Salamanca 50
37
 *   46005 Valencia
38
 *   Spain
39
 *
40
 *   +34 963163400
41
 *   dac@iver.es
42
 */
43
/* CVS MESSAGES:
44
 *
45
 * $Id$
46
 * $Log$
47
 * Revision 1.1  2007-04-25 11:08:38  csanchez
48
 * Parseo correcto con XSOM de esquemas, EntityResolver para los imports
49
 *
50
 *
51
 */
52
/**
53
 * @author Carlos S?nchez Peri??n (sanchez_carper@gva.es)
54
 */
55
public class GMLPointsReader extends GMLReaderBaseTest {
56
	
57
	//Aqui se le pasa el fichero a usar por el test
58
	public String getFile() {
59
		return "testdata/GML-points.gml";
60
	}
61

  
62
	public boolean hasSchema() {
63
		// TODO Auto-generated method stub
64
		return false;
65
	}
66

  
67
	public void makeAsserts() {
68
		// TODO Auto-generated method stub
69
		
70
	}
71
}
0 72

  
trunk/libraries/libGPE-GML/src-test/org/gvsig/gpe/gml/readers/libGPEReaderTestSuite.java
1 1
package org.gvsig.gpe.gml.readers;
2 2

  
3
import org.gvsig.gpe.gml.writers.GMLLayerHeaderTest;
4

  
5 3
import junit.framework.Test;
6 4
import junit.framework.TestSuite;
7 5

  
......
49 47
 *
50 48
 * $Id$
51 49
 * $Log$
52
 * Revision 1.2  2007-04-20 08:33:37  jorpiell
50
 * Revision 1.3  2007-04-25 11:08:38  csanchez
51
 * Parseo correcto con XSOM de esquemas, EntityResolver para los imports
52
 *
53
 * Revision 1.2  2007/04/20 08:33:37  jorpiell
53 54
 * Test updated
54 55
 *
55 56
 * Revision 1.1  2007/04/20 08:13:43  csanchez
......
65 66
	public static Test suite() {
66 67
		TestSuite suite = new TestSuite("Test for org.gvsig.gpe.gml.readers");
67 68
		//$JUnit-BEGIN$
68
		suite.addTestSuite(GMLLinesReader.class);
69
		//suite.addTestSuite(GMLLinesReader.class);
70
		suite.addTestSuite(GMLPointsReader.class);
69 71
		//$JUnit-END$
70 72
		return suite;
71 73
	}
trunk/libraries/libGPE-GML/src-test/org/gvsig/gpe/xml/XSOMSchemaParserTest.java
4 4
import java.io.IOException;
5 5
import java.util.Iterator;
6 6

  
7
import org.xml.sax.ErrorHandler;
7
import junit.framework.TestCase;
8

  
8 9
import org.xml.sax.SAXException;
9 10

  
10 11
import com.sun.xml.xsom.XSElementDecl;
11 12
import com.sun.xml.xsom.XSSchema;
12
import com.sun.xml.xsom.XSSchemaSet;
13 13
import com.sun.xml.xsom.XSType;
14 14
import com.sun.xml.xsom.parser.AnnotationParser;
15
import com.sun.xml.xsom.parser.AnnotationParserFactory;
16 15
import com.sun.xml.xsom.parser.XSOMParser;
17 16
import com.sun.xml.xsom.util.DomAnnotationParserFactory;
18 17

  
19
import junit.framework.TestCase;
20

  
21 18
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
22 19
 *
23 20
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
......
62 59
 *
63 60
 * $Id$
64 61
 * $Log$
65
 * Revision 1.2  2007-04-12 10:24:12  jorpiell
62
 * Revision 1.3  2007-04-25 11:08:38  csanchez
63
 * Parseo correcto con XSOM de esquemas, EntityResolver para los imports
64
 *
65
 * Revision 1.2  2007/04/12 10:24:12  jorpiell
66 66
 * Add the GML and schema tests
67 67
 *
68 68
 * Revision 1.1  2007/04/11 11:16:42  jorpiell
......
76 76
public class XSOMSchemaParserTest extends TestCase {
77 77
	protected void setUp() throws SAXException, IOException{
78 78
		XSOMParser parser = new XSOMParser();
79
		parser.parse(new File("testdata/schemas/2_1_2/feature.xsd"));
79
		parser.parse(new File("testdata/schemas/2.1.2/feature.xsd"));
80 80
	}
81 81

  
82 82
	public void testParseLocalidadSchema() throws SAXException, IOException {
trunk/libraries/libGPE-GML/src/org/gvsig/gpe/gml/GMLSchemaEntityResolver.java
1
package org.gvsig.gpe.gml;
2

  
3
import java.io.File;
4
import java.io.IOException;
5
import java.util.Hashtable;
6

  
7
import org.xml.sax.EntityResolver;
8
import org.xml.sax.InputSource;
9
import org.xml.sax.SAXException;
10

  
11
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
12
 *
13
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
14
 *
15
 * This program is free software; you can redistribute it and/or
16
 * modify it under the terms of the GNU General Public License
17
 * as published by the Free Software Foundation; either version 2
18
 * of the License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU General Public License
26
 * along with this program; if not, write to the Free Software
27
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
28
 *
29
 * For more information, contact:
30
 *
31
 *  Generalitat Valenciana
32
 *   Conselleria d'Infraestructures i Transport
33
 *   Av. Blasco Ib??ez, 50
34
 *   46010 VALENCIA
35
 *   SPAIN
36
 *
37
 *      +34 963862235
38
 *   gvsig@gva.es
39
 *      www.gvsig.gva.es
40
 *
41
 *    or
42
 *
43
 *   IVER T.I. S.A
44
 *   Salamanca 50
45
 *   46005 Valencia
46
 *   Spain
47
 *
48
 *   +34 963163400
49
 *   dac@iver.es
50
 */
51
/* CVS MESSAGES:
52
 *
53
 * $Id$
54
 * $Log$
55
 * Revision 1.1  2007-04-25 11:08:38  csanchez
56
 * Parseo correcto con XSOM de esquemas, EntityResolver para los imports
57
 *
58
 *
59
 */
60
/**
61
 * @author Carlos S?nchez Peri??n (sanchez_carper@gva.es)
62
 */
63
public class GMLSchemaEntityResolver implements EntityResolver{
64
	private static Hashtable schemas = new Hashtable();
65
	private static String PATH = "/home/csanche/workspace/libGPE-GML/testdata/schemas/2.1.2/";
66
	
67
	static{
68
		// System Identifiers of public gml 2.x schemas 
69
		schemas.put("feature.xsd",PATH+"feature.xsd");
70
		schemas.put("geometry.xsd",PATH+"geometry.xsd");
71
		schemas.put("xlinks.xsd",PATH+"xlinks.xsd");
72
	}
73
	
74
	public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
75
		String fileName = getFileName(systemId);
76
		if (schemas.get(fileName)!=null)
77
		{
78
			String fullLocalPath = schemas.get(fileName).toString();
79
			return new InputSource(fullLocalPath);
80
		}
81
		// If no match, returning null makes process continue normally
82
        return null;
83
	}	
84
	
85
	public String getFileName(String fullPathName){
86
		File f=new File(fullPathName);
87
		String fileName = f.getName().toString();
88
		return fileName;
89
	}
90
	
91
	public void setPath(String dir){
92
		PATH=dir;
93
	}
94
}
0 95

  
trunk/libraries/libGPE-GML/src/org/gvsig/gpe/gml/GPEGmlParser.java
8 8
import java.net.MalformedURLException;
9 9
import java.net.URL;
10 10
import java.net.UnknownHostException;
11
import java.util.Iterator;
11 12
import java.util.StringTokenizer;
12 13

  
13 14
import org.gvsig.exceptions.BaseException;
......
20 21
import org.xmlpull.v1.XmlPullParser;
21 22
import org.xmlpull.v1.XmlPullParserException;
22 23

  
24
import com.sun.xml.xsom.XSElementDecl;
25
import com.sun.xml.xsom.XSSchema;
26
import com.sun.xml.xsom.XSSchemaSet;
27
import com.sun.xml.xsom.XSType;
23 28
import com.sun.xml.xsom.parser.XSOMParser;
24 29

  
25 30
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
......
66 71
 *
67 72
 * $Id$
68 73
 * $Log$
69
 * Revision 1.5  2007-04-20 12:04:10  csanchez
74
 * Revision 1.6  2007-04-25 11:08:38  csanchez
75
 * Parseo correcto con XSOM de esquemas, EntityResolver para los imports
76
 *
77
 * Revision 1.5  2007/04/20 12:04:10  csanchez
70 78
 * Actualizacion protoripo libGPE, AƱadidos test para el parser, parseo con XSOM
71 79
 *
72 80
 * Revision 1.4  2007/04/19 11:51:43  csanchez
......
79 87
 */
80 88
/**
81 89
 * @author Carlos S?nchez Peri??n (sanchez_carper@gva.es)
82
 */
90
 **/
91

  
83 92
public class GPEGmlParser extends GPEXmlParser{
84 93

  
85 94
	private XSOMParser schemaParser = null;
......
88 97
	private File schemaFile = null;
89 98
	private int no_schema = 1;
90 99
	
100
	/*********************
101
	 * <GPEGmlParser> 
102
	 * Constructor Method
103
	 *********************/
91 104
	public GPEGmlParser() {
92 105
		// TODO Ap?ndice de constructor generado autom?ticamente
93 106
	}
94 107
	
95
	/*
96
	 * (non-Javadoc)
97
	 * @see org.gvsig.gpe.GPEParser#getFormats()
98
	 */
108
	/**************************************************************
109
	 * <getFormats>
110
	 * Returns the file extensions that are accepted to GML Parser
111
	 * @return String[] Accepted Extensions
112
	 **************************************************************/
99 113
	public String[] getFormats() {
100 114
		String[] formats = new String[2];
101 115
		formats[0] = "GML";
102 116
		formats[1] = "XML";
103 117
		return formats;
104 118
	}
105

  
119
	
120
	/************************************************************************
121
	 * <accept>
122
	 * Returns true if the file is a gml file and the parser has to parse it
123
	 * @param File file
124
	 * @return boolean 
125
	 ************************************************************************/
106 126
	public boolean accept(File file) {
107 127
		if ((file.getName().toUpperCase().endsWith("GML"))
108 128
				|| (file.getName().toUpperCase().endsWith("XML"))) {
......
110 130
		}
111 131
		return false;
112 132
	}
133
	
134
	/***********************************************************
135
	 * <getWriter>
136
	 * (non-Javadoc)
137
	 * @see org.gvsig.gpe.GPEParser#getWriter(java.lang.String)
138
	 ***********************************************************/
139
	public GPEWriterHandler getWriter(String format) {
140
		return new GPEGmlWriterHandler();
141
	}
113 142

  
143
	/**********************************************
144
	 * <getVersions>
145
	 * Returns the gml versions that are supported
146
	 * @return String[] Accepted Versions
147
	 **********************************************/
148
	public String[] getVersions() {
149
		// TODO Ap?ndice de m?todo generado autom?ticamente
150
		return null;
151
	}
152
	
153
	/*************************************
154
	 * <createInputStream>
155
	 * Creates an InputStream from a file.
156
	 * @param File file
157
	 * @return FileInputStream
158
	 *************************************/
114 159
	protected InputStream createInputStream(File file) throws FileNotFoundException {
115 160
		// TODO Ap?ndice de m?todo generado autom?ticamente
116 161
		return new FileInputStream(file);
117 162
	}
118 163
	
164
	/****************************
165
	 * <initParse>
166
	 * Starts to parse the file.
167
	 ****************************/
119 168
	protected void initParse() {
120 169
		//First, it gets the file parser.
121 170
		fileParser = getParser();
122 171
		//it has to parse the head 
123 172
		parseHead();
124 173
	}
125
	/*
126
	 * (non-Javadoc)
127
	 * @see org.gvsig.gpe.GPEParser#getWriter(java.lang.String)
128
	 */
129
	public GPEWriterHandler getWriter(String format) {
130
		return new GPEGmlWriterHandler();
131
	}
132 174

  
133
	public String[] getVersions() {
134
		// TODO Ap?ndice de m?todo generado autom?ticamente
135
		return null;
136
	}
175
	/************************************************************************
176
	 * <parseHead>
177
	 * parses the head of the gml file, it contains the URI of 
178
	 * the remoteschema and namespaces, normally its a FeatureCollection Tag 
179
	 ************************************************************************/
137 180
	private void parseHead(){
138 181
		//nextTag() --> Method from KXML library to get next full tag
139 182
		try {
......
179 222
			//warnings.setElement(new GMLWarningNoSchema());
180 223
		}
181 224
	}
182
	/**
225
	
226
	/***********************************************
227
	 * <parseSchemaLocation>
183 228
	 * It downloads the schema's file and parse it
184 229
	 * @param xmlnsName : Alias
185 230
	 * @param xmlnsValue: URI 
186
	 */
231
	 ***********************************************/
187 232
	private void parseSchemaLocation(String schemaAlias, String schemaURI){
188 233
		//It take the name of the schemas file to open or downlad 
189 234
		StringTokenizer tokenizer = new StringTokenizer(schemaURI, " \t");
......
210 255
				}
211 256
				//It downloads the schema if it's a remote schema
212 257
				schemaFile = getSchemaFile(schemaLocation);
213
            		//first of all parse the gml schemas...
214
				//only for the first time.
215
				parseGMLSchemas();
258
            		initSchemaParser();
216 259
				try {
217 260
					//It parses the schema.
218 261
					schemaParser.parse(schemaFile);
......
223 266
					// TODO Bloque catch generado autom?ticamente
224 267
					System.out.println("Error de lectura/escritura del esquema: "+e.getMessage());
225 268
				}
269
				try {
270
//				Iterator itr = schemaParser.getResult().iterateSchema();
271
					showSchemaInfo();
272
				} catch (SAXException e) {
273
					// TODO Bloque catch generado autom?ticamente
274
					System.out.println("Excepci?n del parser SAX: "+e.getMessage());
275
				}
226 276
            }
227 277
        }
228 278
	}
229
	private void parseGMLSchemas() {
279
	
280
	/*******************************
281
	 * <initSchemaParser>
282
	 * initializes the XSOM parser
283
	 *******************************/
284
	private void initSchemaParser() {
230 285
		//initialize the XSOMParser.
231 286
		if (schemaParser==null){
232 287
			schemaParser = new XSOMParser();
288
			GMLSchemaEntityResolver schemaResolver = new GMLSchemaEntityResolver();
289
			 // Crearemos un EntityResolver() para los imports e includes 
290
			 // que nos convenga almacenar localmente para un parseo r?pido
291
			 // o configurar una ruta local para la b?squeda por defecto.
292
			schemaParser.setEntityResolver(schemaResolver);
293
			
233 294
		}
234
		File ogcSchema = new File("/home/csanche/workspace/libGPE-GML/testdata/schemas/2.1.2/feature.xsd");
235
		try {
236
			schemaParser.parse(ogcSchema);
237
		} catch (SAXException e) {
238
			// TODO Bloque catch generado autom?ticamente
239
			System.out.println("Error al parsear el Schema: "+e.getMessage());
240
		} catch (IOException e) {
241
			// TODO Bloque catch generado autom?ticamente
242
			System.out.println("Error de lectura/escritura del esquema: "+e.getMessage());
243
		}
244
		System.out.println("INFO: Parseado el esquema "+ogcSchema+" con ?xito.");
245
		// TODO Ap?ndice de m?todo generado autom?ticamente
246
		
247 295
	}
248 296

  
249
	/**
297
	/************************************************
298
	 * <parseNamespace>
250 299
	 * It adds an XML namespace tag to the hashtable
251 300
	 * @param xmlnsName : Namespace
252 301
	 * @param xmlnsValue: URI 
253
	 */
302
	 ************************************************/
254 303
	private void parseNameSpace(String xmlnsName,String xmlnsValue){
255 304
		XMLSchemasFactory.addType(xmlnsName,xmlnsValue);		
256 305
	}
257
	/**
306

  
307
	/****************************************************************************
308
	 * <getSchemaFile>
258 309
	 * It downloads the schema if it's a remote schema
259 310
	 * else it tries to open a local file and return if it's succesfull
260
	 * @param String : schemaLocation
261
	 */
311
	 * @param String schema location
312
	 * @return File local (if is a remote file then first it has to download it)
313
	 ****************************************************************************/
262 314
	private File getSchemaFile(String schemaLocation){
263 315
		File f = null;
264
		
316
		                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
265 317
		//If it is a local file, it has to construct the absolute route
266 318
		if (schemaLocation.indexOf("http://") != 0){
267 319
			f = new File(schemaLocation);
......
295 347
			return f;	
296 348
		}
297 349
	}
350
	
351
	/**********************************
352
	 * <showSchemaInfo>
353
	 * Shows the XSSchema Information 
354
	 **********************************/
355
	public void showSchemaInfo() throws SAXException{
356
		Iterator itr = schemaParser.getResult().iterateSchema();
357
		while( itr.hasNext() ) {
358
			  XSSchema s = (XSSchema)itr.next();
359
			  System.out.println("Target namespace: "+s.getTargetNamespace());
360
			  
361
			  Iterator jtr = s.iterateElementDecls();
362
			  while( jtr.hasNext() ) {
363
				  XSElementDecl e = (XSElementDecl)jtr.next();
364
			    
365
				  System.out.print("ELEMENT:" + e.getName() );
366
				  if (e.getAnnotation() != null){
367
					  System.out.print(" ANOTATION: " + e.getAnnotation().getAnnotation());
368
				  }
369
				  XSType type = e.getType();
370
				  if (type.getAnnotation() != null){
371
					  System.out.print(" ANOTATION TYPE: " + type.getAnnotation().getLocator().toString());
372
				  }
373
				  if( e.isAbstract() ){
374
					  System.out.print(" (abstract)");
375
				  }
376
				  System.out.println();
377
			  }
378
		} 
379
		schemaParser.getResult().iterateSchema();
380
		while( itr.hasNext() ) {
381
			XSSchema s = (XSSchema)itr.next();
382
			System.out.println("Target namespace: "+s.getTargetNamespace());
383
		  
384
			Iterator jtr = s.iterateElementDecls();
385
			while( jtr.hasNext() ) {
386
				XSElementDecl e = (XSElementDecl)jtr.next();
387
		    
388
				System.out.print( e.getName() );
389
				if( e.isAbstract() )
390
					System.out.print(" (abstract)");
391
				System.out.println();
392
			}
393
		}
394
	}
298 395
}

Also available in: Unified diff