xml代码优化

This commit is contained in:
Binary Wang 2018-11-12 11:27:14 +08:00
parent 1160209ec2
commit 0d7ea29652
4 changed files with 41 additions and 46 deletions

View File

@ -8,53 +8,47 @@ import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter; import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter; import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver; import com.thoughtworks.xstream.io.xml.XppDriver;
import com.thoughtworks.xstream.security.NullPermission;
import com.thoughtworks.xstream.security.PrimitiveTypePermission;
public class XStreamInitializer { public class XStreamInitializer {
private static final XppDriver XPP_DRIVER = new XppDriver() {
@Override
public HierarchicalStreamWriter createWriter(Writer out) {
return new PrettyPrintWriter(out, getNameCoder()) {
private static final String PREFIX_CDATA = "<![CDATA[";
private static final String SUFFIX_CDATA = "]]>";
private static final String PREFIX_MEDIA_ID = "<MediaId>";
private static final String SUFFIX_MEDIA_ID = "</MediaId>";
@Override
protected void writeText(QuickWriter writer, String text) {
if (text.startsWith(PREFIX_CDATA) && text.endsWith(SUFFIX_CDATA)) {
writer.write(text);
} else if (text.startsWith(PREFIX_MEDIA_ID) && text.endsWith(SUFFIX_MEDIA_ID)) {
writer.write(text);
} else {
super.writeText(writer, text);
}
}
@Override
public String encodeNode(String name) {
//防止将_转换成__
return name;
}
};
}
};
public static XStream getInstance() { public static XStream getInstance() {
XStream xstream = new XStream(new PureJavaReflectionProvider(), new XppDriver() { XStream xstream = new XStream(new PureJavaReflectionProvider(), XPP_DRIVER);
@Override
public HierarchicalStreamWriter createWriter(Writer out) {
return new PrettyPrintWriter(out, getNameCoder()) {
protected String PREFIX_CDATA = "<![CDATA[";
protected String SUFFIX_CDATA = "]]>";
protected String PREFIX_MEDIA_ID = "<MediaId>";
protected String SUFFIX_MEDIA_ID = "</MediaId>";
@Override
protected void writeText(QuickWriter writer, String text) {
if (text.startsWith(this.PREFIX_CDATA) && text.endsWith(this.SUFFIX_CDATA)) {
writer.write(text);
} else if (text.startsWith(this.PREFIX_MEDIA_ID) && text.endsWith(this.SUFFIX_MEDIA_ID)) {
writer.write(text);
} else {
super.writeText(writer, text);
}
}
@Override
public String encodeNode(String name) {
//防止将_转换成__
return name;
}
};
}
});
xstream.ignoreUnknownElements(); xstream.ignoreUnknownElements();
xstream.setMode(XStream.NO_REFERENCES); xstream.setMode(XStream.NO_REFERENCES);
xstream.addPermission(NullPermission.NULL); XStream.setupDefaultSecurity(xstream);
xstream.addPermission(PrimitiveTypePermission.PRIMITIVES);
xstream.allowTypesByWildcard(new String[]{ xstream.allowTypesByWildcard(new String[]{
"me.chanjar.weixin.**", "cn.binarywang.wx.**", "com.github.binarywang.**" "me.chanjar.weixin.**", "cn.binarywang.wx.**", "com.github.binarywang.**"
}); });
XStream.setupDefaultSecurity(xstream);
xstream.setClassLoader(Thread.currentThread().getContextClassLoader()); xstream.setClassLoader(Thread.currentThread().getContextClassLoader());
return xstream; return xstream;
} }

View File

@ -119,7 +119,7 @@ public class WxPayRefundResult extends BaseWxPayResult implements Serializable {
/** /**
* 组装生成退款代金券信息. * 组装生成退款代金券信息.
*/ */
public void composeRefundCoupons() { private void composeRefundCoupons() {
List<WxPayRefundCouponInfo> coupons = Lists.newArrayList(); List<WxPayRefundCouponInfo> coupons = Lists.newArrayList();
Integer refundCount = this.getCouponRefundCount(); Integer refundCount = this.getCouponRefundCount();
if (refundCount == null) { if (refundCount == null) {
@ -139,4 +139,10 @@ public class WxPayRefundResult extends BaseWxPayResult implements Serializable {
this.setRefundCoupons(coupons); this.setRefundCoupons(coupons);
} }
public static WxPayRefundResult fromXML(String xml) {
WxPayRefundResult result = BaseWxPayResult.fromXML(xml, WxPayRefundResult.class);
result.composeRefundCoupons();
return result;
}
} }

View File

@ -144,8 +144,7 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
String url = this.getPayBaseUrl() + "/secapi/pay/refund"; String url = this.getPayBaseUrl() + "/secapi/pay/refund";
String responseContent = this.post(url, request.toXML(), true); String responseContent = this.post(url, request.toXML(), true);
WxPayRefundResult result = BaseWxPayResult.fromXML(responseContent, WxPayRefundResult.class); WxPayRefundResult result = WxPayRefundResult.fromXML(responseContent);
result.composeRefundCoupons();
result.checkResult(this, request.getSignType(), true); result.checkResult(this, request.getSignType(), true);
return result; return result;
} }

View File

@ -13,11 +13,8 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class WxPayRefundResultTest { public class WxPayRefundResultTest {
/**
* Test compose refund coupons.
*/
@Test @Test
public void testComposeRefundCoupons() { public void testFromXML() {
/* /*
该xml字符串来自于官方文档示例稍加改造加上代金卷 该xml字符串来自于官方文档示例稍加改造加上代金卷
refund_channel 是个什么鬼官方文档只字不提 refund_channel 是个什么鬼官方文档只字不提
@ -43,8 +40,7 @@ public class WxPayRefundResultTest {
" <refund_fee>2</refund_fee> \n" + " <refund_fee>2</refund_fee> \n" +
"</xml>"; "</xml>";
WxPayRefundResult result = WxPayRefundResult.fromXML(xmlString, WxPayRefundResult.class); WxPayRefundResult result = WxPayRefundResult.fromXML(xmlString);
result.composeRefundCoupons();
assertThat(result.getRefundCoupons()).isNotEmpty(); assertThat(result.getRefundCoupons()).isNotEmpty();
assertThat(result.getRefundCoupons().get(0).getCouponRefundId()).isEqualTo("123"); assertThat(result.getRefundCoupons().get(0).getCouponRefundId()).isEqualTo("123");