优化代码
This commit is contained in:
parent
c27cd13852
commit
7c3857c789
@ -11,6 +11,7 @@ import java.util.HashMap;
|
|||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
import javax.xml.bind.JAXBContext;
|
import javax.xml.bind.JAXBContext;
|
||||||
import javax.xml.bind.JAXBElement;
|
import javax.xml.bind.JAXBElement;
|
||||||
@ -44,21 +45,11 @@ import com.foxinmy.weixin4j.util.StringUtil;
|
|||||||
public final class XmlStream {
|
public final class XmlStream {
|
||||||
private final static String ROOT_ELEMENT_XML = "xml";
|
private final static String ROOT_ELEMENT_XML = "xml";
|
||||||
private final static String XML_VERSION = "1.0";
|
private final static String XML_VERSION = "1.0";
|
||||||
private final static ThreadLocal<Map<Class<?>, Unmarshaller>> messageUnmarshaller;
|
private final static Map<Class<?>, Unmarshaller> messageUnmarshaller;
|
||||||
private final static ThreadLocal<Map<Class<?>, Marshaller>> messageMarshaller;
|
private final static Map<Class<?>, Marshaller> messageMarshaller;
|
||||||
static {
|
static {
|
||||||
messageUnmarshaller = new ThreadLocal<Map<Class<?>, Unmarshaller>>() {
|
messageUnmarshaller = new ConcurrentHashMap<Class<?>, Unmarshaller>();
|
||||||
@Override
|
messageMarshaller = new ConcurrentHashMap<Class<?>, Marshaller>();
|
||||||
protected Map<Class<?>, Unmarshaller> initialValue() {
|
|
||||||
return new HashMap<Class<?>, Unmarshaller>();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
messageMarshaller = new ThreadLocal<Map<Class<?>, Marshaller>>() {
|
|
||||||
@Override
|
|
||||||
protected Map<Class<?>, Marshaller> initialValue() {
|
|
||||||
return new HashMap<Class<?>, Marshaller>();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -72,26 +63,22 @@ public final class XmlStream {
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static <T> T fromXML(InputStream content, Class<T> clazz) {
|
public static <T> T fromXML(InputStream content, Class<T> clazz) {
|
||||||
Unmarshaller unmarshaller = messageUnmarshaller.get().get(clazz);
|
Unmarshaller unmarshaller = messageUnmarshaller.get(clazz);
|
||||||
if (unmarshaller == null) {
|
if (unmarshaller == null) {
|
||||||
try {
|
try {
|
||||||
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
|
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
|
||||||
unmarshaller = jaxbContext.createUnmarshaller();
|
unmarshaller = jaxbContext.createUnmarshaller();
|
||||||
messageUnmarshaller.get().put(clazz, unmarshaller);
|
messageUnmarshaller.put(clazz, unmarshaller);
|
||||||
} catch (JAXBException e) {
|
} catch (JAXBException e) {
|
||||||
throw new IllegalArgumentException(e);
|
throw new IllegalArgumentException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Source source = new StreamSource(content);
|
Source source = new StreamSource(content);
|
||||||
XmlRootElement rootElement = clazz
|
XmlRootElement rootElement = clazz.getAnnotation(XmlRootElement.class);
|
||||||
.getAnnotation(XmlRootElement.class);
|
|
||||||
if (rootElement == null
|
if (rootElement == null
|
||||||
|| rootElement.name().equals(
|
|| rootElement.name().equals(XmlRootElement.class.getMethod("name").getDefaultValue().toString())) {
|
||||||
XmlRootElement.class.getMethod("name")
|
JAXBElement<T> jaxbElement = unmarshaller.unmarshal(source, clazz);
|
||||||
.getDefaultValue().toString())) {
|
|
||||||
JAXBElement<T> jaxbElement = unmarshaller.unmarshal(source,
|
|
||||||
clazz);
|
|
||||||
return jaxbElement.getValue();
|
return jaxbElement.getValue();
|
||||||
} else {
|
} else {
|
||||||
return (T) unmarshaller.unmarshal(source);
|
return (T) unmarshaller.unmarshal(source);
|
||||||
@ -119,8 +106,7 @@ public final class XmlStream {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static <T> T fromXML(String content, Class<T> clazz) {
|
public static <T> T fromXML(String content, Class<T> clazz) {
|
||||||
return fromXML(
|
return fromXML(new ByteArrayInputStream(content.getBytes(Consts.UTF_8)), clazz);
|
||||||
new ByteArrayInputStream(content.getBytes(Consts.UTF_8)), clazz);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -133,12 +119,10 @@ public final class XmlStream {
|
|||||||
public static String map2xml(Map<String, String> map) {
|
public static String map2xml(Map<String, String> map) {
|
||||||
StringWriter sw = new StringWriter();
|
StringWriter sw = new StringWriter();
|
||||||
try {
|
try {
|
||||||
XMLStreamWriter xw = XMLOutputFactory.newInstance()
|
XMLStreamWriter xw = XMLOutputFactory.newInstance().createXMLStreamWriter(sw);
|
||||||
.createXMLStreamWriter(sw);
|
|
||||||
xw.writeStartDocument(Consts.UTF_8.name(), XML_VERSION);
|
xw.writeStartDocument(Consts.UTF_8.name(), XML_VERSION);
|
||||||
xw.writeStartElement(ROOT_ELEMENT_XML);
|
xw.writeStartElement(ROOT_ELEMENT_XML);
|
||||||
for (Iterator<Entry<String, String>> it = map.entrySet().iterator(); it
|
for (Iterator<Entry<String, String>> it = map.entrySet().iterator(); it.hasNext();) {
|
||||||
.hasNext();) {
|
|
||||||
Entry<String, String> entry = it.next();
|
Entry<String, String> entry = it.next();
|
||||||
if (StringUtil.isBlank(entry.getValue())) {
|
if (StringUtil.isBlank(entry.getValue())) {
|
||||||
continue;
|
continue;
|
||||||
@ -172,12 +156,10 @@ public final class XmlStream {
|
|||||||
public static String map2xml(JSONObject json) {
|
public static String map2xml(JSONObject json) {
|
||||||
StringWriter sw = new StringWriter();
|
StringWriter sw = new StringWriter();
|
||||||
try {
|
try {
|
||||||
XMLStreamWriter xw = XMLOutputFactory.newInstance()
|
XMLStreamWriter xw = XMLOutputFactory.newInstance().createXMLStreamWriter(sw);
|
||||||
.createXMLStreamWriter(sw);
|
|
||||||
xw.writeStartDocument(Consts.UTF_8.name(), XML_VERSION);
|
xw.writeStartDocument(Consts.UTF_8.name(), XML_VERSION);
|
||||||
xw.writeStartElement(ROOT_ELEMENT_XML);
|
xw.writeStartElement(ROOT_ELEMENT_XML);
|
||||||
for (Iterator<Entry<String, Object>> it = json.entrySet()
|
for (Iterator<Entry<String, Object>> it = json.entrySet().iterator(); it.hasNext();) {
|
||||||
.iterator(); it.hasNext();) {
|
|
||||||
Entry<String, Object> entry = it.next();
|
Entry<String, Object> entry = it.next();
|
||||||
if (StringUtil.isBlank(json.getString(entry.getKey()))) {
|
if (StringUtil.isBlank(json.getString(entry.getKey()))) {
|
||||||
continue;
|
continue;
|
||||||
@ -212,8 +194,7 @@ public final class XmlStream {
|
|||||||
Map<String, String> map = new HashMap<String, String>();
|
Map<String, String> map = new HashMap<String, String>();
|
||||||
StringReader sr = new StringReader(content);
|
StringReader sr = new StringReader(content);
|
||||||
try {
|
try {
|
||||||
XMLStreamReader xr = XMLInputFactory.newInstance()
|
XMLStreamReader xr = XMLInputFactory.newInstance().createXMLStreamReader(sr);
|
||||||
.createXMLStreamReader(sr);
|
|
||||||
while (true) {
|
while (true) {
|
||||||
int event = xr.next();
|
int event = xr.next();
|
||||||
if (event == XMLStreamConstants.END_DOCUMENT) {
|
if (event == XMLStreamConstants.END_DOCUMENT) {
|
||||||
@ -266,27 +247,22 @@ public final class XmlStream {
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static <T> void toXML(T t, OutputStream os) {
|
public static <T> void toXML(T t, OutputStream os) {
|
||||||
Class<T> clazz = (Class<T>) t.getClass();
|
Class<T> clazz = (Class<T>) t.getClass();
|
||||||
Marshaller marshaller = messageMarshaller.get().get(clazz);
|
Marshaller marshaller = messageMarshaller.get(clazz);
|
||||||
if (marshaller == null) {
|
if (marshaller == null) {
|
||||||
try {
|
try {
|
||||||
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
|
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
|
||||||
marshaller = jaxbContext.createMarshaller();
|
marshaller = jaxbContext.createMarshaller();
|
||||||
marshaller.setProperty(Marshaller.JAXB_ENCODING,
|
marshaller.setProperty(Marshaller.JAXB_ENCODING, Consts.UTF_8.name());
|
||||||
Consts.UTF_8.name());
|
messageMarshaller.put(clazz, marshaller);
|
||||||
messageMarshaller.get().put(clazz, marshaller);
|
|
||||||
} catch (JAXBException e) {
|
} catch (JAXBException e) {
|
||||||
throw new IllegalArgumentException(e);
|
throw new IllegalArgumentException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
XmlRootElement rootElement = clazz
|
XmlRootElement rootElement = clazz.getAnnotation(XmlRootElement.class);
|
||||||
.getAnnotation(XmlRootElement.class);
|
|
||||||
if (rootElement == null
|
if (rootElement == null
|
||||||
|| rootElement.name().equals(
|
|| rootElement.name().equals(XmlRootElement.class.getMethod("name").getDefaultValue().toString())) {
|
||||||
XmlRootElement.class.getMethod("name")
|
marshaller.marshal(new JAXBElement<T>(new QName(ROOT_ELEMENT_XML), clazz, t), os);
|
||||||
.getDefaultValue().toString())) {
|
|
||||||
marshaller.marshal(new JAXBElement<T>(new QName(
|
|
||||||
ROOT_ELEMENT_XML), clazz, t), os);
|
|
||||||
} else {
|
} else {
|
||||||
marshaller.marshal(t, os);
|
marshaller.marshal(t, os);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -326,8 +326,8 @@ public class WeixinProxy {
|
|||||||
* 要修改的图文消息的id
|
* 要修改的图文消息的id
|
||||||
* @param index
|
* @param index
|
||||||
* 要更新的文章在图文消息中的位置(多图文消息时,此字段才有意义),第一篇为0
|
* 要更新的文章在图文消息中的位置(多图文消息时,此字段才有意义),第一篇为0
|
||||||
* @param articles
|
* @param article
|
||||||
* 图文列表
|
* 图文对象
|
||||||
* @return 处理结果
|
* @return 处理结果
|
||||||
* @throws WeixinException
|
* @throws WeixinException
|
||||||
* @see com.foxinmy.weixin4j.mp.api.MediaApi
|
* @see com.foxinmy.weixin4j.mp.api.MediaApi
|
||||||
|
|||||||
@ -346,8 +346,8 @@ public class MediaApi extends MpApi {
|
|||||||
* 要修改的图文消息的id
|
* 要修改的图文消息的id
|
||||||
* @param index
|
* @param index
|
||||||
* 要更新的文章在图文消息中的位置(多图文消息时,此字段才有意义),第一篇为0
|
* 要更新的文章在图文消息中的位置(多图文消息时,此字段才有意义),第一篇为0
|
||||||
* @param articles
|
* @param article
|
||||||
* 图文列表
|
* 图文对象
|
||||||
* @return 处理结果
|
* @return 处理结果
|
||||||
* @throws WeixinException
|
* @throws WeixinException
|
||||||
* @see com.foxinmy.weixin4j.tuple.MpArticle
|
* @see com.foxinmy.weixin4j.tuple.MpArticle
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user