1 package org.kuali.ole.docstore.common.document;
2
3 import org.apache.log4j.Logger;
4
5 import java.io.ByteArrayInputStream;
6 import java.io.StringWriter;
7 import java.util.ArrayList;
8 import java.util.List;
9 import javax.xml.bind.JAXBContext;
10 import javax.xml.bind.JAXBElement;
11 import javax.xml.bind.Marshaller;
12 import javax.xml.bind.Unmarshaller;
13 import javax.xml.bind.annotation.*;
14 import javax.xml.transform.stream.StreamSource;
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 @XmlAccessorType(XmlAccessType.FIELD)
35 @XmlType(name = "items", propOrder = {
36 "items"
37 })
38 @XmlRootElement(name="itemsDocs")
39 public class Items {
40
41 private static final Logger LOG = Logger.getLogger(Items.class);
42 @XmlElement(name = "itemsDoc")
43 protected List<Item> items;
44
45 public static String serialize(Object object) {
46 String result = null;
47 StringWriter sw = new StringWriter();
48 Items items = (Items) object;
49 try {
50 JAXBContext jaxbContext = JAXBContext.newInstance(Items.class);
51 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
52 jaxbMarshaller.marshal(items, sw);
53 result = sw.toString();
54 } catch (Exception e) {
55 LOG.error("Exception ", e);
56 }
57 return result;
58 }
59
60 public static Object deserialize(String itemsXml) {
61
62 JAXBElement<Items> itemsElement = null;
63 try {
64 JAXBContext jaxbContext = JAXBContext.newInstance(Items.class);
65 Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
66 ByteArrayInputStream input = new ByteArrayInputStream(itemsXml.getBytes("UTF-8"));
67 itemsElement = jaxbUnmarshaller.unmarshal(new StreamSource(input), Items.class);
68 } catch (Exception e) {
69 LOG.error("Exception ", e);
70 }
71 return itemsElement.getValue();
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 public List<Item> getItems() {
95 if (items == null) {
96 items = new ArrayList<Item>();
97 }
98 return this.items;
99 }
100
101 }