All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.ipokerface.weixin.xml.ListSuffixResultDeserializer Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
package cn.ipokerface.weixin.xml;

import cn.ipokerface.weixin.Constant;
import cn.ipokerface.weixin.utils.ReflectionUtils;
import cn.ipokerface.weixin.utils.StringUtil;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by       PokerFace
 * Create Date      2019-12-28.
 * Email:           [email protected]
 * Version          1.0.0
 * 

* Description: */ public class ListSuffixResultDeserializer { private static Pattern DEFAULT_PATTERN; static { String regex = null; try { Object value = ListSuffixResult.class.getMethod("value") .getDefaultValue(); if (value instanceof String) { regex = (String) value; } else if (value instanceof String[]) { regex = ((String[]) value)[0]; } } catch (NoSuchMethodException e) { ; } if (StringUtil.isBlank(regex)) { regex = "(_\\d)$"; } DEFAULT_PATTERN = Pattern.compile(regex); } /** * 对包含$n节点的xml反序列化 * * @param content * xml内容 * @param clazz * @return */ public static T deserialize(String content, Class clazz) { T t = XmlFormatter.fromXML(content, clazz); Map listsuffixFields = getListsuffixFields(clazz); if (!listsuffixFields.isEmpty()) { for (Map.Entry entry : listsuffixFields.entrySet()) { Field field = entry.getKey(); Type type = field.getGenericType(); Class wrapperClazz = null; if (type instanceof ParameterizedType) { wrapperClazz = (Class) ((ParameterizedType) type) .getActualTypeArguments()[0]; } else { continue; } ListWrapper listWrapper = deserializeToListWrapper(content, wrapperClazz, entry.getValue()); if (listWrapper != null) { try { field.setAccessible(true); field.set(t, listWrapper.getItems()); } catch (Exception e) { ; } } } } return t; } @SuppressWarnings("unchecked") public static ListWrapper deserializeToListWrapper(String content, Class clazz, String... matchPattern) { XMLStreamReader xr = null; XMLStreamWriter xw = null; try { xr = XMLInputFactory.newInstance().createXMLStreamReader( new StringReader(content)); List patterns = new ArrayList(); for (String pattern : matchPattern) { patterns.add(Pattern.compile(pattern)); } Matcher matcher = null; Map> outMap = new HashMap>(); while (true) { int event = xr.next(); if (event == XMLStreamConstants.END_DOCUMENT) { break; } else if (event == XMLStreamConstants.START_ELEMENT) { String name = xr.getLocalName(); for (Pattern pattern : patterns) { if ((matcher = pattern.matcher(name)).find()) { while (true) { event = xr.next(); if (event == XMLStreamConstants.START_ELEMENT) { name = xr.getLocalName(); } else if (event == XMLStreamConstants.END_ELEMENT) { break; } else if (event == XMLStreamConstants.CHARACTERS || event == XMLStreamConstants.CDATA) { String key = matcher.group(); if (!pattern.pattern().equals( DEFAULT_PATTERN.pattern())) { matcher = DEFAULT_PATTERN.matcher(name); matcher.find(); key = matcher.group(); } Map innerMap = null; if ((innerMap = outMap.get(key)) == null) { innerMap = new HashMap(); outMap.put(key, innerMap); } innerMap.put(name.replace(key, ""), xr.getText()); } } break; } } } } if (!outMap.isEmpty()) { StringWriter sw = new StringWriter(); xw = XMLOutputFactory.newInstance().createXMLStreamWriter(sw); xw.writeStartDocument(Constant.UTF_8.name(), "1.0"); xw.writeStartElement(clazz.getCanonicalName()); String itemName = StringUtil .uncapitalize(clazz.getSimpleName()); XmlRootElement rootElement = clazz .getAnnotation(XmlRootElement.class); if (rootElement != null && StringUtil.isNotBlank(rootElement.name())) { try { if (!rootElement.name().equals( XmlRootElement.class.getMethod("name") .getDefaultValue().toString())) { itemName = rootElement.name(); } } catch (NoSuchMethodException e) { ; } } for (Map.Entry> outE : outMap .entrySet()) { xw.writeStartElement(itemName); for (Map.Entry innerE : outE .getValue().entrySet()) { xw.writeStartElement(innerE.getKey()); xw.writeCharacters(innerE.getValue()); xw.writeEndElement(); } xw.writeEndElement(); } xw.writeEndElement(); xw.writeEndDocument(); JAXBContext ctx = JAXBContext.newInstance(ListWrapper.class, clazz); Unmarshaller u = ctx.createUnmarshaller(); return u.unmarshal( new StreamSource(new StringReader(sw.getBuffer() .toString())), ListWrapper.class).getValue(); } return null; } catch (XMLStreamException e) { throw new IllegalArgumentException(e); } catch (JAXBException e) { throw new RuntimeException(e); } finally { try { if (xw != null) { xw.close(); } if (xr != null) { xr.close(); } } catch (XMLStreamException e) { ; } } } public static Map getListsuffixFields(Class clazz) { Map listsuffixFields = new HashMap(); Set allFields = ReflectionUtils.getAllField(clazz); ListSuffixResult listsuffixResult = null; for (Field field : allFields) { listsuffixResult = field.getAnnotation(ListSuffixResult.class); if (listsuffixResult != null) { listsuffixFields.put(field, listsuffixResult.value()); } } return listsuffixFields; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy