优化代码

This commit is contained in:
jinyu 2016-04-01 18:22:44 +08:00
parent c27cd13852
commit 7c3857c789
3 changed files with 26 additions and 50 deletions

View File

@ -11,6 +11,7 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
@ -44,21 +45,11 @@ import com.foxinmy.weixin4j.util.StringUtil;
public final class XmlStream {
private final static String ROOT_ELEMENT_XML = "xml";
private final static String XML_VERSION = "1.0";
private final static ThreadLocal<Map<Class<?>, Unmarshaller>> messageUnmarshaller;
private final static ThreadLocal<Map<Class<?>, Marshaller>> messageMarshaller;
private final static Map<Class<?>, Unmarshaller> messageUnmarshaller;
private final static Map<Class<?>, Marshaller> messageMarshaller;
static {
messageUnmarshaller = new ThreadLocal<Map<Class<?>, Unmarshaller>>() {
@Override
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>();
}
};
messageUnmarshaller = new ConcurrentHashMap<Class<?>, Unmarshaller>();
messageMarshaller = new ConcurrentHashMap<Class<?>, Marshaller>();
}
/**
@ -72,26 +63,22 @@ public final class XmlStream {
*/
@SuppressWarnings("unchecked")
public static <T> T fromXML(InputStream content, Class<T> clazz) {
Unmarshaller unmarshaller = messageUnmarshaller.get().get(clazz);
Unmarshaller unmarshaller = messageUnmarshaller.get(clazz);
if (unmarshaller == null) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
unmarshaller = jaxbContext.createUnmarshaller();
messageUnmarshaller.get().put(clazz, unmarshaller);
messageUnmarshaller.put(clazz, unmarshaller);
} catch (JAXBException e) {
throw new IllegalArgumentException(e);
}
}
try {
Source source = new StreamSource(content);
XmlRootElement rootElement = clazz
.getAnnotation(XmlRootElement.class);
XmlRootElement rootElement = clazz.getAnnotation(XmlRootElement.class);
if (rootElement == null
|| rootElement.name().equals(
XmlRootElement.class.getMethod("name")
.getDefaultValue().toString())) {
JAXBElement<T> jaxbElement = unmarshaller.unmarshal(source,
clazz);
|| rootElement.name().equals(XmlRootElement.class.getMethod("name").getDefaultValue().toString())) {
JAXBElement<T> jaxbElement = unmarshaller.unmarshal(source, clazz);
return jaxbElement.getValue();
} else {
return (T) unmarshaller.unmarshal(source);
@ -119,8 +106,7 @@ public final class XmlStream {
* @return
*/
public static <T> T fromXML(String content, Class<T> clazz) {
return fromXML(
new ByteArrayInputStream(content.getBytes(Consts.UTF_8)), clazz);
return fromXML(new ByteArrayInputStream(content.getBytes(Consts.UTF_8)), clazz);
}
/**
@ -133,12 +119,10 @@ public final class XmlStream {
public static String map2xml(Map<String, String> map) {
StringWriter sw = new StringWriter();
try {
XMLStreamWriter xw = XMLOutputFactory.newInstance()
.createXMLStreamWriter(sw);
XMLStreamWriter xw = XMLOutputFactory.newInstance().createXMLStreamWriter(sw);
xw.writeStartDocument(Consts.UTF_8.name(), XML_VERSION);
xw.writeStartElement(ROOT_ELEMENT_XML);
for (Iterator<Entry<String, String>> it = map.entrySet().iterator(); it
.hasNext();) {
for (Iterator<Entry<String, String>> it = map.entrySet().iterator(); it.hasNext();) {
Entry<String, String> entry = it.next();
if (StringUtil.isBlank(entry.getValue())) {
continue;
@ -172,12 +156,10 @@ public final class XmlStream {
public static String map2xml(JSONObject json) {
StringWriter sw = new StringWriter();
try {
XMLStreamWriter xw = XMLOutputFactory.newInstance()
.createXMLStreamWriter(sw);
XMLStreamWriter xw = XMLOutputFactory.newInstance().createXMLStreamWriter(sw);
xw.writeStartDocument(Consts.UTF_8.name(), XML_VERSION);
xw.writeStartElement(ROOT_ELEMENT_XML);
for (Iterator<Entry<String, Object>> it = json.entrySet()
.iterator(); it.hasNext();) {
for (Iterator<Entry<String, Object>> it = json.entrySet().iterator(); it.hasNext();) {
Entry<String, Object> entry = it.next();
if (StringUtil.isBlank(json.getString(entry.getKey()))) {
continue;
@ -212,8 +194,7 @@ public final class XmlStream {
Map<String, String> map = new HashMap<String, String>();
StringReader sr = new StringReader(content);
try {
XMLStreamReader xr = XMLInputFactory.newInstance()
.createXMLStreamReader(sr);
XMLStreamReader xr = XMLInputFactory.newInstance().createXMLStreamReader(sr);
while (true) {
int event = xr.next();
if (event == XMLStreamConstants.END_DOCUMENT) {
@ -266,27 +247,22 @@ public final class XmlStream {
@SuppressWarnings("unchecked")
public static <T> void toXML(T t, OutputStream os) {
Class<T> clazz = (Class<T>) t.getClass();
Marshaller marshaller = messageMarshaller.get().get(clazz);
Marshaller marshaller = messageMarshaller.get(clazz);
if (marshaller == null) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING,
Consts.UTF_8.name());
messageMarshaller.get().put(clazz, marshaller);
marshaller.setProperty(Marshaller.JAXB_ENCODING, Consts.UTF_8.name());
messageMarshaller.put(clazz, marshaller);
} catch (JAXBException e) {
throw new IllegalArgumentException(e);
}
}
try {
XmlRootElement rootElement = clazz
.getAnnotation(XmlRootElement.class);
XmlRootElement rootElement = clazz.getAnnotation(XmlRootElement.class);
if (rootElement == null
|| rootElement.name().equals(
XmlRootElement.class.getMethod("name")
.getDefaultValue().toString())) {
marshaller.marshal(new JAXBElement<T>(new QName(
ROOT_ELEMENT_XML), clazz, t), os);
|| rootElement.name().equals(XmlRootElement.class.getMethod("name").getDefaultValue().toString())) {
marshaller.marshal(new JAXBElement<T>(new QName(ROOT_ELEMENT_XML), clazz, t), os);
} else {
marshaller.marshal(t, os);
}

View File

@ -326,8 +326,8 @@ public class WeixinProxy {
* 要修改的图文消息的id
* @param index
* 要更新的文章在图文消息中的位置多图文消息时此字段才有意义第一篇为0
* @param articles
* 图文列表
* @param article
* 图文对象
* @return 处理结果
* @throws WeixinException
* @see com.foxinmy.weixin4j.mp.api.MediaApi

View File

@ -346,8 +346,8 @@ public class MediaApi extends MpApi {
* 要修改的图文消息的id
* @param index
* 要更新的文章在图文消息中的位置多图文消息时此字段才有意义第一篇为0
* @param articles
* 图文列表
* @param article
* 图文对象
* @return 处理结果
* @throws WeixinException
* @see com.foxinmy.weixin4j.tuple.MpArticle