001package org.kuali.ole.docstore.model.xstream.ingest;
002
003import com.thoughtworks.xstream.XStream;
004import com.thoughtworks.xstream.core.util.QuickWriter;
005import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
006import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
007import com.thoughtworks.xstream.io.xml.XppDriver;
008import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
009import org.kuali.ole.docstore.model.xmlpojo.ingest.Content;
010import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
011import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
012import org.slf4j.Logger;
013import org.slf4j.LoggerFactory;
014
015import java.io.Writer;
016
017/**
018 * Created by IntelliJ IDEA.
019 * User: pvsubrah
020 * Date: 2/28/12
021 * Time: 8:29 PM
022 * To change this template use File | Settings | File Templates.
023 */
024public class IngestDocumentHandler {
025    private static final Logger LOG = LoggerFactory.getLogger(IngestDocumentHandler.class);
026    private static XStream xStream = getXstream();
027
028    private static XStream getXstream() {
029        XStream xStream = new XStream();
030        xStream.registerConverter(new IngestDocumentConverter());
031        xStream.alias("ingestDocument", RequestDocument.class);
032        xStream.alias("linkedIngestDocument", RequestDocument.class);
033        xStream.alias("content", Content.class);
034        xStream.alias("additionalAttributes", AdditionalAttributes.class);
035        return xStream;
036    }
037
038    public RequestDocument toObject(String ingestDocXml) {
039        RequestDocument request = (RequestDocument) xStream.fromXML(ingestDocXml);
040        return request;
041    }
042
043    public String toXML(Request request) {
044        XStream xStream = new XStream(new XppDriver() {
045            public HierarchicalStreamWriter createWriter(Writer out) {
046                return new PrettyPrintWriter(out) {
047                    protected void writeText(QuickWriter writer, String text) {
048                        writer.write("<![CDATA[");
049                        writer.write(text);
050                        writer.write("]]>");
051                    }
052                };
053            }
054        });
055        xStream.registerConverter(new RequestDocumentConverter());
056        xStream.registerConverter(new LinkInfoConverter());
057        xStream.alias("ingestDocument", RequestDocument.class);
058        String xml = xStream.toXML(request);
059        return xml;
060    }
061}