forked from knightliao/disconf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config file content support unicode to utf8
- Loading branch information
1 parent
2e38933
commit a186854
Showing
5 changed files
with
194 additions
and
49 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
...mos/disconf-standalone-demo/src/main/java/com/example/disconf/demo/config/CodeConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (C) 2015 KNIGHT, Inc. All Rights Reserved. | ||
*/ | ||
package com.example.disconf.demo.config; | ||
|
||
import org.springframework.context.annotation.Scope; | ||
import org.springframework.stereotype.Service; | ||
|
||
import com.baidu.disconf.client.common.annotations.DisconfFile; | ||
import com.baidu.disconf.client.common.annotations.DisconfFileItem; | ||
|
||
/** | ||
* Created by knightliao on 15/1/7. | ||
*/ | ||
@Service | ||
@Scope("singleton") | ||
@DisconfFile(filename = "code.properties") | ||
public class CodeConfig { | ||
|
||
private String codeError = ""; | ||
|
||
@DisconfFileItem(name = "syserror.paramtype", associateField = "codeError") | ||
public String getCodeError() { | ||
return codeError; | ||
} | ||
|
||
public void setCodeError(String codeError) { | ||
this.codeError = codeError; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
disconf-web/src/main/java/com/baidu/disconf/web/utils/CodeUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (C) 2015 Baidu, Inc. All Rights Reserved. | ||
*/ | ||
package com.baidu.disconf.web.utils; | ||
|
||
/** | ||
* Created by knightliao on 15/1/7. | ||
*/ | ||
public class CodeUtils { | ||
|
||
/** | ||
* unicode 转换成 utf-8 | ||
* | ||
* @param theString | ||
* | ||
* @return | ||
* | ||
* @author fanhui | ||
* 2007-3-15 | ||
*/ | ||
public static String unicodeToUtf8(String theString) { | ||
char aChar; | ||
int len = theString.length(); | ||
StringBuffer outBuffer = new StringBuffer(len); | ||
for (int x = 0; x < len; ) { | ||
aChar = theString.charAt(x++); | ||
if (aChar == '\\') { | ||
aChar = theString.charAt(x++); | ||
if (aChar == 'u') { | ||
// Read the xxxx | ||
int value = 0; | ||
for (int i = 0; i < 4; i++) { | ||
aChar = theString.charAt(x++); | ||
switch (aChar) { | ||
case '0': | ||
case '1': | ||
case '2': | ||
case '3': | ||
case '4': | ||
case '5': | ||
case '6': | ||
case '7': | ||
case '8': | ||
case '9': | ||
value = (value << 4) + aChar - '0'; | ||
break; | ||
case 'a': | ||
case 'b': | ||
case 'c': | ||
case 'd': | ||
case 'e': | ||
case 'f': | ||
value = (value << 4) + 10 + aChar - 'a'; | ||
break; | ||
case 'A': | ||
case 'B': | ||
case 'C': | ||
case 'D': | ||
case 'E': | ||
case 'F': | ||
value = (value << 4) + 10 + aChar - 'A'; | ||
break; | ||
default: | ||
throw new IllegalArgumentException("Malformed \\uxxxx encoding."); | ||
} | ||
} | ||
outBuffer.append((char) value); | ||
} else { | ||
if (aChar == 't') { | ||
aChar = '\t'; | ||
} else if (aChar == 'r') { | ||
aChar = '\r'; | ||
} else if (aChar == 'n') { | ||
aChar = '\n'; | ||
} else if (aChar == 'f') { | ||
aChar = '\f'; | ||
} | ||
outBuffer.append(aChar); | ||
} | ||
} else { | ||
outBuffer.append(aChar); | ||
} | ||
} | ||
return outBuffer.toString(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
disconf-web/src/test/java/com/baidu/disconf/web/test/utils/CodeUtilsTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.baidu.disconf.web.test.utils; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import com.baidu.disconf.web.utils.CodeUtils; | ||
|
||
/** | ||
* Created by knightliao on 15/1/7. | ||
*/ | ||
public class CodeUtilsTestCase { | ||
|
||
@Test | ||
public void unicodeToUtf8Test() { | ||
|
||
String code = | ||
CodeUtils.unicodeToUtf8("syserror.paramtype=\\u8bf7\\u6c42\\u53c2\\u6570\\u89e3\\u6790\\u9519" + "\\u8bef"); | ||
|
||
Assert.assertEquals("syserror.paramtype=请求参数解析错误", code); | ||
} | ||
} |