001/*
002 * Copyright 2011 The Kuali Foundation.
003 * 
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 * http://www.opensource.org/licenses/ecl2.php
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.ole.docstore.model.xstream.ingest;
017
018import com.thoughtworks.xstream.XStream;
019import com.thoughtworks.xstream.core.util.QuickWriter;
020import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
021import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
022import com.thoughtworks.xstream.io.xml.XppDriver;
023import org.kuali.ole.docstore.model.xmlpojo.ingest.*;
024
025import java.io.Writer;
026
027/**
028 * Created by IntelliJ IDEA.
029 * User: pvsubrah
030 * Date: 9/7/11
031 * Time: 1:17 PM
032 * To change this template use File | Settings | File Templates.
033 */
034public class RequestHandler {
035    public Request toObject(String requestXML) {
036        XStream xStream = new XStream();
037        xStream.registerConverter(new RequestDocumentConverter());
038        xStream.alias("request", Request.class);
039        xStream.alias("ingestDocument", RequestDocument.class);
040        xStream.alias("linkedIngestDocument", RequestDocument.class);
041        xStream.alias("content", Content.class);
042        xStream.alias("additionalAttributes", AdditionalAttributes.class);
043        Request request = (Request) xStream.fromXML(requestXML);
044        return request;
045    }
046
047    public String toXML(Request request) {
048        XStream xStream = new XStream(
049                new XppDriver() {
050                    public HierarchicalStreamWriter createWriter(Writer out) {
051                        return new PrettyPrintWriter(out) {
052                            protected void writeText(QuickWriter writer, String text) {
053                                writer.write("<![CDATA[");
054                                if (text == null) {
055                                    text = "";
056                                }
057                                writer.write(text);
058                                writer.write("]]>");
059                            }
060                        };
061                    }
062                }
063        );
064        xStream.registerConverter(new RequestDocumentConverter());
065        xStream.alias("request", Request.class);
066        xStream.alias("ingestDocument", RequestDocument.class);
067        String xml = xStream.toXML(request);
068        return xml;
069    }
070}