Revision 34026 branches/v2_0_0_prep/libraries/libRemoteServices/src/org/gvsig/remoteclient/wfs/schema/XMLSchemaParser.java

View differences:

XMLSchemaParser.java
1 1
package org.gvsig.remoteclient.wfs.schema;
2 2

  
3
import java.io.File;
4
import java.io.FileReader;
5 3
import java.io.IOException;
6 4

  
7
import org.kxml2.io.KXmlParser;
8 5
import org.xmlpull.v1.XmlPullParserException;
9 6

  
10
import org.gvsig.compat.CompatLocator;
11
import org.gvsig.compat.lang.StringUtils;
12
import org.gvsig.remoteclient.utils.CapabilitiesTags;
13 7
import org.gvsig.remoteclient.utils.EncodingXMLParser;
14
import org.gvsig.remoteclient.wfs.schema.type.XMLComplexType;
15 8

  
16 9
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
17 10
 *
......
100 93
	private String nameSpace = "";
101 94
	private String version = null;
102 95
	
103
	private static final StringUtils stringUtils = CompatLocator.getStringUtils();
104 96
	
105 97
	public XMLSchemaParser(){
106 98
		super();		
......
189 181
	public String getNameSpace(){
190 182
		try{
191 183
		String name = super.getName();
192
		if ((name!=null)&&(stringUtils.split(name, ":").length > 1)){
193
			return stringUtils.split(name, ":")[0];
184
		if ((name!=null)&&(name.split(":").length > 1)){
185
			return name.split(":")[0];
194 186
		}
195 187
		return "";
196 188
		}catch (NullPointerException e){
197 189
			return "";
198 190
		}
199
	}
191
	}	
200 192
	
201
	/********************************************************************************
202
	 *  FUNCTION PARSE(FILE,NAMESPACE) PARSING THE XML SCHEMA ".xsd"			   	*
203
	 * 																			   	*
204
	 *  	-gml version													 		*
205
	 *  	-elementFormDefault {qualified,non-qualified} (not implemented yet)		*
206
	 *  	-namespaces "xmlns" (nor implemented,only in gml file)		 			*
207
	 *  	-targetNamespace (the name of this schema)					 			*
208
	 *		-imports (not implemented)									   			*  
209
	 *		-features -> "elements"="types"										  	*
210
	 *																				*	
211
	 * @param f (file ".xsd" to parse)												*
212
	 * @param nameSpace (Schemas name)												*
213
	 * @throws IOException 															*
214
	 * @throws XmlPullParserException 												*
215
	 *																				*
216
	 ********************************************************************************/
217
	public void parse(File f,String nameSpace) throws XmlPullParserException, IOException{
218
		this.nameSpace = nameSpace;
219
		FileReader reader = null;       
220
		int tag;
221
		
222
		setInput(f);
223
		nextTag();
224
			
225
		//Parsing attributes from the head tag...
226
		if ( getEventType() != KXmlParser.END_DOCUMENT ) 
227
		{     
228
			/************************
229
			 * Etiqueta <schema>	*
230
			 ************************/
231
			setSchemaFromMainTag(getName());
232
			//Searching the init tag "schema"
233
			require(KXmlParser.START_TAG, null, CapabilitiesTags.WFS_SCHEMAROOT); 
234
			for (int i=0 ; i<getAttributeCount() ; i++){
235
				//getting attributes and values
236
				String attName = getAttributeName(i);
237
				String attValue = getAttributeValue(i);
238
				
239
				/********************************
240
				 * Atributo <targetNamespace>	*
241
				 ********************************/
242
				//Target Namespace (URI)
243
				//this is the namespace of all components in the schema
244
				//setTargetNamespace(); 
245
				if (attName.compareTo(GMLTags.XML_TARGET_NAMESPACE)==0){
246
					targetNameSpace = attValue;
247
				}
248
				
249
				/************************************
250
				 * Atributo <elementFormDefault>	*
251
				 ************************************/
252
				//Qualified--> Los elementos del espacio de nombres de destino deben cualificarse 
253
				//con el prefijo del espacio de nombres.
254
				//Unqualified--> No es necesario que los elementos del 
255
				//espacio de nombres de destino est?n cualificados con el prefijo del espacio de nombres.
256
				//(Espacion_de_Nombres:Elemento)
257
				//elementFormDefault(); 
258
				if (attName.compareTo(GMLTags.XML_ELEMENT_FORM_DEFAULT)==0){
259
				}
260
				
261
				/************************************
262
				 * Atributo <attributeFormDefault>	*
263
				 ************************************/
264
				//Lo mismo que el anterior pero con los atributos...
265
				//(Espacio_de_Nombres:Atributo)
266
				//attributeFormDefault();
267
				if (attName.compareTo(GMLTags.XML_ATTRIBUTE_FORM_DEFAULT)==0){
268
				}
269
				
270
				/************************
271
				 * Atributo <Version>	*
272
				 ************************/
273
				//Gets gml version to parse by the right parser.
274
				//getversion();
275
				if (attName.compareTo(GMLTags.VERSION)==0){
276
					version=attValue;
277
				}
278
			}
279
			tag = nextTag();
280
			
281
			while(tag != KXmlParser.END_DOCUMENT)
282
			{
283
				switch(tag)
284
				{
285
				case KXmlParser.START_TAG:
286
					
287
					/************************
288
					 * Etiqueta <import>	*
289
					 ************************/
290
					//imports elements from other schemas (other schema  ".xsd" files)
291
					
292
					/****************************
293
					 * Etiqueta <complexType>	*
294
					 ****************************/
295
					if (getName().compareTo(CapabilitiesTags.COMPLEXTYPE)==0){							
296
						for (int i=0 ; i<getAttributeCount() ; i++){
297
							/********************
298
							 * Atributo <name>	*
299
							 ********************/
300
							if (getAttributeName(i).compareTo(GMLTags.GML_NAME) == 0){
301
								// inserts a new complex type inside the namespace
302
								XMLComplexType complexType = XMLTypesFactory.addComplexType(nameSpace,getAttributeValue(i));
303
				    			parseComplexType(complexType);					    			
304
				    		}
305
				    		
306
						}
307
					}
308
					/****************************
309
					 * Etiqueta <simpleType>	*
310
					 ****************************/
311
					// SIMPLE TYPE elements like enumerations not implemented 
312
					else if (getName().compareTo(CapabilitiesTags.SIMPLETYPE)==0){
313
						parseSimpleType();
314
					}
315
					/************************
316
					 * Etiqueta <element>	*
317
					 ************************/
318
					else if (getName().compareTo(CapabilitiesTags.ELEMENT)==0){							
319
						XMLElement entity = XMLElementsFactory.addType(this);						
320
					}
321
					break;
322
					case KXmlParser.END_TAG:                            
323
						break;
324
					//Show the Text on the screen
325
					case KXmlParser.TEXT:
326
						                
327
						break;
328
				}
329
				tag = next();
330
			}
331
			require(KXmlParser.END_DOCUMENT, null, null);
332
		}
333
	}
334
	
335
	/****************************************************
336
	 *  FUNCTION PARSE SIMPLE TYPE()		 		   	*
337
	 * 											    	*
338
	 *  Parse simple types and its restrictions		 	*
339
	 *													*	
340
	 ****************************************************/
341
    private void parseSimpleType() throws IOException, XmlPullParserException{   
342
		int currentTag;
343
		boolean end = false;		
344
		currentTag = getEventType();
345
		
346
		String typeName = null;
347
		String typeValue = null;
348 193

  
349
		for (int i=0 ; i<getAttributeCount() ; i++){			
350
			if (getAttributeName(i).compareTo(CapabilitiesTags.ELEMENT_NAME) == 0){
351
				typeName = getAttributeValue(i);
352
			}
353
		}
354
		
355
		while (!end){
356
			switch(currentTag){
357
			case KXmlParser.START_TAG:
358
				if (getName().compareTo(CapabilitiesTags.RESTRICTION)==0){
359
					for (int i=0 ; i<getAttributeCount() ; i++){
360
						if (getAttributeName(i).compareTo(CapabilitiesTags.BASE) == 0){
361
							typeValue = getAttributeValue(i);
362
						}
363
					}					
364
				}   
365
				//Falta parsear los tipos enumerados
366
				break;
367
			case KXmlParser.END_TAG:
368
				if (getName().compareTo(CapabilitiesTags.SIMPLETYPE) == 0)
369
					end = true;
370
				break;
371
			case KXmlParser.TEXT:                   
372
				break;
373
			}
374
			if (!end){
375
				currentTag = next();
376
			}			
377
		}
378
		if ((typeName != null) && (typeValue != null)){
379
			XMLTypesFactory.addSimpleType(typeName,typeValue);
380
		}
381
	}
382
    
383
    /****************************************************
384
	 *  FUNCTION PARSE COMPLEX TYPE(COMPLEX TYPE) 	   	*
385
	 * 												   	*
386
	 *  Parse the attributes from a complex type		*
387
	 *													*	
388
	 * @param complexType								*
389
	 *													*
390
	 ****************************************************/
391
	
392
	private void parseComplexType(XMLComplexType complexType) throws IOException, XmlPullParserException{   
393
		int currentTag;
394
		boolean end = false;		
395
		
396
		require(KXmlParser.START_TAG, null, CapabilitiesTags.COMPLEXTYPE);
397
		currentTag = next();
398
		
399
		while (!end) 
400
		{
401
			switch(currentTag)
402
			{
403
			case KXmlParser.START_TAG:
404
				/********************************
405
				 * Etiqueta <complexContent>	*
406
				 ********************************/
407
				if (getName().compareTo(CapabilitiesTags.COMPLEXCONTENT)==0){
408
					parseComplexContent(complexType); 
409
				}
410
				/************************
411
				 * Etiqueta <sequence>	*
412
				 ************************/
413
				else if(getName().compareTo(CapabilitiesTags.SEQUENCE)==0){
414
					parseSequence(complexType);
415
				}
416
				/************************
417
				 * Etiqueta <choice>	*
418
				 ************************/
419
				else if(getName().compareTo(CapabilitiesTags.CHOICE)==0){
420
					parseChoice(complexType);
421
				}
422
				break;
423
			case KXmlParser.END_TAG:
424
				if (getName().compareTo(CapabilitiesTags.COMPLEXTYPE) == 0)
425
					end = true;
426
				break;
427
			case KXmlParser.TEXT:                   
428
				break;
429
			}
430
			if (!end){
431
				currentTag = next();
432
			}	
433
		}		
434
	}
435
	
436
	/************************************************************************************
437
	 *  FUNCION PARSE COMPLEX CONTENT(COMPLEX TYPE)									   	*
438
	 * 																				   	*
439
	 *  Parsea los atributos que componen un contenido complejo 					 	*
440
	 *																					*	
441
	 * @param complexType (elemento complejo a parsear)									*
442
	 *																					*
443
	 ************************************************************************************/
444
	
445
	private void parseComplexContent(XMLComplexType complexType) throws IOException, XmlPullParserException
446
	{   
447
		int currentTag;
448
		boolean end = false;
449
		
450
		//Establece como requisito abrir una etiqueta <complexContent>
451
		require(KXmlParser.START_TAG, null, CapabilitiesTags.COMPLEXCONTENT);
452
		currentTag = next();
453
		
454
		while (!end) 
455
		{
456
			switch(currentTag)
457
			{
458
			//Comparo las posibles etiquetas de apertura, las cuales solo nos interesa <extension>
459
			case KXmlParser.START_TAG:
460
				if (getName().compareTo(CapabilitiesTags.EXTENSION )==0){
461
					parseExtension(complexType); 
462
				}else if (getName().compareTo(CapabilitiesTags.RESTRICTION)==0){
463
					parseRestriction(complexType); 
464
				} 
465
				break;
466
			//encuentro una etiqueta de cierre de complexContent con lo cual cierro el tipo complejo
467
			case KXmlParser.END_TAG:
468
				if (getName().compareTo(CapabilitiesTags.COMPLEXCONTENT) == 0)
469
					end = true;
470
				break;
471
			//Siempre puede existir texto por el medio que ignoro
472
			case KXmlParser.TEXT:                   
473
				break;
474
			}
475
			if (!end){
476
				currentTag = next();
477
			}	
478
		}
479
	}
480
	
481
	//Comprueba que las etiquetas extension se abren y se cierran
482
	private void parseExtension(XMLComplexType complexType) throws IOException, XmlPullParserException
483
	{   
484
		int currentTag;
485
		boolean end = false;
486
				
487
		require(KXmlParser.START_TAG, null, CapabilitiesTags.EXTENSION);
488
		for (int i=0 ; i<getAttributeCount() ; i++){
489
			String attName = getAttributeName(i);
490
			String attValue = getAttributeValue(i);
491
			if (CapabilitiesTags.BASE.equals(attName)){
492
				complexType.setBaseType(attValue);				
493
			}
494
		}
495
		
496
		currentTag = next();		
497
		while (!end) 
498
		{
499
			switch(currentTag)
500
			{
501
			case KXmlParser.START_TAG:
502
				if (getName().compareTo(CapabilitiesTags.SEQUENCE)==0)
503
				{
504
					parseSequence(complexType); 
505
				}   
506
				break;
507
			case KXmlParser.END_TAG:
508
				if (getName().compareTo(CapabilitiesTags.EXTENSION) == 0)
509
					end = true;
510
				break;
511
			case KXmlParser.TEXT:                   
512
				break;
513
			}
514
			if (!end){
515
				currentTag = next();
516
			}			
517
		}
518
	}
519
	
520
	//Comprueba que las etiquetas extension se abren y se cierran
521
	private void parseRestriction(XMLComplexType complexType) throws IOException, XmlPullParserException
522
	{   
523
		int currentTag;
524
		boolean end = false;
525
		
526
		
527
		require(KXmlParser.START_TAG, null, CapabilitiesTags.RESTRICTION);
528
		currentTag = next();
529
		
530
		while (!end) 
531
		{
532
			switch(currentTag)
533
			{
534
			case KXmlParser.START_TAG:
535
				if (getName().compareTo(CapabilitiesTags.SEQUENCE)==0)
536
				{
537
					parseSequence(complexType); 
538
				}   
539
				break;
540
			case KXmlParser.END_TAG:
541
				if (getName().compareTo(CapabilitiesTags.RESTRICTION) == 0)
542
					end = true;
543
				break;
544
			case KXmlParser.TEXT:                   
545
				break;
546
			}
547
			if (!end){
548
				currentTag = next();
549
			}			
550
		}
551
	}
552
	
553
	private void parseSequence(XMLComplexType complexType) throws IOException, XmlPullParserException
554
	{   
555
		int currentTag;
556
		boolean end = false;
557
		XMLElement elemento_previo=null;
558
		
559
		require(KXmlParser.START_TAG, null, CapabilitiesTags.SEQUENCE);
560
		currentTag = next();
561
		
562
		while (!end) 
563
		{
564
			switch(currentTag)
565
			{
566
			case KXmlParser.START_TAG:
567
				/************************
568
				 * Etiqueta <Element>	*
569
				 ************************/
570
				if (getName().compareTo(CapabilitiesTags.ELEMENT)==0){
571
					XMLElement element = new XMLElement(this);
572
					if (element != null){
573
						complexType.addElements(element);
574
					}
575
					elemento_previo=element;
576
				}
577
				/****************************
578
				 * Etiqueta <ComplexType>	*
579
				 ****************************/
580
				else if(getName().compareTo(CapabilitiesTags.COMPLEXTYPE)==0){
581
					for (int i=0 ; i<getAttributeCount() ; i++){
582
			    		if (getAttributeName(i).compareTo(GMLTags.GML_NAME) == 0){
583
			    			XMLComplexType complexTypeChild = XMLTypesFactory.addComplexType(nameSpace,getAttributeValue(i));
584
			    			parseComplexType(complexTypeChild);					    			
585
			    		}			    		
586
					}
587
				}
588
				else if(getName().compareTo(CapabilitiesTags.SIMPLETYPE)==0){
589
					elemento_previo.parseSimpleType(this);
590
				}
591
				break;
592
			case KXmlParser.END_TAG:
593
				if (getName().compareTo(CapabilitiesTags.SEQUENCE) == 0)
594
					end = true;
595
				break;
596
			case KXmlParser.TEXT:                   
597
				break;
598
			}
599
			if (!end){
600
				currentTag = next();
601
			}
602
		}		
603
	}
604

  
605
	private void parseChoice(XMLComplexType complexType) throws IOException, XmlPullParserException
606
	{   
607
		int currentTag;
608
		boolean end = false;
609
			
610
		require(KXmlParser.START_TAG, null, CapabilitiesTags.CHOICE);
611
		currentTag = next();
612
		
613
		complexType.setAttributesType(XMLComplexType.CHOICE_TYPE);
614
		
615
		while (!end) 
616
		{
617
			switch(currentTag)
618
			{
619
			case KXmlParser.START_TAG:
620
				/************************
621
				 * Etiqueta <Element>	*
622
				 ************************/
623
				if (getName().compareTo(CapabilitiesTags.ELEMENT)==0){
624
					XMLElement element = new XMLElement(this);
625
					if (element != null){
626
						complexType.addElements(element);
627
					}					
628
				}				
629
				break;
630
			case KXmlParser.END_TAG:
631
				if (getName().compareTo(CapabilitiesTags.CHOICE) == 0)
632
					end = true;
633
				break;
634
			case KXmlParser.TEXT:                   
635
				break;
636
			}
637
			if (!end){
638
				currentTag = next();
639
			}
640
		}		
641
	}
642

  
643 194
	public String getversion() {
644 195
		if (version == null){
645 196
			//return the default GML version

Also available in: Unified diff