From 3ce7c3c36b39a92d43d64db643555785cf3ee3ac Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 30 Mar 2023 00:09:14 +0800 Subject: [PATCH] fix code --- .../java/cn/hutool/setting/toml/Toml.java | 30 ++++++++ .../cn/hutool/setting/toml/TomlReader.java | 11 ++- .../cn/hutool/setting/toml/TomlWriter.java | 73 +++++++++++-------- .../java/cn/hutool/setting/toml/TomlTest.java | 27 +++++++ 4 files changed, 107 insertions(+), 34 deletions(-) create mode 100644 hutool-setting/src/test/java/cn/hutool/setting/toml/TomlTest.java diff --git a/hutool-setting/src/main/java/cn/hutool/setting/toml/Toml.java b/hutool-setting/src/main/java/cn/hutool/setting/toml/Toml.java index 782b4957a..1ff8e2794 100644 --- a/hutool-setting/src/main/java/cn/hutool/setting/toml/Toml.java +++ b/hutool-setting/src/main/java/cn/hutool/setting/toml/Toml.java @@ -12,10 +12,40 @@ package cn.hutool.setting.toml; +import cn.hutool.core.io.resource.Resource; + +import java.io.Writer; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; +import java.util.Map; +/** + * TOML读写封装
+ * TODO 参考https://github.com/TheElectronWill/night-config改造 + * + * @author TheElectronWill + */ public class Toml { + + /** + * 读取TOML + * + * @param resource 资源 + * @return TOML信息 + */ + public static Map read(final Resource resource){ + return new TomlReader(resource.readUtf8Str(), false).read(); + } + + /** + * 将TOML数据写出到Writer + * @param data TOML数据 + * @param writer {@link Writer} + */ + public static void write(Map data, Writer writer){ + new TomlWriter(writer).write(data); + } + /** * A DateTimeFormatter that uses the TOML format. */ diff --git a/hutool-setting/src/main/java/cn/hutool/setting/toml/TomlReader.java b/hutool-setting/src/main/java/cn/hutool/setting/toml/TomlReader.java index 4e3c30330..2cfe95d67 100644 --- a/hutool-setting/src/main/java/cn/hutool/setting/toml/TomlReader.java +++ b/hutool-setting/src/main/java/cn/hutool/setting/toml/TomlReader.java @@ -145,6 +145,11 @@ public class TomlReader { } } + /** + * 读取TOML + * + * @return TOML + */ @SuppressWarnings("unchecked") public Map read() { final Map map = nextTableContent(); @@ -237,7 +242,7 @@ public class TomlReader { final Object child = valueMap.get(part); final Map childMap; if (child == null) {// implicit table - childMap = new HashMap<>(4); + childMap = new LinkedHashMap<>(4); valueMap.put(part, childMap); } else if (child instanceof Map) {// table childMap = (Map) child; @@ -291,7 +296,7 @@ public class TomlReader { } private Map nextInlineTable() { - final Map map = new HashMap<>(); + final Map map = new LinkedHashMap<>(); while (true) { final char nameFirstChar = nextUsefulOrLinebreak(); String name = null; @@ -350,7 +355,7 @@ public class TomlReader { } private Map nextTableContent() { - final Map map = new HashMap<>(); + final Map map = new LinkedHashMap<>(); while (true) { final char nameFirstChar = nextUseful(true); if (!hasNext() || nameFirstChar == '[') { diff --git a/hutool-setting/src/main/java/cn/hutool/setting/toml/TomlWriter.java b/hutool-setting/src/main/java/cn/hutool/setting/toml/TomlWriter.java index 978968ca5..430a6bc89 100644 --- a/hutool-setting/src/main/java/cn/hutool/setting/toml/TomlWriter.java +++ b/hutool-setting/src/main/java/cn/hutool/setting/toml/TomlWriter.java @@ -13,6 +13,7 @@ package cn.hutool.setting.toml; import cn.hutool.core.array.ArrayUtil; +import cn.hutool.core.io.IORuntimeException; import cn.hutool.core.util.CharUtil; import cn.hutool.setting.SettingException; @@ -28,18 +29,16 @@ import java.util.LinkedList; import java.util.Map; /** - * Class for writing TOML v0.4.0. - *

DateTimes support

+ * TOML生成器 *

- * Any {@link TemporalAccessor} may be added in a Map passed to this writer, this writer can only write three - * kind of datetimes: {@link LocalDate}, {@link LocalDateTime} and {@link ZonedDateTime}. - *

- *

Lenient bare keys

+ * 日期格式支持: + *
    + *
  • 2015-03-20 转为:{@link LocalDate}
  • + *
  • 2015-03-20T19:04:35 转为:{@link LocalDateTime}
  • + *
  • 2015-03-20T19:04:35+01:00 转为:{@link ZonedDateTime}
  • + *
*

- * The {@link TomlWriter} always outputs data that strictly follows the TOML specification. Any key that - * contains one - * or more non-strictly valid character is surrounded by quotes. - *

+ * 此类支持更加宽松的key,除了{@code A-Za-z0-9_- },其他key使用"包装。 * * @author TheElectronWill */ @@ -113,13 +112,13 @@ public class TomlWriter { * Writes the specified data in the TOML format. * * @param data the data to write - * @throws IOException if an error occurs + * @throws IORuntimeException if an error occurs */ - public void write(final Map data) throws IOException { + public void write(final Map data) throws IORuntimeException { writeTableContent(data); } - private void writeTableName() throws IOException { + private void writeTableName() throws IORuntimeException { final Iterator it = tablesNames.iterator(); while (it.hasNext()) { final String namePart = it.next(); @@ -130,7 +129,7 @@ public class TomlWriter { } } - private void writeTableContent(final Map table) throws IOException { + private void writeTableContent(final Map table) throws IORuntimeException { writeTableContent(table, true); writeTableContent(table, false); } @@ -144,7 +143,7 @@ public class TomlWriter { * (and the arrays of tables). */ @SuppressWarnings("unchecked") - private void writeTableContent(final Map table, final boolean simpleValues) throws IOException { + private void writeTableContent(final Map table, final boolean simpleValues) throws IORuntimeException { for (final Map.Entry entry : table.entrySet()) { final String name = entry.getKey(); final Object value = entry.getValue(); @@ -232,7 +231,7 @@ public class TomlWriter { newLine(); } - private void writeKey(final String key) throws IOException { + private void writeKey(final String key) throws IORuntimeException { for (int i = 0; i < key.length(); i++) { final char c = key.charAt(i); if (false == isValidCharOfKey(c)) { @@ -246,13 +245,13 @@ public class TomlWriter { private static boolean isValidCharOfKey(final char c) { return (c >= 'a' && c <= 'z') || - (c >= 'A' && c <= 'Z') || - (c >= '0' && c <= '9') || - c == '-' || - c == '_'; + (c >= 'A' && c <= 'Z') || + (c >= '0' && c <= '9') || + c == '-' || + c == '_'; } - private void writeString(final String str) throws IOException { + private void writeString(final String str) throws IORuntimeException { final StringBuilder sb = new StringBuilder(); sb.append('"'); for (int i = 0; i < str.length(); i++) { @@ -263,7 +262,7 @@ public class TomlWriter { write(sb.toString()); } - private void writeArray(final Collection c) throws IOException { + private void writeArray(final Collection c) throws IORuntimeException { write('['); for (final Object element : c) { writeValue(element); @@ -272,7 +271,7 @@ public class TomlWriter { write(']'); } - private void writeValue(final Object value) throws IOException { + private void writeValue(final Object value) throws IORuntimeException { if (value instanceof String) { writeString((String) value); } else if (value instanceof Number || value instanceof Boolean) { @@ -290,30 +289,42 @@ public class TomlWriter { write(ArrayUtil.toString(value)); } else if (value instanceof Map) {// should not happen because an array of tables is detected by // writeTableContent() - throw new IOException("Unexpected value " + value); + throw new IORuntimeException("Unexpected value " + value); } else { throw new SettingException("Unsupported value of type " + value.getClass().getCanonicalName()); } } - private void newLine() throws IOException { + private void newLine() throws IORuntimeException { if (lineBreaks <= 1) { - writer.write(lineSeparator); + try { + writer.write(lineSeparator); + } catch (final IOException e) { + throw new IORuntimeException(e); + } lineBreaks++; } } - private void write(final char c) throws IOException { - writer.write(c); + private void write(final char c) throws IORuntimeException { + try { + writer.write(c); + } catch (final IOException e) { + throw new IORuntimeException(e); + } lineBreaks = 0; } - private void write(final String str) throws IOException { - writer.write(str); + private void write(final String str) throws IORuntimeException { + try { + writer.write(str); + } catch (final IOException e) { + throw new IORuntimeException(e); + } lineBreaks = 0; } - private void indent() throws IOException { + private void indent() throws IORuntimeException { for (int i = 0; i < indentationLevel; i++) { for (int j = 0; j < indentSize; j++) { write(indentCharacter); diff --git a/hutool-setting/src/test/java/cn/hutool/setting/toml/TomlTest.java b/hutool-setting/src/test/java/cn/hutool/setting/toml/TomlTest.java new file mode 100644 index 000000000..74c538b06 --- /dev/null +++ b/hutool-setting/src/test/java/cn/hutool/setting/toml/TomlTest.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 looly(loolly@aliyun.com) + * Hutool is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +package cn.hutool.setting.toml; + +import cn.hutool.core.io.resource.ResourceUtil; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Map; + +public class TomlTest { + @Test + public void readTest() { + final Map read = Toml.read(ResourceUtil.getResource("test.toml")); + Assert.assertEquals(5, read.size()); + } +}