1 package org.kuali.ole.docstore.common.document;
2
3 import org.apache.commons.lang3.StringUtils;
4 import org.apache.log4j.Logger;
5 import org.kuali.ole.docstore.common.util.ParseXml;
6 import org.w3c.dom.Document;
7 import org.w3c.dom.Node;
8 import org.w3c.dom.NodeList;
9 import org.xml.sax.InputSource;
10 import org.xml.sax.SAXException;
11
12 import javax.xml.bind.JAXBContext;
13 import javax.xml.bind.Marshaller;
14 import javax.xml.bind.annotation.*;
15 import javax.xml.parsers.DocumentBuilder;
16 import javax.xml.parsers.DocumentBuilderFactory;
17 import javax.xml.parsers.ParserConfigurationException;
18 import java.io.IOException;
19 import java.io.StringReader;
20 import java.io.StringWriter;
21 import java.util.ArrayList;
22 import java.util.List;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 @XmlAccessorType(XmlAccessType.FIELD)
44 @XmlType(name = "bibTree", propOrder = {
45 "holdingsTrees",
46 "bib"
47 })
48
49 @XmlRootElement(name = "bibDocTree")
50 public class BibTree
51 extends DocstoreDocument {
52
53 private static final Logger LOG = Logger.getLogger(BibTree.class);
54
55 @XmlElementWrapper(name = "holdingsDocsTree")
56
57 @XmlElement(name = "holdingsDocTree")
58 protected List<HoldingsTree> holdingsTrees;
59
60 @XmlElement(name = "bibDoc")
61 protected Bib bib;
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 public List<HoldingsTree> getHoldingsTrees() {
84 if (holdingsTrees == null) {
85 holdingsTrees = new ArrayList<HoldingsTree>();
86 }
87 return this.holdingsTrees;
88 }
89
90
91
92
93
94
95
96 public Bib getBib() {
97 return bib;
98 }
99
100
101
102
103
104
105
106 public void setBib(Bib value) {
107 this.bib = value;
108 }
109
110 @Override
111 public String serialize(Object object) {
112 String result = null;
113 StringWriter sw = new StringWriter();
114 BibTree bibTree = (BibTree) object;
115 try {
116 JAXBContext jaxbContext = JAXBContext.newInstance(BibTree.class);
117 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
118 jaxbMarshaller.marshal(bibTree, sw);
119 result = sw.toString();
120 } catch (Exception e) {
121 LOG.error("Exception :", e);
122 }
123 return result;
124 }
125
126 @Override
127 public Object deserialize(String bibTreeXml) {
128
129 BibTree bibTree = new BibTree();
130 try {
131 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
132 DocumentBuilder builder = factory.newDocumentBuilder();
133 Document doc = builder.parse(new InputSource(new StringReader(bibTreeXml)));
134 NodeList root = doc.getChildNodes();
135 Node bibTreeNode = ParseXml.getNode("bibDocTree", root);
136 Node bibNode = ParseXml.getNode("bibDoc", bibTreeNode.getChildNodes());
137 String BibXml = ParseXml.nodeToString(bibNode);
138 Bib bib = new Bib();
139 bib = (Bib) bib.deserialize(BibXml);
140 bibTree.setBib(bib);
141 Node holdingsTreesNode = ParseXml.getNode("holdingsDocsTree", bibTreeNode.getChildNodes());
142 String holdingsTreeXml = null;
143 HoldingsTree holdingsTree = new HoldingsTree();
144 NodeList nodeList = doc.getElementsByTagName("holdingsDocTree");
145 for (int i = 0; i < nodeList.getLength(); i++) {
146 Node holdingsTreeNode = ParseXml.getNode("holdingsDocTree", holdingsTreesNode.getChildNodes(), i);
147 holdingsTreeXml = ParseXml.nodeToString(holdingsTreeNode);
148 bibTree.getHoldingsTrees().add((HoldingsTree) holdingsTree.deserialize(holdingsTreeXml));
149 }
150 } catch (SAXException e) {
151 LOG.error("Exception :", e);
152 } catch (IOException e) {
153 LOG.error("Exception :", e);
154 } catch (ParserConfigurationException e) {
155 LOG.error("Exception :", e);
156 }
157
158
159 return bibTree;
160 }
161
162 @Override
163 public Object deserializeContent(Object object) {
164 return null;
165 }
166
167 @Override
168 public Object deserializeContent(String content) {
169 return null;
170 }
171
172 @Override
173 public String serializeContent(Object object) {
174 return null;
175 }
176 }