001
002package gudusoft.gsqlparser.dlineage.util;
003
004import gudusoft.gsqlparser.util.Logger;
005import gudusoft.gsqlparser.util.LoggerFactory;
006import gudusoft.gsqlparser.util.SQLUtil;
007
008import javax.xml.bind.JAXBContext;
009import javax.xml.bind.Marshaller;
010import javax.xml.bind.Unmarshaller;
011import java.io.*;
012import java.util.UUID;
013import java.util.zip.ZipEntry;
014import java.util.zip.ZipInputStream;
015import java.util.zip.ZipOutputStream;
016
017@SuppressWarnings("unchecked")
018public class XML2Model {
019    private static final Logger logger = LoggerFactory.getLogger(XML2Model.class);
020    public static <T> T loadXML(Class<T> t, String xml) {
021        StringReader reader = null;
022        try {
023            JAXBContext context = JAXBContext.newInstance(t);
024            Unmarshaller unmarshaller = context.createUnmarshaller();
025            reader = new StringReader(xml);
026            return (T) unmarshaller.unmarshal(reader);
027        } catch (Exception e) {
028            logger.error("load java object from xml failed.", e);
029            return null;
030        } finally {
031            if (reader != null) {
032                reader.close();
033            }
034        }
035    }
036
037    public static <T> T loadXML(Class<T> t, File file) {
038        ZipInputStream zis = null;
039        BufferedReader reader = null;
040        try {
041            JAXBContext context = JAXBContext.newInstance(t);
042            Unmarshaller unmarshaller = context.createUnmarshaller();
043            zis = new ZipInputStream(new FileInputStream(file));
044            zis.getNextEntry();
045            reader = new BufferedReader(new InputStreamReader(zis, "UTF-8"));
046            return (T) unmarshaller.unmarshal(reader);
047        } catch (Exception e) {
048            logger.error("load java object from xml failed.", e);
049            return null;
050        } finally {
051            if (reader != null) {
052                try {
053                    reader.close();
054                } catch (IOException e) {
055                    logger.error("Close reader failed.", e);
056                }
057            }
058            if (zis != null) {
059                try {
060                    zis.close();
061                } catch (IOException e) {
062                    logger.error("Close ZipInputStream failed.", e);
063                }
064            }
065        }
066    }
067
068    public static <T> String saveXML(T t) throws Exception {
069        File tempFile = null;
070        try {
071            JAXBContext context = JAXBContext.newInstance(t.getClass());
072            Marshaller marshaller = context.createMarshaller();
073            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
074            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
075            tempFile = File.createTempFile(UUID.randomUUID().toString(), null);
076            FileWriter writer = new FileWriter(tempFile);
077            marshaller.marshal(t, writer);
078            writer.close();
079            String xml = SQLUtil.readFile(tempFile);
080            return xml;
081        } catch (Exception e) {
082            throw e;
083        } finally {
084            if (tempFile != null && tempFile.exists()) {
085                tempFile.delete();
086            }
087        }
088    }
089
090    public static <T> void saveXML(T t, File file) throws Exception {
091        BufferedWriter writer = null;
092        ZipOutputStream zos = null;
093        try {
094            JAXBContext context = JAXBContext.newInstance(t.getClass());
095            Marshaller marshaller = context.createMarshaller();
096            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
097            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
098            zos = new ZipOutputStream(new FileOutputStream(file));
099            zos.setLevel(2);
100            zos.putNextEntry(new ZipEntry("data"));
101            writer = new BufferedWriter(new OutputStreamWriter(zos, "UTF-8"));
102            marshaller.marshal(t, writer);
103        } catch (Exception e) {
104            throw e;
105        } finally {
106            if (writer != null) {
107                try {
108                    writer.close();
109                } catch (IOException e) {
110                    logger.error("Close writer failed.", e);
111                }
112            }
113
114            if (zos != null) {
115                try {
116                    zos.close();
117                } catch (IOException e) {
118                    logger.error("Close ZipOutputStream failed.", e);
119                }
120            }
121        }
122    }
123}