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