1 package org.kuali.ole.docstore.common.document;
2
3 import org.apache.log4j.Logger;
4 import org.kuali.ole.docstore.common.util.ParseXml;
5 import org.w3c.dom.Document;
6 import org.w3c.dom.Node;
7 import org.w3c.dom.NodeList;
8 import org.xml.sax.InputSource;
9 import org.xml.sax.SAXException;
10
11 import java.io.IOException;
12 import java.io.StringReader;
13 import java.io.StringWriter;
14 import java.util.ArrayList;
15 import java.util.List;
16 import javax.xml.bind.JAXBContext;
17 import javax.xml.bind.Marshaller;
18 import javax.xml.bind.annotation.*;
19 import javax.xml.parsers.DocumentBuilder;
20 import javax.xml.parsers.DocumentBuilderFactory;
21 import javax.xml.parsers.ParserConfigurationException;
22
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 = "holdingsTrees", propOrder = {
45 "holdingsTrees"
46 })
47
48 @XmlRootElement(name = "holdingsDocsTree")
49 public class HoldingsTrees {
50
51 private static final Logger LOG = Logger.getLogger(HoldingsTrees.class);
52 @XmlElement(name = "holdingsDocTree")
53 protected List<HoldingsTree> holdingsTrees;
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public List<HoldingsTree> getHoldingsTrees() {
78 if (holdingsTrees == null) {
79 holdingsTrees = new ArrayList<HoldingsTree>();
80 }
81 return this.holdingsTrees;
82 }
83
84
85 public static String serialize(Object object) {
86 String result = null;
87 StringWriter sw = new StringWriter();
88 HoldingsTrees bibsTrees = (HoldingsTrees) object;
89 try {
90 JAXBContext jaxbContext = JAXBContext.newInstance(HoldingsTrees.class);
91 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
92 jaxbMarshaller.marshal(bibsTrees, sw);
93 result = sw.toString();
94 } catch (Exception e) {
95 LOG.error("Exception ", e);
96 }
97 return result;
98 }
99
100 public static Object deserialize(String holdingsTreesXml) {
101
102 HoldingsTrees holdingsTrees = new HoldingsTrees();
103 try {
104 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
105 DocumentBuilder builder = factory.newDocumentBuilder();
106 Document doc = builder.parse(new InputSource(new StringReader(holdingsTreesXml)));
107 NodeList root = doc.getChildNodes();
108 Node holdingsTreesNode = ParseXml.getNode("holdingsDocsTree", root);
109 HoldingsTree holdingsTree = new HoldingsTree();
110 NodeList nodeList = doc.getElementsByTagName("holdingsDocTree");
111 for (int i = 0; i < nodeList.getLength(); i++) {
112 Node bibTreeNode = ParseXml.getNodeXml(holdingsTreesNode.getChildNodes(),i);
113 holdingsTrees.getHoldingsTrees().add((HoldingsTree) holdingsTree.deserialize(ParseXml.nodeToString(bibTreeNode)));
114 }
115 } catch (SAXException e) {
116 LOG.error("Exception ", e);
117 } catch (IOException e) {
118 LOG.error("Exception ", e);
119 } catch (ParserConfigurationException e) {
120 LOG.error("Exception ", e);
121 }
122 return holdingsTrees;
123 }
124
125 }