Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
liyiorg committed Nov 12, 2015
2 parents 5efe1ce + d06d708 commit fe2d76b
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions src/main/java/weixin/popular/util/XMLConverUtil.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
package weixin.popular.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import com.sun.xml.bind.marshaller.CharacterEscapeHandler;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
* XML 数据接收对象转换工具类
Expand All @@ -24,8 +17,19 @@
*/
public class XMLConverUtil{

private static Map<Class<?>,Unmarshaller> uMap = new HashMap<Class<?>,Unmarshaller>();
private static Map<Class<?>,Marshaller> mMap = new HashMap<Class<?>,Marshaller>();
private static final ThreadLocal<Map<Class<?>,Marshaller>> mMapLocal = new ThreadLocal<Map<Class<?>,Marshaller>>() {
@Override
protected Map<Class<?>, Marshaller> initialValue() {
return new HashMap<>();
}
};

private static final ThreadLocal<Map<Class<?>,Unmarshaller>> uMapLocal = new ThreadLocal<Map<Class<?>,Unmarshaller>>(){
@Override
protected Map<Class<?>, Unmarshaller> initialValue() {
return new HashMap<>();
}
};

/**
* XML to Object
Expand Down Expand Up @@ -59,12 +63,13 @@ public static <T> T convertToObject(Class<T> clazz,InputStream inputStream){
@SuppressWarnings("unchecked")
public static <T> T convertToObject(Class<T> clazz,Reader reader){
try {
Map<Class<?>, Unmarshaller> uMap = uMapLocal.get();
if(!uMap.containsKey(clazz)){
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
uMap.put(clazz,unmarshaller);
uMap.put(clazz, unmarshaller);
}
return (T)uMap.get(clazz).unmarshal(reader);
return (T) uMap.get(clazz).unmarshal(reader);
} catch (JAXBException e) {
e.printStackTrace();
}
Expand All @@ -78,6 +83,7 @@ public static <T> T convertToObject(Class<T> clazz,Reader reader){
*/
public static String convertToXML(Object object){
try {
Map<Class<?>, Marshaller> mMap = mMapLocal.get();
if(!mMap.containsKey(object.getClass())){
JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
Marshaller marshaller = jaxbContext.createMarshaller();
Expand Down

0 comments on commit fe2d76b

Please sign in to comment.