XmlUtil.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. package com.genersoft.iot.vmp.gb28181.utils;
  2. import com.alibaba.fastjson2.JSONArray;
  3. import com.alibaba.fastjson2.JSONObject;
  4. import com.genersoft.iot.vmp.gb28181.bean.Device;
  5. import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
  6. import com.genersoft.iot.vmp.gb28181.event.subscribe.catalog.CatalogEvent;
  7. import com.genersoft.iot.vmp.utils.DateUtil;
  8. import org.dom4j.Attribute;
  9. import org.dom4j.Document;
  10. import org.dom4j.DocumentException;
  11. import org.dom4j.Element;
  12. import org.dom4j.io.SAXReader;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.util.ObjectUtils;
  16. import org.springframework.util.ReflectionUtils;
  17. import javax.sip.RequestEvent;
  18. import javax.sip.message.Request;
  19. import java.io.ByteArrayInputStream;
  20. import java.io.StringReader;
  21. import java.lang.reflect.Field;
  22. import java.lang.reflect.InvocationTargetException;
  23. import java.lang.reflect.ParameterizedType;
  24. import java.lang.reflect.Type;
  25. import java.util.*;
  26. /**
  27. * 基于dom4j的工具包
  28. *
  29. *
  30. */
  31. public class XmlUtil {
  32. /**
  33. * 日志服务
  34. */
  35. private static Logger logger = LoggerFactory.getLogger(XmlUtil.class);
  36. /**
  37. * 解析XML为Document对象
  38. *
  39. * @param xml 被解析的XMl
  40. *
  41. * @return Document
  42. */
  43. public static Element parseXml(String xml) {
  44. Document document = null;
  45. //
  46. StringReader sr = new StringReader(xml);
  47. SAXReader saxReader = new SAXReader();
  48. try {
  49. document = saxReader.read(sr);
  50. } catch (DocumentException e) {
  51. logger.error("解析失败", e);
  52. }
  53. return null == document ? null : document.getRootElement();
  54. }
  55. /**
  56. * 获取element对象的text的值
  57. *
  58. * @param em 节点的对象
  59. * @param tag 节点的tag
  60. * @return 节点
  61. */
  62. public static String getText(Element em, String tag) {
  63. if (null == em) {
  64. return null;
  65. }
  66. Element e = em.element(tag);
  67. //
  68. return null == e ? null : e.getText().trim();
  69. }
  70. /**
  71. * 递归解析xml节点,适用于 多节点数据
  72. *
  73. * @param node node
  74. * @param nodeName nodeName
  75. * @return List<Map<String, Object>>
  76. */
  77. public static List<Map<String, Object>> listNodes(Element node, String nodeName) {
  78. if (null == node) {
  79. return null;
  80. }
  81. // 初始化返回
  82. List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
  83. // 首先获取当前节点的所有属性节点
  84. List<Attribute> list = node.attributes();
  85. Map<String, Object> map = null;
  86. // 遍历属性节点
  87. for (Attribute attribute : list) {
  88. if (nodeName.equals(node.getName())) {
  89. if (null == map) {
  90. map = new HashMap<String, Object>();
  91. listMap.add(map);
  92. }
  93. // 取到的节点属性放到map中
  94. map.put(attribute.getName(), attribute.getValue());
  95. }
  96. }
  97. // 遍历当前节点下的所有节点 ,nodeName 要解析的节点名称
  98. // 使用递归
  99. Iterator<Element> iterator = node.elementIterator();
  100. while (iterator.hasNext()) {
  101. Element e = iterator.next();
  102. listMap.addAll(listNodes(e, nodeName));
  103. }
  104. return listMap;
  105. }
  106. /**
  107. * xml转json
  108. *
  109. * @param element
  110. * @param json
  111. */
  112. public static void node2Json(Element element, JSONObject json) {
  113. // 如果是属性
  114. for (Object o : element.attributes()) {
  115. Attribute attr = (Attribute) o;
  116. if (!ObjectUtils.isEmpty(attr.getValue())) {
  117. json.put("@" + attr.getName(), attr.getValue());
  118. }
  119. }
  120. List<Element> chdEl = element.elements();
  121. if (chdEl.isEmpty() && !ObjectUtils.isEmpty(element.getText())) {// 如果没有子元素,只有一个值
  122. json.put(element.getName(), element.getText());
  123. }
  124. for (Element e : chdEl) { // 有子元素
  125. if (!e.elements().isEmpty()) { // 子元素也有子元素
  126. JSONObject chdjson = new JSONObject();
  127. node2Json(e, chdjson);
  128. Object o = json.get(e.getName());
  129. if (o != null) {
  130. JSONArray jsona = null;
  131. if (o instanceof JSONObject) { // 如果此元素已存在,则转为jsonArray
  132. JSONObject jsono = (JSONObject) o;
  133. json.remove(e.getName());
  134. jsona = new JSONArray();
  135. jsona.add(jsono);
  136. jsona.add(chdjson);
  137. }
  138. if (o instanceof JSONArray) {
  139. jsona = (JSONArray) o;
  140. jsona.add(chdjson);
  141. }
  142. json.put(e.getName(), jsona);
  143. } else {
  144. if (!chdjson.isEmpty()) {
  145. json.put(e.getName(), chdjson);
  146. }
  147. }
  148. } else { // 子元素没有子元素
  149. for (Object o : element.attributes()) {
  150. Attribute attr = (Attribute) o;
  151. if (!ObjectUtils.isEmpty(attr.getValue())) {
  152. json.put("@" + attr.getName(), attr.getValue());
  153. }
  154. }
  155. if (!e.getText().isEmpty()) {
  156. json.put(e.getName(), e.getText());
  157. }
  158. }
  159. }
  160. }
  161. public static Element getRootElement(RequestEvent evt) throws DocumentException {
  162. return getRootElement(evt, "gb2312");
  163. }
  164. public static Element getRootElement(RequestEvent evt, String charset) throws DocumentException {
  165. Request request = evt.getRequest();
  166. return getRootElement(request.getRawContent(), charset);
  167. }
  168. public static Element getRootElement(byte[] content, String charset) throws DocumentException {
  169. if (charset == null) {
  170. charset = "gb2312";
  171. }
  172. SAXReader reader = new SAXReader();
  173. reader.setEncoding(charset);
  174. Document xml = reader.read(new ByteArrayInputStream(content));
  175. return xml.getRootElement();
  176. }
  177. private enum ChannelType{
  178. CivilCode, BusinessGroup,VirtualOrganization,Other
  179. }
  180. public static DeviceChannel channelContentHander(Element itemDevice, Device device, String event){
  181. DeviceChannel deviceChannel = new DeviceChannel();
  182. deviceChannel.setDeviceId(device.getDeviceId());
  183. Element channdelIdElement = itemDevice.element("DeviceID");
  184. if (channdelIdElement == null) {
  185. logger.warn("解析Catalog消息时发现缺少 DeviceID");
  186. return null;
  187. }
  188. String channelId = channdelIdElement.getTextTrim();
  189. if (ObjectUtils.isEmpty(channelId)) {
  190. logger.warn("解析Catalog消息时发现缺少 DeviceID");
  191. return null;
  192. }
  193. deviceChannel.setChannelId(channelId);
  194. if (event != null && !event.equals(CatalogEvent.ADD) && !event.equals(CatalogEvent.UPDATE)) {
  195. // 除了ADD和update情况下需要识别全部内容,
  196. return deviceChannel;
  197. }
  198. ChannelType channelType = ChannelType.Other;
  199. if (channelId.length() <= 8) {
  200. channelType = ChannelType.CivilCode;
  201. deviceChannel.setHasAudio(false);
  202. }else {
  203. if (channelId.length() == 20) {
  204. int code = Integer.parseInt(channelId.substring(10, 13));
  205. switch (code){
  206. case 215:
  207. channelType = ChannelType.BusinessGroup;
  208. deviceChannel.setHasAudio(false);
  209. break;
  210. case 216:
  211. channelType = ChannelType.VirtualOrganization;
  212. deviceChannel.setHasAudio(false);
  213. break;
  214. case 136:
  215. case 137:
  216. case 138:
  217. deviceChannel.setHasAudio(true);
  218. break;
  219. default:
  220. deviceChannel.setHasAudio(false);
  221. break;
  222. }
  223. }
  224. }
  225. Element channdelNameElement = itemDevice.element("Name");
  226. String channelName = channdelNameElement != null ? channdelNameElement.getTextTrim() : "";
  227. deviceChannel.setName(channelName);
  228. String civilCode = XmlUtil.getText(itemDevice, "CivilCode");
  229. deviceChannel.setCivilCode(civilCode);
  230. if (channelType == ChannelType.CivilCode && civilCode == null) {
  231. deviceChannel.setParental(1);
  232. // 行政区划如果没有传递具体值,则推测一个
  233. if (channelId.length() > 2) {
  234. deviceChannel.setCivilCode(channelId.substring(0, channelId.length() - 2));
  235. }
  236. }
  237. if (channelType.equals(ChannelType.CivilCode)) {
  238. // 行政区划其他字段没必要识别了,默认在线即可
  239. deviceChannel.setStatus(true);
  240. deviceChannel.setParental(1);
  241. deviceChannel.setCreateTime(DateUtil.getNow());
  242. deviceChannel.setUpdateTime(DateUtil.getNow());
  243. return deviceChannel;
  244. }
  245. /**
  246. * 行政区划展示设备树与业务分组展示设备树是两种不同的模式
  247. * 行政区划展示设备树 各个目录之间主要靠deviceId做关联,摄像头通过CivilCode指定其属于那个行政区划;都是不超过十位的编号; 结构如下:
  248. * 河北省
  249. * --> 石家庄市
  250. * --> 摄像头
  251. *String parentId = XmlUtil.getText(itemDevice, "ParentID");
  252. if (parentId != null) {
  253. if (parentId.contains("/")) {
  254. String lastParentId = parentId.substring(parentId.lastIndexOf("/") + 1);
  255. String businessGroup = parentId.substring(0, parentId.indexOf("/"));
  256. deviceChannel.setParentId(lastParentId);
  257. }else {
  258. deviceChannel.setParentId(parentId);
  259. }
  260. }
  261. deviceCh --> 正定县
  262. * --> 摄像头
  263. * --> 摄像头
  264. *
  265. * 业务分组展示设备树是顶级是业务分组,其下的虚拟组织靠BusinessGroupID指定其所属的业务分组;摄像头通过ParentId来指定其所属于的虚拟组织:
  266. * 业务分组
  267. * --> 虚拟组织
  268. * --> 摄像头
  269. * --> 虚拟组织
  270. * --> 摄像头
  271. * --> 摄像头
  272. */
  273. String parentId = XmlUtil.getText(itemDevice, "ParentID");
  274. String businessGroupID = XmlUtil.getText(itemDevice, "BusinessGroupID");
  275. if (parentId != null) {
  276. if (parentId.contains("/")) {
  277. String lastParentId = parentId.substring(parentId.lastIndexOf("/") + 1);
  278. if (businessGroupID == null) {
  279. businessGroupID = parentId.substring(0, parentId.indexOf("/"));
  280. }
  281. deviceChannel.setParentId(lastParentId);
  282. }else {
  283. deviceChannel.setParentId(parentId);
  284. }
  285. // 兼容设备通道信息中自己为自己父节点的情况
  286. if (deviceChannel.getParentId().equals(deviceChannel.getChannelId())) {
  287. deviceChannel.setParentId(null);
  288. }
  289. }
  290. deviceChannel.setBusinessGroupId(businessGroupID);
  291. if (channelType.equals(ChannelType.BusinessGroup) || channelType.equals(ChannelType.VirtualOrganization)) {
  292. // 业务分组和虚拟组织 其他字段没必要识别了,默认在线即可
  293. deviceChannel.setStatus(true);
  294. deviceChannel.setParental(1);
  295. deviceChannel.setCreateTime(DateUtil.getNow());
  296. deviceChannel.setUpdateTime(DateUtil.getNow());
  297. return deviceChannel;
  298. }
  299. Element statusElement = itemDevice.element("Status");
  300. if (statusElement != null) {
  301. String status = statusElement.getTextTrim().trim();
  302. // ONLINE OFFLINE HIKVISION DS-7716N-E4 NVR的兼容性处理
  303. if (status.equals("ON") || status.equals("On") || status.equals("ONLINE") || status.equals("OK")) {
  304. deviceChannel.setStatus(true);
  305. }
  306. if (status.equals("OFF") || status.equals("Off") || status.equals("OFFLINE")) {
  307. deviceChannel.setStatus(false);
  308. }
  309. }else {
  310. deviceChannel.setStatus(true);
  311. }
  312. // 识别自带的目录标识
  313. String parental = XmlUtil.getText(itemDevice, "Parental");
  314. // 由于海康会错误的发送65535作为这里的取值,所以这里除非是0否则认为是1
  315. if (!ObjectUtils.isEmpty(parental) && parental.length() == 1 && Integer.parseInt(parental) == 0) {
  316. deviceChannel.setParental(0);
  317. }else {
  318. deviceChannel.setParental(1);
  319. }
  320. deviceChannel.setManufacture(XmlUtil.getText(itemDevice, "Manufacturer"));
  321. deviceChannel.setModel(XmlUtil.getText(itemDevice, "Model"));
  322. deviceChannel.setOwner(XmlUtil.getText(itemDevice, "Owner"));
  323. deviceChannel.setCertNum(XmlUtil.getText(itemDevice, "CertNum"));
  324. deviceChannel.setBlock(XmlUtil.getText(itemDevice, "Block"));
  325. deviceChannel.setAddress(XmlUtil.getText(itemDevice, "Address"));
  326. deviceChannel.setPassword(XmlUtil.getText(itemDevice, "Password"));
  327. String safetyWay = XmlUtil.getText(itemDevice, "SafetyWay");
  328. if (ObjectUtils.isEmpty(safetyWay)) {
  329. deviceChannel.setSafetyWay(0);
  330. } else {
  331. deviceChannel.setSafetyWay(Integer.parseInt(safetyWay));
  332. }
  333. String registerWay = XmlUtil.getText(itemDevice, "RegisterWay");
  334. if (ObjectUtils.isEmpty(registerWay)) {
  335. deviceChannel.setRegisterWay(1);
  336. } else {
  337. deviceChannel.setRegisterWay(Integer.parseInt(registerWay));
  338. }
  339. if (XmlUtil.getText(itemDevice, "Certifiable") == null
  340. || XmlUtil.getText(itemDevice, "Certifiable") == "") {
  341. deviceChannel.setCertifiable(0);
  342. } else {
  343. deviceChannel.setCertifiable(Integer.parseInt(XmlUtil.getText(itemDevice, "Certifiable")));
  344. }
  345. if (XmlUtil.getText(itemDevice, "ErrCode") == null
  346. || XmlUtil.getText(itemDevice, "ErrCode") == "") {
  347. deviceChannel.setErrCode(0);
  348. } else {
  349. deviceChannel.setErrCode(Integer.parseInt(XmlUtil.getText(itemDevice, "ErrCode")));
  350. }
  351. deviceChannel.setEndTime(XmlUtil.getText(itemDevice, "EndTime"));
  352. deviceChannel.setSecrecy(XmlUtil.getText(itemDevice, "Secrecy"));
  353. deviceChannel.setIpAddress(XmlUtil.getText(itemDevice, "IPAddress"));
  354. if (XmlUtil.getText(itemDevice, "Port") == null || XmlUtil.getText(itemDevice, "Port") == "") {
  355. deviceChannel.setPort(0);
  356. } else {
  357. deviceChannel.setPort(Integer.parseInt(XmlUtil.getText(itemDevice, "Port")));
  358. }
  359. String longitude = XmlUtil.getText(itemDevice, "Longitude");
  360. if (NumericUtil.isDouble(longitude)) {
  361. deviceChannel.setLongitude(Double.parseDouble(longitude));
  362. } else {
  363. deviceChannel.setLongitude(0.00);
  364. }
  365. String latitude = XmlUtil.getText(itemDevice, "Latitude");
  366. if (NumericUtil.isDouble(latitude)) {
  367. deviceChannel.setLatitude(Double.parseDouble(latitude));
  368. } else {
  369. deviceChannel.setLatitude(0.00);
  370. }
  371. deviceChannel.setGpsTime(DateUtil.getNow());
  372. if (XmlUtil.getText(itemDevice, "PTZType") == null || "".equals(XmlUtil.getText(itemDevice, "PTZType"))) {
  373. //兼容INFO中的信息
  374. Element info = itemDevice.element("Info");
  375. if(XmlUtil.getText(info, "PTZType") == null || "".equals(XmlUtil.getText(info, "PTZType"))){
  376. deviceChannel.setPTZType(0);
  377. }else{
  378. deviceChannel.setPTZType(Integer.parseInt(XmlUtil.getText(info, "PTZType")));
  379. }
  380. } else {
  381. deviceChannel.setPTZType(Integer.parseInt(XmlUtil.getText(itemDevice, "PTZType")));
  382. }
  383. return deviceChannel;
  384. }
  385. /**
  386. * 新增方法支持内部嵌套
  387. *
  388. * @param element xmlElement
  389. * @param clazz 结果类
  390. * @param <T> 泛型
  391. * @return 结果对象
  392. * @throws NoSuchMethodException
  393. * @throws InvocationTargetException
  394. * @throws InstantiationException
  395. * @throws IllegalAccessException
  396. */
  397. public static <T> T loadElement(Element element, Class<T> clazz) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
  398. Field[] fields = clazz.getDeclaredFields();
  399. T t = clazz.getDeclaredConstructor().newInstance();
  400. for (Field field : fields) {
  401. ReflectionUtils.makeAccessible(field);
  402. MessageElement annotation = field.getAnnotation(MessageElement.class);
  403. if (annotation == null) {
  404. continue;
  405. }
  406. String value = annotation.value();
  407. String subVal = annotation.subVal();
  408. Element element1 = element.element(value);
  409. if (element1 == null) {
  410. continue;
  411. }
  412. if ("".equals(subVal)) {
  413. // 无下级数据
  414. Object fieldVal = element1.isTextOnly() ? element1.getText() : loadElement(element1, field.getType());
  415. Object o = simpleTypeDeal(field.getType(), fieldVal);
  416. ReflectionUtils.setField(field, t, o);
  417. } else {
  418. // 存在下级数据
  419. ArrayList<Object> list = new ArrayList<>();
  420. Type genericType = field.getGenericType();
  421. if (!(genericType instanceof ParameterizedType)) {
  422. continue;
  423. }
  424. Class<?> aClass = (Class<?>) ((ParameterizedType) genericType).getActualTypeArguments()[0];
  425. for (Element element2 : element1.elements(subVal)) {
  426. list.add(loadElement(element2, aClass));
  427. }
  428. ReflectionUtils.setField(field, t, list);
  429. }
  430. }
  431. return t;
  432. }
  433. /**
  434. * 简单类型处理
  435. *
  436. * @param tClass
  437. * @param val
  438. * @return
  439. */
  440. private static Object simpleTypeDeal(Class<?> tClass, Object val) {
  441. if (tClass.equals(String.class)) {
  442. return val.toString();
  443. }
  444. if (tClass.equals(Integer.class)) {
  445. return Integer.valueOf(val.toString());
  446. }
  447. if (tClass.equals(Double.class)) {
  448. return Double.valueOf(val.toString());
  449. }
  450. if (tClass.equals(Long.class)) {
  451. return Long.valueOf(val.toString());
  452. }
  453. return val;
  454. }
  455. }