XmlMsgParser.java
1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* Copyright (c) 2011-2014, L.cm 卢春梦 (qq596392912@gmail.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
*/
package com.jfinal.wxaapp.msg;
import java.lang.reflect.Field;
import com.jfinal.log.Log;
import com.jfinal.weixin.sdk.utils.XmlHelper;
import com.jfinal.wxaapp.msg.bean.WxaMsg;
/**
* xml格式消息解析
* @author l.cm
*
*/
public class XmlMsgParser extends MsgModelParser implements IMsgParser {
private static Log log = Log.getLog(XmlMsgParser.class);
@Override
public WxaMsg parser(String msgStr) {
XmlHelper xmlHelper = XmlHelper.of(msgStr);
MsgModel msgModel = toMsgModel(xmlHelper);
return parserMsg(msgModel);
}
private static MsgModel toMsgModel(XmlHelper xmlHelper) {
MsgModel msgModel = new MsgModel();
Field[] fields = MsgModel.class.getDeclaredFields();
for (Field field : fields) {
XPath xpathAnno = field.getAnnotation(XPath.class);
if (xpathAnno == null) {
continue;
}
String xpath = xpathAnno.value();
Class<?> type = field.getType();
String strValue = xmlHelper.getString(xpath);
Object value = convert(type, strValue);
if (!field.isAccessible()) {
field.setAccessible(true);
}
try {
field.set(msgModel, value);
} catch (IllegalArgumentException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
return msgModel;
}
private static final Object convert(Class<?> type, String s) {
if (type == String.class) {
return ("".equals(s) ? null : s);
}
s = s.trim();
if ("".equals(s)) {
return null;
}
if (type == Integer.class || type == int.class) {
return Integer.parseInt(s);
}
if (type == Long.class || type == long.class) {
return Long.parseLong(s);
}
throw new RuntimeException("Please add code in " + XmlMsgParser.class + ". The type can't be converted: " + type.getName());
}
}