View Javadoc
1   package org.kuali.ole.docstore.common.document;
2   
3   import org.apache.log4j.Logger;
4   import org.kuali.ole.docstore.common.document.content.enums.DocCategory;
5   import org.kuali.ole.docstore.common.document.content.enums.DocFormat;
6   import org.kuali.ole.docstore.common.document.content.enums.DocType;
7   
8   import javax.xml.bind.JAXBContext;
9   import javax.xml.bind.JAXBElement;
10  import javax.xml.bind.Marshaller;
11  import javax.xml.bind.Unmarshaller;
12  import javax.xml.bind.annotation.XmlRootElement;
13  import javax.xml.transform.stream.StreamSource;
14  import java.io.ByteArrayInputStream;
15  import java.io.StringWriter;
16  
17  /**
18   * Created with IntelliJ IDEA.
19   * User: chenchulakshmig
20   * Date: 2/25/14
21   * Time: 2:57 PM
22   * To change this template use File | Settings | File Templates.
23   */
24  @XmlRootElement(name = "license")
25  public class LicenseOnixpl extends License {
26  
27      private static final Logger LOG = Logger.getLogger(LicenseOnixpl.class);
28  
29      public LicenseOnixpl() {
30          category = DocCategory.WORK.getCode();
31          type = DocType.LICENSE.getCode();
32          format = DocFormat.ONIXPL.getCode();
33      }
34  
35      @Override
36      public String serialize(Object object) {
37          String result = null;
38          StringWriter sw = new StringWriter();
39          LicenseOnixpl licenseOnixpl = (LicenseOnixpl) object;
40          try {
41              JAXBContext jaxbContext = JAXBContext.newInstance(LicenseOnixpl.class);
42              Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
43              jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
44              jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
45              jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
46              jaxbMarshaller.marshal(licenseOnixpl, sw);
47              result = sw.toString();
48          } catch (Exception e) {
49              LOG.error("Exception :", e);
50          }
51          return result;
52      }
53  
54      @Override
55      public Object deserialize(String content) {
56          JAXBElement<LicenseOnixpl> licenseOnixplElement = null;
57          try {
58              JAXBContext jaxbContext = JAXBContext.newInstance(LicenseOnixpl.class);
59              Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
60              ByteArrayInputStream input = new ByteArrayInputStream(content.getBytes("UTF-8"));
61              licenseOnixplElement = jaxbUnmarshaller.unmarshal(new StreamSource(input), LicenseOnixpl.class);
62          } catch (Exception e) {
63              LOG.error("Exception :", e);
64          }
65          return licenseOnixplElement.getValue();
66      }
67  }