Skip to content

Commit

Permalink
add unicode process, update mock.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Bosn committed Apr 18, 2014
1 parent abd665d commit dada08d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 12 deletions.
1 change: 1 addition & 0 deletions UPDATELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
### rap v0.8.1 ###
* [优化] mock工具中的值若包含中文将以unicode格式输出。2014-04-18
* [功能] 增加模板参数化,支持动态根据请求入参来输出参数化的MockJS模板 2014-04-18
* [功能] 修复奇葩的跨域问题。修改了所有Mock服务的HTTP headers。 2014-04-10
* [体验] 修复一系列用户体验的小问题。修复KISSY.use(undefined)的插件BUG。2014-04-08
Expand Down
6 changes: 3 additions & 3 deletions WebContent/stat/js/util/mock-min.js

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions src/Tester.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.taobao.rigel.rap.common.Patterns;
import com.taobao.rigel.rap.common.StringUtils;

public class Tester {

/**
* @param args
*/
public static void main(String[] args) {
/**
int num = 1;
Pattern p = Pattern.compile(Patterns.MOCK_TEMPLATE_PATTERN);
Matcher matcher = p.matcher("${page=1008}");
Expand All @@ -20,6 +18,9 @@ public static void main(String[] args) {
}
}
*/

System.out.println(StringUtils.chineseToUnicode("123测试一下aaa"));

}

Expand Down
47 changes: 44 additions & 3 deletions src/com/taobao/rigel/rap/common/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public static String getMD5(String src) {
}
return hexString.toString();
}

public static String getMD5(byte[] source) {
String s = null;
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
Expand Down Expand Up @@ -396,13 +396,54 @@ public static String getMD5(byte[] source) {
}
return s;
}

public static String getDoubleMD5(String src) {
if (src != null) {
src = getMD5(src);
src = getMD5(src);
}
return src;
}
}

/**
* 把中文转成Unicode码
*
* @param str
* @return
*/
public static String chineseToUnicode(String str) {
if (str == null) {
str = "";
}
String result = "";
for (int i = 0; i < str.length(); i++) {
int chr1 = (char) str.charAt(i);
if (chr1 >= 19968 && chr1 <= 171941) {// 汉字范围 \u4e00-\u9fa5 (中文)
result += "\\u" + Integer.toHexString(chr1);
} else {
result += str.charAt(i);
}
}
return result;
}

/**
* 判断是否为中文字符
*
* @param c
* @return
*/
public static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (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) {
return true;
}
return false;
}

}
4 changes: 2 additions & 2 deletions src/com/taobao/rigel/rap/mock/service/impl/MockMgrImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ private void buildMockTemplate(StringBuilder json, Parameter para, int index) {
if (para.getParameterList() == null
|| para.getParameterList().size() == 0) {
json.append(para.getMockJSIdentifier() + ":"
+ mockjsValue(para, index));
+ StringUtils.chineseToUnicode(mockjsValue(para, index)));
} else {
// object and array<object>
json.append(para.getMockJSIdentifier() + ":");
Expand Down Expand Up @@ -616,7 +616,7 @@ private String mockjsValue(Parameter para, int index) {
mockValue = tagMap.get("{mock}");
escape = false;
}

mockValue = processMockValueWithParams(para, mockValue);


Expand Down

0 comments on commit dada08d

Please sign in to comment.