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.AdditionalAttributes;
24  import org.kuali.ole.docstore.model.xmlpojo.ingest.Content;
25  import org.kuali.ole.docstore.model.xmlpojo.ingest.Response;
26  import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
27  
28  import java.io.Writer;
29  
30  /**
31   * Created by IntelliJ IDEA.
32   * User: pvsubrah
33   * Date: 9/13/11
34   * Time: 10:58 PM
35   * To change this template use File | Settings | File Templates.
36   */
37  public class ResponseHandler {
38      public Response toObject(String requestXML) {
39          XStream xStream = new XStream();
40          xStream.registerConverter(new ResponseDocumentConverter());
41          xStream.alias("response", Response.class);
42          xStream.alias("document", ResponseDocument.class);
43          xStream.alias("content", Content.class);
44          //        xStream.alias("linkedDocument", ResponseDocument.class);
45          Response response = (Response) xStream.fromXML(requestXML);
46          return response;
47      }
48  
49      public String toXML(Response response) {
50          XStream xStream = new XStream(new XppDriver() {
51              public HierarchicalStreamWriter createWriter(Writer out) {
52                  return new PrettyPrintWriter(out) {
53                      protected void writeText(QuickWriter writer, String text) {
54                          writer.write("<![CDATA[");
55                          writer.write(text);
56                          writer.write("]]>");
57                      }
58                  };
59              }
60          });
61          xStream.registerConverter(new ResponseDocumentConverter());
62          xStream.alias("response", Response.class);
63          xStream.alias("document", ResponseDocument.class);
64          xStream.alias("content", Content.class);
65          //        xStream.alias("linkedDocument", ResponseDocument.class);
66          return xStream.toXML(response);
67      }
68  }