Skip to content

Commit 0d7ea29

Browse files
committed
xml代码优化
1 parent 1160209 commit 0d7ea29

File tree

4 files changed

+40
-45
lines changed

4 files changed

+40
-45
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/xml/XStreamInitializer.java

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,47 @@
88
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
99
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
1010
import com.thoughtworks.xstream.io.xml.XppDriver;
11-
import com.thoughtworks.xstream.security.NullPermission;
12-
import com.thoughtworks.xstream.security.PrimitiveTypePermission;
1311

1412
public class XStreamInitializer {
15-
16-
public static XStream getInstance() {
17-
XStream xstream = new XStream(new PureJavaReflectionProvider(), new XppDriver() {
18-
19-
@Override
20-
public HierarchicalStreamWriter createWriter(Writer out) {
21-
return new PrettyPrintWriter(out, getNameCoder()) {
22-
protected String PREFIX_CDATA = "<![CDATA[";
23-
protected String SUFFIX_CDATA = "]]>";
24-
protected String PREFIX_MEDIA_ID = "<MediaId>";
25-
protected String SUFFIX_MEDIA_ID = "</MediaId>";
26-
27-
@Override
28-
protected void writeText(QuickWriter writer, String text) {
29-
if (text.startsWith(this.PREFIX_CDATA) && text.endsWith(this.SUFFIX_CDATA)) {
30-
writer.write(text);
31-
} else if (text.startsWith(this.PREFIX_MEDIA_ID) && text.endsWith(this.SUFFIX_MEDIA_ID)) {
32-
writer.write(text);
33-
} else {
34-
super.writeText(writer, text);
35-
}
36-
13+
private static final XppDriver XPP_DRIVER = new XppDriver() {
14+
@Override
15+
public HierarchicalStreamWriter createWriter(Writer out) {
16+
return new PrettyPrintWriter(out, getNameCoder()) {
17+
private static final String PREFIX_CDATA = "<![CDATA[";
18+
private static final String SUFFIX_CDATA = "]]>";
19+
private static final String PREFIX_MEDIA_ID = "<MediaId>";
20+
private static final String SUFFIX_MEDIA_ID = "</MediaId>";
21+
22+
@Override
23+
protected void writeText(QuickWriter writer, String text) {
24+
if (text.startsWith(PREFIX_CDATA) && text.endsWith(SUFFIX_CDATA)) {
25+
writer.write(text);
26+
} else if (text.startsWith(PREFIX_MEDIA_ID) && text.endsWith(SUFFIX_MEDIA_ID)) {
27+
writer.write(text);
28+
} else {
29+
super.writeText(writer, text);
3730
}
3831

39-
@Override
40-
public String encodeNode(String name) {
41-
//防止将_转换成__
42-
return name;
43-
}
44-
};
45-
}
46-
});
32+
}
4733

34+
@Override
35+
public String encodeNode(String name) {
36+
//防止将_转换成__
37+
return name;
38+
}
39+
};
40+
}
41+
};
42+
43+
public static XStream getInstance() {
44+
XStream xstream = new XStream(new PureJavaReflectionProvider(), XPP_DRIVER);
4845
xstream.ignoreUnknownElements();
4946
xstream.setMode(XStream.NO_REFERENCES);
50-
xstream.addPermission(NullPermission.NULL);
51-
xstream.addPermission(PrimitiveTypePermission.PRIMITIVES);
47+
XStream.setupDefaultSecurity(xstream);
5248
xstream.allowTypesByWildcard(new String[]{
5349
"me.chanjar.weixin.**", "cn.binarywang.wx.**", "com.github.binarywang.**"
5450
});
5551

56-
XStream.setupDefaultSecurity(xstream);
57-
5852
xstream.setClassLoader(Thread.currentThread().getContextClassLoader());
5953
return xstream;
6054
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayRefundResult.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public class WxPayRefundResult extends BaseWxPayResult implements Serializable {
119119
/**
120120
* 组装生成退款代金券信息.
121121
*/
122-
public void composeRefundCoupons() {
122+
private void composeRefundCoupons() {
123123
List<WxPayRefundCouponInfo> coupons = Lists.newArrayList();
124124
Integer refundCount = this.getCouponRefundCount();
125125
if (refundCount == null) {
@@ -139,4 +139,10 @@ public void composeRefundCoupons() {
139139

140140
this.setRefundCoupons(coupons);
141141
}
142+
143+
public static WxPayRefundResult fromXML(String xml) {
144+
WxPayRefundResult result = BaseWxPayResult.fromXML(xml, WxPayRefundResult.class);
145+
result.composeRefundCoupons();
146+
return result;
147+
}
142148
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ public WxPayRefundResult refund(WxPayRefundRequest request) throws WxPayExceptio
144144

145145
String url = this.getPayBaseUrl() + "/secapi/pay/refund";
146146
String responseContent = this.post(url, request.toXML(), true);
147-
WxPayRefundResult result = BaseWxPayResult.fromXML(responseContent, WxPayRefundResult.class);
148-
result.composeRefundCoupons();
147+
WxPayRefundResult result = WxPayRefundResult.fromXML(responseContent);
149148
result.checkResult(this, request.getSignType(), true);
150149
return result;
151150
}

weixin-java-pay/src/test/java/com/github/binarywang/wxpay/bean/result/WxPayRefundResultTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@
1313
*/
1414
public class WxPayRefundResultTest {
1515

16-
/**
17-
* Test compose refund coupons.
18-
*/
1916
@Test
20-
public void testComposeRefundCoupons() {
17+
public void testFromXML() {
2118
/*
2219
该xml字符串来自于官方文档示例,稍加改造,加上代金卷
2320
refund_channel 是个什么鬼,官方文档只字不提
@@ -43,8 +40,7 @@ public void testComposeRefundCoupons() {
4340
" <refund_fee>2</refund_fee> \n" +
4441
"</xml>";
4542

46-
WxPayRefundResult result = WxPayRefundResult.fromXML(xmlString, WxPayRefundResult.class);
47-
result.composeRefundCoupons();
43+
WxPayRefundResult result = WxPayRefundResult.fromXML(xmlString);
4844

4945
assertThat(result.getRefundCoupons()).isNotEmpty();
5046
assertThat(result.getRefundCoupons().get(0).getCouponRefundId()).isEqualTo("123");

0 commit comments

Comments
 (0)