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 javax.xml.bind.JAXBContext;
12 import javax.xml.bind.Marshaller;
13 import javax.xml.bind.annotation.*;
14 import javax.xml.parsers.DocumentBuilder;
15 import javax.xml.parsers.DocumentBuilderFactory;
16 import javax.xml.parsers.ParserConfigurationException;
17 import java.io.IOException;
18 import java.io.StringReader;
19 import java.io.StringWriter;
20 import java.util.ArrayList;
21 import java.util.List;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 @XmlAccessorType(XmlAccessType.FIELD)
42 @XmlType(name = "bibTrees", propOrder = {
43 "bibTrees"
44 })
45 @XmlRootElement(name = "bibDocsTree")
46 public class BibTrees {
47
48 private static final Logger LOG = Logger.getLogger(BibTrees.class);
49 @XmlElement(name = "bibDocTree")
50 protected List<BibTree> bibTrees;
51
52 public static String serialize(Object object) {
53 String result = null;
54 StringWriter sw = new StringWriter();
55 BibTrees bibsTrees = (BibTrees) object;
56 try {
57 JAXBContext jaxbContext = JAXBContext.newInstance(BibTrees.class);
58 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
59 jaxbMarshaller.marshal(bibsTrees, sw);
60 result = sw.toString();
61 } catch (Exception e) {
62 LOG.error("Exception :", e);
63 }
64 return result;
65 }
66
67 public static Object deserialize(String bibTreesXml) {
68
69 BibTrees bibTrees = new BibTrees();
70 try {
71 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
72 DocumentBuilder builder = factory.newDocumentBuilder();
73 Document doc = builder.parse(new InputSource(new StringReader(bibTreesXml)));
74 NodeList root = doc.getChildNodes();
75 Node bibTreesNode = ParseXml.getNode("bibDocsTree", root);
76 BibTree bibTree = new BibTree();
77 NodeList nodeList = doc.getElementsByTagName("bibDocTree");
78 for (int i = 0; i < nodeList.getLength(); i++) {
79 Node bibTreeNode = ParseXml.getNodeXml(bibTreesNode.getChildNodes(), i);
80 bibTrees.getBibTrees().add((BibTree) bibTree.deserialize(ParseXml.nodeToString(bibTreeNode)));
81 }
82 } catch (SAXException e) {
83 LOG.error("Exception :", e);
84 } catch (IOException e) {
85 LOG.error("Exception :", e);
86 } catch (ParserConfigurationException e) {
87 LOG.error("Exception :", e);
88 }
89
90
91 return bibTrees;
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114 public List<BibTree> getBibTrees() {
115 if (bibTrees == null) {
116 bibTrees = new ArrayList<BibTree>();
117 }
118 return this.bibTrees;
119 }
120
121
122 }