DataFormatConverter.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. package com.bfkj.unidia.DataUtils;
  2. import com.fasterxml.jackson.core.JsonFactory;
  3. import com.fasterxml.jackson.core.JsonGenerator;
  4. import com.fasterxml.jackson.core.JsonProcessingException;
  5. import com.fasterxml.jackson.databind.DeserializationFeature;
  6. import com.fasterxml.jackson.databind.ObjectMapper;
  7. import com.fasterxml.jackson.databind.SerializationFeature;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.springframework.stereotype.Component;
  11. import org.xml.sax.Attributes;
  12. import org.xml.sax.InputSource;
  13. import org.xml.sax.SAXException;
  14. import org.xml.sax.helpers.DefaultHandler;
  15. import javax.xml.parsers.ParserConfigurationException;
  16. import javax.xml.parsers.SAXParserFactory;
  17. import javax.xml.stream.*;
  18. import javax.xml.stream.events.*;
  19. import java.io.*;
  20. import java.util.*;
  21. /**
  22. * 高性能、线程安全的数据格式转换工具类
  23. * 支持:
  24. * - XML转Map(流式处理)
  25. * - 字符串转Map(自动识别JSON/XML)
  26. * - Map转JSON/XML(支持命名空间、CDATA)
  27. * - 流式处理,适用于海量数据场景
  28. */
  29. @Component
  30. public class DataFormatConverter {
  31. private static final Logger logger = LoggerFactory.getLogger(DataFormatConverter.class);
  32. private static final ObjectMapper objectMapper = new ObjectMapper();
  33. private static final ThreadLocal<XMLInputFactory> xmlInputFactory = ThreadLocal.withInitial(() -> {
  34. XMLInputFactory factory = XMLInputFactory.newInstance();
  35. try {
  36. factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
  37. factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
  38. } catch (Exception e) {
  39. logger.warn("XMLInputFactory 创建失败", e);
  40. return null;
  41. }
  42. return factory;
  43. });
  44. private static final ThreadLocal<XMLOutputFactory> xmlOutputFactory = ThreadLocal.withInitial(() -> {
  45. XMLOutputFactory factory = XMLOutputFactory.newInstance();
  46. try {
  47. factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
  48. } catch (Exception e) {
  49. logger.warn("XMLOutputFactory 初始化失败", e);
  50. return null;
  51. }
  52. return factory;
  53. });
  54. private static final ThreadLocal<SAXParserFactory> saxParserFactory = ThreadLocal.withInitial(() -> {
  55. SAXParserFactory factory = SAXParserFactory.newInstance();
  56. factory.setNamespaceAware(true);
  57. return factory;
  58. });
  59. static {
  60. objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  61. objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
  62. }
  63. // ===== XML 转 Map(流式处理) =====
  64. public static Map<String, Object> xmlToMap(String xml) throws XMLStreamException {
  65. try (StringReader reader = new StringReader(xml)) {
  66. return xmlToMap(reader);
  67. }catch (XMLStreamException e){
  68. logger.error("XML 转 Map 失败", e);
  69. return null;
  70. }
  71. }
  72. public static Map<String, Object> xmlToMap(Reader reader) throws XMLStreamException {
  73. XMLEventReader eventReader = xmlInputFactory.get().createXMLEventReader(reader);
  74. try {
  75. return parse(eventReader);
  76. }catch (XMLStreamException e){
  77. logger.error("XML 转 Map 失败", e);
  78. return null;
  79. }finally {
  80. eventReader.close();
  81. }
  82. }
  83. public static Map<String, Object> streamToMap(InputStream inputStream) throws XMLStreamException {
  84. XMLStreamReader xmlReader = xmlInputFactory.get().createXMLStreamReader(inputStream);
  85. XMLEventReader eventReader = null;
  86. try {
  87. eventReader = xmlInputFactory.get().createXMLEventReader(xmlReader);
  88. return parse(eventReader);
  89. } catch (XMLStreamException e) {
  90. logger.error("XML 转 Map 失败", e);
  91. return null;
  92. }finally {
  93. if (eventReader != null) {
  94. eventReader.close();
  95. }
  96. xmlReader.close();
  97. }
  98. }
  99. private static Map<String, Object> parse(XMLEventReader eventReader) throws XMLStreamException {
  100. Map<String, Object> result = new HashMap<>();
  101. Deque<String> path = new ArrayDeque<>();
  102. Deque<Map<String, Object>> mapStack = new ArrayDeque<>();
  103. StringBuffer buffer = new StringBuffer();
  104. while (eventReader.hasNext()) {
  105. if (buffer==null) buffer = new StringBuffer();
  106. XMLEvent event = eventReader.nextEvent();
  107. if (event.isStartElement()) {
  108. StartElement startElement = event.asStartElement();
  109. String qualifiedName = startElement.getName().getLocalPart();
  110. // 命名空间处理(略)
  111. path.push(qualifiedName);
  112. mapStack.push(new HashMap<>());
  113. // 属性处理
  114. Iterator<Attribute> attributes = startElement.getAttributes();
  115. while (attributes.hasNext()) {
  116. Attribute attr = attributes.next();
  117. String attrQName = attr.getName().getLocalPart();
  118. if (mapStack.isEmpty()) {
  119. logger.error("无效XML结构:属性未关联到元素");
  120. continue;
  121. }
  122. mapStack.peek().put("@" + attrQName, attr.getValue());
  123. }
  124. }
  125. else if (event.isCharacters()) {
  126. Characters characters = event.asCharacters();
  127. if (!characters.isWhiteSpace()) {
  128. String text = characters.getData();
  129. if (text.equals(".J/G/PEKPORT//30JUN/205440L///XI/CL")){
  130. System.out.println(text);
  131. }
  132. if (!mapStack.isEmpty() && mapStack.peek() != null) {
  133. if (characters.isCData()) {
  134. mapStack.peek().put("#cdata", text);
  135. } else {
  136. buffer.append(text);
  137. mapStack.peek().put("#text", buffer.toString());
  138. }
  139. }
  140. }
  141. }
  142. else if (event.isEndElement()) {
  143. if (buffer!=null){
  144. System.out.println("------------------------------buffer");
  145. System.out.println(buffer.toString());
  146. }
  147. buffer = null;
  148. if (!mapStack.isEmpty()) {
  149. Map<String, Object> current = mapStack.pop();
  150. String key = path.pop();
  151. mergeMap(result, key, current);
  152. }
  153. }
  154. }
  155. return result;
  156. }
  157. // ===== Map 转 XML =====
  158. public static String mapToXml(Map<String, Object> map) {
  159. return mapToXml(map, "UTF-8", "1.0");
  160. }
  161. /**
  162. * 集合xml批量转json字符串
  163. * @param content
  164. * @return
  165. * @throws XMLStreamException
  166. */
  167. public static List<String> xmlsToMap(List<String> content) throws XMLStreamException {
  168. List<String> maps = new ArrayList<>();
  169. if (content!=null&&content.size()>0){
  170. for (String str : content) {
  171. System.out.println("接收到需要转换Map的数据");
  172. Map<String, Object> map = xmlToMap(str);
  173. String jsonStr = mapToJson(map);
  174. maps.add(jsonStr);
  175. }
  176. }
  177. return maps;
  178. }
  179. public static String mapToXml(Map<String, Object> map, String encoding, String version) {
  180. StringWriter writer = new StringWriter();
  181. try {
  182. XMLStreamWriter xmlWriter = xmlOutputFactory.get().createXMLStreamWriter(writer);
  183. try {
  184. xmlWriter.writeStartDocument(encoding, version);
  185. writeXml(map, xmlWriter);
  186. xmlWriter.writeEndDocument();
  187. } finally {
  188. xmlWriter.close();
  189. }
  190. } catch (XMLStreamException e) {
  191. throw new RuntimeException("Map转XML失败", e);
  192. }
  193. return writer.toString();
  194. }
  195. private static void writeXml(Map<String, Object> map, XMLStreamWriter writer) throws XMLStreamException {
  196. for (Map.Entry<String, Object> entry : map.entrySet()) {
  197. String key = entry.getKey();
  198. Object value = entry.getValue();
  199. if (value instanceof Map) {
  200. writer.writeStartElement(key);
  201. writeXml((Map<String, Object>) value, writer);
  202. writer.writeEndElement();
  203. } else if (value instanceof List) {
  204. for (Object item : (List<?>) value) {
  205. writer.writeStartElement(key);
  206. if (item instanceof Map) {
  207. writeXml((Map<String, Object>) item, writer);
  208. } else {
  209. writer.writeCData(item.toString()); // 写入 CDATA
  210. }
  211. writer.writeEndElement();
  212. }
  213. } else if (key.startsWith("@")) {
  214. String attrName = key.substring(1);
  215. writer.writeAttribute(attrName, value.toString());
  216. } else if ("#text".equals(key)) {
  217. writer.writeCharacters(value.toString());
  218. } else if ("#cdata".equals(key)) {
  219. writer.writeCData(value.toString());
  220. } else if (value != null) {
  221. writer.writeStartElement(key);
  222. writer.writeCharacters(value.toString());
  223. writer.writeEndElement();
  224. }
  225. }
  226. }
  227. // ===== 工具方法 =====
  228. private static void mergeMap(Map<String, Object> target, String key, Map<String, Object> value) {
  229. if (target.containsKey(key)) {
  230. Object existing = target.get(key);
  231. if (existing instanceof List) {
  232. ((List<Object>) existing).add(value);
  233. } else if (existing instanceof Map) {
  234. List<Object> list = new ArrayList<>();
  235. list.add(existing);
  236. list.add(value);
  237. target.put(key, list);
  238. }
  239. } else {
  240. target.put(key, value);
  241. }
  242. }
  243. // ===== 自动识别格式 =====
  244. public static Map<String, Object> stringToMap(String input) {
  245. if (input == null || input.trim().isEmpty()) {
  246. throw new IllegalArgumentException("输入不能为空");
  247. }
  248. char firstChar = input.trim().charAt(0);
  249. if (firstChar == '{' || firstChar == '[') {
  250. try {
  251. return objectMapper.readValue(input, Map.class);
  252. } catch (JsonProcessingException e) {
  253. logger.error("input输入参数:{}",input);
  254. throw new RuntimeException("JSON解析失败", e);
  255. }
  256. } else if (firstChar == '<') {
  257. try {
  258. return xmlToMap(input);
  259. } catch (XMLStreamException e) {
  260. throw new RuntimeException("XML解析失败", e);
  261. }
  262. } else {
  263. throw new IllegalArgumentException("不支持的输入格式");
  264. }
  265. }
  266. public static String mapToJson(Map<?, ?> map) {
  267. try {
  268. return objectMapper.writeValueAsString(map);
  269. } catch (JsonProcessingException e) {
  270. throw new RuntimeException("Map转JSON失败", e);
  271. }
  272. }
  273. // ===== SAX 解析器回退 =====
  274. public static Map<String, Object> saxXmlToMap(String xml) throws ParserConfigurationException, SAXException, IOException {
  275. SAXParserFactory factory = saxParserFactory.get();
  276. CustomHandler handler = new CustomHandler();
  277. try (StringReader reader = new StringReader(xml)) {
  278. factory.newSAXParser().parse(new InputSource(reader), handler);
  279. }
  280. return handler.getRoot();
  281. }
  282. private static class CustomHandler extends DefaultHandler {
  283. private final Deque<String> elementStack = new ArrayDeque<>();
  284. private final Map<String, Object> root = new HashMap<>();
  285. @Override
  286. public void startElement(String uri, String localName, String qName, Attributes attributes) {
  287. String key = qName;
  288. elementStack.push(key);
  289. Map<String, Object> attrs = new HashMap<>();
  290. for (int i = 0; i < attributes.getLength(); i++) {
  291. String attrQName = attributes.getQName(i);
  292. String attrValue = attributes.getValue(i);
  293. attrs.put("@" + attrQName, attrValue);
  294. }
  295. if (!attrs.isEmpty()) {
  296. root.put("@" + key, attrs);
  297. }
  298. }
  299. @Override
  300. public void characters(char[] ch, int start, int length) {
  301. String text = new String(ch, start, length).trim();
  302. if (!text.isEmpty() && !elementStack.isEmpty()) {
  303. root.put("#text", text);
  304. }
  305. }
  306. @Override
  307. public void endElement(String uri, String localName, String qName) {
  308. elementStack.pop();
  309. }
  310. public Map<String, Object> getRoot() {
  311. return root;
  312. }
  313. }
  314. /**
  315. * 将对象转换为字符串(支持Map/List自动转JSON,其他类型返回toString)
  316. * 特性:
  317. * 1. 支持Map/List递归转换为JSON字符串
  318. * 2. 完全禁用JSON转义符生成(含ASCII控制字符)
  319. * 3. 支持任意对象转换(非容器类型直接调用toString)
  320. * 4. 处理null值安全
  321. */
  322. public static String convertObjectToString(Object obj) {
  323. if (obj == null) {
  324. return null;
  325. }
  326. try {
  327. if(obj instanceof String strObj){
  328. return strObj;
  329. }
  330. // 使用禁用转义的ObjectMapper实例
  331. return NoEscapeObjectMapperHolder.INSTANCE.writeValueAsString(obj);
  332. } catch (JsonProcessingException e) {
  333. if (obj.getClass().isArray()) {
  334. // 处理数组类型
  335. return Arrays.deepToString(new Object[]{obj}).substring(1, Arrays.deepToString(new Object[]{obj}).length()-1);
  336. }
  337. return obj.toString();
  338. }
  339. }
  340. private static class NoEscapeObjectMapperHolder {
  341. // 使用构建器创建带特性的工厂实例
  342. private static final ObjectMapper INSTANCE = new ObjectMapper(
  343. JsonFactory.builder()
  344. // .enable(JsonWriteFeature.QUOTE_CHARACTERS) // 启用无引号特性
  345. .build()
  346. );
  347. static {
  348. INSTANCE.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  349. INSTANCE.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
  350. // 保留原有配置
  351. INSTANCE.getFactory().configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, false);
  352. }
  353. }
  354. public static Map<String, Object> ObjectToMap(Object obj) {
  355. if(obj instanceof Map<?,?>){
  356. return (Map<String, Object>) obj;
  357. }
  358. return stringToMap((String) obj);
  359. }
  360. }