XmlUtil.java 16 KB

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