Skip to content

Commit

Permalink
Merge pull request #167 from Gmugra/163_localization_support_for_erro…
Browse files Browse the repository at this point in the history
…r_messages

#163 : toml/json: Localization support for error messages
  • Loading branch information
Gmugra authored Jun 4, 2021
2 parents e59a85e + 02b9489 commit 46cd43c
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.logging.Logger;

import net.cactusthorn.config.core.loader.Loader;
import net.cactusthorn.config.extras.json.util.JSONToMapParser;

public class ClasspathJSONLoader implements Loader {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.logging.Logger;

import net.cactusthorn.config.core.loader.Loader;
import net.cactusthorn.config.extras.json.util.JSONToMapParser;

public class UrlJSONLoader implements Loader {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.cactusthorn.config.extras.json;
package net.cactusthorn.config.extras.json.util;

import static net.cactusthorn.config.extras.json.util.JsonMessages.msg;
import static net.cactusthorn.config.extras.json.util.JsonMessages.Key.ROOT_OBJECT;
import static net.cactusthorn.config.extras.json.util.JsonMessages.Key.OBJECTS_IN_ARRAY;
import static net.cactusthorn.config.extras.json.util.JsonMessages.Key.ARRAYS_IN_ARRAY;

import java.io.IOException;
import java.io.Reader;
Expand All @@ -43,7 +48,7 @@ public Map<String, String> parse(Reader reader) throws IOException {
return Collections.emptyMap();
}
if (!rootElement.isJsonObject()) {
throw new UnsupportedOperationException("root must be object");
throw new UnsupportedOperationException(msg(ROOT_OBJECT));
}
Map<String, String> result = new HashMap<>();
processObjectElement(new ArrayDeque<>(), result, rootElement.getAsJsonObject());
Expand Down Expand Up @@ -74,10 +79,10 @@ private void processArrayElement(Deque<String> key, Map<String, String> result,
continue;
}
if (arrayElement.isJsonObject()) {
throw new UnsupportedOperationException(keyAsString + " - objects in array are not supported");
throw new UnsupportedOperationException(msg(OBJECTS_IN_ARRAY, keyAsString));
}
if (arrayElement.isJsonArray()) {
throw new UnsupportedOperationException(keyAsString + " - arrays in array are not supported");
throw new UnsupportedOperationException(msg(ARRAYS_IN_ARRAY, keyAsString));
}
joiner.add(arrayElement.getAsString());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2021, Alexei Khatskevich
*
* Licensed under the BSD 3-Clause license.
* You may obtain a copy of the License at
*
* https://github.com/Gmugra/net.cactusthorn.config/blob/main/LICENSE
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.cactusthorn.config.extras.json.util;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

public final class JsonMessages {

private static final String BANDLE = JsonMessages.class.getName();
private static final ResourceBundle MESSAGES = ResourceBundle.getBundle(BANDLE, Locale.getDefault());

private JsonMessages() {
}

public enum Key {
ROOT_OBJECT, OBJECTS_IN_ARRAY, ARRAYS_IN_ARRAY
}

public static String msg(Key key) {
return MESSAGES.getString(key.name());
}

public static String msg(Key key, Object argument) {
return MessageFormat.format(MESSAGES.getString(key.name()), argument);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ROOT_OBJECT=root must be object
OBJECTS_IN_ARRAY={0} - objects in array are not supported
ARRAYS_IN_ARRAY={0} - arrays in array are not supported
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.cactusthorn.config.extras.json;
package net.cactusthorn.config.extras.json.util;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.logging.Logger;

import net.cactusthorn.config.core.loader.Loader;
import net.cactusthorn.config.extras.toml.util.TOMLToMapParser;

public class ClasspathTOMLLoader implements Loader {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.logging.Logger;

import net.cactusthorn.config.core.loader.Loader;
import net.cactusthorn.config.extras.toml.util.TOMLToMapParser;

public class UrlTOMLLoader implements Loader {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.cactusthorn.config.extras.toml;
package net.cactusthorn.config.extras.toml.util;

import static net.cactusthorn.config.extras.toml.util.TomlMessages.msg;
import static net.cactusthorn.config.extras.toml.util.TomlMessages.Key.ARRAYS_IN_ARRAY;
import static net.cactusthorn.config.extras.toml.util.TomlMessages.Key.TABLES_IN_ARRAY;

import java.io.IOException;
import java.io.Reader;
Expand Down Expand Up @@ -60,10 +64,10 @@ private String convertArray(String key, TomlArray tomlArray) {
return null;
}
if (tomlArray.containsArrays()) {
throw new UnsupportedOperationException(key + " - arrays in array are not supported");
throw new UnsupportedOperationException(msg(ARRAYS_IN_ARRAY, key));
}
if (tomlArray.containsTables()) {
throw new UnsupportedOperationException(key + " - tables in array are not supported");
throw new UnsupportedOperationException(msg(TABLES_IN_ARRAY, key));
}
return tomlArray.toList().stream().map(o -> o.toString()).collect(Collectors.joining(","));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2021, Alexei Khatskevich
*
* Licensed under the BSD 3-Clause license.
* You may obtain a copy of the License at
*
* https://github.com/Gmugra/net.cactusthorn.config/blob/main/LICENSE
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.cactusthorn.config.extras.toml.util;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

public final class TomlMessages {

private static final String BANDLE = TomlMessages.class.getName();
private static final ResourceBundle MESSAGES = ResourceBundle.getBundle(BANDLE, Locale.getDefault());

private TomlMessages() {
}

public enum Key {
ARRAYS_IN_ARRAY, TABLES_IN_ARRAY
}

public static String msg(Key key, Object argument) {
return MessageFormat.format(MESSAGES.getString(key.name()), argument);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ARRAYS_IN_ARRAY={0} - arrays in array are not supported
TABLES_IN_ARRAY={0} - tables in array are not supported
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.cactusthorn.config.extras.toml;
package net.cactusthorn.config.extras.toml.util;

import static org.junit.jupiter.api.Assertions.*;

Expand Down

0 comments on commit 46cd43c

Please sign in to comment.