View Javadoc
1   package org.kuali.ole.docstore.common.document.ids;
2   
3   import org.apache.log4j.Logger;
4   
5   import javax.xml.bind.JAXBContext;
6   import javax.xml.bind.JAXBElement;
7   import javax.xml.bind.Marshaller;
8   import javax.xml.bind.Unmarshaller;
9   import javax.xml.bind.annotation.*;
10  import javax.xml.transform.stream.StreamSource;
11  import java.io.ByteArrayInputStream;
12  import java.io.StringWriter;
13  import java.util.ArrayList;
14  import java.util.List;
15  
16  /**
17   * Created with IntelliJ IDEA.
18   * User: sambasivam
19   * Date: 6/6/14
20   * Time: 12:54 PM
21   * To change this template use File | Settings | File Templates.
22   */
23  
24  @XmlAccessorType(XmlAccessType.FIELD)
25  @XmlType(name = "bib", propOrder = {
26          "id",
27          "holdingsIds"
28  })
29  @XmlRootElement(name = "bib")
30  
31  public class BibId {
32      private static final Logger LOG = Logger.getLogger(BibId.class);
33      @XmlElement(name = "id")
34      protected String id;
35  
36      @XmlElementWrapper(name = "holdingsIds")
37      @XmlElement(name = "holdingsId")
38      protected List<HoldingsId> holdingsIds;
39  
40      public String getId() {
41          return id;
42      }
43  
44      public void setId(String id) {
45          this.id = id;
46      }
47  
48      public List<HoldingsId> getHoldingsIds() {
49          if(holdingsIds == null) {
50              holdingsIds = new ArrayList<>();
51          }
52          return holdingsIds;
53      }
54  
55      public static String serialize(Object object) {
56          String result = null;
57          StringWriter sw = new StringWriter();
58          BibId bibId = (BibId) object;
59          try {
60              JAXBContext jaxbContext = JAXBContext.newInstance(BibId.class);
61              Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
62              jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
63              jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
64              jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
65              jaxbMarshaller.marshal(bibId, sw);
66              result = sw.toString();
67          } catch (Exception e) {
68              LOG.error("Exception :", e);
69          }
70          return result;
71      }
72  
73      public static Object deserialize(String content) {
74  
75          JAXBElement<BibId> bibIdJAXBElement = null;
76          try {
77              JAXBContext jaxbContext = JAXBContext.newInstance(BibId.class);
78              Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
79              ByteArrayInputStream input = new ByteArrayInputStream(content.getBytes("UTF-8"));
80              bibIdJAXBElement = jaxbUnmarshaller.unmarshal(new StreamSource(input), BibId.class);
81          } catch (Exception e) {
82              LOG.error("Exception :", e);
83          }
84          return bibIdJAXBElement.getValue();
85      }
86  
87  }