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