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   import org.kuali.ole.docstore.common.document.factory.JAXBContextFactory;
8   import org.kuali.ole.docstore.common.exception.DocstoreDeserializeException;
9   import org.kuali.ole.docstore.common.exception.DocstoreResources;
10  
11  import javax.xml.bind.Marshaller;
12  import javax.xml.bind.Unmarshaller;
13  import javax.xml.bind.annotation.XmlRootElement;
14  import javax.xml.transform.stream.StreamSource;
15  import java.io.ByteArrayInputStream;
16  import java.io.StringWriter;
17  
18  /**
19   * Created with IntelliJ IDEA.
20   * User: sambasivam
21   * Date: 12/13/13
22   * Time: 3:31 PM
23   * To change this template use File | Settings | File Templates.
24   */
25  @XmlRootElement(name = "bibDoc")
26  public class BibDc extends Bib {
27  
28      private static final Logger LOG = Logger.getLogger(BibDc.class);
29      public BibDc() {
30          category=DocCategory.WORK.getCode();
31          type=DocType.BIB.getCode();
32          format=DocFormat.DUBLIN_CORE.getCode();
33      }
34  
35      @Override
36      public String serialize(Object object) {
37          String result = null;
38          BibDc bib = (BibDc) object;
39          try {
40              StringWriter sw = new StringWriter();
41              Marshaller jaxbMarshaller = JAXBContextFactory.getInstance().getMarshaller(BibDc.class);
42              synchronized (jaxbMarshaller) {
43              jaxbMarshaller.marshal(bib, sw);
44              }
45              result = sw.toString();
46          } catch (Exception e) {
47              LOG.error("Exception :", e);
48          }
49          return result;
50      }
51  
52      @Override
53      public Object deserialize(String content) {
54          BibDc bib = new BibDc();
55          try {
56              Unmarshaller unmarshaller = JAXBContextFactory.getInstance().getUnMarshaller(BibDc.class);
57              ByteArrayInputStream input = new ByteArrayInputStream(content.getBytes("UTF-8"));
58              synchronized (unmarshaller) {
59                  bib = unmarshaller.unmarshal(new StreamSource(input), BibDc.class).getValue();
60              }
61          } catch (Exception e) {
62              LOG.error("Exception :", e);
63              throw new DocstoreDeserializeException(DocstoreResources.BIB_CREATION_FAILED, DocstoreResources.BIB_CREATION_FAILED);
64          }
65          return bib;
66      }
67  
68  }