View Javadoc
1   /*
2    * Copyright 2011 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.docstore.model.xstream.ingest;
17  
18  import com.thoughtworks.xstream.XStream;
19  import com.thoughtworks.xstream.core.util.QuickWriter;
20  import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
21  import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
22  import com.thoughtworks.xstream.io.xml.XppDriver;
23  import org.kuali.ole.docstore.model.xmlpojo.ingest.*;
24  
25  import java.io.Writer;
26  
27  /**
28   * Created by IntelliJ IDEA.
29   * User: pvsubrah
30   * Date: 9/7/11
31   * Time: 1:17 PM
32   * To change this template use File | Settings | File Templates.
33   */
34  public class RequestHandler {
35      public Request toObject(String requestXML) {
36          XStream xStream = new XStream();
37          xStream.registerConverter(new RequestDocumentConverter());
38          xStream.alias("request", Request.class);
39          xStream.alias("ingestDocument", RequestDocument.class);
40          xStream.alias("linkedIngestDocument", RequestDocument.class);
41          xStream.alias("content", Content.class);
42          xStream.alias("additionalAttributes", AdditionalAttributes.class);
43          Request request = (Request) xStream.fromXML(requestXML);
44          return request;
45      }
46  
47      public String toXML(Request request) {
48          XStream xStream = new XStream(
49                  new XppDriver() {
50                      public HierarchicalStreamWriter createWriter(Writer out) {
51                          return new PrettyPrintWriter(out) {
52                              protected void writeText(QuickWriter writer, String text) {
53                                  writer.write("<![CDATA[");
54                                  if (text == null) {
55                                      text = "";
56                                  }
57                                  writer.write(text);
58                                  writer.write("]]>");
59                              }
60                          };
61                      }
62                  }
63          );
64          xStream.registerConverter(new RequestDocumentConverter());
65          xStream.alias("request", Request.class);
66          xStream.alias("ingestDocument", RequestDocument.class);
67          String xml = xStream.toXML(request);
68          return xml;
69      }
70  }