1 package org.kuali.ole.docstore.model.jaxb.config;
2
3 import org.kuali.ole.docstore.model.xmlpojo.config.DocumentConfig;
4 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory;
6
7 import javax.xml.bind.JAXBContext;
8 import javax.xml.bind.JAXBElement;
9 import javax.xml.bind.Marshaller;
10 import javax.xml.bind.Unmarshaller;
11 import javax.xml.transform.stream.StreamSource;
12 import java.io.ByteArrayInputStream;
13 import java.io.StringWriter;
14
15
16
17
18
19
20
21
22 public class DocumentConfigConverter {
23 public static final Logger LOG = LoggerFactory.getLogger(DocumentConfigConverter.class);
24
25 public DocumentConfig unmarshal(String docXml) {
26
27 JAXBElement<DocumentConfig> docElement = null;
28 try {
29 JAXBContext jaxbContext = JAXBContext.newInstance(DocumentConfig.class);
30 Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
31 ByteArrayInputStream input = new ByteArrayInputStream(docXml.getBytes("UTF-8"));
32 docElement = jaxbUnmarshaller.unmarshal(new StreamSource(input), DocumentConfig.class);
33 }
34 catch (Exception e) {
35 LOG.info(e.getMessage(), e);
36 }
37 return docElement.getValue();
38 }
39
40 public String marshal(DocumentConfig docConfig) {
41
42 JAXBElement<DocumentConfig> docElement = null;
43 String result = null;
44 StringWriter sw = new StringWriter();
45 try {
46 JAXBContext jaxbContext = JAXBContext.newInstance(DocumentConfig.class);
47 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
48 jaxbMarshaller.marshal(docConfig, sw);
49 result = sw.toString();
50 LOG.info("DocSearchConfig Xml is " + result);
51 }
52 catch (Exception e) {
53 LOG.info(e.getMessage(), e);
54 }
55 return result;
56 }
57 }