Skip to content

Commit b4a454d

Browse files
committed
重构代码,去掉带来过多jar包依赖的xml-path,使用java自带的xpath来实现相应功能。
1 parent 1649b30 commit b4a454d

File tree

5 files changed

+104
-65
lines changed

5 files changed

+104
-65
lines changed

weixin-java-pay/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@
2626
<version>3.7</version>
2727
<!-- 由于较新的3.8版本需要jdk8,故而此处采用较低版本 -->
2828
</dependency>
29-
<dependency>
30-
<groupId>io.rest-assured</groupId>
31-
<artifactId>xml-path</artifactId>
32-
<version>3.0.1</version>
33-
</dependency>
3429
<dependency>
3530
<groupId>com.github.binarywang</groupId>
3631
<artifactId>qrcode-utils</artifactId>

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

Lines changed: 70 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
package com.github.binarywang.wxpay.bean.result;
22

3+
import com.google.common.base.Joiner;
34
import com.google.common.collect.Maps;
45
import com.thoughtworks.xstream.XStream;
56
import com.thoughtworks.xstream.annotations.XStreamAlias;
6-
import io.restassured.internal.path.xml.NodeChildrenImpl;
7-
import io.restassured.internal.path.xml.NodeImpl;
8-
import io.restassured.path.xml.XmlPath;
9-
import io.restassured.path.xml.element.Node;
10-
import io.restassured.path.xml.exception.XmlPathException;
117
import me.chanjar.weixin.common.util.ToStringUtils;
128
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
9+
import org.apache.commons.lang3.StringUtils;
1310
import org.slf4j.Logger;
1411
import org.slf4j.LoggerFactory;
15-
12+
import org.w3c.dom.Document;
13+
import org.w3c.dom.NodeList;
14+
import org.xml.sax.SAXException;
15+
16+
import javax.xml.parsers.DocumentBuilderFactory;
17+
import javax.xml.parsers.ParserConfigurationException;
18+
import javax.xml.xpath.XPathConstants;
19+
import javax.xml.xpath.XPathExpressionException;
20+
import javax.xml.xpath.XPathFactory;
21+
import java.io.ByteArrayInputStream;
22+
import java.io.IOException;
1623
import java.math.BigDecimal;
17-
import java.util.Iterator;
1824
import java.util.Map;
1925

2026
/**
@@ -82,6 +88,11 @@ public abstract class WxPayBaseResult {
8288
@XStreamAlias("sign")
8389
private String sign;
8490

91+
/**
92+
* xml的Document对象,用于解析xml文本
93+
*/
94+
private Document xmlDoc;
95+
8596
/**
8697
* 将单位分转换成单位圆
8798
*
@@ -211,51 +222,74 @@ public void setSubMchId(String subMchId) {
211222
* 将bean通过保存的xml字符串转换成map
212223
*/
213224
public Map<String, String> toMap() {
214-
Map<String, String> result = Maps.newHashMap();
215-
XmlPath xmlPath = new XmlPath(this.xmlString);
216-
NodeImpl rootNode = null;
217-
try {
218-
rootNode = xmlPath.get("xml");
219-
} catch (XmlPathException e) {
225+
if (StringUtils.isBlank(this.xmlString)) {
220226
throw new RuntimeException("xml数据有问题,请核实!");
221227
}
222228

223-
if (rootNode == null) {
224-
throw new RuntimeException("xml数据有问题,请核实!");
225-
}
229+
Map<String, String> result = Maps.newHashMap();
230+
Document doc = this.getXmlDoc();
226231

227-
Iterator<Node> iterator = rootNode.children().nodeIterator();
228-
while (iterator.hasNext()) {
229-
Node node = iterator.next();
230-
result.put(node.name(), node.value());
232+
try {
233+
NodeList list = (NodeList) XPathFactory.newInstance().newXPath()
234+
.compile("/xml/*")
235+
.evaluate(doc, XPathConstants.NODESET);
236+
int len = list.getLength();
237+
for (int i = 0; i < len; i++) {
238+
result.put(list.item(i).getNodeName(), list.item(i).getTextContent());
239+
}
240+
} catch (XPathExpressionException e) {
241+
throw new RuntimeException("非法的xml文本内容:" + xmlString);
231242
}
232243

233244
return result;
234245
}
235246

236-
private String getXmlValue(XmlPath xmlPath, String path) {
237-
if (xmlPath.get(path) instanceof NodeChildrenImpl) {
238-
if (((NodeChildrenImpl) xmlPath.get(path)).size() == 0) {
239-
return null;
240-
}
247+
/**
248+
* 将xml字符串转换成Document对象,以便读取其元素值
249+
*/
250+
protected Document getXmlDoc() {
251+
if (this.xmlDoc != null) {
252+
return this.xmlDoc;
253+
}
254+
255+
try {
256+
this.xmlDoc = DocumentBuilderFactory
257+
.newInstance()
258+
.newDocumentBuilder()
259+
.parse(new ByteArrayInputStream(this.xmlString.getBytes("UTF-8")));
260+
return xmlDoc;
261+
} catch (SAXException | IOException | ParserConfigurationException e) {
262+
throw new RuntimeException("非法的xml文本内容:" + this.xmlString);
241263
}
242264

243-
return xmlPath.getString(path);
244265
}
245266

246-
protected <T> T getXmlValue(XmlPath xmlPath, String path, Class<T> clz) {
247-
String value = this.getXmlValue(xmlPath, path);
248-
if (value == null) {
249-
return null;
267+
/**
268+
* 获取xml中元素的值
269+
*/
270+
protected String getXmlValue(String... path) {
271+
Document doc = this.getXmlDoc();
272+
String expression = String.format("/%s//text()", Joiner.on("/").join(path));
273+
try {
274+
return (String) XPathFactory
275+
.newInstance()
276+
.newXPath()
277+
.compile(expression)
278+
.evaluate(doc, XPathConstants.STRING);
279+
} catch (XPathExpressionException e) {
280+
throw new RuntimeException("未找到相应路径的文本:" + expression);
250281
}
282+
}
251283

252-
switch (clz.getSimpleName()) {
253-
case "String":
254-
return (T) value;
255-
case "Integer":
256-
return (T) Integer.valueOf(value);
284+
/**
285+
* 获取xml中元素的值,作为int值返回
286+
*/
287+
protected Integer getXmlValueAsInt(String... path) {
288+
String result = this.getXmlValue(path);
289+
if (StringUtils.isBlank(result)) {
290+
return null;
257291
}
258292

259-
throw new UnsupportedOperationException("暂时不支持此种类型的数据");
293+
return Integer.valueOf(result);
260294
}
261295
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.google.common.collect.Lists;
44
import com.thoughtworks.xstream.annotations.XStreamAlias;
5-
import io.restassured.path.xml.XmlPath;
65

76
import java.util.List;
87

@@ -395,11 +394,10 @@ public void setTradeStateDesc(String tradeStateDesc) {
395394
public void composeCoupons() {
396395
if (this.couponCount != null && this.couponCount > 0) {
397396
this.coupons = Lists.newArrayList();
398-
XmlPath xmlPath = new XmlPath(this.getXmlString());
399397
for (int i = 0; i < this.couponCount; i++) {
400-
this.coupons.add(new Coupon(this.getXmlValue(xmlPath, "xml.coupon_type_" + i, String.class),
401-
this.getXmlValue(xmlPath, "xml.coupon_id_" + i, String.class),
402-
this.getXmlValue(xmlPath, "xml.coupon_fee_" + i, Integer.class)));
398+
this.coupons.add(new Coupon(this.getXmlValue("xml/coupon_type_" + i),
399+
this.getXmlValue("xml/coupon_id_" + i),
400+
this.getXmlValueAsInt("xml/coupon_fee_" + i)));
403401
}
404402
}
405403
}

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.google.common.collect.Lists;
44
import com.thoughtworks.xstream.annotations.XStreamAlias;
5-
import io.restassured.path.xml.XmlPath;
65

76
import java.util.List;
87

@@ -190,22 +189,21 @@ public void setRefundRecords(List<RefundRecord> refundRecords) {
190189
public void composeRefundRecords() {
191190
if (this.refundCount != null && this.refundCount > 0) {
192191
this.refundRecords = Lists.newArrayList();
193-
XmlPath xmlPath = new XmlPath(this.getXmlString());
194192

195193
for (int i = 0; i < this.refundCount; i++) {
196194
RefundRecord refundRecord = new RefundRecord();
197195
this.refundRecords.add(refundRecord);
198196

199-
refundRecord.setOutRefundNo(this.getXmlValue(xmlPath, "xml.out_refund_no_" + i, String.class));
200-
refundRecord.setRefundId(this.getXmlValue(xmlPath, "xml.refund_id_" + i, String.class));
201-
refundRecord.setRefundChannel(this.getXmlValue(xmlPath, "xml.refund_channel_" + i, String.class));
202-
refundRecord.setRefundFee(this.getXmlValue(xmlPath, "xml.refund_fee_" + i, Integer.class));
203-
refundRecord.setSettlementRefundFee(this.getXmlValue(xmlPath, "xml.settlement_refund_fee_" + i, Integer.class));
204-
refundRecord.setCouponType(this.getXmlValue(xmlPath, "xml.coupon_type_" + i, String.class));
205-
refundRecord.setCouponRefundFee(this.getXmlValue(xmlPath, "xml.coupon_refund_fee_" + i, Integer.class));
206-
refundRecord.setCouponRefundCount(this.getXmlValue(xmlPath, "xml.coupon_refund_count_" + i, Integer.class));
207-
refundRecord.setRefundStatus(this.getXmlValue(xmlPath, "xml.refund_status_" + i, String.class));
208-
refundRecord.setRefundRecvAccout(this.getXmlValue(xmlPath, "xml.refund_recv_accout_" + i, String.class));
197+
refundRecord.setOutRefundNo(this.getXmlValue("xml/out_refund_no_" + i));
198+
refundRecord.setRefundId(this.getXmlValue("xml/refund_id_" + i));
199+
refundRecord.setRefundChannel(this.getXmlValue("xml/refund_channel_" + i));
200+
refundRecord.setRefundFee(this.getXmlValueAsInt("xml/refund_fee_" + i));
201+
refundRecord.setSettlementRefundFee(this.getXmlValueAsInt("xml/settlement_refund_fee_" + i));
202+
refundRecord.setCouponType(this.getXmlValue("xml/coupon_type_" + i));
203+
refundRecord.setCouponRefundFee(this.getXmlValueAsInt("xml/coupon_refund_fee_" + i));
204+
refundRecord.setCouponRefundCount(this.getXmlValueAsInt("xml/coupon_refund_count_" + i));
205+
refundRecord.setRefundStatus(this.getXmlValue("xml/refund_status_" + i));
206+
refundRecord.setRefundRecvAccout(this.getXmlValue("xml/refund_recv_accout_" + i));
209207

210208
if (refundRecord.getCouponRefundCount() == null || refundRecord.getCouponRefundCount() == 0) {
211209
continue;
@@ -215,8 +213,8 @@ public void composeRefundRecords() {
215213
for (int j = 0; j < refundRecord.getCouponRefundCount(); j++) {
216214
coupons.add(
217215
new RefundRecord.RefundCoupon(
218-
this.getXmlValue(xmlPath, "xml.coupon_refund_id_" + i + "_" + j, String.class),
219-
this.getXmlValue(xmlPath, "xml.coupon_refund_fee_" + i + "_" + j, Integer.class)
216+
this.getXmlValue("xml/coupon_refund_id_" + i + "_" + j),
217+
this.getXmlValueAsInt("xml/coupon_refund_fee_" + i + "_" + j)
220218
)
221219
);
222220
}

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.github.binarywang.wxpay.bean.result;
22

3-
import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryResult;
43
import org.testng.*;
54
import org.testng.annotations.*;
65

@@ -13,6 +12,15 @@
1312
* </pre>
1413
*/
1514
public class WxPayBaseResultTest {
15+
16+
@Test
17+
public void testGetXmlValue() throws Exception {
18+
}
19+
20+
@Test
21+
public void testXml2Doc() throws Exception {
22+
}
23+
1624
@Test
1725
public void testToMap() throws Exception {
1826
WxPayOrderQueryResult result = new WxPayOrderQueryResult();
@@ -40,11 +48,17 @@ public void testToMap() throws Exception {
4048
Map<String, String> map = result.toMap();
4149
System.out.println(map);
4250

43-
Assert.assertEquals("SUCCESS", map.get("return_code"));
44-
Assert.assertEquals("订单额外描述", map.get("attach"));
51+
Assert.assertEquals(map.get("return_code"), "SUCCESS");
52+
Assert.assertEquals(map.get("attach"), "订单额外描述");
53+
54+
}
4555

46-
result.setXmlString("");
47-
System.out.println(result.toMap());
56+
@Test(expectedExceptions = {RuntimeException.class})
57+
public void testToMap_with_empty_xmlString() {
58+
WxPayOrderQueryResult result = new WxPayOrderQueryResult();
59+
result.setXmlString(" ");
60+
Map<String, String> map = result.toMap();
61+
System.out.println(map);
4862
}
4963

5064
}

0 commit comments

Comments
 (0)