View Javadoc
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.Document;
6   import org.w3c.dom.Node;
7   import org.w3c.dom.NodeList;
8   import org.xml.sax.InputSource;
9   import org.xml.sax.SAXException;
10  
11  import javax.xml.bind.*;
12  import javax.xml.bind.annotation.XmlAccessType;
13  import javax.xml.bind.annotation.XmlAccessorType;
14  import javax.xml.bind.annotation.XmlRootElement;
15  import javax.xml.bind.annotation.XmlType;
16  import javax.xml.parsers.DocumentBuilder;
17  import javax.xml.parsers.DocumentBuilderFactory;
18  import javax.xml.parsers.ParserConfigurationException;
19  import javax.xml.transform.stream.StreamSource;
20  import java.io.ByteArrayInputStream;
21  import java.io.IOException;
22  import java.io.StringReader;
23  import java.io.StringWriter;
24  
25  /**
26   * Created with IntelliJ IDEA.
27   * User: chenchulakshmig
28   * Date: 2/25/14
29   * Time: 2:53 PM
30   * To change this template use File | Settings | File Templates.
31   */
32  @XmlRootElement(name = "license")
33  public class License extends DocstoreDocument {
34  
35      private static final Logger LOG = Logger.getLogger(License.class);
36  
37      @Override
38      public String serialize(Object object) {
39          String result = null;
40          StringWriter sw = new StringWriter();
41          License license = (License) object;
42          try {
43              JAXBContext jaxbContext = JAXBContext.newInstance(License.class);
44              Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
45              jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
46              jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
47              jaxbMarshaller.marshal(license, sw);
48              result = sw.toString();
49          } catch (Exception e) {
50              LOG.error("Exception :", e);
51          }
52          return result;
53      }
54  
55      @Override
56      public Object deserialize(String content) {
57          JAXBElement<License> licenseElement = null;
58          try {
59              DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
60              DocumentBuilder builder = factory.newDocumentBuilder();
61              Document doc = builder.parse(new InputSource(new StringReader(content)));
62              NodeList root = doc.getChildNodes();
63              Node license = ParseXml.getNode("license", root);
64                  NodeList nodes = license.getChildNodes();
65                  String licensexml = ParseXml.nodeToString(license);
66                  if (ParseXml.getNodeValue("format", nodes).equals("onixpl")) {
67                      JAXBContext jc = JAXBContext.newInstance(LicenseOnixpl.class);
68                      Unmarshaller unmarshaller1 = jc.createUnmarshaller();
69                      StreamSource xmlSource = new StreamSource(new StringReader(licensexml));
70                      JAXBElement<LicenseOnixpl> je1 = unmarshaller1.unmarshal(xmlSource, LicenseOnixpl.class);
71                      LicenseOnixpl licenseOnixpl = je1.getValue();
72                      return licenseOnixpl;
73                  } else {
74                      JAXBContext jc = JAXBContext.newInstance(LicenseAttachment.class);
75                      Unmarshaller unmarshaller1 = jc.createUnmarshaller();
76                      StreamSource xmlSource = new StreamSource(new StringReader(licensexml));
77                      JAXBElement<LicenseAttachment> je1 = unmarshaller1.unmarshal(xmlSource, LicenseAttachment.class);
78                      LicenseAttachment licenseAttachment = je1.getValue();
79                      return licenseAttachment;
80                  }
81          } catch (ParserConfigurationException e) {
82              LOG.error("Exception ", e);
83          } catch (SAXException e) {
84              LOG.error("Exception ", e);
85          } catch (IOException e) {
86              LOG.error("Exception ", e);
87          } catch (JAXBException e) {
88              e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
89          }
90  
91          return null;
92      }
93  
94      @Override
95      public Object deserializeContent(Object object) {
96          return null;  //To change body of implemented methods use File | Settings | File Templates.
97      }
98  
99      @Override
100     public Object deserializeContent(String content) {
101         return null;  //To change body of implemented methods use File | Settings | File Templates.
102     }
103 
104     @Override
105     public String serializeContent(Object object) {
106         return null;  //To change body of implemented methods use File | Settings | File Templates.
107     }
108 }