From 74a3a2d75eb9cb0b3476659076540d4dd31b346e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rg=20Prante?= Date: Fri, 22 Nov 2019 14:56:38 +0100 Subject: [PATCH] fix loop in loading settings from map --- build.gradle | 9 ++- .../settings/AbstractSettingsLoader.java | 74 +++++++++++-------- .../settings/PropertiesSettingsLoader.java | 27 +++++++ .../org/xbib/content/settings/Settings.java | 7 +- .../xbib/content/settings/SettingsLoader.java | 5 ++ .../xbib/content/settings/SettingsTest.java | 19 +++++ gradle.properties | 2 +- gradle/wrapper/gradle-wrapper.properties | 4 +- 8 files changed, 106 insertions(+), 41 deletions(-) diff --git a/build.gradle b/build.gradle index bdae363..379fe31 100644 --- a/build.gradle +++ b/build.gradle @@ -184,8 +184,9 @@ subprojects { } } } - - nexusStaging { - packageGroup = "org.xbib" - } +} + +nexusStaging { + packageGroup = "org.xbib" + //stagingProfileId = "org.xbib" } diff --git a/content-core/src/main/java/org/xbib/content/settings/AbstractSettingsLoader.java b/content-core/src/main/java/org/xbib/content/settings/AbstractSettingsLoader.java index a24aff7..fd59b43 100644 --- a/content-core/src/main/java/org/xbib/content/settings/AbstractSettingsLoader.java +++ b/content-core/src/main/java/org/xbib/content/settings/AbstractSettingsLoader.java @@ -1,6 +1,7 @@ package org.xbib.content.settings; import org.xbib.content.XContent; +import org.xbib.content.XContentBuilder; import org.xbib.content.XContentGenerator; import org.xbib.content.XContentParser; import org.xbib.content.io.BytesReference; @@ -8,7 +9,7 @@ import org.xbib.content.io.BytesStreamOutput; import java.io.IOException; import java.util.ArrayList; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -33,29 +34,16 @@ public abstract class AbstractSettingsLoader implements SettingsLoader { } } - public String flatMapAsString(BytesReference bytesReference) throws IOException { - try (XContentParser parser = content().createParser(bytesReference); - BytesStreamOutput bytesStreamOutput = new BytesStreamOutput(); - XContentGenerator generator = content().createGenerator(bytesStreamOutput)) { - generator.writeStartObject(); - for (Map.Entry entry : load(parser).entrySet()) { - generator.writeFieldName(entry.getKey()); - String value = entry.getValue(); - if (value == null) { - generator.writeNull(); - } else { - generator.writeString(value); - } - } - generator.writeEndObject(); - generator.flush(); - return bytesStreamOutput.bytes().toUtf8(); - } + @Override + public Map load(Map map) throws IOException { + XContentBuilder builder = XContentBuilder.builder(content()); + builder.map(map); + return load(builder.string()); } public Map load(XContentParser xContentParser) throws IOException { StringBuilder sb = new StringBuilder(); - Map map = new HashMap<>(); + Map map = new LinkedHashMap<>(); List path = new ArrayList<>(); XContentParser.Token token = xContentParser.nextToken(); if (token == null) { @@ -65,7 +53,33 @@ public abstract class AbstractSettingsLoader implements SettingsLoader { return map; } - private void parseObject(Map settings, StringBuilder sb, List path, + public String flatMapAsString(BytesReference bytesReference) throws IOException { + try (XContentParser parser = content().createParser(bytesReference); + BytesStreamOutput bytesStreamOutput = new BytesStreamOutput(); + XContentGenerator generator = content().createGenerator(bytesStreamOutput)) { + return flatMapAsString(parser, bytesStreamOutput, generator); + } + } + + private String flatMapAsString(XContentParser parser, + BytesStreamOutput bytesStreamOutput, + XContentGenerator generator) throws IOException { + generator.writeStartObject(); + for (Map.Entry entry : load(parser).entrySet()) { + generator.writeFieldName(entry.getKey()); + String value = entry.getValue(); + if (value == null) { + generator.writeNull(); + } else { + generator.writeString(value); + } + } + generator.writeEndObject(); + generator.flush(); + return bytesStreamOutput.bytes().toUtf8(); + } + + private void parseObject(Map map, StringBuilder sb, List path, XContentParser parser, String objFieldName) throws IOException { if (objFieldName != null) { path.add(objFieldName); @@ -75,13 +89,13 @@ public abstract class AbstractSettingsLoader implements SettingsLoader { XContentParser.Token token; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.START_OBJECT) { - parseObject(settings, sb, path, parser, currentFieldName); + parseObject(map, sb, path, parser, currentFieldName); } else if (token == XContentParser.Token.START_ARRAY) { - parseArray(settings, sb, path, parser, currentFieldName); + parseArray(map, sb, path, parser, currentFieldName); } else if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else { - parseValue(settings, sb, path, parser, currentFieldName); + parseValue(map, sb, path, parser, currentFieldName); } } @@ -90,31 +104,31 @@ public abstract class AbstractSettingsLoader implements SettingsLoader { } } - private void parseArray(Map settings, StringBuilder sb, List path, + private void parseArray(Map map, StringBuilder sb, List path, XContentParser parser, String name) throws IOException { XContentParser.Token token; int counter = 0; String fieldName = name; while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { if (token == XContentParser.Token.START_OBJECT) { - parseObject(settings, sb, path, parser, fieldName + '.' + (counter++)); + parseObject(map, sb, path, parser, fieldName + '.' + (counter++)); } else if (token == XContentParser.Token.START_ARRAY) { - parseArray(settings, sb, path, parser, fieldName + '.' + (counter++)); + parseArray(map, sb, path, parser, fieldName + '.' + (counter++)); } else if (token == XContentParser.Token.FIELD_NAME) { fieldName = parser.currentName(); } else { - parseValue(settings, sb, path, parser, fieldName + '.' + (counter++)); + parseValue(map, sb, path, parser, fieldName + '.' + (counter++)); } } } - private void parseValue(Map settings, StringBuilder sb, List path, + private void parseValue(Map map, StringBuilder sb, List path, XContentParser parser, String fieldName) throws IOException { sb.setLength(0); for (String s : path) { sb.append(s).append('.'); } sb.append(fieldName); - settings.put(sb.toString(), parser.text()); + map.put(sb.toString(), parser.text()); } } diff --git a/content-core/src/main/java/org/xbib/content/settings/PropertiesSettingsLoader.java b/content-core/src/main/java/org/xbib/content/settings/PropertiesSettingsLoader.java index 2fac5bf..afa0b18 100644 --- a/content-core/src/main/java/org/xbib/content/settings/PropertiesSettingsLoader.java +++ b/content-core/src/main/java/org/xbib/content/settings/PropertiesSettingsLoader.java @@ -1,6 +1,9 @@ package org.xbib.content.settings; +import org.xbib.content.io.BytesReference; import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; import java.io.StringReader; import java.util.Collections; import java.util.HashMap; @@ -34,6 +37,30 @@ public class PropertiesSettingsLoader implements SettingsLoader { } } + @Override + public Map load(BytesReference ref) throws IOException { + Properties props = new Properties(); + try (Reader reader = new InputStreamReader(ref.streamInput())) { + props.load(reader); + Map result = new HashMap<>(); + for (Map.Entry entry : props.entrySet()) { + result.put((String) entry.getKey(), (String) entry.getValue()); + } + return result; + } + } + + @Override + public Map load(Map source) throws IOException { + Properties props = new Properties(); + props.putAll(source); + Map result = new HashMap<>(); + for (Map.Entry entry : props.entrySet()) { + result.put((String) entry.getKey(), (String) entry.getValue()); + } + return result; + } + @Override public boolean canLoad(String source) { return true; diff --git a/content-core/src/main/java/org/xbib/content/settings/Settings.java b/content-core/src/main/java/org/xbib/content/settings/Settings.java index c032a0d..df1ad88 100644 --- a/content-core/src/main/java/org/xbib/content/settings/Settings.java +++ b/content-core/src/main/java/org/xbib/content/settings/Settings.java @@ -3,9 +3,7 @@ package org.xbib.content.settings; import static org.xbib.content.util.unit.ByteSizeValue.parseBytesSizeValue; import static org.xbib.content.util.unit.TimeValue.parseTimeValue; -import org.xbib.content.XContentBuilder; import org.xbib.content.json.JsonSettingsLoader; -import org.xbib.content.json.JsonXContent; import org.xbib.content.util.unit.ByteSizeValue; import org.xbib.content.util.unit.TimeValue; @@ -614,8 +612,9 @@ public class Settings { * @return builder */ public Builder loadFromMap(Map map) { - try (XContentBuilder builder = JsonXContent.contentBuilder()) { - put(new JsonSettingsLoader().load(builder.map(map).string())); + SettingsLoader settingsLoader = new JsonSettingsLoader(); + try { + put(settingsLoader.load(map)); } catch (Exception e) { throw new SettingsException("Failed to load settings from [" + map + "]", e); } diff --git a/content-core/src/main/java/org/xbib/content/settings/SettingsLoader.java b/content-core/src/main/java/org/xbib/content/settings/SettingsLoader.java index 210885c..2b12a79 100644 --- a/content-core/src/main/java/org/xbib/content/settings/SettingsLoader.java +++ b/content-core/src/main/java/org/xbib/content/settings/SettingsLoader.java @@ -1,5 +1,6 @@ package org.xbib.content.settings; +import org.xbib.content.io.BytesReference; import java.io.IOException; import java.util.Map; import java.util.Set; @@ -24,5 +25,9 @@ public interface SettingsLoader { */ Map load(String source) throws IOException; + Map load(BytesReference bytesReference) throws IOException; + + Map load(Map source) throws IOException; + boolean canLoad(String source); } diff --git a/content-core/src/test/java/org/xbib/content/settings/SettingsTest.java b/content-core/src/test/java/org/xbib/content/settings/SettingsTest.java index 8ca2f8b..4ac3452 100644 --- a/content-core/src/test/java/org/xbib/content/settings/SettingsTest.java +++ b/content-core/src/test/java/org/xbib/content/settings/SettingsTest.java @@ -15,6 +15,7 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.logging.Logger; @@ -156,4 +157,22 @@ public class SettingsTest extends Assert { String result = loader.flatMapAsString(ref); assertEquals("{\"a.b\":\"c\"}", result); } + + @Test + public void testLoadFromMap() throws IOException { + Map map = new LinkedHashMap<>(); + Map code = new LinkedHashMap<>(); + code.put("a", "b"); + code.put("b", "c"); + Map name = new LinkedHashMap<>(); + name.put("a", "b"); + name.put("b", "c"); + List list = Arrays.asList("a","b"); + map.put("code", code); + map.put("name", name); + map.put("list", list); + JsonSettingsLoader loader = new JsonSettingsLoader(); + Map result = loader.load(map); + assertEquals("{code.a=b, code.b=c, name.a=b, name.b=c, list.0=a, list.1=b}", result.toString()); + } } diff --git a/gradle.properties b/gradle.properties index ef15143..c0f98ce 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ group = org.xbib name = content -version = 2.0.4 +version = 2.0.5 xbib-net.version = 2.0.3 jackson.version = 2.9.10 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 9f1c64e..2f9381c 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ -#Sun Sep 15 22:30:22 CEST 2019 -distributionUrl=https\://services.gradle.org/distributions/gradle-5.6-all.zip +#Fri Nov 22 14:55:55 CET 2019 +distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStorePath=wrapper/dists