Revision 44098

View differences:

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/TestSQLLexer.java
4 4
import junit.framework.TestCase;
5 5
import org.gvsig.expressionevaluator.LexicalAnalyzer;
6 6
import org.gvsig.expressionevaluator.LexicalAnalyzer.Token;
7
import org.gvsig.expressionevaluator.impl.ExpressionEvaluatorImplLibrary;
7
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
8 8

  
9 9
public class TestSQLLexer extends TestCase {
10 10

  
......
15 15
    @Override
16 16
    protected void setUp() throws Exception {
17 17
        super.setUp();
18
        new ExpressionEvaluatorImplLibrary().initialize();
18
        new DefaultLibrariesInitializer().fullInitialize();
19 19
    }
20 20

  
21 21
    @Override
......
118 118
        assertEquals("true", t1.getLiteral());
119 119
    }
120 120

  
121
    public void testNegativeInteger() {
122
        String expression = "-23";
123

  
124
        LexicalAnalyzer lex = createLexicalAnalyzer(expression);
125
        Token t1 = lex.next();
126
        assertEquals(Token.INTEGER_LITERAL, t1.getType());
127
        assertEquals("-23", t1.getLiteral());
128
        assertEquals(new Long(-23), t1.getValue());
129
    }
130

  
131
    public void testPositiveInteger() {
132
        String expression = "+23";
133

  
134
        LexicalAnalyzer lex = createLexicalAnalyzer(expression);
135
        Token t1 = lex.next();
136
        assertEquals(Token.INTEGER_LITERAL, t1.getType());
137
        assertEquals("+23", t1.getLiteral());
138
        assertEquals(new Long(23), t1.getValue());
139
    }
140

  
141 121
    public void testInteger() {
142 122
        String expression = "23";
143 123

  
......
158 138
        assertEquals(new Double(23.1), t1.getValue());
159 139
    }
160 140

  
161
    public void testPositiveDecimal() {
162
        String expression = "+23.1";
163

  
164
        LexicalAnalyzer lex = createLexicalAnalyzer(expression);
165
        Token t1 = lex.next();
166
        assertEquals(Token.FLOATING_POINT_LITERAL, t1.getType());
167
        assertEquals("+23.1", t1.getLiteral());
168
        assertEquals(new Double(23.1), t1.getValue());
169
    }
170

  
171
    public void testNegativeDecimal() {
172
        String expression = "-23.1";
173

  
174
        LexicalAnalyzer lex = createLexicalAnalyzer(expression);
175
        Token t1 = lex.next();
176
        assertEquals(Token.FLOATING_POINT_LITERAL, t1.getType());
177
        assertEquals("-23.1", t1.getLiteral());
178
        assertEquals(new Double(-23.1), t1.getValue());
179
    }
180

  
181 141
    public void testStringLiteral1() {
182 142
        String expression = "'hola macu'";
183 143

  
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/TestOptimizer.java
354 354
        Compiler compiler = createCompiler();
355 355

  
356 356
        Code code = compiler.compileExpression(source);
357
        assertEquals("+(\"precio\", abs(-10))", code.toString());
357
        assertEquals("+(\"precio\", abs(negate(10)))", code.toString());
358 358

  
359 359
        SymbolTable symbolTable = createSymbolTable();
360 360
        Optimizer optimizer = createOptimizer(symbolTable);
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
10 10
import org.gvsig.expressionevaluator.SymbolTable;
11 11
import org.gvsig.expressionevaluator.Interpreter;
12 12
import org.gvsig.expressionevaluator.Code;
13
import org.gvsig.expressionevaluator.impl.ExpressionEvaluatorImplLibrary;
13
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
14 14

  
15 15
public class TestInterpreter extends TestCase {
16 16
    
......
21 21
    @Override
22 22
    protected void setUp() throws Exception {
23 23
        super.setUp();
24
        new ExpressionEvaluatorImplLibrary().initialize();
24
        new DefaultLibrariesInitializer().fullInitialize();
25 25
    }
26 26
    
27 27
    @Override
......
125 125
        assertEquals(210, ((Number)v).intValue());
126 126
    }
127 127
    
128
    public void testSimpleAdd1() {
129
        String source = "5+10";
130

  
131
        SymbolTable symbolTable = createSymbolTable();
132
        Compiler compiler = createCompiler();
133
        Interpreter interpreter = createInterpreter(symbolTable);
134
        
135
        Code code = compiler.compileExpression(source);
136
        Object v = interpreter.run(code);
137
        assertEquals(15, ((Number)v).intValue());
138
    }
139
    
140
    public void testSimpleAdd2() {
141
        String source = "5 + 10";
142

  
143
        SymbolTable symbolTable = createSymbolTable();
144
        Compiler compiler = createCompiler();
145
        Interpreter interpreter = createInterpreter(symbolTable);
146
        
147
        Code code = compiler.compileExpression(source);
148
        Object v = interpreter.run(code);
149
        assertEquals(15, ((Number)v).intValue());
150
    }
151
    
152
    public void testSimpleAdd3() {
153
        String source = "10+-5";
154

  
155
        SymbolTable symbolTable = createSymbolTable();
156
        Compiler compiler = createCompiler();
157
        Interpreter interpreter = createInterpreter(symbolTable);
158
        
159
        Code code = compiler.compileExpression(source);
160
        Object v = interpreter.run(code);
161
        assertEquals(5, ((Number)v).intValue());
162
    }
163
    
164
    public void testSimpleSub1() {
165
        String source = "10-5";
166

  
167
        SymbolTable symbolTable = createSymbolTable();
168
        Compiler compiler = createCompiler();
169
        Interpreter interpreter = createInterpreter(symbolTable);
170
        
171
        Code code = compiler.compileExpression(source);
172
        Object v = interpreter.run(code);
173
        assertEquals(5, ((Number)v).intValue());
174
    }
175
    
176
    public void testSimpleSub2() {
177
        String source = "10 - 5";
178

  
179
        SymbolTable symbolTable = createSymbolTable();
180
        Compiler compiler = createCompiler();
181
        Interpreter interpreter = createInterpreter(symbolTable);
182
        
183
        Code code = compiler.compileExpression(source);
184
        Object v = interpreter.run(code);
185
        assertEquals(5, ((Number)v).intValue());
186
    }
187
    
188
    public void testSimpleNumber() {
189
        String source = "23";
190

  
191
        SymbolTable symbolTable = createSymbolTable();
192
        Compiler compiler = createCompiler();
193
        Interpreter interpreter = createInterpreter(symbolTable);
194
        
195
        Code code = compiler.compileExpression(source);
196
        Object v = interpreter.run(code);
197
        assertEquals(23, ((Number)v).intValue());
198
    }
199
    
200
    public void testSimpleNegativeNumber() {
201
        String source = "-23";
202

  
203
        SymbolTable symbolTable = createSymbolTable();
204
        Compiler compiler = createCompiler();
205
        Interpreter interpreter = createInterpreter(symbolTable);
206
        
207
        Code code = compiler.compileExpression(source);
208
        Object v = interpreter.run(code);
209
        assertEquals(-23, ((Number)v).intValue());
210
    }
211
    
212
    public void testNegateFunction() {
213
        String source = "-PI()";
214

  
215
        SymbolTable symbolTable = createSymbolTable();
216
        Compiler compiler = createCompiler();
217
        Interpreter interpreter = createInterpreter(symbolTable);
218
        
219
        Code code = compiler.compileExpression(source);
220
        Object v = interpreter.run(code);
221
        assertEquals(-Math.PI, ((Number)v).doubleValue());
222
    }
223
    
128 224
    public void testSub1() {
129 225
        String source = "precio - 10";
130 226

  
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/TestLoadResource.java
6 6
import org.gvsig.expressionevaluator.Function;
7 7
import org.gvsig.expressionevaluator.impl.function.numeric.ACosFunction;
8 8
import org.gvsig.expressionevaluator.impl.function.spatial.STAreaFunction;
9
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
9 10

  
10 11
/**
11 12
 *
......
20 21
    @Override
21 22
    protected void setUp() throws Exception {
22 23
        super.setUp();
23
        new ExpressionEvaluatorImplLibrary().initialize();
24
        new DefaultLibrariesInitializer().fullInitialize();
24 25
    }
25 26

  
26 27
    @Override
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/TestCompiler.java
7 7
import org.gvsig.expressionevaluator.Compiler;
8 8
import org.gvsig.expressionevaluator.Code;
9 9
import org.gvsig.expressionevaluator.ExpressionSyntaxException;
10
import org.gvsig.expressionevaluator.impl.ExpressionEvaluatorImplLibrary;
10
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
11 11

  
12 12

  
13 13
public class TestCompiler extends TestCase {
......
19 19
    @Override
20 20
    protected void setUp() throws Exception {
21 21
        super.setUp();
22
        new ExpressionEvaluatorImplLibrary().initialize();
22
        new DefaultLibrariesInitializer().fullInitialize();
23 23
    }
24 24
    
25 25
    @Override
......
168 168
        Compiler compiler = createCompiler();
169 169
        
170 170
        Code code = compiler.compileExpression(source);
171
        assertEquals("+(\"precio\", abs(-10))", code.toString());
171
        assertEquals("+(\"precio\", abs(negate(10)))", code.toString());
172 172
    }
173 173
    
174 174
    public void testPI() {
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/TestCodeToStringConverter.java
2 2

  
3 3
import static junit.framework.Assert.assertEquals;
4 4
import junit.framework.TestCase;
5
import org.gvsig.expressionevaluator.impl.ExpressionEvaluatorImplLibrary;
6 5
import org.gvsig.expressionevaluator.spi.AbstractCodeToStringConverter;
7 6
import org.gvsig.expressionevaluator.Code;
8 7
import org.gvsig.expressionevaluator.CodeToStringConverter;
......
14 13
import org.gvsig.expressionevaluator.SymbolTable;
15 14
import org.gvsig.expressionevaluator.impl.DefaultCompiler;
16 15
import org.gvsig.expressionevaluator.impl.SQLLexicalAnalyzer;
16
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
17 17

  
18 18
/**
19 19
 *
......
28 28
    @Override
29 29
    protected void setUp() throws Exception {
30 30
        super.setUp();
31
        new ExpressionEvaluatorImplLibrary().initialize();
31
        new DefaultLibrariesInitializer().fullInitialize();
32 32
    }
33 33

  
34 34
    @Override
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/DefaultInterpreter.java
15 15
import org.gvsig.expressionevaluator.Code.Caller;
16 16
import org.gvsig.expressionevaluator.Code.Caller.Arguments;
17 17
import org.gvsig.expressionevaluator.Code.Method;
18
import org.gvsig.expressionevaluator.ExpressionRuntimeException;
18 19
import org.gvsig.expressionevaluator.Function;
19
import org.gvsig.expressionevaluator.Optimizer;
20 20
import org.gvsig.expressionevaluator.impl.function.operator.BinaryOperator;
21 21
import org.gvsig.expressionevaluator.impl.function.obj.InvokeMethodFunction;
22 22
import org.gvsig.expressionevaluator.impl.function.operator.UnaryOperator;
......
126 126
    public Object run(Code code) {
127 127
        try {
128 128
            return this.runCode(code);
129
        } catch (Exception ex) {
130
            throw new RuntimeException(ex);
129
        } catch(RuntimeException ex) {
130
            throw ex;
131
        } catch(Exception ex) {
132
            throw new ExpressionRuntimeException(code, "", ex);
131 133
        }
132 134
    }
133 135

  
......
161 163
        case Code.IDENTIFIER:
162 164
            String name = ((Identifier) code).name();
163 165
            if( !this.getSymbolTable().exists(name) ) {
164
                throw new RuntimeException("Variable '" + name + "' not found.");
166
                throw new ExpressionRuntimeException(
167
                        code, 
168
                        I18N.Undefined_variable_XIdentifierX(name),
169
                        I18N.Use_single_quotes_to_enter_literal_strings()
170
                );
165 171
            }
166 172
            value = this.getSymbolTable().value(name);
167 173
            break;
......
183 189
            if( function == null ) {
184 190
                function = this.getSymbolTable().function(caller.name());
185 191
                if( function == null ) {
186
                    throw new RuntimeException("Function '" + caller.name() + "' not found.");
192
                    throw new ExpressionRuntimeException(code, I18N.Undefined_function_XIdentifierX(caller.name()));
187 193
                }
188 194
                caller.function(function);
189 195
            }
......
192 198
                switch( caller.type() ) {
193 199
                case Caller.UNARY_OPERATOR:
194 200
                    if( args == null || args.count() != 1 ) {
195
                        throw new RuntimeException("Number of argument mistmatch in operator '"+function.name()+"', expected 1 got " + args.count() + ".");
201
                        throw new ExpressionRuntimeException(code, I18N.Number_of_argument_mistmatch_in_operator_XIdentifierX_expected_1_got_XargcX(function.name(),args==null?0:args.count()));
196 202
                    }
197 203
                    value = ((UnaryOperator) function).call(this, runCode(args.get(0)));
198 204
                    break;
199 205

  
200 206
                case Caller.BINARY_OPERATOR:
201 207
                    if( args == null || args.count() != 2 ) {
202
                        throw new RuntimeException("Number of argument mistmatch in operator '"+function.name()+"', expected 2 got " + args.count() + ".");
208
                        throw new ExpressionRuntimeException(code, I18N.Number_of_argument_mistmatch_in_operator_XIdentifierX_expected_2_got_XargcX(function.name(),args==null?0:args.count()));
203 209
                    }
204 210
                    value = ((BinaryOperator) function).call(this, runCode(args.get(0)), runCode(args.get(1)));
205 211
                    break;
......
207 213
                case Caller.FUNCTION:
208 214
                    int argc = (args == null) ? 0 : args.count();
209 215
                    if( !function.argc().contains(argc) ) {
210
                        throw new RuntimeException("Number of argument mistmatch in function '"+function.name()+"', expected " + function.argc() + " got " + argc + ".");
216
                        throw new ExpressionRuntimeException(code, I18N.Number_of_argument_mistmatch_in_function_XIdentifierX_expected_XexpectedX_got_XfoundX(function.name(),function.argc(),argc));
211 217
                    }
212 218
                    if( function.useArgumentsInsteadObjects() ) {
213 219
                        value = function.call(this, args);
......
222 228
                        value = function.call(this, argvalues);
223 229
                    }
224 230
                }
231
            } catch (RuntimeException ex) {
232
                throw ex;
225 233
            } catch (Exception ex) {
226 234
                String argsstr = "???";
227 235
                try {
......
229 237
                } catch(Exception ex2) {
230 238
                    // Ignore.
231 239
                }
232
                throw new RuntimeException("Problems calling function '"+function.name()+"' with args ("+argsstr+").", ex);
240
                throw new ExpressionRuntimeException(code, I18N.Problems_calling_function_XIdentifierX_with_args_XargsX(function.name(),argsstr));
233 241
            }
234 242
            break;
235 243
            
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/symboltable/SQLSymbolTable.java
3 3
import org.gvsig.expressionevaluator.Function;
4 4
import org.gvsig.expressionevaluator.impl.function.date.DateFunction;
5 5
import org.gvsig.expressionevaluator.impl.function.date.NowFunction;
6
import org.gvsig.expressionevaluator.impl.function.date.RelativeInstantFunction;
7
import org.gvsig.expressionevaluator.impl.function.date.RelativeIntervalFunction;
8 6
import org.gvsig.expressionevaluator.impl.function.date.TimeFunction;
9 7
import org.gvsig.expressionevaluator.impl.function.date.TimestampFunction;
10 8
import org.gvsig.expressionevaluator.impl.function.numeric.ACosFunction;
......
50 48
import org.gvsig.expressionevaluator.impl.function.operator.ModOperator;
51 49
import org.gvsig.expressionevaluator.impl.function.operator.MulOperator;
52 50
import org.gvsig.expressionevaluator.impl.function.operator.NeOperator;
51
import org.gvsig.expressionevaluator.impl.function.operator.NegOperator;
53 52
import org.gvsig.expressionevaluator.impl.function.operator.NotOperator;
54 53
import org.gvsig.expressionevaluator.impl.function.operator.OrOperator;
55 54
import org.gvsig.expressionevaluator.impl.function.operator.SubstOperator;
......
108 107
        
109 108
        this.addOperator(new IlikeOperator());
110 109
        this.addOperator(new LikeOperator());
110
        this.addOperator(new NegOperator());
111 111
        
112 112
        this.addFunction(new ACosFunction());
113 113
        this.addFunction(new ASinFunction());
......
170 170
        this.addFunction(new IIFFunction());
171 171
        this.addFunction(new IFNULLFunction());
172 172

  
173
        this.addFunction(new RelativeInstantFunction());
174
        this.addFunction(new RelativeIntervalFunction());
175 173
    }
176 174

  
177 175
    private void addOperator(Function operator) {
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/symboltable/UtilsSymbolTable.java
1
package org.gvsig.expressionevaluator.impl.symboltable;
2

  
3
import org.gvsig.expressionevaluator.Function;
4
import org.gvsig.expressionevaluator.impl.function.date.RelativeInstantFunction;
5
import org.gvsig.expressionevaluator.impl.function.date.RelativeIntervalFunction;
6
import org.gvsig.expressionevaluator.impl.function.numeric.DecrFunction;
7
import org.gvsig.expressionevaluator.impl.function.numeric.IncrFunction;
8
import org.gvsig.expressionevaluator.impl.function.obj.CodeBlockFunction;
9
import org.gvsig.expressionevaluator.impl.function.obj.InvokeStaticMethodFunction;
10
import org.gvsig.expressionevaluator.impl.function.obj.LetFunction;
11
import org.gvsig.expressionevaluator.impl.function.obj.WhileFunction;
12
import org.gvsig.expressionevaluator.impl.function.obj.forFunction;
13
import org.gvsig.expressionevaluator.spi.AbstractSymbolTable;
14

  
15
/**
16
 *
17
 * @author jjdelcerro
18
 */
19
public class UtilsSymbolTable extends AbstractSymbolTable {
20
    
21
    @SuppressWarnings("OverridableMethodCallInConstructor")
22
    public UtilsSymbolTable() {
23
        super("Utilities");
24

  
25
        this.addFunction(new RelativeInstantFunction());
26
        this.addFunction(new RelativeIntervalFunction());
27
        
28
        this.addFunction(new IncrFunction());
29
        this.addFunction(new DecrFunction());
30

  
31
        this.addFunction(new CodeBlockFunction());
32
        this.addFunction(new InvokeStaticMethodFunction());
33
        this.addFunction(new LetFunction());
34
        this.addFunction(new WhileFunction());
35
        this.addFunction(new forFunction());
36
    }
37

  
38
    private void addOperator(Function operator) {
39
        this.addFunction(operator);
40
    }
41
    
42
}
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/DefaultCompiler.java
87 87
                lexer.next();
88 88
                op2 = parse_not();
89 89
                if( op2==null ) {
90
                    throw new ExpressionSyntaxException(lexer);
90
                    throw new ExpressionSyntaxException(I18N.Cant_recognize_the_second_operand_of_OR_operator(),lexer);
91 91
                }
92 92
                op1 = codeBuilder.or(op1, op2);
93 93
                break;
......
95 95
                lexer.next();
96 96
                op2 = parse_not();
97 97
                if( op2==null ) {
98
                    throw new ExpressionSyntaxException(lexer);
98
                    throw new ExpressionSyntaxException(I18N.Cant_recognize_the_second_operand_of_AND_operator(),lexer);
99 99
                }
100 100
                op1 = codeBuilder.and(op1, op2);
101 101
                break;
......
128 128
                lexer.next();
129 129
                op2 = parse_sum();
130 130
                if( op2==null ) {
131
                    throw new ExpressionSyntaxException(lexer);
131
                    throw new ExpressionSyntaxException(I18N.Cant_recognize_the_second_operand_of_LT_operator(),lexer);
132 132
                }
133 133
                op1 = codeBuilder.lt(op1, op2);
134 134
                break;
......
136 136
                lexer.next();
137 137
                op2 = parse_sum();
138 138
                if( op2==null ) {
139
                    throw new ExpressionSyntaxException(lexer);
139
                    throw new ExpressionSyntaxException(I18N.Cant_recognize_the_second_operand_of_GT_operator(),lexer);
140 140
                }
141 141
                op1 = codeBuilder.gt(op1, op2);
142 142
                break;
......
144 144
                lexer.next();
145 145
                op2 = parse_sum();
146 146
                if( op2==null ) {
147
                    throw new ExpressionSyntaxException(lexer);
147
                    throw new ExpressionSyntaxException(I18N.Cant_recognize_the_second_operand_of_LE_operator(),lexer);
148 148
                }
149 149
                op1 = codeBuilder.le(op1, op2);
150 150
                break;
......
152 152
                lexer.next();
153 153
                op2 = parse_sum();
154 154
                if( op2==null ) {
155
                    throw new ExpressionSyntaxException(lexer);
155
                    throw new ExpressionSyntaxException(I18N.Cant_recognize_the_second_operand_of_GE_operator(),lexer);
156 156
                }
157 157
                op1 = codeBuilder.ge(op1, op2);
158 158
                break;
......
160 160
                lexer.next();
161 161
                op2 = parse_sum();
162 162
                if( op2==null ) {
163
                    throw new ExpressionSyntaxException(lexer);
163
                    throw new ExpressionSyntaxException(I18N.Cant_recognize_the_second_operand_of_EQ_operator(),lexer);
164 164
                }
165 165
                op1 = codeBuilder.eq(op1, op2);
166 166
                break;
......
168 168
                lexer.next();
169 169
                op2 = parse_sum();
170 170
                if( op2==null ) {
171
                    throw new ExpressionSyntaxException(lexer);
171
                    throw new ExpressionSyntaxException(I18N.Cant_recognize_the_second_operand_of_NEQ_operator(),lexer);
172 172
                }
173 173
                op1 = codeBuilder.ne(op1, op2);
174 174
                break;
......
181 181
                    } else {
182 182
                        op2 = parse_sum();
183 183
                        if( op2==null ) {
184
                            throw new ExpressionSyntaxException(lexer);
184
                            throw new ExpressionSyntaxException(I18N.Cant_recognize_the_second_operand_of_IS_operator(),lexer);
185 185
                        }
186 186
                        op1 = codeBuilder.is(op1, op2);
187 187
                    }
......
195 195
                lexer.next();
196 196
                op2 = parse_sum();
197 197
                if( op2==null ) {
198
                    throw new ExpressionSyntaxException(lexer);
198
                    throw new ExpressionSyntaxException(I18N.Cant_recognize_the_second_operand_of_REGEXP_operator(),lexer);
199 199
                }
200 200
                op1 = codeBuilder.regexp(op1, op2);
201 201
                break;
......
203 203
                lexer.next();
204 204
                op2 = parse_sum();
205 205
                if( op2==null ) {
206
                    throw new ExpressionSyntaxException(lexer);
206
                    throw new ExpressionSyntaxException(I18N.Cant_recognize_the_second_operand_of_LIKE_operator(),lexer);
207 207
                }
208 208
                op1 = codeBuilder.like(op1, op2);
209 209
                break;
......
211 211
                lexer.next();
212 212
                op2 = parse_sum();
213 213
                if( op2==null ) {
214
                    throw new ExpressionSyntaxException(lexer);
214
                    throw new ExpressionSyntaxException(I18N.Cant_recognize_the_second_operand_of_ILIKE_operator(),lexer);
215 215
                }
216 216
                op1 = codeBuilder.ilike(op1, op2);
217 217
                break;
......
253 253
                lexer.next();
254 254
                op2 = parse_getattr();
255 255
                if( op2==null ) {
256
                    throw new ExpressionSyntaxException(lexer);
256
                    throw new ExpressionSyntaxException(I18N.Cant_recognize_the_second_operand_of_MULT_operator(),lexer);
257 257
                }
258 258
                op1 = codeBuilder.mult(op1, op2);
259 259
                break;
......
261 261
                lexer.next();
262 262
                op2 = parse_getattr();
263 263
                if( op2==null ) {
264
                    throw new ExpressionSyntaxException(lexer);
264
                    throw new ExpressionSyntaxException(I18N.Cant_recognize_the_second_operand_of_DIV_operator(),lexer);
265 265
                }
266 266
                op1 = codeBuilder.div(op1, op2);
267 267
                break;
......
269 269
                lexer.next();
270 270
                op2 = parse_getattr();
271 271
                if( op2==null ) {
272
                    throw new ExpressionSyntaxException(lexer);
272
                    throw new ExpressionSyntaxException(I18N.Cant_recognize_the_second_operand_of_MOD_operator(),lexer);
273 273
                }
274 274
                op1 = codeBuilder.mod(op1, op2);
275 275
                break;
......
292 292
                next = lexer.look();
293 293
                if( next.getType()!=Token.IDENTIFIER ) {
294 294
                    throw new ExpressionSyntaxException(
295
                        "An attribute identifier was expected and '"+next.getLiteral()+"' was found", 
296
                        lexer.getSource(),
297
                        lexer.getPosition()
295
                        I18N.An_attribute_identifier_was_expected_and_XliteralX_was_found(next.getLiteral()),
296
                        lexer
298 297
                    );
299 298
                }
300 299
                String id = (String) next.getLiteral();
......
306 305
                    next = lexer.next();
307 306
                    if( next.getType() != Token.PARENTHESIS_CLOSE ) {
308 307
                        throw new ExpressionSyntaxException(
309
                            "Closing parenthesis was expected and '"+next.getLiteral()+"' was found", 
310
                            lexer.getSource(),
311
                            lexer.getPosition()
308
                            I18N.Closing_parenthesis_was_expected_and_XliteralX_was_found(next.getLiteral()),
309
                            lexer
312 310
                        );
313 311
                    }
314 312
                    return codeBuilder.method(op1, id, args);
......
329 327
                lexer.next();
330 328
                Code value = parseExpression();
331 329
                Token next = lexer.next();
332
                if( next.getType() != Token.PARENTHESIS_CLOSE ) {
333
                    throw new ExpressionSyntaxException(
334
                        "Closing parenthesis was expected and '"+next.getLiteral()+"' was found",
335
                        lexer.getSource(),
336
                        lexer.getPosition()
337
                    );
330
                switch(next.getType()) {
331
                    case Token.PARENTHESIS_CLOSE:
332
                        break;
333
                    case Token.EOF:
334
                        throw new ExpressionSyntaxException(
335
                            I18N.Closing_parenthesis_was_expected_and_end_of_source_was_found(),
336
                            lexer
337
                        );
338
                    default:
339
                        throw new ExpressionSyntaxException(
340
                            I18N.Closing_parenthesis_was_expected_and_XliteralX_was_found(next.getLiteral()),
341
                            lexer
342
                        );
338 343
                }
339 344
                return value;
340 345
            }
......
346 351
                    lexer.next();
347 352
                    Arguments args = parseArgs();
348 353
                    next = lexer.next();
349
                    if( next.getType() != Token.PARENTHESIS_CLOSE ) {
350
                        throw new ExpressionSyntaxException(
351
                            "Closing parenthesis was expected and '"+next.getLiteral()+"' was found", 
352
                            lexer.getSource(),
353
                            lexer.getPosition()
354
                        );
354
                    switch(next.getType()) {
355
                        case Token.PARENTHESIS_CLOSE:
356
                            break;
357
                        case Token.EOF:
358
                            throw new ExpressionSyntaxException(
359
                                I18N.Closing_parenthesis_was_expected_and_end_of_source_was_found(),
360
                                lexer
361
                            );
362
                        default:
363
                            throw new ExpressionSyntaxException(
364
                                I18N.Closing_parenthesis_was_expected_and_XliteralX_was_found(next.getLiteral()),
365
                                lexer
366
                            );
355 367
                    }
356 368
                    return codeBuilder.function(id, args);
357 369
                } else {
......
376 388
        case Token.FALSE:
377 389
            lexer.next();
378 390
            return codeBuilder.constant(false);
391
        case Token.OP_SUBST:
392
            lexer.next();
393
            Code code = parse_termino();
394
            return codeBuilder.negate(code);
395
        case Token.EOF:
396
            throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(),lexer);
379 397
        }
380 398
        return null;
381 399
    }
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/I18N.java
1
package org.gvsig.expressionevaluator.impl;
2

  
3
import org.apache.commons.lang3.Range;
4

  
5
/**
6
 *
7
 * @author jjdelcerro
8
 */
9
public class I18N {
10

  
11
    public static String Cant_recognize_the_second_operand_of_AND_operator() {
12
        return "Can't recognize the second operand of \"AND\" operator.";
13
    }
14
    
15
    public static String Cant_recognize_the_second_operand_of_OR_operator() {
16
        return "Can't recognize the second operand of \"OR\" operator.";
17
    }
18
    
19
    public static String Cant_recognize_the_second_operand_of_LT_operator() {
20
        return "Can't recognize the second operand of \"<\" operator.";
21
    }
22
    
23
    public static String Cant_recognize_the_second_operand_of_LE_operator() {
24
        return "Can't recognize the second operand of \"<=\" operator.";
25
    }
26
    
27
    public static String Cant_recognize_the_second_operand_of_GT_operator() {
28
        return "Can't recognize the second operand of \">\" operator.";
29
    }
30
    
31
    public static String Cant_recognize_the_second_operand_of_GE_operator() {
32
        return "Can't recognize the second operand of \">=\" operator.";
33
    }
34
    
35
    public static String Cant_recognize_the_second_operand_of_EQ_operator() {
36
        return "Can't recognize the second operand of \"=\" operator.";
37
    }
38
    
39
    public static String Cant_recognize_the_second_operand_of_NEQ_operator() {
40
        return "Can't recognize the second operand of \"<>\" operator.";
41
    }
42
    
43
    public static String Cant_recognize_the_second_operand_of_IS_operator() {
44
        return "Can't recognize the second operand of \"IS\" operator.";
45
    }
46
    
47
    public static String Cant_recognize_the_second_operand_of_REGEXP_operator() {
48
        return "Can't recognize the second operand of \"REGEXP\" operator.";
49
    }
50
    
51
    public static String Cant_recognize_the_second_operand_of_LIKE_operator() {
52
        return "Can't recognize the second operand of \"LIKE\" operator.";
53
    }
54
    
55
    public static String Cant_recognize_the_second_operand_of_ILIKE_operator() {
56
        return "Can't recognize the second operand of \"ILIKE\" operator.";
57
    }
58
    
59
    public static String Cant_recognize_the_second_operand_of_MULT_operator() {
60
        return "Can't recognize the second operand of \"*\" operator.";
61
    }
62
    
63
    public static String Cant_recognize_the_second_operand_of_DIV_operator() {
64
        return "Can't recognize the second operand of \"/\" operator.";
65
    }
66
    
67
    public static String Cant_recognize_the_second_operand_of_MOD_operator() {
68
        return "Can't recognize the second operand of \"%\" operator.";
69
    }
70
    
71
    public static String An_attribute_identifier_was_expected_and_XliteralX_was_found(String literal) {
72
        return String.format("An attribute identifier was expected and '%s' was found", literal);
73
    }
74
    
75
    public static String Closing_parenthesis_was_expected_and_XliteralX_was_found(String literal) {
76
        return String.format("Closing parenthesis was expected and '%s' was found", literal);
77
    }
78
    
79
    public static String End_of_string_was_expected_and_end_of_source_was_found() {
80
        return "End of string was expected and end of source was found";
81
    }
82
    
83
    public static String Incorrect_string_length() {
84
        return "Incorrect string length";
85
    }
86
    
87
    public static String Incorrect_identifier_length() {
88
        return "Incorrect identifier length";
89
    }
90
    
91
    public static String unexpected_end_of_source() {
92
        return "Unexpected end of source";
93
    }
94
    
95
    public static String Closing_parenthesis_was_expected_and_end_of_source_was_found() {
96
        return "Closing ')' was expected and end of source was found";
97
    }
98
        
99
    public static String Closing_square_bracket_was_expected_and_end_of_source_was_found() {
100
        return "Closing ']' was expected and end of source was found";
101
    }
102

  
103
    public static String Undefined_variable_XIdentifierX(String identifier) {
104
        return String.format("Undefined variable '%s'", identifier);
105
    }
106

  
107
    public static String Undefined_function_XIdentifierX(String identifier) {
108
        return String.format("Undefined function '%s'", identifier);
109
    }
110
    
111
    public static String Number_of_argument_mistmatch_in_operator_XIdentifierX_expected_1_got_XargcX(String identifier, int argc) {
112
        return String.format("Number of argument mistmatch in operator '%s', expected 1 got %d", identifier, argc);
113
    }
114

  
115
    public static String Number_of_argument_mistmatch_in_operator_XIdentifierX_expected_2_got_XargcX(String identifier, int argc) {
116
        return String.format("Number of argument mistmatch in operator '%s', expected 2 got %d", identifier, argc);
117
    }
118

  
119
    public static String Number_of_argument_mistmatch_in_function_XIdentifierX_expected_XexpectedX_got_XfoundX(String identifier, int expected, int found) {
120
        return String.format("Number of argument mistmatch in function '%s', expected %d got %d", identifier, expected, found);
121
    }
122

  
123
    public static String Number_of_argument_mistmatch_in_function_XIdentifierX_expected_XexpectedX_got_XfoundX(String identifier, Range expected, int found) {
124
        return String.format("Number of argument mistmatch in function '%s', expected %s got %d", identifier, expected.toString(), found);
125
    }
126

  
127
    public static String Problems_calling_function_XIdentifierX_with_args_XargsX(String identifier, String args) {
128
        return String.format("Problems calling function '%s' with args %s", identifier, args);
129
    }
130

  
131
    static String Use_single_quotes_to_enter_literal_strings() {
132
        return "Use single quotes to enter literal strings.";
133
    }
134

  
135
}
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/DefaultExpressionEvaluatorManager.java
18 18
import org.gvsig.expressionevaluator.SymbolTable;
19 19
import org.gvsig.expressionevaluator.impl.symboltable.OGCSymbolTable;
20 20
import org.gvsig.expressionevaluator.impl.symboltable.SQLSymbolTable;
21
import org.gvsig.expressionevaluator.impl.symboltable.UtilsSymbolTable;
21 22
import org.slf4j.Logger;
22 23
import org.slf4j.LoggerFactory;
23 24

  
......
35 36
        this.autoloadSymbolTables = new HashSet<>();
36 37
        this.registerSymbolTable(new SQLSymbolTable(), true);
37 38
        this.registerSymbolTable(new OGCSymbolTable(), true);
39
        this.registerSymbolTable(new UtilsSymbolTable(), true);
38 40
    }
39 41

  
40 42
    @Override
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/function/numeric/IncrFunction.java
1
package org.gvsig.expressionevaluator.impl.function.numeric;
2

  
3
import org.apache.commons.lang3.Range;
4
import org.gvsig.expressionevaluator.Interpreter;
5
import org.gvsig.expressionevaluator.MutableSymbolTable;
6
import org.gvsig.expressionevaluator.spi.AbstractFunction;
7

  
8
public class IncrFunction extends AbstractFunction {
9

  
10
    public IncrFunction() {
11
        super("Numeric", "INCR",Range.between(1,3),
12
            "Increase the value of the indicated identifier and return its value.",
13
            "Incr({{identifier_name}})",
14
            new String[]{
15
                "identifier name - the identifier used as counter.",
16
                "first value - Optional. The value with which the identifier must be initialized in its first use. By default zero",
17
                "step - Optional. The value used to increase the identifier. By default one",
18
            },
19
            "Long",
20
            false
21
        );
22
    }
23

  
24
    @Override
25
    public boolean allowConstantFolding() {
26
        return true;
27
    }
28
    
29
    @Override
30
    public Object call(Interpreter interpreter, Object[] args) {
31
        String identifierName = this.getStr(args, 0);
32
        long firstValue = 0;
33
        long increment = 1; 
34
        long value;
35
        switch(args.length) {
36
            default:
37
            case 1:
38
                break;
39
            case 2:
40
                firstValue = this.getLong(args, 1);
41
                break;
42
            case 3:
43
                firstValue = this.getLong(args, 1);
44
                increment = this.getLong(args, 2);
45
                break;
46
        }
47
        MutableSymbolTable symbolTable = (MutableSymbolTable) interpreter.getSymbolTable();
48
        if( symbolTable.exists(identifierName) ) {
49
            value = ((Number) symbolTable.value(identifierName)).longValue();
50
        } else {
51
            value = firstValue;
52
        }
53
        value = value+increment;
54
        symbolTable.setVar(identifierName, value);
55
        return value;
56
    }
57
    
58

  
59
}
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/function/numeric/DecrFunction.java
1
package org.gvsig.expressionevaluator.impl.function.numeric;
2

  
3
import org.apache.commons.lang3.Range;
4
import org.gvsig.expressionevaluator.Interpreter;
5
import org.gvsig.expressionevaluator.MutableSymbolTable;
6
import org.gvsig.expressionevaluator.spi.AbstractFunction;
7

  
8
public class DecrFunction extends AbstractFunction {
9

  
10
    public DecrFunction() {
11
        super("Numeric", "DECR",Range.between(1,3),
12
            "Decrease the value of the indicated identifier and return its value.",
13
            "Decr({{identifier_name}})",
14
            new String[]{
15
                "identifier name - the identifier used as counter.",
16
                "first value - Optional. The value with which the identifier must be initialized in its first use. By default zero",
17
                "step - Optional. The value used to decrease the identifier. By default one",
18
            },
19
            "Long",
20
            false
21
        );
22
    }
23

  
24
    @Override
25
    public boolean allowConstantFolding() {
26
        return true;
27
    }
28
    
29
    @Override
30
    public Object call(Interpreter interpreter, Object[] args) {
31
        String identifierName = this.getStr(args, 0);
32
        long firstValue = 0;
33
        long increment = 1; 
34
        long value;
35
        switch(args.length) {
36
            default:
37
            case 1:
38
                break;
39
            case 2:
40
                firstValue = this.getLong(args, 1);
41
                break;
42
            case 3:
43
                firstValue = this.getLong(args, 1);
44
                increment = this.getLong(args, 2);
45
                break;
46
        }
47
        MutableSymbolTable symbolTable = (MutableSymbolTable) interpreter.getSymbolTable();
48
        if( symbolTable.exists(identifierName) ) {
49
            value = ((Number) symbolTable.value(identifierName)).longValue();
50
        } else {
51
            value = firstValue;
52
        }
53
        value = value-increment;
54
        symbolTable.setVar(identifierName, value);
55
        return value;
56
    }
57
    
58

  
59
}
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/function/obj/InvokeStaticMethodFunction.java
12 12
    public InvokeStaticMethodFunction() {
13 13
        super(
14 14
            Function.GROUP_OTHER, 
15
            "invokeStaticMethod", 
15
            "CALLEX", 
16 16
            Range.between(2, Integer.MAX_VALUE),
17
            "The InvokeStaticMethod() function call a static mehod of a class.",
18
            "InvokeStaticMethod( className, methodName, arguments... )"
17
            "The CALLEX() function call a static mehod of a class.",
18
            "CALLEX( className, methodName, arguments... )",
19
            null,
20
            "Object",
21
            false
19 22
        );
20 23
    }
21 24
    
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/function/obj/LetFunction.java
10 10
public class LetFunction extends AbstractFunction {
11 11

  
12 12
    public LetFunction() {
13
        super(Function.GROUP_OTHER, "let", Range.is(2));
13
        super(
14
            Function.GROUP_OTHER, 
15
            "LET", 
16
            Range.is(2),
17
            "Assigns the value indicated in the second argument to the variable indicated in the first argument.",
18
            "LET({{identifier_name}},value)",
19
            new String[]{
20
                "identifier_name - Name of the variable",
21
                "value - Value to assign to the variable"
22
            },
23
            "Object",
24
            false
25
        );
14 26
    }
15 27

  
16 28
    @Override
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/function/obj/CodeBlockFunction.java
12 12
    public CodeBlockFunction() {
13 13
        super(
14 14
                Function.GROUP_OTHER, 
15
                "CodeBlock", 
16
                Range.between(0,Integer.MAX_VALUE),
17
                "",
18
                "CodeBlock( arguments... )"
15
                "BLOCK", 
16
                Range.between(1,Integer.MAX_VALUE),
17
                "Evaluate each of the arguments sequentially.",
18
                "BLOCK( {{arguments...}} )",
19
                null,
20
                "Object",
21
                false
19 22
        );
20 23
    }
21 24

  
......
26 29

  
27 30
    @Override
28 31
    public Object call(Interpreter interpreter, Object[] args) throws Exception {
29
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
32
        throw new UnsupportedOperationException("Not supported yet.");
30 33
    }
31 34
    
32 35
    @Override
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/function/obj/forFunction.java
14 14
                "FOR", 
15 15
                Range.is(4),
16 16
                "The for() function evaluate body and incr while the condition is true.",
17
                "FOR( init, condition, incr, body )"
17
                "FOR( init, condition, incr, body )",
18
                null,
19
                "Object",
20
                false
18 21
        );
19 22
    }
20 23

  
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/function/obj/WhileFunction.java
14 14
                "WHILE", 
15 15
                Range.is(2),
16 16
                "The while() function evaluate expression while the condition is true.",
17
                "WHILE( condition, expression )"
17
                "WHILE( condition, expression )",
18
                null,
19
                "Object",
20
                false
18 21
        );
19 22
    }
20 23

  
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/function/obj/InvokeMethodFunction.java
13 13

  
14 14
    public InvokeMethodFunction(Object obj, String methodName) {
15 15
        super(
16
            Function.GROUP_OTHER, "invokeMethod", Range.between(0, Integer.MAX_VALUE)
16
            Function.GROUP_OTHER, 
17
            "CALLM", 
18
            Range.between(0, Integer.MAX_VALUE),
19
            null,
20
            null,
21
            null,
22
            "Object",
23
            false
17 24
        );
18 25
        this.obj = obj;
19 26
        this.methodname = methodName;
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/function/operator/NegOperator.java
1
package org.gvsig.expressionevaluator.impl.function.operator;
2

  
3
import org.gvsig.expressionevaluator.Function;
4
import org.gvsig.expressionevaluator.Interpreter;
5

  
6

  
7
public class NegOperator extends AbstractUnaryOperator {
8

  
9
    public NegOperator() {
10
        super(Function.GROUP_NUMERIC,"NEGATE");   
11
    }
12

  
13
    @Override
14
    public boolean allowConstantFolding() {
15
        return true;
16
    }
17
    
18
    @Override
19
    public Object call(Interpreter interpreter, Object arg) {
20
        if( !(arg instanceof Number) ) {
21
            throw new IllegalArgumentException("NEGATE function require a number");
22
        }
23
        if( arg instanceof Double ) {
24
          return -((Number)arg).doubleValue();  
25
        } 
26
        if( arg instanceof Float ) {
27
          return -((Number)arg).floatValue();  
28
        } 
29
        if( arg instanceof Long ) {
30
          return -((Number)arg).longValue();  
31
        } 
32
        if( arg instanceof Integer ) {
33
          return -((Number)arg).intValue();  
34
        } 
35
        return -((Number)arg).doubleValue();  
36
    }
37
    
38
}
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/DefaultCodeBuilder.java
340 340
    }
341 341

  
342 342
    @Override
343
    public Code negate(Code op1) {
344
        return operator("negate", op1);
345
    }
346

  
347
    @Override
343 348
    public Code add(Code op1, Code op2) {
344 349
        return operator("+", op1, op2);
345 350
    }
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
1 1
package org.gvsig.expressionevaluator.impl;
2 2

  
3
import org.gvsig.expressionevaluator.ExpressionSyntaxException;
3 4
import org.gvsig.expressionevaluator.spi.AbstractLexicalAnalyzer;
4 5
import org.gvsig.expressionevaluator.LexicalAnalyzer;
5 6

  
......
62 63
        case '<':
63 64
            ch = getch();
64 65
            switch( ch ) {
66
            case EOF:
67
                throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this);
65 68
            case '>':
66 69
                token.set(Token.OP_NE, "<>");
67 70
                return token;
......
75 78

  
76 79
        case '>':
77 80
            ch = getch();
78
            if( ch == '=' ) {
81
            switch( ch ) {
82
            case EOF:
83
                throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this);
84
            case '=':
79 85
                token.set(Token.OP_GE, ">=");
80 86
                return token;
81 87
            }
......
96 102
            token.set(Token.PARENTHESIS_CLOSE, ")");
97 103
            return token;
98 104
        case '+':
99
            ch = getch();
100
            if( !Character.isDigit(ch) ) {
101
                ungetch();
102
                token.set(Token.OP_ADD, "+");
103
                return token;
104
            }
105
            ungetch();
106
            parseNumber();
107
            token.setLiteral("+" + token.getLiteral());
105
            token.set(Token.OP_ADD, "+");
108 106
            return token;
107
//            ch = getch();
108
//            if( ch == EOF ) {
109
//                throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this);
110
//            }
111
//            if( !Character.isDigit(ch) ) {
112
//                ungetch();
113
//                token.set(Token.OP_ADD, "+");
114
//                return token;
115
//            }
116
//            ungetch();
117
//            parseNumber();
118
//            token.setLiteral("+" + token.getLiteral());
119
//            return token;
109 120
        case '-':
110 121
            ch = getch();
111
            if( ch=='>' ) {
122
            switch( ch ) {
123
            case EOF:
124
                throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this);
125
            case '>':
112 126
                // SQL Extension to access object methods and attributes
113 127
                token.set(Token.OP_GETATTR, "->");
114 128
                return token;
115 129
            }
116
            if( Character.isDigit(ch) ) {
117
                ungetch();
118
                ungetch();
119
                parseNumber();
120
                return token;
121
            }
130
//            if( Character.isDigit(ch) ) {
131
//                ungetch();
132
//                ungetch();
133
//                parseNumber();
134
//                return token;
135
//            }
136
            ungetch();
122 137
            token.set(Token.OP_SUBST, "-");
123 138
            return token;
124 139

  
......
127 142
            ch = getch();
128 143
            while( ch != '"' ) {
129 144
                if( ch == EOF ) {
130
                    throw new RuntimeException("Found end of source and expected end of string");
145
                    throw new ExpressionSyntaxException(I18N.End_of_string_was_expected_and_end_of_source_was_found(), this);
131 146
                }
132 147
                buffer.add(ch);
133 148
                ch = getch();
134 149
            }
135 150
            if( buffer.length() < 1 ) {
136
                throw new RuntimeException();
151
                throw new ExpressionSyntaxException(I18N.Incorrect_string_length(), this);
137 152
            }
138 153
            token.set(Token.IDENTIFIER, buffer.toString());
139 154
            return token;
......
143 158
            ch = getch();
144 159
            while( ch != ']' ) {
145 160
                if( ch == EOF ) {
146
                    throw new RuntimeException("Found end of source and expected end of string");
161
                    throw new ExpressionSyntaxException(I18N.Closing_square_bracket_was_expected_and_end_of_source_was_found(), this);
147 162
                }
148 163
                buffer.add(ch);
149 164
                ch = getch();
150 165
            }
151 166
            if( buffer.length() < 1 ) {
152
                throw new RuntimeException();
167
                throw new ExpressionSyntaxException(I18N.Incorrect_identifier_length(), this);
153 168
            }
154 169
            token.set(Token.IDENTIFIER, buffer.toString());
155 170
            return token;
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/AbstractFunction.java
14 14
import org.gvsig.expressionevaluator.Code;
15 15
import org.gvsig.expressionevaluator.Code.Caller.Arguments;
16 16
import org.gvsig.expressionevaluator.Function;
17
import org.gvsig.expressionevaluator.I18N;
17 18
import org.gvsig.expressionevaluator.Interpreter;
18 19
import org.gvsig.fmap.geom.Geometry;
19 20
import org.gvsig.fmap.geom.primitive.Point;
......
141 142
    
142 143
    protected int getInt(Object args[], int n) {
143 144
        if( args.length < n  ) {
144
            throw new IllegalArgumentException("Required argument "+n+" and only found " + args.length +" arguments in call to '"+name()+"'.");
145
            throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n));
145 146
        }
146
        if( !(args[n] instanceof Number) ) {
147
            String type = (args[n]==null)? "null" : args[n].getClass().getCanonicalName();
148
            throw new IllegalArgumentException("Type not allowed for argument " + n + " in '" + name() + "' function, expected Number and got " + type + ".");
147
        Object value = args[n];
148
        if( value == null ) {
149
            throw new IllegalArgumentException(I18N.Illegal_null_value_for_argument_XargnX_of_XIdentifierX_function(name(), n));
149 150
        }
150
        return ((Number)args[n]).intValue();
151
        if( !(value instanceof Number) ) {
152
            String type = value.getClass().getCanonicalName();
153
            throw new IllegalArgumentException(
154
                    I18N.The_type_of_the_argument_XargnX_for_the_XIdentifierX_function_is_incorrect(name(), n) + " " +
155
                    I18N.Expected_XexpectedX_and_found_XfoundX("Number",type)
156
            );
157
        }
158
        return ((Number)value).intValue();
151 159
    }
152 160

  
153 161
    protected long getLong(Object args[], int n) {
154 162
        if( args.length < n  ) {
155
            throw new IllegalArgumentException("Required argument "+n+" and only found " + args.length +" arguments in call to '"+name()+"'.");
163
            throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n));
156 164
        }
157
        if( !(args[n] instanceof Number) ) {
158
            String type = (args[n]==null)? "null" : args[n].getClass().getCanonicalName();
159
            throw new IllegalArgumentException("Type not allowed for argument " + n + " in '" + name() + "' function, expected Number and got " + type + ".");
165
        Object value = args[n];
166
        if( value == null ) {
167
            throw new IllegalArgumentException(I18N.Illegal_null_value_for_argument_XargnX_of_XIdentifierX_function(name(), n));
160 168
        }
161
        return ((Number)args[n]).longValue();
169
        if( !(value instanceof Number) ) {
170
            String type = value.getClass().getCanonicalName();
171
            throw new IllegalArgumentException(
172
                    I18N.The_type_of_the_argument_XargnX_for_the_XIdentifierX_function_is_incorrect(name(), n) + " " +
173
                    I18N.Expected_XexpectedX_and_found_XfoundX("Number",type)
174
            );
175
        }
176
        return ((Number)value).longValue();
162 177
    }
163 178

  
164 179
    protected double getDouble(Object args[], int n) {
165 180
        if( args.length < n  ) {
166
            throw new IllegalArgumentException("Required argument "+n+" and only found " + args.length +" arguments in call to '"+name()+"'.");
181
            throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n));
167 182
        }
168
        if( !(args[n] instanceof Number) ) {
169
            String type = (args[n]==null)? "null" : args[n].getClass().getCanonicalName();
170
            throw new IllegalArgumentException("Type not allowed for argument " + n + " in '" + name() + "' function, expected Number and got " + type + ".");
183
        Object value = args[n];
184
        if( value == null ) {
185
            throw new IllegalArgumentException(I18N.Illegal_null_value_for_argument_XargnX_of_XIdentifierX_function(name(), n));
171 186
        }
172
        return ((Number)args[n]).doubleValue();
187
        if( !(value instanceof Number) ) {
188
            String type = value.getClass().getCanonicalName();
189
            throw new IllegalArgumentException(
190
                    I18N.The_type_of_the_argument_XargnX_for_the_XIdentifierX_function_is_incorrect(name(), n) + " " +
191
                    I18N.Expected_XexpectedX_and_found_XfoundX("Number",type)
192
            );
193
        }
194
        return ((Number)value).doubleValue();
173 195
    }
174 196
    
175 197
    protected String getStr(Object args[], int n) {
176 198
        if( args.length < n  ) {
177
            throw new IllegalArgumentException("Required argument "+n+" and only found " + args.length +" arguments in call to '"+name()+"'.");
199
            throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n));
178 200
        }
179 201
        return Objects.toString(args[n], "");
180 202
    }
181 203
    
182 204
    protected Object getObject(Object args[], int n) {
183 205
        if( args.length < n  ) {
184
            throw new IllegalArgumentException("Required argument "+n+" and only found " + args.length +" arguments in call to '"+name()+"'.");
206
            throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n));
185 207
        }
186 208
        return args[n];
187 209
    }
188 210
    
189 211
    protected Object getObject(Interpreter interpreter, Arguments args, int n) {
190 212
        if( args.count() < n  ) {
191
            throw new IllegalArgumentException("Required argument "+n+" and only found " + args.count() +" arguments in call to '"+name()+"'.");
213
            throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.count(), n));
192 214
        }
193 215
        Code arg = args.get(n);
194 216
        Object value = interpreter.run(arg);
......
196 218
    }
197 219
    
198 220
    protected Geometry getGeom(Object[] args, int n) {
221
        return this.getGeom(args, n, false);
222
    }
223
    
224
    protected Geometry getGeom(Object[] args, int n, boolean allowNull) {
199 225
        if( args.length < n  ) {
200
            throw new IllegalArgumentException("Required argument "+n+" and only found " + args.length +" arguments in call to '"+name()+"'.");
226
            throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n));
201 227
        }
202
        if( !(args[n] instanceof Geometry) ) {
203
            String type = (args[n]==null)? "null" : args[n].getClass().getCanonicalName();
204
            throw new IllegalArgumentException("Type not allowed for argument " + n + " in '" + name() + "' function, expected Geometry and got " + type + ".");
228
        Object value = args[n];
229
        if( value == null ) {
230
            if( allowNull ) {
231
                return null;
232
            }
233
            throw new IllegalArgumentException(I18N.Illegal_null_value_for_argument_XargnX_of_XIdentifierX_function(name(), n));
205 234
        }
206
        return (Geometry)args[n];
235
        if( !(value instanceof Geometry) ) {
236
            String type = value.getClass().getCanonicalName();
237
            throw new IllegalArgumentException(
238
                    I18N.The_type_of_the_argument_XargnX_for_the_XIdentifierX_function_is_incorrect(name(), n) + " " +
239
                    I18N.Expected_XexpectedX_and_found_XfoundX("Geometry",type)
240
            );
241
        }
242
        return (Geometry)value;
207 243
    }
208 244

  
209 245
    protected Point getPoint(Object[] args, int n) {
210 246
        if( args.length < n  ) {
211
            throw new IllegalArgumentException("Required argument "+n+" and only found " + args.length +" arguments in call to '"+name()+"'.");
247
            throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n));
212 248
        }
213
        if( !(args[n] instanceof Point) ) {
214
            String type = (args[n]==null)? "null" : args[n].getClass().getCanonicalName();
215
            throw new IllegalArgumentException("Type not allowed for argument " + n + " in '" + name() + "' function, expected Point and got " + type + ".");
249
        Object value = args[n];
250
        if( value == null ) {
251
            return null;
216 252
        }
217
        return (Point)args[n];
253
        if( !(value instanceof Point) ) {
254
            String type = value.getClass().getCanonicalName();
255
            throw new IllegalArgumentException(
256
                    I18N.The_type_of_the_argument_XargnX_for_the_XIdentifierX_function_is_incorrect(name(), n) + " " +
257
                    I18N.Expected_XexpectedX_and_found_XfoundX("Point",type)
258
            );
259
        }
260
        return (Point)value;
218 261
    }
219 262
    
220 263
    protected boolean getBoolean(Object args[], int n, Double accuracy) {
221 264
        if( args.length < n  ) {
222
            throw new IllegalArgumentException("Required argument "+n+" and only found " + args.length +" arguments in call to '"+name()+"'.");
265
            throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n));
223 266
        }
224 267
        Object value = args[n];
225 268
        return toBoolean(value, accuracy);
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
7 7
import java.util.Locale;
8 8
import java.util.Map;
9 9
import java.util.Stack;
10
import org.gvsig.expressionevaluator.ExpressionRuntimeException;
10 11
import org.gvsig.expressionevaluator.ExpressionSyntaxException;
12
import org.gvsig.expressionevaluator.I18N;
11 13
import org.gvsig.tools.lang.Cloneable;
12 14

  
13 15
public abstract class AbstractLexicalAnalyzer implements LexicalAnalyzer {
......
223 225
        char ch = getch();
224 226
        while (true) {
225 227
            if (ch == EOF) {
226
                throw new ExpressionSyntaxException("Found end of source and expected end of string", this);
228
                throw new ExpressionSyntaxException(I18N.End_of_string_was_expected_and_end_of_source_was_found(), this);
227 229
            }
228 230
            if (ch == '\'') {
229 231
                ch = getch();
......
245 247
        this.nfPos.setIndex(this.position);
246 248
        Number n = nf.parse(source, this.nfPos);
247 249
        if (this.nfPos.getIndex() == this.position) {
248
            throw new RuntimeException("Expected a number at position " + this.nfPos.getIndex() + ".");
250
            throw new ExpressionRuntimeException(I18N.Expected_a_number_at_position_XpositionX(this.nfPos.getIndex()));
249 251
        }
250 252
        String literal = source.substring(this.position, this.nfPos.getIndex());
251 253
        this.position = this.nfPos.getIndex();
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff