001package org.kuali.ole.docstore.common.document;
002
003import org.apache.log4j.Logger;
004import org.kuali.ole.docstore.common.util.ParseXml;
005import org.w3c.dom.NodeList;
006import org.xml.sax.InputSource;
007
008import javax.xml.bind.*;
009import javax.xml.bind.annotation.*;
010import javax.xml.parsers.DocumentBuilder;
011import javax.xml.parsers.DocumentBuilderFactory;
012import javax.xml.parsers.ParserConfigurationException;
013import javax.xml.transform.stream.StreamSource;
014import java.io.ByteArrayInputStream;
015import java.io.IOException;
016import java.io.StringReader;
017import java.io.StringWriter;
018import java.util.ArrayList;
019import java.util.List;
020
021import org.w3c.dom.Document;
022import org.w3c.dom.Node;
023import org.xml.sax.SAXException;
024
025
026/**
027 * Created with IntelliJ IDEA.
028 * User: chenchulakshmig
029 * Date: 2/25/14
030 * Time: 3:41 PM
031 * To change this template use File | Settings | File Templates.
032 */
033@XmlAccessorType(XmlAccessType.FIELD)
034@XmlType(name = "licenses", propOrder = {
035        "licenses"
036})
037@XmlRootElement
038public class Licenses {
039
040    private static final Logger LOG = Logger.getLogger(Licenses.class);
041
042    @XmlElement(name = "license")
043    protected List<License> licenses;
044
045    public List<License> getLicenses() {
046        if (licenses == null) {
047            licenses = new ArrayList<>();
048        }
049        return licenses;
050    }
051
052    public static String serialize(Object object) {
053        String result = null;
054        StringWriter sw = new StringWriter();
055        Licenses licenses = (Licenses) object;
056        for(License license : licenses.getLicenses()) {
057            if(license instanceof LicenseOnixpl) {
058                try {
059                    JAXBContext jaxbContext = JAXBContext.newInstance(LicenseOnixpl.class);
060                    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
061                    jaxbMarshaller.marshal(license, sw);
062                } catch (Exception e) {
063                    LOG.error("Exception :", e);
064                }
065
066            }
067            else {
068                try {
069                    JAXBContext jaxbContext = JAXBContext.newInstance(LicenseAttachment.class);
070                    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
071                    jaxbMarshaller.marshal(license, sw);
072                } catch (Exception e) {
073                    LOG.error("Exception :", e);
074                }
075
076            }
077        }
078
079        result = "<licenses>" + sw.append("</licenses>").toString();
080        result = result.replace("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>", "");
081        return result;
082    }
083
084    public static Object deserialize(String licensesXml) {
085        JAXBElement<Licenses> licensesElement = null;
086        try {
087            JAXBContext jaxbContext = JAXBContext.newInstance(Licenses.class);
088            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
089            ByteArrayInputStream input = new ByteArrayInputStream(licensesXml.getBytes("UTF-8"));
090            licensesElement = jaxbUnmarshaller.unmarshal(new StreamSource(input), Licenses.class);
091        } catch (Exception e) {
092            LOG.error("Exception :", e);
093        }
094        Licenses licenses = licensesElement.getValue();
095        licenses.getLicenses().clear();
096        try {
097            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
098            DocumentBuilder builder = factory.newDocumentBuilder();
099            Document doc = builder.parse(new InputSource(new StringReader(licensesXml)));
100            NodeList root = doc.getChildNodes();
101            Node licenseTree = ParseXml.getNode("licenses", root);
102            NodeList nodeList = doc.getElementsByTagName("license");
103            for (int i = 0; i < nodeList.getLength(); i++) {
104                Node license = ParseXml.getNodeXml(licenseTree.getChildNodes(), i);
105                NodeList nodes = license.getChildNodes();
106                String licensexml = ParseXml.nodeToString(license);
107                if (ParseXml.getNodeValue("format", nodes).equals("onixpl")) {
108                    JAXBContext jc = JAXBContext.newInstance(LicenseOnixpl.class);
109                    Unmarshaller unmarshaller1 = jc.createUnmarshaller();
110                    StreamSource xmlSource = new StreamSource(new StringReader(licensexml));
111                    JAXBElement<LicenseOnixpl> je1 = unmarshaller1.unmarshal(xmlSource, LicenseOnixpl.class);
112                    LicenseOnixpl licenseOnixpl = je1.getValue();
113                    licenses.getLicenses().add(licenseOnixpl);
114                } else {
115                    JAXBContext jc = JAXBContext.newInstance(LicenseAttachment.class);
116                    Unmarshaller unmarshaller1 = jc.createUnmarshaller();
117                    StreamSource xmlSource = new StreamSource(new StringReader(licensexml));
118                    JAXBElement<LicenseAttachment> je1 = unmarshaller1.unmarshal(xmlSource, LicenseAttachment.class);
119                    LicenseAttachment licenseAttachment = je1.getValue();
120                    licenses.getLicenses().add(licenseAttachment);
121                }
122            }
123        } catch (ParserConfigurationException e) {
124            LOG.error("Exception ", e);
125        } catch (SAXException e) {
126            LOG.error("Exception ", e);
127        } catch (IOException e) {
128            LOG.error("Exception ", e);
129        } catch (JAXBException e) {
130            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
131        }
132        return licenses;
133    }
134
135}