Revision 9564

View differences:

branches/v10/applications/appgvSIG/src/com/iver/cit/gvsig/gui/filter/TestFilterExpressionFromWhereIsEmpty_Method.java
1
package com.iver.cit.gvsig.gui.filter;
2

  
3
import junit.framework.TestCase;
4

  
5

  
6
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
7
 *
8
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
9
 *
10
 * This program is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU General Public License
12
 * as published by the Free Software Foundation; either version 2
13
 * of the License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
23
 *
24
 * For more information, contact:
25
 *
26
 *  Generalitat Valenciana
27
 *   Conselleria d'Infraestructures i Transport
28
 *   Av. Blasco Ib??ez, 50
29
 *   46010 VALENCIA
30
 *   SPAIN
31
 *
32
 *      +34 963862235
33
 *   gvsig@gva.es
34
 *      www.gvsig.gva.es
35
 *
36
 *    or
37
 *
38
 *   IVER T.I. S.A
39
 *   Salamanca 50
40
 *   46005 Valencia
41
 *   Spain
42
 *
43
 *   +34 963163400
44
 *   dac@iver.es
45
 */
46

  
47
/**
48
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
49
 */
50
public class TestFilterExpressionFromWhereIsEmpty_Method extends TestCase {
51
	/*
52
	 *  (non-Javadoc)
53
	 * @see junit.framework.TestCase#setUp()
54
	 */
55
	protected void setUp() throws Exception {
56
		super.setUp();
57
	}
58

  
59
	/*
60
	 *  (non-Javadoc)
61
	 * @see junit.framework.TestCase#tearDown()
62
	 */
63
	protected void tearDown() throws Exception {
64
		super.tearDown();
65
	}	
66

  
67
	/**
68
	 * Test 1 (valid)
69
	 */
70
	public void test1() {
71
		String expression = new String("select * from 'gdbms144426c_10fc90fa1aa__7c18' where ;");
72
		
73
		System.out.println("? Es vac?o el filtro en: " + expression + " ? ");
74

  
75
		if (this.filterExpressionFromWhereIsEmpty(expression))
76
			System.out.println("Si.");
77
		else
78
			System.out.println("No.");
79
	}
80
	
81
	/**
82
	 * Test 2 (invalid)
83
	 */
84
	public void test2() {
85
		String expression = new String("select * from 'gdbms158fd70_10fc92ee61e__7c18' where layer < '61';");
86
		
87
		System.out.println("? Es vac?o el filtro en: " + expression + " ? ");
88

  
89
		if (this.filterExpressionFromWhereIsEmpty(expression))
90
			System.out.println("Si.");
91
		else
92
			System.out.println("No.");
93
	}	
94
	
95
	/**
96
	 * Returns true if the WHERE subconsultation of the filterExpression is empty ("")
97
	 * 
98
	 * @param expression An string
99
	 * @return A boolean value 
100
	 */
101
	private boolean filterExpressionFromWhereIsEmpty(String expression) {
102
		String subExpression = expression.trim();
103
		int pos;	
104
		
105
		// Remove last ';' if exists
106
		if (subExpression.charAt(subExpression.length() -1) == ';')
107
			subExpression = subExpression.substring(0, subExpression.length() -1).trim();
108
		
109
		// If there is no 'where' clause
110
		if ((pos = subExpression.indexOf("where")) == -1)
111
			return false;
112
		
113
		// If there is no subexpression in the WHERE clause -> true
114
		subExpression = subExpression.substring(pos + 5, subExpression.length()).trim(); // + 5 is the length of 'where'
115
		if ( subExpression.length() == 0 )
116
			return true;
117
		else
118
			return false;
119
	}
120
}
0 121

  
branches/v10/libraries/libIverUtiles/src-test/com/iver/utiles/stringNumberUtilities/TestStringNumberUtilities.java
1
package com.iver.utiles.stringNumberUtilities;
2

  
3
import junit.framework.TestCase;
4

  
5
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
6
 *
7
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
22
 *
23
 * For more information, contact:
24
 *
25
 *  Generalitat Valenciana
26
 *   Conselleria d'Infraestructures i Transport
27
 *   Av. Blasco Ib??ez, 50
28
 *   46010 VALENCIA
29
 *   SPAIN
30
 *
31
 *      +34 963862235
32
 *   gvsig@gva.es
33
 *      www.gvsig.gva.es
34
 *
35
 *    or
36
 *
37
 *   IVER T.I. S.A
38
 *   Salamanca 50
39
 *   46005 Valencia
40
 *   Spain
41
 *
42
 *   +34 963163400
43
 *   dac@iver.es
44
 */
45

  
46
/**
47
 * Tests the methods of the class StringNumberUtilities
48
 * 
49
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
50
 */
51
public class TestStringNumberUtilities extends TestCase{
52
	private String word;
53
	
54
	/*
55
	 *  (non-Javadoc)
56
	 * @see junit.framework.TestCase#setUp()
57
	 */
58
	protected void setUp() throws Exception {
59
		super.setUp();
60
		word = new String();
61
	}
62

  
63
	/*
64
	 *  (non-Javadoc)
65
	 * @see junit.framework.TestCase#tearDown()
66
	 */
67
	protected void tearDown() throws Exception {
68
		super.tearDown();
69
	}
70
	
71
	/**
72
	 * A test
73
	 */
74
	public void test1() {
75
		word = "2.3";
76
		ShowText.showIsNumberText(word);
77

  
78
		if (StringNumberUtilities.isNumber(word)) {
79
			ShowText.showYes();
80
		}
81
		else {
82
			ShowText.showNo();
83
			fail();
84
		}
85
	}
86

  
87
	/**
88
	 * A test
89
	 */
90
	public void test2() {
91
		word = "a as wz rwa";
92
		ShowText.showIsNumberText(word);
93

  
94
		if (StringNumberUtilities.isNumber(word)) {
95
			ShowText.showYes();
96
		}
97
		else {
98
			ShowText.showNo();
99
			fail();
100
		}
101
	}
102

  
103
	/**
104
	 * A test
105
	 */
106
	public void test3() {
107
		word = "2";
108
		ShowText.showIsNumberText(word);
109

  
110
		if (StringNumberUtilities.isNumber(word)) {
111
			ShowText.showYes();
112
		}
113
		else {
114
			ShowText.showNo();
115
			fail();
116
		}
117
	}
118

  
119
	/**
120
	 * A test
121
	 */
122
	public void test4() {
123
		word = "";
124
		ShowText.showIsNumberText(word);
125

  
126
		if (StringNumberUtilities.isNumber(word)) {
127
			ShowText.showYes();
128
		}
129
		else {
130
			ShowText.showNo();
131
			fail();
132
		}
133
	}
134

  
135
	/**
136
	 * A test
137
	 */
138
	public void test5() {
139
		word = "-2.3";
140
		ShowText.showIsNumberText(word);
141

  
142
		if (StringNumberUtilities.isNumber(word)) {
143
			ShowText.showYes();
144
		}
145
		else {
146
			ShowText.showNo();
147
			fail();
148
		}
149
	}
150

  
151
	/**
152
	 * A test
153
	 */
154
	public void test6() {
155
		word = "-300";
156
		ShowText.showIsNumberText(word);
157

  
158
		if (StringNumberUtilities.isNumber(word)) {
159
			ShowText.showYes();
160
		}
161
		else {
162
			ShowText.showNo();
163
			fail();
164
		}
165
	}
166

  
167
	/**
168
	 * A test
169
	 */
170
	public void test7() {
171
		word = "2.3";
172
		ShowText.showIsNaturalText(word);
173

  
174
		if (StringNumberUtilities.isNaturalNumber(word)) {
175
			ShowText.showYes();
176
		}
177
		else {
178
			ShowText.showNo();
179
			fail();
180
		}
181
	}
182

  
183
	/**
184
	 * A test
185
	 */
186
	public void test8() {
187
		word = "334";
188
		ShowText.showIsNaturalText(word);
189

  
190
		if (StringNumberUtilities.isNaturalNumber(word)) {
191
			ShowText.showYes();
192
		}
193
		else {
194
			ShowText.showNo();
195
			fail();
196
		}
197
	}
198

  
199
	/**
200
	 * A test
201
	 */
202
	public void test9() {
203
		word = "-2a3";
204
		ShowText.showIsIntegerText(word);
205

  
206
		if (StringNumberUtilities.isIntegerNumber(word)) {
207
			ShowText.showYes();
208
		}
209
		else {
210
			ShowText.showNo();
211
			fail();
212
		}
213
	}
214

  
215
	/**
216
	 * A test
217
	 */
218
	public void test10() {
219
		word = "-23";
220
		ShowText.showIsIntegerText(word);
221

  
222
		if (StringNumberUtilities.isIntegerNumber(word)) {
223
			ShowText.showYes();
224
		}
225
		else {
226
			ShowText.showNo();
227
			fail();
228
		}
229
	}
230
	
231
	/**
232
	 * A test
233
	 */
234
	public void test11() {
235
		word = "7";
236
		ShowText.showIsIntegerText(word);
237

  
238
		if (StringNumberUtilities.isIntegerNumber(word)) {
239
			ShowText.showYes();
240
		}
241
		else {
242
			ShowText.showNo();
243
			fail();
244
		}
245
	}
246
	
247
	/**
248
	 * A test
249
	 */
250
	public void test12() {
251
		word = "2.3";
252
		ShowText.showIsIntegerText(word);
253

  
254
		if (StringNumberUtilities.isIntegerNumber(word)) {
255
			ShowText.showYes();
256
		}
257
		else {
258
			ShowText.showNo();
259
			fail();
260
		}
261
	}
262

  
263
	/**
264
	 * A test
265
	 */
266
	public void test13() {
267
		word = "2.3";
268
		ShowText.showIsRealText(word);
269

  
270
		if (StringNumberUtilities.isRealNumber(word)) {
271
			ShowText.showYes();
272
		}
273
		else {
274
			ShowText.showNo();
275
			fail();
276
		}
277
	}
278
	
279
	/**
280
	 * A test
281
	 */
282
	public void test14() {
283
		word = "37";
284
		ShowText.showIsRealText(word);
285

  
286
		if (StringNumberUtilities.isRealNumber(word)) {
287
			ShowText.showYes();
288
		}
289
		else {
290
			ShowText.showNo();
291
			fail();
292
		}
293
	}
294
	
295
	/**
296
	 * A test
297
	 */
298
	public void test15() {
299
		word = "-4.6";
300
		ShowText.showIsRealText(word);
301

  
302
		if (StringNumberUtilities.isRealNumber(word)) {
303
			ShowText.showYes();
304
		}
305
		else {
306
			ShowText.showNo();
307
			fail();
308
		}
309
	}
310
	
311
	/**
312
	 * A test
313
	 */
314
	public void test16() {
315
		word = "-8000";
316
		ShowText.showIsRealText(word);
317

  
318
		if (StringNumberUtilities.isRealNumber(word)) {
319
			ShowText.showYes();
320
		}
321
		else {
322
			ShowText.showNo();
323
			fail();
324
		}
325
	}
326

  
327
	/**
328
	 * A test
329
	 */
330
	public void test17() {
331
		word = "-80k00";
332
		ShowText.showIsRealText(word);
333

  
334
		if (StringNumberUtilities.isRealNumber(word)) {
335
			ShowText.showYes();
336
		}
337
		else {
338
			ShowText.showNo();
339
			fail();
340
		}
341
	}
342
		
343
	/**
344
	 * A test
345
	 */
346
	public void test18() {
347
		word = ".6";
348
		ShowText.showIsRealText(word);
349

  
350
		if (StringNumberUtilities.isRealNumber(word)) {
351
			ShowText.showYes();
352
		}
353
		else {
354
			ShowText.showNo();
355
			fail();
356
		}
357
	}
358
	
359
	/**
360
	 * A test
361
	 */
362
	public void test19() {
363
		word = "-.6";
364
		ShowText.showIsRealText(word);
365

  
366
		if (StringNumberUtilities.isRealNumber(word)) {
367
			ShowText.showYes();
368
		}
369
		else {
370
			ShowText.showNo();
371
			fail();
372
		}
373
	}
374
	
375
	/**
376
	 * A test
377
	 */
378
	public void test20() {
379
		word = "b-.6";
380
		ShowText.showIsRealText(word);
381

  
382
		if (StringNumberUtilities.isRealNumber(word)) {
383
			ShowText.showYes();
384
		}
385
		else {
386
			ShowText.showNo();
387
			fail();
388
		}
389
	}
390
	
391
	
392
	/**
393
	 * A test
394
	 */
395
	public void test21() {
396
		word = "-c.6";
397
		ShowText.showIsRealText(word);
398

  
399
		if (StringNumberUtilities.isRealNumber(word)) {
400
			ShowText.showYes();
401
		}
402
		else {
403
			ShowText.showNo();
404
			fail();
405
		}
406
	}
407
	
408
	
409
	/**
410
	 * A test
411
	 */
412
	public void test22() {
413
		word = "-.d6";
414
		ShowText.showIsRealText(word);
415

  
416
		if (StringNumberUtilities.isRealNumber(word)) {
417
			ShowText.showYes();
418
		}
419
		else {
420
			ShowText.showNo();
421
			fail();
422
		}
423
	}
424
	
425
	/**
426
	 * A test
427
	 */
428
	public void test23() {
429
		word = "-.6e";
430
		ShowText.showIsRealText(word);
431

  
432
		if (StringNumberUtilities.isRealNumber(word)) {
433
			ShowText.showYes();
434
		}
435
		else {
436
			ShowText.showNo();
437
			fail();
438
		}
439
	}
440
	
441
	/**
442
	 * A test
443
	 */
444
	public void test24() {
445
		word = "+.6";
446
		ShowText.showIsRealText(word);
447

  
448
		if (StringNumberUtilities.isRealNumber(word)) {
449
			ShowText.showYes();
450
		}
451
		else {
452
			ShowText.showNo();
453
			fail();
454
		}
455
	}
456
	
457
	/**
458
	 * Shows a text sentence
459
	 * 
460
	 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
461
	 */
462
	private static class ShowText {
463
		/**
464
		 * Shows the text: Is number?
465
		 * 
466
		 * @param word An String
467
		 */
468
		public static void showIsNumberText(String word) {
469
			System.out.print("? Es n?mero \'" + word + "\' ? ");
470
		}
471

  
472
		/**
473
		 * Shows the text: Is natural number?
474
		 * 
475
		 * @param word An String
476
		 */
477
		public static void showIsNaturalText(String word) {
478
			System.out.print("? Es n?mero natural \'" + word + "\' ? ");
479
		}
480

  
481
		/**
482
		 * Shows the text: Is integer number?
483
		 * 
484
		 * @param word An String
485
		 */
486
		public static void showIsIntegerText(String word) {
487
			System.out.print("? Es n?mero entero \'" + word + "\' ? ");
488
		}
489

  
490
		/**
491
		 * Shows the text: Is rational number?
492
		 * 
493
		 * @param word An String
494
		 */
495
		public static void showIsRealText(String word) {
496
			System.out.print("? Es n?mero real \'" + word + "\' ? ");
497
		}
498
		
499
		/**
500
		 * Shows the text: Yes
501
		 * 
502
		 * @param word An String
503
		 */
504
		public static void showYes() {
505
			System.out.println("Si.");
506
		}
507
		
508
		/**
509
		 * Shows the text: No
510
		 * 
511
		 * @param word An String
512
		 */
513
		public static void showNo() {
514
			System.out.println("No.");
515
		}
516
	}
517
}
0 518

  
branches/v10/libraries/libRemoteServices/src/org/gvsig/remoteClient/gml/types/XMLComplexType.java
1
package org.gvsig.remoteClient.gml.types;
2

  
3
import java.util.Iterator;
4
import java.util.LinkedHashMap;
5
import java.util.Map;
6
import java.util.Set;
7
import java.util.Vector;
8

  
9
import org.gvsig.remoteClient.gml.schemas.XMLElement;
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.3  2006-12-29 18:05:20  jorpiell
56
 * Se tienen en cuenta los simpleTypes y los choices, adem?s de los atributos multiples
57
 *
58
 * Revision 1.1  2006/12/22 11:25:04  csanchez
59
 * Nuevo parser GML 2.x para gml's sin esquema
60
 *
61
 * Revision 1.7  2006/10/31 13:52:37  ppiqueras
62
 * Mejoras para uso de features complejas
63
 *
64
 * Revision 1.6  2006/10/31 12:24:33  jorpiell
65
 * En caso de que el elemento forme parte de un tipo complejo tiene que tener un enlace al tipo del objeto padre
66
 *
67
 * Revision 1.5  2006/10/10 12:52:28  jorpiell
68
 * Soporte para features complejas.
69
 *
70
 * Revision 1.4  2006/10/02 08:33:49  jorpiell
71
 * Cambios del 10 copiados al head
72
 *
73
 * Revision 1.2.2.1  2006/09/19 12:23:15  jorpiell
74
 * Ya no se depende de geotools
75
 *
76
 * Revision 1.3  2006/09/18 12:08:55  jorpiell
77
 * Se han hecho algunas modificaciones que necesitaba el WFS
78
 *
79
 * Revision 1.2  2006/08/29 08:43:13  jorpiell
80
 * Dos peque?os cambios para tener en cuenta las referencias
81
 *
82
 * Revision 1.1  2006/08/10 12:00:49  jorpiell
83
 * Primer commit del driver de Gml
84
 *
85
 *
86
 */
87
/**
88
 * This class implements an XSD complex type
89
 * 
90
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
91
 * @author Carlos S?nchez Peri??n (sanchez_carper@gva.es)
92
 * 
93
 */
94
public class XMLComplexType implements IXMLType {
95
	public static int SEQUENCE_TYPE = 0;
96
	public static int CHOICE_TYPE = 1;
97
	
98
	private String type = null;
99
	private LinkedHashMap attributes = null;
100
	private int attributesType = 0; 
101
			
102
	public XMLComplexType(String type) {
103
		super();
104
		this.type = type;
105
		attributes = new LinkedHashMap();
106
	}
107
	
108
	/*
109
	 *  (non-Javadoc)
110
	 * @see org.gvsig.remoteClient.gml.schemas.IXMLType#getType()
111
	 */
112
	public int getType() {
113
		return IXMLType.COMPLEX;
114
	}
115
	
116
	/*
117
	 *  (non-Javadoc)
118
	 * @see org.gvsig.remoteClient.gml.schemas.IXMLType#getName()
119
	 */
120
	public String getName() {
121
		return type;
122
	}
123
	
124
	/**
125
	 * @return Returns the subtypes.
126
	 */
127
	public Map getSubtypes() {
128
		return attributes;
129
	}
130
	
131
	/**
132
	 * @param subtypes The subtypes to set.
133
	 */
134
	public void addSubtypes(XMLElement element) {
135
		if (element.getName() != null){
136
			this.attributes.put(element.getName(),element);
137
		}
138
	}
139
	
140
	/**
141
	 * 
142
	 * @param name
143
	 * @return
144
	 */
145
	public XMLElement getAttribute(String name){
146
		return (XMLElement)attributes.get(name);
147
	}
148

  
149
	/**
150
	 * @return Returns the vElements.
151
	 */
152
	public Vector getAttributes() {
153
		Set keys = attributes.keySet();
154
		Iterator it = keys.iterator();
155
		Vector vector = new Vector();
156
		while(it.hasNext()){
157
			vector.add(attributes.get((String)it.next()));
158
		}
159
		return vector;
160
	}
161

  
162
	public int getAttributesType() {
163
		return attributesType;
164
	}
165

  
166
	public void setAttributesType(int attributesType) {
167
		this.attributesType = attributesType;
168
	}
169
	
170
	
171
	
172
}
0 173

  
branches/v10/extensions/extWFS2/src-test/com/iver/cit/gvsig/gui/panels/sqlQueryValidation/TestSQLQueryValidation.java
1
package com.iver.cit.gvsig.gui.panels.sqlQueryValidation;
2

  
3
import junit.framework.TestCase;
4

  
5
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
6
 *
7
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
22
 *
23
 * For more information, contact:
24
 *
25
 *  Generalitat Valenciana
26
 *   Conselleria d'Infraestructures i Transport
27
 *   Av. Blasco Ib??ez, 50
28
 *   46010 VALENCIA
29
 *   SPAIN
30
 *
31
 *      +34 963862235
32
 *   gvsig@gva.es
33
 *      www.gvsig.gva.es
34
 *
35
 *    or
36
 *
37
 *   IVER T.I. S.A
38
 *   Salamanca 50
39
 *   46005 Valencia
40
 *   Spain
41
 *
42
 *   +34 963163400
43
 *   dac@iver.es
44
 */
45

  
46
/**
47
 * Tests the methods of the class SQLQueryValidation
48
 *    (This class is made without inner static methods for don't create problems to Zql, in this way,
49
 *     it's possible to add more tests, with or without long queries.)
50
 * 
51
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
52
 */
53
 public class TestSQLQueryValidation extends TestCase{
54
	
55
	/*
56
	 *  (non-Javadoc)
57
	 * @see junit.framework.TestCase#setUp()
58
	 */
59
	protected void setUp() throws Exception {
60
		super.setUp();
61
	}
62

  
63
	/*
64
	 *  (non-Javadoc)
65
	 * @see junit.framework.TestCase#tearDown()
66
	 */
67
	protected void tearDown() throws Exception {
68
		super.tearDown();
69
	}
70
	
71
	/**
72
	 * A test (Correct)
73
	 */
74
	public void test1() {
75
		String query = "SELECT r.name, f.id FROM room r, flat f WHERE (r.user_name LIKE 'P%') AND (r.flat = f.id) AND (r.color_wall LIKE 'white') AND (r.height < 2.20);";
76
		
77
		System.out.println("?Es v?lida '" + query + "' ?");
78
		SQLQueryValidation sqlQueryValidation = new SQLQueryValidation(query, false);
79

  
80
		if (sqlQueryValidation.validateQuery()) {
81
			System.out.println("Yes.");
82
		}
83
		else {
84
			System.out.println("No.");
85
			System.out.println(sqlQueryValidation.getErrorPositionAsMessage());
86
			System.out.println(sqlQueryValidation.getErrorMessage());
87
			fail();
88
		}
89
	}
90
	
91
	/**
92
	 * A test (Correct)
93
	 */
94
	public void test2() {
95
		String query = "SELECT * FROM House;";
96

  
97
		System.out.println("?Es v?lida '" + query + "' ?");
98
		SQLQueryValidation sqlQueryValidation = new SQLQueryValidation(query, false);
99

  
100
		if (sqlQueryValidation.validateQuery()) {
101
			System.out.println("Yes.");
102
		}
103
		else {
104
			System.out.println("No.");
105
			System.out.println(sqlQueryValidation.getErrorPositionAsMessage());
106
			System.out.println(sqlQueryValidation.getErrorMessage());
107
			fail();
108
		}
109
	}
110

  
111
	/**
112
	 * A test (Incorrect)
113
	 */
114
	public void test3() {
115
		String query = "SELECT a* FROM House;";
116

  
117
		System.out.println("?Es v?lida '" + query + "' ?");
118
		SQLQueryValidation sqlQueryValidation = new SQLQueryValidation(query, false);
119

  
120
		if (sqlQueryValidation.validateQuery()) {
121
			System.out.println("Yes.");
122
		}
123
		else {
124
			System.out.println("No.");
125
			System.out.println(sqlQueryValidation.getErrorPositionAsMessage());
126
			System.out.println(sqlQueryValidation.getErrorMessage());
127
			fail();
128
		}
129
	}
130
	
131
	/**
132
	 * A test (Correct)
133
	 */
134
	public void test4() {
135
		String query = "SELECT * FROM House;";
136

  
137
		System.out.println("?Es v?lida '" + query + "' ?");
138
		SQLQueryValidation sqlQueryValidation = new SQLQueryValidation(query, false);
139

  
140
		if (sqlQueryValidation.validateQuery()) {
141
			System.out.println("Yes.");
142
		}
143
		else {
144
			System.out.println("No.");
145
			System.out.println(sqlQueryValidation.getErrorPositionAsMessage());
146
			System.out.println(sqlQueryValidation.getErrorMessage());
147
			fail();
148
		}
149
	}
150

  
151
	/**
152
	 * A test (Correct)
153
	 */
154
	public void test5() {
155
		String query = "r.level = f.level AND r.user_name LIKE \'P%\';";
156

  
157
		System.out.println("?Es v?lida '" + query + "' ?");
158
		SQLQueryValidation sqlQueryValidation = new SQLQueryValidation(query, true);
159

  
160
		if (sqlQueryValidation.validateQuery()) {
161
			System.out.println("Yes.");
162
		}
163
		else {
164
			System.out.println("No.");
165
			System.out.println(sqlQueryValidation.getErrorPositionAsMessage());
166
			System.out.println(sqlQueryValidation.getErrorMessage());
167
			fail();
168
		}
169
	}
170

  
171
	/**
172
	 * A test (Incorrect)
173
	 */
174
	public void test6() {
175
		String query = "r.level = f.level a e3 w 	q3 	 ?32	9'}97AND r.user_name LIKE \'P%\';";
176

  
177
		System.out.println("?Es v?lida '" + query + "' ?");
178
		SQLQueryValidation sqlQueryValidation = new SQLQueryValidation(query, true);
179

  
180
		if (sqlQueryValidation.validateQuery()) {
181
			System.out.println("Yes.");
182
		}
183
		else {
184
			System.out.println("No.");
185
			System.out.println(sqlQueryValidation.getErrorPositionAsMessage());
186
			System.out.println(sqlQueryValidation.getErrorMessage());			
187
			fail();
188
		}
189
	}
190
 }
0 191

  
branches/v10/extensions/extWFS2/src/com/iver/cit/gvsig/gui/panels/sqlQueryValidation/SQLQueryValidation.java
1
package com.iver.cit.gvsig.gui.panels.sqlQueryValidation;
2

  
3
import java.io.ByteArrayInputStream;
4

  
5
import Zql.ZqlParser;
6

  
7
import com.iver.andami.PluginServices;
8

  
9
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
10
 *
11
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
12
 *
13
 * This program is free software; you can redistribute it and/or
14
 * modify it under the terms of the GNU General Public License
15
 * as published by the Free Software Foundation; either version 2
16
 * of the License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
26
 *
27
 * For more information, contact:
28
 *
29
 *  Generalitat Valenciana
30
 *   Conselleria d'Infraestructures i Transport
31
 *   Av. Blasco Ib??ez, 50
32
 *   46010 VALENCIA
33
 *   SPAIN
34
 *
35
 *      +34 963862235
36
 *   gvsig@gva.es
37
 *      www.gvsig.gva.es
38
 *
39
 *    or
40
 *
41
 *   IVER T.I. S.A
42
 *   Salamanca 50
43
 *   46005 Valencia
44
 *   Spain
45
 *
46
 *   +34 963163400
47
 *   dac@iver.es
48
 */
49

  
50
/**
51
 * Class for validate a complete query or the sentence of a query from WHERE 
52
 *
53
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
54
 */
55
public class SQLQueryValidation {
56
	private String query;
57
	private boolean onlyWhereStatement;
58
	private int[] errorPosition; //[0] -> line, [1] -> column        ( (-1, -1) -> no error ) ( (-2, -2) -> error: query = null)
59
	private String errorMessage;
60
	private String errorPositionAsMessage;
61
	private final String preQuery = "SELECT a FROM b WHERE ";
62
	private final int preQueryLenght = preQuery.length();
63
	
64
	/**
65
	 * Default constructor with a parameter
66
	 * 
67
	 * @param _query The query to validate
68
	 * @param _onlyWhereStatement If the query is only the part of WHERE ... in a query
69
	 */
70
	public SQLQueryValidation(String _query, boolean _onlyWhereStatement) {
71
		query = new String(_query);
72
		onlyWhereStatement = _onlyWhereStatement;
73
		
74
		// By default no error position
75
		errorPosition = new int[2];
76
		errorPosition[0] = -1;
77
		errorPosition[1] = -1;
78
		
79
		errorPositionAsMessage = null;
80
		
81
		// By default no error message
82
		errorMessage = null;
83
	}
84
		
85
	/**
86
	 * Validates a query
87
	 * 
88
	 * Returns 'null' if there has been some error
89
	 */
90
	public boolean validateQuery() {
91
		String completeQuery = new String();
92
		
93
		// If the query is null -> error 
94
		if (query == null)	{
95
			errorPosition[0] = -2;
96
			errorPosition[1] = -2;
97
			defineErrorPositionAsMessageAttribute(errorPosition);
98
			errorMessage = new String( PluginServices.getText(null, "queryIsNull"));
99
			
100
			return false;
101
		}
102
		
103
		// If no query itsn't considered as a error
104
		if ((query.compareTo("")) == 0) {
105
			errorPosition[0] = -1;
106
			errorPosition[1] = -1;
107
			defineErrorPositionAsMessageAttribute(errorPosition);
108
			errorMessage = null;
109
			
110
			return true;
111
		}
112
		
113
		if (onlyWhereStatement)
114
			completeQuery = preQuery + query.trim().replaceAll("\"", "");
115
		else
116
			completeQuery = query.trim().replaceAll("\"", "");
117
		
118
		if ((completeQuery.length() > 0) && (completeQuery.charAt(completeQuery.length() - 1) != ';'))
119
			completeQuery += ";";
120

  
121
		try {
122
			// Starts the parser
123
			ZqlParser p = new ZqlParser();
124
			p.initParser(new ByteArrayInputStream(completeQuery.getBytes()));
125
	      
126
			// Analyzes the query
127
			p.readStatement();
128
			
129
			// If arrives here -> there has been no errors
130
			errorPosition[0] = -1;
131
			errorPosition[1] = -1;
132
			defineErrorPositionAsMessageAttribute(errorPosition);
133
			errorMessage = null;
134
			return true;
135
		} catch (Exception e) {
136
			// Get the line an column where the syntax error starts
137
			String line = new String(e.getMessage().substring(e.getMessage().indexOf("line"), e.getMessage().indexOf(',')));
138
			line = line.substring(line.indexOf(' ')+1, line.length());
139
			
140
			String column = new String(e.getMessage().substring(e.getMessage().indexOf("column"), e.getMessage().indexOf('.')));
141
			column = column.substring(column.indexOf(' ')+1, column.length());			
142

  
143
			// Get the line an column of the error
144
			errorPosition[0] = Integer.valueOf(line.trim()).intValue();			
145
			errorPosition[1] = Integer.valueOf(column.trim()).intValue();
146
			
147
			if (onlyWhereStatement) {
148
				this.errorPosition[1] -= this.preQueryLenght; // Substract the lenght of the pre-query added
149
			}
150
			
151
			defineErrorPositionAsMessageAttribute(this.errorPosition);
152
			
153
			// Defines the error message
154
			errorMessage = e.getMessage();
155

  
156
			return false;
157
		}
158
	}
159
	
160
	/**
161
	 * Returns an string with an error message if there has been an error, or 'null' if there hasn't been
162
	 * 
163
	 * @return An string or null
164
	 */
165
	public String getErrorMessage() {
166
		return this.errorMessage;
167
	}
168
	
169
	/**
170
	 * Returns an string with a text describing the (first) position of the error
171
	 * 
172
	 * @return An string or null
173
	 */
174
	public String getErrorPositionAsMessage() {
175
		return this.errorPositionAsMessage;
176
	}
177
	
178
	/**
179
	 * Returns the error position (line, column) or (-1, -1) if there hasn't been any error
180
	 * 
181
	 * @return An array of 2 integer values (first: line, second: column) 
182
	 */
183
	public int[] getErrorPosition() {
184
		return errorPosition;
185
	}
186
	
187
	/**
188
	 * Creates a message with information about the message
189
	 * 
190
	 * @param position An array with 2 elements: (row, column)
191
	 */
192
	private void defineErrorPositionAsMessageAttribute(int position[]) {
193
		
194
		// Defines the error message
195
		errorPositionAsMessage = new String( PluginServices.getText(null, "line") + ": " + errorPosition[0] + ", " + PluginServices.getText(null, "column") + ": " + errorPosition[1] );
196
	}
197
}
0 198

  

Also available in: Unified diff