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