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 public static String serialize(Object object) {
44 String result = null;
45 ItemMap itemMap = (ItemMap) object;
46 try {
47 StringWriter sw = new StringWriter();
48 Marshaller jaxbMarshaller = JAXBContextFactory.getInstance().getMarshaller(ItemMap.class);
49 synchronized (jaxbMarshaller) {
50 jaxbMarshaller.marshal(itemMap, sw);
51 }
52 result = sw.toString();
53 } catch (Exception e) {
54 LOG.error("Exception ", e);
55 }
56 return result;
57 }
58
59
60 public static Object deserialize(String content) {
61 ItemMap itemMap = new ItemMap();
62 try {
63 Unmarshaller unmarshaller = JAXBContextFactory.getInstance().getUnMarshaller(ItemMap.class);
64 ByteArrayInputStream input = new ByteArrayInputStream(content.getBytes("UTF-8"));
65 synchronized (unmarshaller) {
66 itemMap = unmarshaller.unmarshal(new StreamSource(input), ItemMap.class).getValue();
67 }
68 } catch (Exception e) {
69 LOG.error("Exception ", e);
70 }
71 return itemMap;
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 public HashMap<String, Item> getItemMap() {
95 if (itemMap == null) {
96 itemMap = new HashMap<>();
97 }
98 return this.itemMap;
99 }
100 }