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.JAXBContext;
7 import javax.xml.bind.Marshaller;
8 import javax.xml.bind.Unmarshaller;
9 import javax.xml.bind.annotation.*;
10 import javax.xml.stream.XMLStreamReader;
11 import javax.xml.transform.stream.StreamSource;
12 import java.io.ByteArrayInputStream;
13 import java.io.StringWriter;
14 import java.util.ArrayList;
15 import java.util.List;
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 @XmlAccessorType(XmlAccessType.FIELD)
37 @XmlType(name = "bibTree", propOrder = {
38 "holdingsTrees",
39 "bib"
40 })
41
42 @XmlRootElement(name = "bibDocTree")
43 public class BibTree {
44
45 private static final Logger LOG = Logger.getLogger(BibTree.class);
46
47 @XmlElementWrapper(name = "holdingsDocsTree")
48
49 @XmlElement(name = "holdingsDocTree")
50 protected List<HoldingsTree> holdingsTrees;
51
52 @XmlElement(name = "bibDoc")
53 protected Bib bib;
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 public List<HoldingsTree> getHoldingsTrees() {
76 if (holdingsTrees == null) {
77 holdingsTrees = new ArrayList<HoldingsTree>();
78 }
79 return this.holdingsTrees;
80 }
81
82
83
84
85
86
87
88 public Bib getBib() {
89 return bib;
90 }
91
92
93
94
95
96
97
98 public void setBib(Bib value) {
99 this.bib = value;
100 }
101
102 public String serialize(Object object) {
103 String result = null;
104 BibTree bibTree = (BibTree) object;
105 try {
106 StringWriter sw = new StringWriter();
107 Marshaller jaxbMarshaller = JAXBContextFactory.getInstance().getMarshaller(BibTree.class);
108 synchronized (jaxbMarshaller) {
109 jaxbMarshaller.marshal(bibTree, sw);
110 }
111 result = sw.toString();
112 } catch (Exception e) {
113 LOG.error("Exception :", e);
114 }
115 return result;
116 }
117
118 public Object deserialize(String bibTreeXml) {
119 BibTree bibTree = new BibTree();
120 try {
121 ByteArrayInputStream bibTreeInputStream = new ByteArrayInputStream(bibTreeXml.getBytes("UTF-8"));
122 StreamSource streamSource = new StreamSource(bibTreeInputStream);
123 XMLStreamReader xmlStreamReader = JAXBContextFactory.getInstance().getXmlInputFactory().createXMLStreamReader(streamSource);
124 Unmarshaller unmarshaller = JAXBContextFactory.getInstance().getUnMarshaller(BibTree.class);
125 synchronized (unmarshaller) {
126 bibTree = unmarshaller.unmarshal(xmlStreamReader, BibTree.class).getValue();
127 }
128 } catch (Exception e) {
129 LOG.error("Exception :", e);
130 }
131 return bibTree;
132 }
133
134 }