Revision 44392

View differences:

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
1 1
package org.gvsig.expressionevaluator.impl;
2 2

  
3
import java.io.InputStream;
4 3
import java.io.StringWriter;
5 4
import java.net.URI;
6
import java.net.URL;
7 5
import org.gvsig.expressionevaluator.Grammar;
8 6
import org.gvsig.expressionevaluator.GrammarFactory;
9 7
import java.util.Collection;
10 8
import java.util.HashMap;
11 9
import java.util.Map;
12 10
import java.util.Objects;
11
import java.util.regex.Matcher;
12
import java.util.regex.Pattern;
13 13
import org.apache.commons.io.FilenameUtils;
14 14
import org.apache.commons.io.IOUtils;
15 15
import org.apache.commons.lang3.ArrayUtils;
......
27 27
import org.gvsig.expressionevaluator.SymbolTableFactory;
28 28
import org.gvsig.expressionevaluator.Compiler;
29 29
import org.gvsig.expressionevaluator.GrammarSet;
30
import org.gvsig.expressionevaluator.impl.javascripting.CosaScriptEngineFactory;
31 30
import org.gvsig.tools.bookmarksandhistory.Bookmarks;
32 31
import org.gvsig.tools.bookmarksandhistory.History;
33 32
import org.gvsig.tools.bookmarksandhistory.impl.BaseBookmarks;
34 33
import org.gvsig.tools.bookmarksandhistory.impl.BaseHistory;
34
import org.gvsig.tools.resourcesstorage.ResourcesStorage;
35 35
import org.gvsig.tools.script.Script;
36 36
import org.slf4j.Logger;
37 37
import org.slf4j.LoggerFactory;
......
256 256
    @Override
257 257
    public Bookmarks<Expression> getBookmarks() {
258 258
        if( this.bookmarks==null ) {
259
            this.bookmarks = new BaseBookmarks<Expression>();
259
            this.bookmarks = new BaseBookmarks<>();
260 260
        }
261 261
        return this.bookmarks;
262 262
    }
......
264 264
    @Override
265 265
    public History<Expression> getHistory() {
266 266
        if( this.history==null ) {
267
            this.history = new BaseHistory<Expression>(20);
267
            this.history = new BaseHistory<>(20);
268 268
        }
269 269
        return this.history;
270 270
    }
......
281 281
    }
282 282

  
283 283
    @Override
284
    public Script loadScript(URI location) {
285
        InputStream is = null;
286
        String code = null;
287
        String name = null;
284
    public Script loadScript(final URI location) {
285
        ResourcesStorage.Resource res = null;
288 286
        try {
289
            URL url = location.toURL();
290
            is = url.openStream();
291
            code = IOUtils.toString(is);
292
            name = FilenameUtils.getBaseName(location.getPath());
293
        } catch(Exception ex) {
287
            if( location==null ) {
288
                return null;
289
            }
290
            res = ResourcesStorage.createResource(location);
291
            if( res == null || !res.exists() ) {
292
                return null;
293
            }
294
            Script script = loadScript(res, FilenameUtils.getBaseName(location.getPath()));
295
            return script;
296
        } catch (Exception ex) {
297
            LOGGER.warn("Can't load script from URI.", ex);
294 298
            return null;
295 299
        } finally {
296
            IOUtils.closeQuietly(is);
300
            IOUtils.closeQuietly(res);
297 301
        }
298
        return this.createScript(name, code, CosaScriptEngineFactory.LANGUAGE_NAME);
299 302
    }
303

  
304

  
305
    @Override
306
    public Script loadScript(ResourcesStorage storage, String name) {
307
        ResourcesStorage.Resource res = null;
308
        try {
309
            if( storage==null ) {
310
                return null;
311
            }
312
            res = storage.getResource(name);
313
            if( res == null || !res.exists() ) {
314
                return null;
315
            }
316
            Script script = loadScript(res, name);
317
            return script;
318
        } catch (Exception ex) {
319
            LOGGER.warn("Can't load script from resources storage.", ex);
320
            return null;
321
        } finally {
322
            IOUtils.closeQuietly(res);
323
        }
324

  
325
    }
326

  
327
    private static final Pattern RE_LANG = Pattern.compile(".*lang[:]\\s*([a-zA-Z_][a-zA-Z_0-9]*)");
328
    private static final Pattern RE_ENCODING = Pattern.compile(".*encoding[:]\\s*([a-zA-Z_][a-zA-Z_0-9]*)");
329
    
330
    private Script loadScript(ResourcesStorage.Resource res, String name) {
331
        try {
332
            if( res == null || !res.exists() ) {
333
                return null;
334
            }
335
            byte[] head_bytes = new byte[500];
336
            IOUtils.read(res.asInputStream(), head_bytes);
337
            IOUtils.closeQuietly(res);
338
            String head = new String(head_bytes);
339
            if( StringUtils.isEmpty(head) ) {
340
                return null;
341
            }
342
            head = StringUtils.split("\n")[0];
300 343
            
344
            String lang = "cosa";
345
            String encoding = null;
346
            Matcher m = RE_LANG.matcher(head);
347
            if( m.groupCount()==1 ) {
348
                String s = m.group(1);
349
                if( !StringUtils.isBlank(s) ) {
350
                    lang = s;
351
                }
352
            }
353
            m = RE_ENCODING.matcher(head);
354
            if( m.groupCount()==1 ) {
355
                String s = m.group(1);
356
                if( !StringUtils.isBlank(s) ) {
357
                    encoding = s;
358
                }
359
            }
360

  
361
            String source;
362
            if( StringUtils.isBlank(encoding) ) {
363
                source = IOUtils.toString(res.asInputStream());
364
            } else {
365
                source = IOUtils.toString(res.asInputStream(), encoding);
366
            }
367
            Script script = this.createScript(name, source, lang);
368
            return script;
369
        } catch (Exception ex) {
370
            LOGGER.warn("Can't load script from resource.", ex);
371
            return null;
372
        } finally {
373
            IOUtils.closeQuietly(res);
374
        }
375

  
376
    }
301 377
}
302 378

  

Also available in: Unified diff