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