Revision 44421
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.api/src/main/java/org/gvsig/expressionevaluator/spi/AbstractLexicalAnalyzer.java | ||
---|---|---|
313 | 313 |
token.set(Token.STRING_LITERAL, buffer.toString()); |
314 | 314 |
} |
315 | 315 |
|
316 |
protected void parseSpecialNumber() { |
|
317 |
char ch = getch(); |
|
318 |
if( ch!='@' ) { |
|
319 |
throw new ExpressionSyntaxException(I18N.Wrong_special_number_start(), this); |
|
320 |
} |
|
321 |
buffer.clear(); |
|
322 |
ch = getch(); |
|
323 |
while (true) { |
|
324 |
if (ch == EOF) { |
|
325 |
throw new ExpressionSyntaxException(I18N.End_of_string_was_expected_and_end_of_source_was_found(), this); |
|
326 |
} |
|
327 |
if( !Character.isAlphabetic(ch) ) { |
|
328 |
break; |
|
329 |
} |
|
330 |
buffer.add(ch); |
|
331 |
ch = getch(); |
|
332 |
} |
|
333 |
String formatName = buffer.toString(); |
|
334 |
if( StringUtils.isBlank(formatName) ) { |
|
335 |
formatName = "DMS"; |
|
336 |
} |
|
337 |
formatName = formatName.toUpperCase(); |
|
338 |
/* |
|
339 |
From https://es.wikipedia.org/wiki/Sistema_de_coordenadas#Coordenadas_geogr%C3%A1ficas |
|
340 |
|
|
341 |
DD --- Decimal Degree (Grados Polares) |
|
342 |
DM --- Degree:Minute (Grados:Minutos) |
|
343 |
DMS -- Degree:Minute:Second |
|
344 |
*/ |
|
345 |
switch(formatName) { |
|
346 |
case "DMS": |
|
347 |
ungetch(); |
|
348 |
parseDMSNumber(); |
|
349 |
break; |
|
350 |
default: |
|
351 |
throw new ExpressionSyntaxException(I18N.Special_number_format_not_supported(formatName), this); |
|
352 |
} |
|
353 |
} |
|
354 |
|
|
355 |
private void parseDMSNumber() { |
|
356 |
int d; |
|
357 |
int m; |
|
358 |
int s_i; |
|
359 |
int s_d; |
|
360 |
double s; |
|
361 |
String ll; |
|
362 |
char ch; |
|
363 |
|
|
364 |
// Parseamos los grados |
|
365 |
skipblanks(); |
|
366 |
ch = getch(); |
|
367 |
buffer.clear(); |
|
368 |
if( !Character.isDigit(ch) ) { |
|
369 |
throw new ExpressionSyntaxException(I18N.Expected_a_number_at_position_XpositionX(this.getPosition()), this); |
|
370 |
} |
|
371 |
while (true) { |
|
372 |
if (ch == EOF) { |
|
373 |
throw new ExpressionSyntaxException(I18N.End_of_string_was_expected_and_end_of_source_was_found(), this); |
|
374 |
} |
|
375 |
if( !Character.isDigit(ch) ) { |
|
376 |
break; |
|
377 |
} |
|
378 |
buffer.add(ch); |
|
379 |
ch = getch(); |
|
380 |
} |
|
381 |
if( !StringUtils.contains(" ?:", ch) ) { |
|
382 |
throw new ExpressionSyntaxException(I18N.Expected_XexpectedX_and_found_XfoundX(" ", String.valueOf(ch)), this); |
|
383 |
} |
|
384 |
d = Integer.parseInt(buffer.toString()); |
|
385 |
|
|
386 |
// Parseamos los minutos |
|
387 |
skipblanks(); |
|
388 |
ch = getch(); |
|
389 |
buffer.clear(); |
|
390 |
if( !Character.isDigit(ch) ) { |
|
391 |
throw new ExpressionSyntaxException(I18N.Expected_a_number_at_position_XpositionX(this.getPosition()), this); |
|
392 |
} |
|
393 |
while (true) { |
|
394 |
if (ch == EOF) { |
|
395 |
throw new ExpressionSyntaxException(I18N.End_of_string_was_expected_and_end_of_source_was_found(), this); |
|
396 |
} |
|
397 |
if( !Character.isDigit(ch) ) { |
|
398 |
break; |
|
399 |
} |
|
400 |
buffer.add(ch); |
|
401 |
ch = getch(); |
|
402 |
} |
|
403 |
if( !StringUtils.contains(" ':", ch) ) { |
|
404 |
throw new ExpressionSyntaxException(I18N.Expected_XexpectedX_and_found_XfoundX(" ", String.valueOf(ch)), this); |
|
405 |
} |
|
406 |
m = Integer.parseInt(buffer.toString()); |
|
407 |
|
|
408 |
// Parseamos la parte entera de los segundos |
|
409 |
skipblanks(); |
|
410 |
ch = getch(); |
|
411 |
buffer.clear(); |
|
412 |
if( !Character.isDigit(ch) ) { |
|
413 |
throw new ExpressionSyntaxException(I18N.Expected_a_number_at_position_XpositionX(this.getPosition()), this); |
|
414 |
} |
|
415 |
while (true) { |
|
416 |
if (ch == EOF) { |
|
417 |
throw new ExpressionSyntaxException(I18N.End_of_string_was_expected_and_end_of_source_was_found(), this); |
|
418 |
} |
|
419 |
if( !Character.isDigit(ch) ) { |
|
420 |
break; |
|
421 |
} |
|
422 |
buffer.add(ch); |
|
423 |
ch = getch(); |
|
424 |
} |
|
425 |
s_i = Integer.parseInt(buffer.toString()); |
|
426 |
|
|
427 |
if( ch == '.' ) { |
|
428 |
// Parseamos la parte decimal de los segundos |
|
429 |
skipblanks(); |
|
430 |
ch = getch(); |
|
431 |
buffer.clear(); |
|
432 |
if( !Character.isDigit(ch) ) { |
|
433 |
throw new ExpressionSyntaxException(I18N.Expected_a_number_at_position_XpositionX(this.getPosition()), this); |
|
434 |
} |
|
435 |
while (true) { |
|
436 |
if (ch == EOF) { |
|
437 |
throw new ExpressionSyntaxException(I18N.End_of_string_was_expected_and_end_of_source_was_found(), this); |
|
438 |
} |
|
439 |
if( !Character.isDigit(ch) ) { |
|
440 |
break; |
|
441 |
} |
|
442 |
buffer.add(ch); |
|
443 |
ch = getch(); |
|
444 |
} |
|
445 |
s_d = Integer.parseInt(buffer.toString()); |
|
446 |
} else { |
|
447 |
s_d = 0; |
|
448 |
} |
|
449 |
if( !StringUtils.contains(" \"", ch) ) { |
|
450 |
throw new ExpressionSyntaxException(I18N.Expected_XexpectedX_and_found_XfoundX(" ", String.valueOf(ch)), this); |
|
451 |
} |
|
452 |
|
|
453 |
s = Double.parseDouble(s_i+"."+s_d); |
|
454 |
|
|
455 |
double dd = d + m / 60.0 + s / 3600.0; |
|
456 |
|
|
457 |
skipblanks(); |
|
458 |
ch = getch(); |
|
459 |
switch(ch) { |
|
460 |
case 'N': |
|
461 |
case 'n': |
|
462 |
if( dd>90 ) { |
|
463 |
throw new ExpressionSyntaxException(I18N.Incorrect_value_for_latitude(dd), this); |
|
464 |
} |
|
465 |
dd = -dd; |
|
466 |
break; |
|
467 |
|
|
468 |
case 'S': |
|
469 |
case 's': |
|
470 |
if( dd>90 ) { |
|
471 |
throw new ExpressionSyntaxException(I18N.Incorrect_value_for_latitude(dd), this); |
|
472 |
} |
|
473 |
break; |
|
474 |
case 'E': |
|
475 |
case 'e': |
|
476 |
if( dd>180 ) { |
|
477 |
throw new ExpressionSyntaxException(I18N.Incorrect_value_for_latitude(dd), this); |
|
478 |
} |
|
479 |
break; |
|
480 |
|
|
481 |
case 'O': |
|
482 |
case 'o': |
|
483 |
case 'W': |
|
484 |
case 'w': |
|
485 |
if( dd>180 ) { |
|
486 |
throw new ExpressionSyntaxException(I18N.Incorrect_value_for_latitude(dd), this); |
|
487 |
} |
|
488 |
dd = -dd; |
|
489 |
break; |
|
490 |
|
|
491 |
default: |
|
492 |
throw new ExpressionSyntaxException(I18N.Expected_XexpectedX_and_found_XfoundX("N/S/E/W", String.valueOf(ch)), this); |
|
493 |
} |
|
494 |
ll = String.valueOf(Character.toUpperCase(ch)); |
|
495 |
|
|
496 |
token.set( |
|
497 |
Token.FLOATING_POINT_LITERAL, |
|
498 |
String.format("@%d? %d' %f\" %s", d,m,s,ll) , |
|
499 |
dd |
|
500 |
); |
|
501 |
|
|
502 |
} |
|
503 |
|
|
316 | 504 |
protected void parseNumber() { |
317 | 505 |
this.nfPos.setIndex(this.position); |
318 | 506 |
Number n = nf.parse(source, this.nfPos); |
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.api/src/main/java/org/gvsig/expressionevaluator/I18N.java | ||
---|---|---|
37 | 37 |
public static String Expected_a_number_at_position_XpositionX(int position) { |
38 | 38 |
return String.format("Expected a number at position %d.", position); |
39 | 39 |
} |
40 |
|
|
41 |
public static String Wrong_special_number_start() { |
|
42 |
return "wrong special number start"; |
|
43 |
} |
|
44 |
|
|
45 |
public static String Special_number_format_not_supported(String formatName) { |
|
46 |
return String.format("special number format '%s' not supported", formatName); |
|
47 |
} |
|
48 |
|
|
49 |
public static String Incorrect_value_for_latitude(double dd) { |
|
50 |
return String.format("Incorrect value for latitude (%f)", dd); |
|
51 |
} |
|
52 |
|
|
53 |
public static String Incorrect_value_for_longitude(double dd) { |
|
54 |
return String.format("Incorrect value for longitude (%f)", dd); |
|
55 |
} |
|
40 | 56 |
|
41 | 57 |
|
42 | 58 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/java/org/gvsig/expressionevaluator/impl/SQLLexicalAnalyzer.java | ||
---|---|---|
211 | 211 |
case '\'': |
212 | 212 |
parseString(); |
213 | 213 |
return token; |
214 |
|
|
215 |
case '@': |
|
216 |
ungetch(); |
|
217 |
parseSpecialNumber(); |
|
218 |
return token; |
|
214 | 219 |
} |
220 |
|
|
215 | 221 |
if( Character.isDigit(ch) ) { |
216 | 222 |
ungetch(); |
217 | 223 |
parseNumber(); |
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/test/java/org/gvsig/expresionevaluator/impl/TestEvaluatePage.java | ||
---|---|---|
1 |
package org.gvsig.expresionevaluator.impl; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import junit.framework.TestCase; |
|
5 |
import org.gvsig.expressionevaluator.SymbolTable; |
|
6 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
|
7 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
|
8 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
|
9 |
import org.gvsig.expressionevaluator.MutableSymbolTable; |
|
10 |
import org.gvsig.tools.ToolsLocator; |
|
11 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
|
12 |
|
|
13 |
public class TestEvaluatePage extends TestCase { |
|
14 |
|
|
15 |
public TestEvaluatePage(String testName) { |
|
16 |
super(testName); |
|
17 |
} |
|
18 |
|
|
19 |
@Override |
|
20 |
protected void setUp() throws Exception { |
|
21 |
super.setUp(); |
|
22 |
new DefaultLibrariesInitializer().fullInitialize(); |
|
23 |
} |
|
24 |
|
|
25 |
@Override |
|
26 |
protected void tearDown() throws Exception { |
|
27 |
super.tearDown(); |
|
28 |
} |
|
29 |
|
|
30 |
// TODO add test methods here. The name must begin with 'test'. For example: |
|
31 |
// public void testHello() {} |
|
32 |
|
|
33 |
protected SymbolTable createSymbolTable() { |
|
34 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
|
35 |
MutableSymbolTable symbolTable = manager.createSymbolTable(); |
|
36 |
symbolTable.setVar("precio", 200); |
|
37 |
symbolTable.setVar("PI", Math.PI); |
|
38 |
return symbolTable; |
|
39 |
} |
|
40 |
|
|
41 |
public void testSimple1() { |
|
42 |
|
|
43 |
SymbolTable symbolTable = createSymbolTable(); |
|
44 |
String s = ExpressionUtils.evaluateDynamicText(symbolTable, "el precion es <%=precio%>"); |
|
45 |
|
|
46 |
assertEquals("el precion es 200", s); |
|
47 |
} |
|
48 |
|
|
49 |
public void testSimple2() { |
|
50 |
String s = ExpressionUtils.evaluateDynamicText("el home es <%=HOME()%>"); |
|
51 |
|
|
52 |
assertEquals("el home es "+ToolsLocator.getFoldersManager().getHome().toString(), s); |
|
53 |
} |
|
54 |
|
|
55 |
public void testSimple3() { |
|
56 |
String s = ExpressionUtils.evaluateDynamicText("un fichero temporal <%=UNIQUETEMPORARYFILE('nombre.txt')%>"); |
|
57 |
|
|
58 |
// El resultado viene a ser algo como: |
|
59 |
// "un fichero temporal /tmp/tmp-gvsig/nombre-1-16d2f3e4f44.txt" |
|
60 |
if( !s.startsWith("un fichero temporal ") || !s.contains("nombre-") || !s.endsWith(".txt") ) { |
|
61 |
assertTrue("Expected 'un fichero temporal *nombre-*.txt' and found '"+s+"'", false); |
|
62 |
} |
|
63 |
} |
|
64 |
|
|
65 |
public void test1() { |
|
66 |
|
|
67 |
SymbolTable symbolTable = createSymbolTable(); |
|
68 |
String s = ExpressionUtils.evaluateDynamicText(symbolTable, "el precion es <%=precio%> y PI*100 es <%=TOINTEGER(PI*100)%>"); |
|
69 |
|
|
70 |
assertEquals("el precion es 200 y PI*100 es 314", s); |
|
71 |
} |
|
72 |
|
|
73 |
public void testFilename1() { |
|
74 |
File source = new File("<%=UNIQUETEMPORARYFILE('nombre.txt')%>"); |
|
75 |
File target = ExpressionUtils.evaluateFilename(source); |
|
76 |
|
|
77 |
// El resultado viene a ser algo como: |
|
78 |
// /tmp/tmp-gvsig/nombre-1-16d2f3e4f44.txt |
|
79 |
String s = target.getAbsolutePath(); |
|
80 |
if( !s.contains("nombre-") || !s.endsWith(".txt") ) { |
|
81 |
assertTrue("Expected '*nombre-*.txt' and found '"+s+"'", false); |
|
82 |
} |
|
83 |
} |
|
84 |
|
|
85 |
public void testFilename2() { |
|
86 |
File source = new File("/tmp/prueba.txt"); |
|
87 |
File target = ExpressionUtils.evaluateFilename(source); |
|
88 |
|
|
89 |
String s = target.getAbsolutePath(); |
|
90 |
assertEquals("/tmp/prueba.txt", s); |
|
91 |
} |
|
92 |
|
|
93 |
|
|
94 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/test/java/org/gvsig/expresionevaluator/impl/TestInterpreter.java | ||
---|---|---|
85 | 85 |
assertEquals(23, ((Number)v).intValue()); |
86 | 86 |
} |
87 | 87 |
|
88 |
public void testNumberDMS() { |
|
89 |
SymbolTable symbolTable = createSymbolTable(); |
|
90 |
Compiler compiler = createCompiler(); |
|
91 |
Interpreter interpreter = createInterpreter(symbolTable); |
|
92 |
|
|
93 |
String source = "@10 30 1.2 N"; |
|
94 |
|
|
95 |
Code code = compiler.compileExpression(source); |
|
96 |
Object v = interpreter.run(code); |
|
97 |
assertEquals(-10.5003, Math.round(((Number)v).doubleValue()*10000d)/10000d); |
|
98 |
} |
|
99 |
|
|
88 | 100 |
public void testTrue() { |
89 | 101 |
SymbolTable symbolTable = createSymbolTable(); |
90 | 102 |
Compiler compiler = createCompiler(); |
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/test/java/org/gvsig/expresionevaluator/impl/TestEvaluateDynamicText.java | ||
---|---|---|
1 |
package org.gvsig.expresionevaluator.impl; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import junit.framework.TestCase; |
|
5 |
import org.gvsig.expressionevaluator.SymbolTable; |
|
6 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
|
7 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
|
8 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
|
9 |
import org.gvsig.expressionevaluator.MutableSymbolTable; |
|
10 |
import org.gvsig.tools.ToolsLocator; |
|
11 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
|
12 |
|
|
13 |
public class TestEvaluateDynamicText extends TestCase { |
|
14 |
|
|
15 |
public TestEvaluateDynamicText(String testName) { |
|
16 |
super(testName); |
|
17 |
} |
|
18 |
|
|
19 |
@Override |
|
20 |
protected void setUp() throws Exception { |
|
21 |
super.setUp(); |
|
22 |
new DefaultLibrariesInitializer().fullInitialize(); |
|
23 |
} |
|
24 |
|
|
25 |
@Override |
|
26 |
protected void tearDown() throws Exception { |
|
27 |
super.tearDown(); |
|
28 |
} |
|
29 |
|
|
30 |
// TODO add test methods here. The name must begin with 'test'. For example: |
|
31 |
// public void testHello() {} |
|
32 |
|
|
33 |
protected SymbolTable createSymbolTable() { |
|
34 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
|
35 |
MutableSymbolTable symbolTable = manager.createSymbolTable(); |
|
36 |
symbolTable.setVar("precio", 200); |
|
37 |
symbolTable.setVar("PI", Math.PI); |
|
38 |
return symbolTable; |
|
39 |
} |
|
40 |
|
|
41 |
public void testSimple1() { |
|
42 |
|
|
43 |
SymbolTable symbolTable = createSymbolTable(); |
|
44 |
String s = ExpressionUtils.evaluateDynamicText(symbolTable, "el precion es <%=precio%>"); |
|
45 |
|
|
46 |
assertEquals("el precion es 200", s); |
|
47 |
} |
|
48 |
|
|
49 |
public void testSimple2() { |
|
50 |
String s = ExpressionUtils.evaluateDynamicText("el home es <%=HOME()%>"); |
|
51 |
|
|
52 |
assertEquals("el home es "+ToolsLocator.getFoldersManager().getHome().toString(), s); |
|
53 |
} |
|
54 |
|
|
55 |
public void testSimple3() { |
|
56 |
String s = ExpressionUtils.evaluateDynamicText("un fichero temporal <%=UNIQUETEMPORARYFILE('nombre.txt')%>"); |
|
57 |
|
|
58 |
// El resultado viene a ser algo como: |
|
59 |
// "un fichero temporal /tmp/tmp-gvsig/nombre-1-16d2f3e4f44.txt" |
|
60 |
if( !s.startsWith("un fichero temporal ") || !s.contains("nombre-") || !s.endsWith(".txt") ) { |
|
61 |
assertTrue("Expected 'un fichero temporal *nombre-*.txt' and found '"+s+"'", false); |
|
62 |
} |
|
63 |
} |
|
64 |
|
|
65 |
public void test1() { |
|
66 |
|
|
67 |
SymbolTable symbolTable = createSymbolTable(); |
|
68 |
String s = ExpressionUtils.evaluateDynamicText(symbolTable, "el precion es <%=precio%> y PI*100 es <%=TOINTEGER(PI*100)%>"); |
|
69 |
|
|
70 |
assertEquals("el precion es 200 y PI*100 es 314", s); |
|
71 |
} |
|
72 |
|
|
73 |
public void testFilename1() { |
|
74 |
File source = new File("<%=UNIQUETEMPORARYFILE('nombre.txt')%>"); |
|
75 |
File target = ExpressionUtils.evaluateFilename(source); |
|
76 |
|
|
77 |
// El resultado viene a ser algo como: |
|
78 |
// /tmp/tmp-gvsig/nombre-1-16d2f3e4f44.txt |
|
79 |
String s = target.getAbsolutePath(); |
|
80 |
if( !s.contains("nombre-") || !s.endsWith(".txt") ) { |
|
81 |
assertTrue("Expected '*nombre-*.txt' and found '"+s+"'", false); |
|
82 |
} |
|
83 |
} |
|
84 |
|
|
85 |
public void testFilename2() { |
|
86 |
File source = new File("/tmp/prueba.txt"); |
|
87 |
File target = ExpressionUtils.evaluateFilename(source); |
|
88 |
|
|
89 |
String s = target.getAbsolutePath(); |
|
90 |
assertEquals("/tmp/prueba.txt", s); |
|
91 |
} |
|
92 |
|
|
93 |
|
|
94 |
} |
Also available in: Unified diff