package cc.smtweb.system.bpm.util;

import cc.smtweb.framework.core.exception.JsonParseException;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class XmlUtil {
    private static final XmlMapper OBJECT_MAPPER = new XmlMapper();

    static {
        OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        OBJECT_MAPPER.enable(SerializationFeature.INDENT_OUTPUT);
    }

    private XmlUtil() {
    }

    public static <T> T readValue(String str, Class<T> clazz) {
        try {
            if (StringUtils.isBlank(str)) {
                return null;
            } else {
                return OBJECT_MAPPER.readValue(str, clazz);
            }
        } catch (Exception e) {
            throw new JsonParseException("can't convert this json to " + clazz + " type", e);
        }
    }

    public static <T> T readValue(File file, Class<T> clazz) {
        try {
            if (file == null || !file.exists()) {
                return null;
            } else {
                return OBJECT_MAPPER.readValue(file, clazz);
            }
        } catch (Exception e) {
            throw new JsonParseException("can't convert this json to " + clazz + " type", e);
        }
    }

    public static <T> T readValue(InputStream is, Class<T> clazz) {
        try {
            if (is == null) {
                return null;
            } else {
                return OBJECT_MAPPER.readValue(is, clazz);
            }
        } catch (Exception e) {
            throw new JsonParseException("can't convert this xml to " + clazz + " type", e);
        }
    }

    public static String writeValue(Object obj) {
        try {
            return OBJECT_MAPPER.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new JsonParseException("can't convert type " + obj.getClass() + " to json string", e);
        }
    }

    public static void writeValue(OutputStream outputStream, Object obj) {
        try {
            OBJECT_MAPPER.writeValue(outputStream, obj);
        } catch (JsonProcessingException e) {
            throw new JsonParseException("can't convert type " + obj.getClass() + " to json string", e);
        } catch (IOException e) {
            throw new JsonParseException(e.getMessage(), e);
        }
    }

    public static void writeValue(File file, Object obj) {
        try {
            OBJECT_MAPPER.writeValue(file, obj);
        } catch (JsonProcessingException e) {
            throw new JsonParseException("can't convert type " + obj.getClass() + " to json string", e);
        } catch (IOException e) {
            throw new JsonParseException(e.getMessage(), e);
        }
    }
}
