XmlUtil.java 17 KB

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