Jaxb工具类

package com.apusic.esb.util.jaxb;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

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

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;

import com.apusic.esb.util.UtilPlugin;

/**
 * JAXB工具,通过使用注解,实现java对象与XML文件的相互转换
 * @author TigerlChen
 */
public class JaxbUtil {
	private static ConcurrentHashMap<Class<?>, JAXBContext> jaxbContextMap = new ConcurrentHashMap<Class<?>,JAXBContext>();
	
	/**
     * 将JAXB实现对象转换成XML格式的字符串
     * @param <T> 这里的类是具体类,不能是接口
     * @param tclz 转换对象实例
     * @return
     */
    public static <T> String marshToXmlBinding(Class<T> tclz,T t) throws JAXBException {
            JAXBContext jc = null;
            if(jaxbContextMap.get(tclz)==null) {
                Map<String, String> properties = new HashMap<String, String>();
            	jc = JAXBContext.newInstance(new Class<?>[]{tclz},properties);
            	jaxbContextMap.put(tclz, jc);
            }else{
            	jc = jaxbContextMap.get(tclz);
            }
            
            Marshaller u = jc.createMarshaller();
            // XML内容格式化
            u.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            StringWriter  sw = new  StringWriter ();
            u.marshal(t, sw);
            return sw.toString();
    }
    
    /**
     * 将XML格式的字符串转换成JAXB实现对象
     * @param <T> 这里的类是具体类,不能是接口
     * @param tclz
     * @param file 当目录存在文件不存在时,自动创建;当目录不存在时,将抛异常
     * @return
     */
    public static <T> void marshToXmlBinding(Class<T> tclz, T t, File file) throws JAXBException {
        if (tclz == null || file == null) {
            return ;
        }
        
        JAXBContext jc = null;
        if (jaxbContextMap.get(tclz) == null) {
            Map<String, String> properties = new HashMap<String, String>();
            jc = JAXBContext.newInstance(new Class<?>[] {tclz}, properties);
            jaxbContextMap.put(tclz, jc);
        } else {
            jc = jaxbContextMap.get(tclz);
        }

        Marshaller u = jc.createMarshaller();
        u.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        u.marshal(t, file);
    }
    
    public static <T> void marshToXmlBinding(Class<T> tclz, T t, IFile file) throws JAXBException {
        String str = marshToXmlBinding(tclz, t);
        ByteArrayInputStream stream = null;
        try {
            stream = new ByteArrayInputStream(str.getBytes("UTF8"));
            if (!file.exists()) {

                file.create(stream, true, new NullProgressMonitor());

            } else {
                file.setContents(stream, true, false, new NullProgressMonitor());
            }
        } catch (CoreException e) {
            UtilPlugin.log(e);
        } catch (UnsupportedEncodingException e) {
            UtilPlugin.log(e);
        }
    }

    /**
     * 将XML格式的字符串转换成JAXB实现对象
     * @param <T> 这里的类是具体类,不能是接口
     * @param tclz
     * @param xmlstr
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T unmarshToObjBinding(Class<T> tclz,String xmlstr) {
        try {
            JAXBContext jc = null;
            if(jaxbContextMap.get(tclz)==null) {
                Map<String, String> properties = new HashMap<String, String>();
            	jc = JAXBContext.newInstance(new Class<?>[]{tclz},properties);
            	jaxbContextMap.put(tclz, jc);
            }else{
            	jc = jaxbContextMap.get(tclz);
            }
            
            Unmarshaller un = jc.createUnmarshaller();
            return (T) un.unmarshal(new ByteArrayInputStream(xmlstr.getBytes("utf-8")));
            
        } catch (JAXBException e) {
            UtilPlugin.log(e);
        } catch (UnsupportedEncodingException e) {
            UtilPlugin.log(e);
		}
		return null;
    }
    
    /**
     * 将XML格式的文件转换成JAXB实现对象
     * @param <T>
     * @param tclz
     * @param file
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T unmarshToObjBinding(Class<T> tclz,File file) {
        if (tclz == null || file == null || !file.exists()) {
            return null;
        }
        
        try {
            JAXBContext jc = null;
            if(jaxbContextMap.get(tclz)==null) {
                Map<String, String> properties = new HashMap<String, String>();
                jc = JAXBContext.newInstance(new Class<?>[]{tclz},properties);
                jaxbContextMap.put(tclz, jc);
            }else{
                jc = jaxbContextMap.get(tclz);
            }
            
            Unmarshaller un = jc.createUnmarshaller();
            return (T) un.unmarshal(file);
            
        } catch (JAXBException e) {
            UtilPlugin.log(e);
        }
        return null;
    }
    
    @SuppressWarnings("unchecked")
    public static <T> T unmarshToObjBinding(Class<T> tclz,InputStream inputStream) {
        if (tclz == null || inputStream == null) {
            return null;
        }
        
        try {
            JAXBContext jc = null;
            if(jaxbContextMap.get(tclz)==null) {
                Map<String, String> properties = new HashMap<String, String>();
                jc = JAXBContext.newInstance(new Class<?>[]{tclz},properties);
                jaxbContextMap.put(tclz, jc);
            }else{
                jc = jaxbContextMap.get(tclz);
            }
            
            Unmarshaller un = jc.createUnmarshaller();
            return (T) un.unmarshal(inputStream);
            
        } catch (JAXBException e) {
            UtilPlugin.log(e);
        }
        return null;
    }
    
    public static <T> T unmarshToObjBinding(Class<T> tclz, IFile file) {
        return unmarshToObjBinding(tclz, file.getLocation().toFile());
    }
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值