mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Merge pull request #1939 from zkzlx/master
fixed chinese garbled for nacos-config
This commit is contained in:
commit
bf08ab3110
@ -30,8 +30,6 @@ import org.springframework.core.env.PropertySource;
|
|||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import static com.alibaba.cloud.nacos.parser.NacosDataParserHandler.DOT;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Nacos-specific loader, If need to support other methods of parsing,you need to do the
|
* Nacos-specific loader, If need to support other methods of parsing,you need to do the
|
||||||
* following steps:
|
* following steps:
|
||||||
@ -47,6 +45,11 @@ import static com.alibaba.cloud.nacos.parser.NacosDataParserHandler.DOT;
|
|||||||
*/
|
*/
|
||||||
public abstract class AbstractPropertySourceLoader implements PropertySourceLoader {
|
public abstract class AbstractPropertySourceLoader implements PropertySourceLoader {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* symbol: dot.
|
||||||
|
*/
|
||||||
|
static final String DOT = ".";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prevent interference with other loaders.Nacos-specific loader, unless the reload
|
* Prevent interference with other loaders.Nacos-specific loader, unless the reload
|
||||||
* changes it.
|
* changes it.
|
||||||
|
@ -25,7 +25,10 @@ import java.util.Map;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import com.alibaba.cloud.nacos.utils.NacosConfigUtils;
|
||||||
|
|
||||||
import org.springframework.boot.env.OriginTrackedMapPropertySource;
|
import org.springframework.boot.env.OriginTrackedMapPropertySource;
|
||||||
|
import org.springframework.boot.env.PropertiesPropertySourceLoader;
|
||||||
import org.springframework.boot.env.PropertySourceLoader;
|
import org.springframework.boot.env.PropertySourceLoader;
|
||||||
import org.springframework.core.env.EnumerablePropertySource;
|
import org.springframework.core.env.EnumerablePropertySource;
|
||||||
import org.springframework.core.env.PropertySource;
|
import org.springframework.core.env.PropertySource;
|
||||||
@ -33,25 +36,17 @@ import org.springframework.core.io.support.SpringFactoriesLoader;
|
|||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import static com.alibaba.cloud.nacos.parser.AbstractPropertySourceLoader.DOT;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zkz
|
* @author zkz
|
||||||
*/
|
*/
|
||||||
public final class NacosDataParserHandler {
|
public final class NacosDataParserHandler {
|
||||||
|
|
||||||
/**
|
|
||||||
* symbol: dot.
|
|
||||||
*/
|
|
||||||
public static final String DOT = ".";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* constant.
|
|
||||||
*/
|
|
||||||
public static final String VALUE = "value";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* default extension.
|
* default extension.
|
||||||
*/
|
*/
|
||||||
public static final String DEFAULT_EXTENSION = "properties";
|
private static final String DEFAULT_EXTENSION = "properties";
|
||||||
|
|
||||||
private static List<PropertySourceLoader> propertySourceLoaders;
|
private static List<PropertySourceLoader> propertySourceLoaders;
|
||||||
|
|
||||||
@ -80,8 +75,18 @@ public final class NacosDataParserHandler {
|
|||||||
if (!canLoadFileExtension(propertySourceLoader, extension)) {
|
if (!canLoadFileExtension(propertySourceLoader, extension)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
NacosByteArrayResource nacosByteArrayResource = new NacosByteArrayResource(
|
NacosByteArrayResource nacosByteArrayResource;
|
||||||
configValue.getBytes(), configName);
|
if (propertySourceLoader instanceof PropertiesPropertySourceLoader) {
|
||||||
|
// PropertiesPropertySourceLoader internal is to use the ISO_8859_1,
|
||||||
|
// the Chinese will be garbled, needs to transform into unicode.
|
||||||
|
nacosByteArrayResource = new NacosByteArrayResource(
|
||||||
|
NacosConfigUtils.selectiveConvertUnicode(configValue).getBytes(),
|
||||||
|
configName);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
nacosByteArrayResource = new NacosByteArrayResource(
|
||||||
|
configValue.getBytes(), configName);
|
||||||
|
}
|
||||||
nacosByteArrayResource.setFilename(getFileName(configName, extension));
|
nacosByteArrayResource.setFilename(getFileName(configName, extension));
|
||||||
List<PropertySource<?>> propertySourceList = propertySourceLoader
|
List<PropertySource<?>> propertySourceList = propertySourceLoader
|
||||||
.load(configName, nacosByteArrayResource);
|
.load(configName, nacosByteArrayResource);
|
||||||
|
@ -28,14 +28,16 @@ import org.springframework.boot.env.OriginTrackedMapPropertySource;
|
|||||||
import org.springframework.core.env.PropertySource;
|
import org.springframework.core.env.PropertySource;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
|
|
||||||
import static com.alibaba.cloud.nacos.parser.NacosDataParserHandler.DOT;
|
|
||||||
import static com.alibaba.cloud.nacos.parser.NacosDataParserHandler.VALUE;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zkz
|
* @author zkz
|
||||||
*/
|
*/
|
||||||
public class NacosJsonPropertySourceLoader extends AbstractPropertySourceLoader {
|
public class NacosJsonPropertySourceLoader extends AbstractPropertySourceLoader {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* constant.
|
||||||
|
*/
|
||||||
|
private static final String VALUE = "value";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the file extensions that the loader supports (excluding the '.').
|
* Returns the file extensions that the loader supports (excluding the '.').
|
||||||
* @return the file extensions
|
* @return the file extensions
|
||||||
|
@ -127,8 +127,7 @@ public class NacosXmlPropertySourceLoader extends AbstractPropertySourceLoader
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
String key = StringUtils.isEmpty(parentKey) ? name
|
String key = StringUtils.isEmpty(parentKey) ? name : parentKey + DOT + name;
|
||||||
: parentKey + NacosDataParserHandler.DOT + name;
|
|
||||||
NamedNodeMap nodeMap = node.getAttributes();
|
NamedNodeMap nodeMap = node.getAttributes();
|
||||||
parseNodeAttr(nodeMap, map, key);
|
parseNodeAttr(nodeMap, map, key);
|
||||||
if (node.getNodeType() == Node.ELEMENT_NODE && node.hasChildNodes()) {
|
if (node.getNodeType() == Node.ELEMENT_NODE && node.hasChildNodes()) {
|
||||||
@ -159,8 +158,8 @@ public class NacosXmlPropertySourceLoader extends AbstractPropertySourceLoader
|
|||||||
if (StringUtils.isEmpty(node.getNodeValue())) {
|
if (StringUtils.isEmpty(node.getNodeValue())) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
map.put(String.join(NacosDataParserHandler.DOT, parentKey,
|
map.put(String.join(DOT, parentKey, node.getNodeName()),
|
||||||
node.getNodeName()), node.getNodeValue());
|
node.getNodeValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,61 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.alibaba.cloud.nacos.utils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zkzlx
|
||||||
|
*/
|
||||||
|
public final class NacosConfigUtils {
|
||||||
|
|
||||||
|
private NacosConfigUtils(){
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert Chinese characters to Unicode.
|
||||||
|
* @param configValue value of config
|
||||||
|
* @return new string
|
||||||
|
*/
|
||||||
|
public static String selectiveConvertUnicode(String configValue) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
char[] chars = configValue.toCharArray();
|
||||||
|
for (char aChar : chars) {
|
||||||
|
if (isChinese(aChar)) {
|
||||||
|
sb.append("\\u").append(Integer.toHexString(aChar));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sb.append(aChar);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* char is chinese?
|
||||||
|
* @param c a character
|
||||||
|
* @return true or false
|
||||||
|
*/
|
||||||
|
public static boolean isChinese(char c) {
|
||||||
|
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
|
||||||
|
return ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|
||||||
|
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|
||||||
|
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|
||||||
|
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|
||||||
|
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|
||||||
|
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user