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 @XmlAccessorType(XmlAccessType.FIELD)
35 @XmlType(name = "bibTrees", propOrder = {
36 "bibTrees"
37 })
38 @XmlRootElement(name = "bibDocsTree")
39 public class BibTrees {
40
41 private static final Logger LOG = Logger.getLogger(BibTrees.class);
42 @XmlElement(name = "bibDocTree")
43 protected List<BibTree> bibTrees;
44
45 public static String serialize(Object object) {
46 String result = null;
47 BibTrees bibTrees = (BibTrees) object;
48 try {
49 StringWriter sw = new StringWriter();
50 Marshaller jaxbMarshaller = JAXBContextFactory.getInstance().getMarshaller(BibTrees.class);
51 synchronized (jaxbMarshaller) {
52 jaxbMarshaller.marshal(bibTrees, sw);
53 }
54 result = sw.toString();
55 } catch (Exception e) {
56 LOG.error("Exception :", e);
57 }
58 return result;
59 }
60
61 public static Object deserialize(String bibTreesXml) {
62
63 BibTrees bibTrees = new BibTrees();
64 try {
65 ByteArrayInputStream bibTreeInputStream = new ByteArrayInputStream(bibTreesXml.getBytes("UTF-8"));
66 StreamSource streamSource = new StreamSource(bibTreeInputStream);
67 XMLStreamReader xmlStreamReader = JAXBContextFactory.getInstance().getXmlInputFactory().createXMLStreamReader(streamSource);
68 Unmarshaller unmarshaller = JAXBContextFactory.getInstance().getUnMarshaller(BibTrees.class);
69 synchronized (unmarshaller) {
70 bibTrees = unmarshaller.unmarshal(xmlStreamReader, BibTrees.class).getValue();
71 }
72 } catch (Exception e) {
73 LOG.error("Exception :", e);
74 }
75 return bibTrees;
76 }
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98 public List<BibTree> getBibTrees() {
99 if (bibTrees == null) {
100 bibTrees = new ArrayList<BibTree>();
101 }
102 return this.bibTrees;
103 }
104
105
106 }