XmlUtil.java 14 KB

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