This commit is contained in:
Looly
2023-04-15 14:48:47 +08:00
parent 241aea60e4
commit 2c3eac6046
20 changed files with 676 additions and 289 deletions

View File

@@ -201,7 +201,7 @@ public final class Props extends Properties implements TypeGetter<CharSequence>
public void load(final Resource resource) {
Assert.notNull(resource, "Props resource must be not null!");
this.resource = resource;
PropsLoaderUtil.loadTo(this, resource, this.charset);
ResourceUtil.loadTo(this, resource, this.charset);
}
/**

View File

@@ -1,73 +0,0 @@
/*
* 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 org.dromara.hutool.setting.dialect;
import org.dromara.hutool.core.io.IORuntimeException;
import org.dromara.hutool.core.io.resource.Resource;
import org.dromara.hutool.core.io.resource.ResourceUtil;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Properties;
/**
* {@link Properties} 资源内容加载工具
*
* @author looly
* @since 6.0.0
*/
public class PropsLoaderUtil {
/**
* 加载配置文件内容到{@link Properties}中<br>
* 需要注意的是,如果资源文件的扩展名是.xml会调用{@link Properties#loadFromXML(InputStream)} 读取。
*
* @param properties {@link Properties}文件
* @param resource 资源
* @param charset 编码对XML无效
*/
public static void loadTo(final Properties properties, final Resource resource, final Charset charset) {
final String filename = resource.getName();
if (filename != null && filename.endsWith(".xml")) {
// XML
try (final InputStream in = resource.getStream()) {
properties.loadFromXML(in);
} catch (final IOException e) {
throw new IORuntimeException(e);
}
} else {
// .properties
try (final BufferedReader reader = resource.getReader(charset)) {
properties.load(reader);
} catch (final IOException e) {
throw new IORuntimeException(e);
}
}
}
/**
* 加载指定名称的所有配置文件内容到{@link Properties}中
*
* @param properties {@link Properties}文件
* @param resourceName 资源名可以是相对classpath的路径也可以是绝对路径
* @param classLoader {@link ClassLoader}{@code null}表示使用默认的当前上下文ClassLoader
* @param charset 编码对XML无效
*/
public static void loadAllTo(final Properties properties, final String resourceName, final ClassLoader classLoader, final Charset charset) {
for (final Resource resource : ResourceUtil.getResources(resourceName, classLoader)) {
loadTo(properties, resource, charset);
}
}
}