View Javadoc
1   package org.kuali.ole.docstore.model.xstream.ingest;
2   
3   import com.thoughtworks.xstream.XStream;
4   import com.thoughtworks.xstream.core.util.QuickWriter;
5   import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
6   import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
7   import com.thoughtworks.xstream.io.xml.XppDriver;
8   import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
9   import org.kuali.ole.docstore.model.xmlpojo.ingest.Content;
10  import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
11  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
12  import org.slf4j.Logger;
13  import org.slf4j.LoggerFactory;
14  
15  import java.io.Writer;
16  
17  /**
18   * Created by IntelliJ IDEA.
19   * User: pvsubrah
20   * Date: 2/28/12
21   * Time: 8:29 PM
22   * To change this template use File | Settings | File Templates.
23   */
24  public class IngestDocumentHandler {
25      private static final Logger LOG = LoggerFactory.getLogger(IngestDocumentHandler.class);
26      private static XStream xStream = getXstream();
27  
28      private static XStream getXstream() {
29          XStream xStream = new XStream();
30          xStream.registerConverter(new IngestDocumentConverter());
31          xStream.alias("ingestDocument", RequestDocument.class);
32          xStream.alias("linkedIngestDocument", RequestDocument.class);
33          xStream.alias("content", Content.class);
34          xStream.alias("additionalAttributes", AdditionalAttributes.class);
35          return xStream;
36      }
37  
38      public RequestDocument toObject(String ingestDocXml) {
39          RequestDocument request = (RequestDocument) xStream.fromXML(ingestDocXml);
40          return request;
41      }
42  
43      public String toXML(Request request) {
44          XStream xStream = new XStream(new XppDriver() {
45              public HierarchicalStreamWriter createWriter(Writer out) {
46                  return new PrettyPrintWriter(out) {
47                      protected void writeText(QuickWriter writer, String text) {
48                          writer.write("<![CDATA[");
49                          writer.write(text);
50                          writer.write("]]>");
51                      }
52                  };
53              }
54          });
55          xStream.registerConverter(new RequestDocumentConverter());
56          xStream.registerConverter(new LinkInfoConverter());
57          xStream.alias("ingestDocument", RequestDocument.class);
58          String xml = xStream.toXML(request);
59          return xml;
60      }
61  }