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.lang.reflect.Array;
14  import java.util.ArrayList;
15  import java.util.List;
16  
17  /**
18   * Created with IntelliJ IDEA.
19   * User: sambasivam
20   * Date: 6/6/14
21   * Time: 2:25 PM
22   * To change this template use File | Settings | File Templates.
23   */
24  
25  @XmlAccessorType(XmlAccessType.FIELD)
26  @XmlType(name = "bibIds", propOrder = {
27          "bibIds"
28  })
29  @XmlRootElement(name = "bibIds")
30  public class BibIds {
31      private static final Logger LOG = Logger.getLogger(BibIds.class);
32      @XmlElementWrapper(name = "bibIds")
33      @XmlElement(name = "bibId")
34      public List<BibId> bibIds;
35  
36  
37      public List<BibId> getBibIds() {
38          if(bibIds == null) {
39              bibIds = new ArrayList<>();
40          }
41          return bibIds;
42      }
43  
44  
45      public static String serialize(Object object) {
46          String result = null;
47          StringWriter sw = new StringWriter();
48          BibIds bibIds = (BibIds) object;
49          try {
50              JAXBContext jaxbContext = JAXBContext.newInstance(BibIds.class);
51              Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
52              jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
53              jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
54              jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
55              jaxbMarshaller.marshal(bibIds, sw);
56              result = sw.toString();
57          } catch (Exception e) {
58              LOG.error("Exception :", e);
59          }
60          return result;
61      }
62  
63      public static Object deserialize(String content) {
64  
65          JAXBElement<BibIds> bibIdsJAXBElement = null;
66          try {
67              JAXBContext jaxbContext = JAXBContext.newInstance(BibIds.class);
68              Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
69              ByteArrayInputStream input = new ByteArrayInputStream(content.getBytes("UTF-8"));
70              bibIdsJAXBElement = jaxbUnmarshaller.unmarshal(new StreamSource(input), BibIds.class);
71          } catch (Exception e) {
72              LOG.error("Exception :", e);
73          }
74          return bibIdsJAXBElement.getValue();
75      }
76  
77  
78  }