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 jaxbMarshaller.marshal(bibTree, sw);
108 result = sw.toString();
109 } catch (Exception e) {
110 LOG.error("Exception :", e);
111 }
112 return result;
113 }
114
115 public Object deserialize(String bibTreeXml) {
116 BibTree bibTree = new BibTree();
117 try {
118 ByteArrayInputStream bibTreeInputStream = new ByteArrayInputStream(bibTreeXml.getBytes());
119 StreamSource streamSource = new StreamSource(bibTreeInputStream);
120 XMLStreamReader xmlStreamReader = JAXBContextFactory.getInstance().getXmlInputFactory().createXMLStreamReader(streamSource);
121
122 Unmarshaller unmarshaller = JAXBContextFactory.getInstance().getUnMarshaller(BibTree.class);
123 bibTree = unmarshaller.unmarshal(xmlStreamReader, BibTree.class).getValue();
124 } catch (Exception e) {
125 LOG.error("Exception :", e);
126 }
127 return bibTree;
128 }
129
130 }