Revision 650 org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/src/main/java/org/gvsig/scripting/impl/DefaultScriptingScript.java

View differences:

DefaultScriptingScript.java
1 1
package org.gvsig.scripting.impl;
2 2

  
3
import com.sun.org.apache.xalan.internal.xsltc.runtime.BasisLibrary;
4
import java.io.BufferedReader;
5 3
import java.io.File;
6 4
import java.io.IOException;
7 5
import java.io.InputStream;
8
import java.io.InputStreamReader;
9 6
import java.util.List;
10 7

  
11 8
import javax.script.Compilable;
......
18 15
import org.apache.commons.io.FilenameUtils;
19 16
import org.apache.commons.io.IOUtils;
20 17
import org.apache.commons.lang3.StringUtils;
18
import org.apache.commons.lang3.exception.ExceptionUtils;
21 19
import org.gvsig.scripting.CompileErrorException;
22 20
import org.gvsig.scripting.ExecuteErrorException;
23 21
import org.gvsig.scripting.Main;
......
31 29
import org.gvsig.tools.observer.impl.DelegateWeakReferencingObservable;
32 30
import org.gvsig.tools.task.AbstractMonitorableTask;
33 31
import org.ini4j.Ini;
32
import org.python.core.PyException;
33
import org.python.core.PyString;
34
import org.python.core.PyTraceback;
34 35
import org.slf4j.Logger;
35 36
import org.slf4j.LoggerFactory;
36 37

  
......
323 324
                        this.compiledCode.eval();
324 325
                    }
325 326
                } catch (ScriptException e) {
326
                    CompileErrorException ce = new CompileErrorException(e.getMessage(), this.getName(), e.getLineNumber(), e.getColumnNumber(), e);
327
                    Object[] location = this.getLocation(e);
328
                    CompileErrorException ce = new CompileErrorException(
329
                            e.getMessage(), 
330
                            (File) location[0], 
331
                            (int) location[1], 
332
                            (int) location[2], 
333
                            e
334
                    );
327 335
                    notifyErrors(ce, "compile");
328 336
                    throw ce;
329 337
                } catch (Throwable e) {
330
                    CompileErrorException ce = new CompileErrorException(e.getMessage(), this.getName(), e);
338
                    CompileErrorException ce = new CompileErrorException(e.getMessage(), this.getScriptFile(), e);
331 339
                    notifyErrors(new Exception(e), "compile");
332 340
                    throw ce;
333 341
                }
......
336 344
                try {
337 345
                    engine.eval(code);
338 346
                } catch (ScriptException e) {
339
                    CompileErrorException ce = new CompileErrorException(e.getMessage(), this.getName(), e.getLineNumber(), e.getColumnNumber(), e);
347
                    Object[] location = this.getLocation(e);
348
                    CompileErrorException ce = new CompileErrorException(
349
                            e.getMessage(), 
350
                            (File) location[0], 
351
                            (int) location[1], 
352
                            (int) location[2], 
353
                            e
354
                    );                    
340 355
                    notifyErrors(ce, "compile");
341 356
                    throw ce;
342 357
                }
......
406 421
                return null;
407 422
            }
408 423
        } catch (ScriptException e) {
409
            ExecuteErrorException ee = new ExecuteErrorException(e.getMessage(), this.getName(), e.getLineNumber(), e.getColumnNumber(), e);
424
            Object[] location = this.getLocation(e);
425
            ExecuteErrorException ee = new ExecuteErrorException(
426
                    e.getMessage(), 
427
                    (File) location[0], 
428
                    (int) location[1], 
429
                    (int) location[2], 
430
                    e
431
            );
410 432
            notifyErrors(ee, "invoke");
411 433
            throw ee;
412 434

  
413 435
        } catch (Error | Exception e) {
414
            ExecuteErrorException ee = new ExecuteErrorException(e.getMessage(), this.getName(), e);
436
            ExecuteErrorException ee = new ExecuteErrorException(e.getMessage(), this.getScriptFile(), e);
415 437
            notifyErrors(ee, "invoke");
416 438
            throw ee;
417 439
        }
418 440
    }
419 441

  
420 442
    @Override
443
    public File getScriptFile() {
444
        return this.getFileResource(extension);
445
    }
446

  
447
    private Object[] getLocation(ScriptException e) {
448
        Throwable[] es = ExceptionUtils.getThrowables(e);
449
        Exception dbgex; // Para debug con mas comodidad
450
        for( Throwable t : es) {
451
            if( t instanceof PyException ) {
452
                try {
453
                    PyException pyex = (PyException)t;
454
                    PyTraceback tb = pyex.traceback;
455
                    if( tb!=null ) {
456
                        while( tb.tb_next!=null ) {
457
                            tb = (PyTraceback) tb.tb_next;
458
                        }
459
                        String s = tb.tb_frame.f_globals.__getitem__(new PyString("__file__")).asString();
460
                        if( s.endsWith("$py.class") ) {
461
                            s = s.substring(0, s.length()-9) + ".py";
462
                            File resource = new File(s);
463
                            return new Object[] { resource, tb.tb_lineno, 0};
464
                        }
465
                        return new Object[] { this.getScriptFile(), tb.tb_lineno, 0};
466
                    }
467
                } catch(Exception ex) {
468
                    // Pass
469
                    dbgex = ex;
470
                }
471
            }
472
        }        
473
        int column = e.getColumnNumber();
474
        if( column < 0 ) {
475
            column = 0;
476
        }
477
        return new Object[] { this.getScriptFile(), e.getLineNumber(), column };
478
    }
479
    
480
    
481
    @Override
421 482
    public Object invokeMethod(final Object obj, final String name, Object[] args)
422 483
            throws NoSuchMethodException {
423 484

  
......
430 491
            try {
431 492
                return invocable.invokeMethod(obj, name, args);
432 493
            } catch (ScriptException e) {
433
                ExecuteErrorException ee = new ExecuteErrorException(e.getMessage(), this.getName(), e.getLineNumber(), e.getColumnNumber(), e);
494
                ExecuteErrorException ee = new ExecuteErrorException(e.getMessage(), this.getScriptFile(), e.getLineNumber(), e.getColumnNumber(), e);
434 495
                notifyErrors(ee, "invoke");
435 496
                throw ee;
436 497
            } catch (Throwable e) {
437
                ExecuteErrorException ee = new ExecuteErrorException(e.getMessage(), this.getName(), e);
498
                ExecuteErrorException ee = new ExecuteErrorException(e.getMessage(), this.getScriptFile(), e);
438 499
                notifyErrors(ee, "invoke");
439 500
                throw ee;
440 501
            }

Also available in: Unified diff