From bbade0b63aa4ae22fc0f38455a878ebdf00d6da4 Mon Sep 17 00:00:00 2001 From: RZR-UA Date: Wed, 2 Oct 2024 18:02:14 +0200 Subject: [PATCH] Add initFromResources readRenderingConstantsFromIS --- .../osmand/render/RenderingRulesStorage.java | 62 +++++++++++-------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/render/RenderingRulesStorage.java b/OsmAnd-java/src/main/java/net/osmand/render/RenderingRulesStorage.java index db619c6dece..e1ec312ccc1 100644 --- a/OsmAnd-java/src/main/java/net/osmand/render/RenderingRulesStorage.java +++ b/OsmAnd-java/src/main/java/net/osmand/render/RenderingRulesStorage.java @@ -71,7 +71,31 @@ public String getDependsName() { public interface RenderingRulesStorageResolver { RenderingRulesStorage resolve(String name, RenderingRulesStorageResolver ref) throws XmlPullParserException, IOException; } - + + public static RenderingRulesStorage initFromResources(String styleName, String resourceName) { + try { + final RenderingRulesStorage storage = new RenderingRulesStorage(styleName, + readRenderingConstantsFromIS(RenderingRulesStorage.class.getResourceAsStream(resourceName))); + final RenderingRulesStorageResolver resolver = (name, ref) -> { + final String resource = name + ".render.xml"; + final RenderingRulesStorage depends = new RenderingRulesStorage(name, + readRenderingConstantsFromIS(RenderingRulesStorage.class.getResourceAsStream(resource))); + final InputStream depStream = RenderingRulesStorage.class.getResourceAsStream(resource); + depends.parseRulesFromXmlInputStream(depStream, ref, false); + depStream.close(); + return depends; + }; + final InputStream xmlStream = RenderingRulesStorage.class.getResourceAsStream(resourceName); + storage.parseRulesFromXmlInputStream(xmlStream, resolver, false); + xmlStream.close(); + return storage; + } catch (XmlPullParserException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + public RenderingRulesStorage(String name, Map renderingConstants){ getDictionaryValue(""); this.renderingName = name; @@ -616,26 +640,9 @@ public static void main(String[] args) throws XmlPullParserException, IOExceptio styleName = "test"; styleFile = new File(stylesDir, styleName +".render.xml"); } - final Map renderingConstants = new LinkedHashMap(); + + final Map renderingConstants = readRenderingConstantsFromIS(defaultIS); - try { - XmlPullParser parser = PlatformUtil.newXMLPullParser(); - parser.setInput(defaultIS, "UTF-8"); - int tok; - while ((tok = parser.next()) != XmlPullParser.END_DOCUMENT) { - if (tok == XmlPullParser.START_TAG) { - String tagName = parser.getName(); - if (tagName.equals("renderingConstant")) { - if (!renderingConstants.containsKey(parser.getAttributeValue("", "name"))) { - renderingConstants.put(parser.getAttributeValue("", "name"), - parser.getAttributeValue("", "value")); - } - } - } - } - } finally { - defaultIS.close(); - } RenderingRulesStorage storage = new RenderingRulesStorage(styleName, renderingConstants); final RenderingRulesStorageResolver resolver = new RenderingRulesStorageResolver() { @Override @@ -699,12 +706,8 @@ protected static void testSearch(RenderingRulesStorage storage) { // System.out.println((System.nanoTime()- tm)/ (1e6f * count) ); } - public static RenderingRulesStorage getTestStorageForStyle(String filePath) throws XmlPullParserException, IOException { - RenderingRulesStorage.STORE_ATTRIBUTES = true; + public static Map readRenderingConstantsFromIS(InputStream is) throws XmlPullParserException, IOException { Map renderingConstants = new LinkedHashMap(); - - InputStream is = new FileInputStream(filePath); - // buggy attributes try { XmlPullParser parser = PlatformUtil.newXMLPullParser(); parser.setInput(is, "UTF-8"); @@ -723,6 +726,15 @@ public static RenderingRulesStorage getTestStorageForStyle(String filePath) thro } finally { is.close(); } + return renderingConstants; + } + + public static RenderingRulesStorage getTestStorageForStyle(String filePath) throws XmlPullParserException, IOException { + RenderingRulesStorage.STORE_ATTRIBUTES = true; + + InputStream is = new FileInputStream(filePath); + Map renderingConstants = readRenderingConstantsFromIS(is); + is = new FileInputStream(filePath); RenderingRulesStorage storage = new RenderingRulesStorage("default", renderingConstants); final RenderingRulesStorageResolver resolver = new RenderingRulesStorageResolver() {