1 package org.kuali.ole.docstore.common.document;
2
3 import org.apache.log4j.Logger;
4 import org.kuali.ole.docstore.common.util.ParseXml;
5 import org.w3c.dom.NodeList;
6 import org.xml.sax.InputSource;
7
8 import javax.xml.bind.*;
9 import javax.xml.bind.annotation.*;
10 import javax.xml.parsers.DocumentBuilder;
11 import javax.xml.parsers.DocumentBuilderFactory;
12 import javax.xml.parsers.ParserConfigurationException;
13 import javax.xml.transform.stream.StreamSource;
14 import java.io.ByteArrayInputStream;
15 import java.io.IOException;
16 import java.io.StringReader;
17 import java.io.StringWriter;
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Node;
23 import org.xml.sax.SAXException;
24
25
26
27
28
29
30
31
32
33 @XmlAccessorType(XmlAccessType.FIELD)
34 @XmlType(name = "licenses", propOrder = {
35 "licenses"
36 })
37 @XmlRootElement
38 public class Licenses {
39
40 private static final Logger LOG = Logger.getLogger(Licenses.class);
41
42 @XmlElement(name = "license")
43 protected List<License> licenses;
44
45 public List<License> getLicenses() {
46 if (licenses == null) {
47 licenses = new ArrayList<>();
48 }
49 return licenses;
50 }
51
52 public static String serialize(Object object) {
53 String result = null;
54 StringWriter sw = new StringWriter();
55 Licenses licenses = (Licenses) object;
56 for(License license : licenses.getLicenses()) {
57 if(license instanceof LicenseOnixpl) {
58 try {
59 JAXBContext jaxbContext = JAXBContext.newInstance(LicenseOnixpl.class);
60 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
61 jaxbMarshaller.marshal(license, sw);
62 } catch (Exception e) {
63 LOG.error("Exception :", e);
64 }
65
66 }
67 else {
68 try {
69 JAXBContext jaxbContext = JAXBContext.newInstance(LicenseAttachment.class);
70 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
71 jaxbMarshaller.marshal(license, sw);
72 } catch (Exception e) {
73 LOG.error("Exception :", e);
74 }
75
76 }
77 }
78
79 result = "<licenses>" + sw.append("</licenses>").toString();
80 result = result.replace("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>", "");
81 return result;
82 }
83
84 public static Object deserialize(String licensesXml) {
85 JAXBElement<Licenses> licensesElement = null;
86 try {
87 JAXBContext jaxbContext = JAXBContext.newInstance(Licenses.class);
88 Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
89 ByteArrayInputStream input = new ByteArrayInputStream(licensesXml.getBytes("UTF-8"));
90 licensesElement = jaxbUnmarshaller.unmarshal(new StreamSource(input), Licenses.class);
91 } catch (Exception e) {
92 LOG.error("Exception :", e);
93 }
94 Licenses licenses = licensesElement.getValue();
95 licenses.getLicenses().clear();
96 try {
97 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
98 DocumentBuilder builder = factory.newDocumentBuilder();
99 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();
131 }
132 return licenses;
133 }
134
135 }