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.HashMap;
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 @XmlAccessorType(XmlAccessType.FIELD)
33 @XmlType(name = "itemMap", propOrder = {
34 "itemMap"
35 })
36 @XmlRootElement(name="itemsDocs")
37 public class ItemMap {
38
39 private static final Logger LOG = Logger.getLogger(ItemMap.class);
40 @XmlElement(name = "itemsDoc")
41 protected HashMap<String, Item> itemMap;
42
43
44
45 public static String serialize(Object object) {
46 String result = null;
47 ItemMap itemMap = (ItemMap) object;
48 try {
49 StringWriter sw = new StringWriter();
50 Marshaller jaxbMarshaller = JAXBContextFactory.getInstance().getMarshaller(ItemMap.class);
51 synchronized (jaxbMarshaller) {
52 jaxbMarshaller.marshal(itemMap, 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 ItemMap itemMap = new ItemMap();
64 try {
65 Unmarshaller unmarshaller = JAXBContextFactory.getInstance().getUnMarshaller(ItemMap.class);
66 ByteArrayInputStream input = new ByteArrayInputStream(content.getBytes("UTF-8"));
67 synchronized (unmarshaller) {
68 itemMap = unmarshaller.unmarshal(new StreamSource(input), ItemMap.class).getValue();
69 }
70 } catch (Exception e) {
71 LOG.error("Exception ", e);
72 }
73 return itemMap;
74 }
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 public HashMap<String, Item> getItemMap() {
97 if (itemMap == null) {
98 itemMap = new HashMap<>();
99 }
100 return this.itemMap;
101 }
102 }