From cd3dc048809ae64c11dc20d5b50ab66867196e0d Mon Sep 17 00:00:00 2001 From: ilasica Date: Mon, 9 Feb 2015 10:25:18 +0300 Subject: [PATCH] Add beanshell use. Use new optional field RULE in table INFO. Value "beanshell" makes to use interpreter for processing of a script which put into the field URL. If there is no field RULE or its value differs from "beanshell", content of the field URL used as before. --- .../src/net/osmand/map/TileSourceManager.java | 2 +- .../src/net/osmand/plus/SQLiteTileSource.java | 26 ++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/OsmAnd-java/src/net/osmand/map/TileSourceManager.java b/OsmAnd-java/src/net/osmand/map/TileSourceManager.java index 0f9c220ac9..154c55609b 100644 --- a/OsmAnd-java/src/net/osmand/map/TileSourceManager.java +++ b/OsmAnd-java/src/net/osmand/map/TileSourceManager.java @@ -30,7 +30,7 @@ import bsh.Interpreter; public class TileSourceManager { private static final Log log = PlatformUtil.getLog(TileSourceManager.class); - private static final String RULE_BEANSHELL = "beanshell"; + public static final String RULE_BEANSHELL = "beanshell"; public static final String RULE_YANDEX_TRAFFIC = "yandex_traffic"; private static final String RULE_WMS = "wms_tile"; diff --git a/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java b/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java index 9fad66f85c..748706837f 100644 --- a/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java +++ b/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java @@ -12,6 +12,7 @@ import net.osmand.IndexConstants; import net.osmand.PlatformUtil; import net.osmand.data.QuadRect; import net.osmand.map.ITileSource; +import net.osmand.map.TileSourceManager; import net.osmand.map.TileSourceManager.TileSourceTemplate; import net.osmand.plus.api.SQLiteAPI.SQLiteConnection; import net.osmand.plus.api.SQLiteAPI.SQLiteCursor; @@ -19,6 +20,7 @@ import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; +import bsh.Interpreter; import android.database.sqlite.SQLiteDiskIOException; import android.graphics.Bitmap; import android.graphics.BitmapFactory; @@ -33,7 +35,7 @@ public class SQLiteTileSource implements ITileSource { private ITileSource base; private String urlTemplate = null; private String name; - private SQLiteConnection db; + private SQLiteConnection db = null; private final File file; private int minZoom = 1; private int maxZoom = 17; @@ -41,7 +43,8 @@ public class SQLiteTileSource implements ITileSource { private boolean timeSupported = false; private int expirationTimeMillis = -1; // never private boolean isEllipsoid = false; - + private String rule = null; + static final int tileSize = 256; private OsmandApplication ctx; private boolean onlyReadonlyAvailable = false; @@ -101,6 +104,7 @@ public class SQLiteTileSource implements ITileSource { return base != null ? base.getTileSize() : tileSize; } + Interpreter bshInterpreter = null; @Override public String getUrlToLoad(int x, int y, int zoom) { if (zoom > maxZoom) @@ -109,7 +113,22 @@ public class SQLiteTileSource implements ITileSource { if(db == null || db.isReadOnly() || urlTemplate == null){ return null; } - return MessageFormat.format(urlTemplate, zoom+"", x+"", y+""); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ + + if(TileSourceManager.RULE_BEANSHELL.equalsIgnoreCase(rule)){ + try { + if(bshInterpreter == null){ + bshInterpreter = new Interpreter(); + bshInterpreter.eval(urlTemplate); + } + return (String) bshInterpreter.eval("getTileUrl("+zoom+","+x+","+y+");"); + } catch (bsh.EvalError e) { + log.error(e.getMessage(), e); + return null; + } + } + else { + return MessageFormat.format(urlTemplate, zoom+"", x+"", y+""); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ + } } @Override @@ -414,6 +433,7 @@ public class SQLiteTileSource implements ITileSource { } public void closeDB(){ + bshInterpreter = null; if(db != null){ db.close(); db = null;