packagecom.jiayu.his.util;importjava.io.StringReader;importjava.io.StringWriter;importjava.util.Collection;importjava.util.concurrent.ConcurrentHashMap;importjava.util.concurrent.ConcurrentMap;importjavax.xml.bind.JAXBContext;importjavax.xml.bind.JAXBElement;importjavax.xml.bind.JAXBException;importjavax.xml.bind.Marshaller;importjavax.xml.bind.Unmarshaller;importjavax.xml.bind.annotation.XmlAnyElement;importjavax.xml.namespace.QName;importorg.apache.commons.lang3.StringUtils;importorg.apache.commons.lang3.Validate;/*** 使用Jaxb2.0实现XMLJava Object的Mapper.
* 在创建时需要设定所有需要序列化的Root对象的Class.
* 特别支持Root对象是Collection的情形.*/
public classJaxbUtil {
@SuppressWarnings("rawtypes")private static ConcurrentMap jaxbContexts = new ConcurrentHashMap();/*** Java Object->Xml without encoding.*/@SuppressWarnings("rawtypes")public staticString toXml(Object root) {
Class clazz=Reflections.getUserClass(root);return toXml(root, clazz, null);
}/*** Java Object->Xml with encoding.*/@SuppressWarnings("rawtypes")public staticString toXml(Object root, String encoding) {
Class clazz=Reflections.getUserClass(root);returntoXml(root, clazz, encoding);
}/*** Java Object->Xml with encoding.*/@SuppressWarnings("rawtypes")public staticString toXml(Object root, Class clazz, String encoding) {try{
StringWriter writer= newStringWriter();
createMarshaller(clazz, encoding).marshal(root, writer);returnwriter.toString();
}catch(JAXBException e) {throwExceptions.unchecked(e);
}
}/*** Java Collection->Xml without encoding, 特别支持Root Element是Collection的情形.*/@SuppressWarnings("rawtypes")public static String toXml(Collection>root, String rootName, Class clazz) {return toXml(root, rootName, clazz, null);
}/*** Java Collection->Xml with encoding, 特别支持Root Element是Collection的情形.*/@SuppressWarnings("rawtypes")public static String toXml(Collection>root, String rootName, Class clazz, String encoding) {try{
CollectionWrapper wrapper= newCollectionWrapper();
wrapper.collection=root;
JAXBElement wrapperElement = new JAXBElement(newQName(rootName),
CollectionWrapper.class, wrapper);
StringWriter writer= newStringWriter();
createMarshaller(clazz, encoding).marshal(wrapperElement, writer);returnwriter.toString();
}catch(JAXBException e) {throwExceptions.unchecked(e);
}
}/*** Xml->Java Object.*/@SuppressWarnings("unchecked")public static T fromXml(String xml, Classclazz) {try{
StringReader reader= newStringReader(xml);return(T) createUnmarshaller(clazz).unmarshal(reader);
}catch(JAXBException e) {throwExceptions.unchecked(e);
}
}/*** 创建Marshaller并设定encoding(可为null).
* 线程不安全,需要每次创建或pooling。*/@SuppressWarnings("rawtypes")public staticMarshaller createMarshaller(Class clazz, String encoding) {try{
JAXBContext jaxbContext=getJaxbContext(clazz);
Marshaller marshaller=jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);if(StringUtils.isNotBlank(encoding)) {
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
}returnmarshaller;
}catch(JAXBException e) {throwExceptions.unchecked(e);
}
}/*** 创建UnMarshaller.
* 线程不安全,需要每次创建或pooling。*/@SuppressWarnings("rawtypes")public staticUnmarshaller createUnmarshaller(Class clazz) {try{
JAXBContext jaxbContext=getJaxbContext(clazz);returnjaxbContext.createUnmarshaller();
}catch(JAXBException e) {throwExceptions.unchecked(e);
}
}
@SuppressWarnings("rawtypes")protected staticJAXBContext getJaxbContext(Class clazz) {
Validate.notNull(clazz,"'clazz' must not be null");
JAXBContext jaxbContext=jaxbContexts.get(clazz);if (jaxbContext == null) {try{
jaxbContext= JAXBContext.newInstance(clazz, CollectionWrapper.class);
jaxbContexts.putIfAbsent(clazz, jaxbContext);
}catch(JAXBException ex) {throw new RuntimeException("Could not instantiate JAXBContext for class [" + clazz + "]: "
+ex.getMessage(), ex);
}
}returnjaxbContext;
}/*** 封装Root Element 是 Collection的情况.*/
public static classCollectionWrapper {
@XmlAnyElementprotected Collection>collection;
}
}