View Javadoc
1   package org.kuali.ole.docstore.model.xstream.ingest;
2   
3   import com.thoughtworks.xstream.converters.Converter;
4   import com.thoughtworks.xstream.converters.MarshallingContext;
5   import com.thoughtworks.xstream.converters.UnmarshallingContext;
6   import com.thoughtworks.xstream.io.HierarchicalStreamReader;
7   import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
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.ResponseDocument;
11  import org.xml.sax.InputSource;
12  
13  import javax.xml.parsers.DocumentBuilder;
14  import javax.xml.parsers.DocumentBuilderFactory;
15  import java.io.StringReader;
16  import java.util.Iterator;
17  import java.util.List;
18  
19  /**
20   * Created by IntelliJ IDEA.
21   * User: Pranitha
22   * Date: 3/7/12
23   * Time: 12:29 PM
24   * To change this template use File | Settings | File Templates.
25   */
26  public class ResponseDocumentConverter
27          implements Converter {
28  
29      @Override
30      public void marshal(Object o, HierarchicalStreamWriter hierarchicalStreamWriter,
31                          MarshallingContext marshallingContext) {
32          ResponseDocument responseDocument = (ResponseDocument) o;
33          if (responseDocument.getId() != null) {
34              hierarchicalStreamWriter.addAttribute("id", responseDocument.getId());
35          }
36          if (responseDocument.getCategory() != null) {
37              hierarchicalStreamWriter.addAttribute("category", responseDocument.getCategory());
38          }
39          if (responseDocument.getType() != null) {
40              hierarchicalStreamWriter.addAttribute("type", responseDocument.getType());
41          }
42          if (responseDocument.getFormat() != null) {
43              hierarchicalStreamWriter.addAttribute("format", responseDocument.getFormat());
44          }
45          if (responseDocument.getStatus() != null) {
46              hierarchicalStreamWriter.startNode("status");
47              hierarchicalStreamWriter.setValue(responseDocument.getStatus());
48              hierarchicalStreamWriter.endNode();
49          }
50          if (responseDocument.getStatus() != null) {
51              hierarchicalStreamWriter.startNode("statusMessage");
52              hierarchicalStreamWriter.setValue(responseDocument.getStatusMessage());
53              hierarchicalStreamWriter.endNode();
54          }
55  
56          if (responseDocument.getUuid() != null) {
57              hierarchicalStreamWriter.startNode("uuid");
58              hierarchicalStreamWriter.setValue(responseDocument.getUuid());
59              hierarchicalStreamWriter.endNode();
60          }
61          if (responseDocument.getContent() != null) {
62              hierarchicalStreamWriter.startNode("content");
63              hierarchicalStreamWriter.setValue(responseDocument.getContent().getContent());
64              hierarchicalStreamWriter.endNode();
65          }
66  
67          if (responseDocument.getDocumentName() != null) {
68              hierarchicalStreamWriter.startNode("documentName");
69              hierarchicalStreamWriter.setValue(responseDocument.getDocumentName());
70              hierarchicalStreamWriter.endNode();
71          }
72          if (responseDocument.getDocumentTitle() != null) {
73              hierarchicalStreamWriter.startNode("documentTitle");
74              hierarchicalStreamWriter.setValue(responseDocument.getDocumentTitle());
75              hierarchicalStreamWriter.endNode();
76          }
77          if (responseDocument.getDocumentMimeType() != null) {
78              hierarchicalStreamWriter.startNode("documentMimeType");
79              hierarchicalStreamWriter.setValue(responseDocument.getDocumentMimeType());
80              hierarchicalStreamWriter.endNode();
81          }
82          if (responseDocument.getVersion() != null) {
83              hierarchicalStreamWriter.startNode("version");
84              hierarchicalStreamWriter.setValue(responseDocument.getVersion());
85              hierarchicalStreamWriter.endNode();
86          }
87  
88  
89          if (responseDocument.getAdditionalAttributes() != null) {
90              AdditionalAttributeConverter addConverter = new AdditionalAttributeConverter();
91              AdditionalAttributes additionalAttributes = responseDocument.getAdditionalAttributes();
92              addConverter.marshal(additionalAttributes, hierarchicalStreamWriter, marshallingContext);
93          }
94  
95          List<ResponseDocument> linkedResponseDocuments = responseDocument.getLinkedDocuments();
96          if (linkedResponseDocuments != null && linkedResponseDocuments.size() > 0) {
97              hierarchicalStreamWriter.startNode("linkedDocuments");
98              for (Iterator<ResponseDocument> iterator = linkedResponseDocuments.iterator(); iterator.hasNext(); ) {
99                  hierarchicalStreamWriter.startNode("linkedDocument");
100                 ResponseDocument respDoc = iterator.next();
101                 marshal(respDoc, hierarchicalStreamWriter, marshallingContext);
102                 List<ResponseDocument> instanceResposeDocuments = responseDocument.getLinkedInstanceDocuments();
103                 if (instanceResposeDocuments != null && instanceResposeDocuments.size() > 0) {
104                     hierarchicalStreamWriter.startNode("linkedDocuments");
105                     for (Iterator<ResponseDocument> instanceIterator = instanceResposeDocuments
106                             .iterator(); instanceIterator.hasNext(); ) {
107                         hierarchicalStreamWriter.startNode("linkedDocument");
108                         ResponseDocument instanceDoc = instanceIterator.next();
109                         marshalInstance(instanceDoc, hierarchicalStreamWriter, marshallingContext);
110                         hierarchicalStreamWriter.endNode();
111                     }
112                     hierarchicalStreamWriter.endNode();
113                 }
114                 hierarchicalStreamWriter.endNode();
115             }
116             hierarchicalStreamWriter.endNode();
117         }
118     }
119 
120     private void setAdditionalAttributesObject(HierarchicalStreamReader hierarchicalStreamReader,
121                                                ResponseDocument responseDocument,
122                                                UnmarshallingContext unmarshallingContext) {
123         AdditionalAttributeConverter addConverter = new AdditionalAttributeConverter();
124         AdditionalAttributes additionalAttributes = (AdditionalAttributes) addConverter
125                 .unmarshal(hierarchicalStreamReader, unmarshallingContext);
126         responseDocument.setAdditionalAttributes(additionalAttributes);
127     }
128 
129     private void marshalInstance(ResponseDocument instanceDoc, HierarchicalStreamWriter hierarchicalStreamWriter,
130                                  MarshallingContext marshallingContext) {
131         ResponseDocument responseDocument = (ResponseDocument) instanceDoc;
132         hierarchicalStreamWriter.addAttribute("id", responseDocument.getId());
133         hierarchicalStreamWriter.addAttribute("category", responseDocument.getCategory());
134         hierarchicalStreamWriter.addAttribute("type", responseDocument.getType());
135         hierarchicalStreamWriter.addAttribute("format", responseDocument.getFormat());
136         hierarchicalStreamWriter.startNode("content");
137         hierarchicalStreamWriter.setValue(responseDocument.getContent().getContent());
138         hierarchicalStreamWriter.endNode();
139         hierarchicalStreamWriter.startNode("uuid");
140         hierarchicalStreamWriter.setValue(responseDocument.getUuid());
141         hierarchicalStreamWriter.endNode();
142     }
143 
144     @Override
145     public Object unmarshal(HierarchicalStreamReader hierarchicalStreamReader,
146                             UnmarshallingContext unmarshallingContext) {
147 
148         ResponseDocument responseDocument = new ResponseDocument();
149         responseDocument.setId(hierarchicalStreamReader.getAttribute("id"));
150         responseDocument.setCategory(hierarchicalStreamReader.getAttribute("category"));
151         responseDocument.setType(hierarchicalStreamReader.getAttribute("type"));
152         responseDocument.setFormat(hierarchicalStreamReader.getAttribute("format"));
153 
154 
155         while (hierarchicalStreamReader.hasMoreChildren()) {
156             hierarchicalStreamReader.moveDown();
157             if (hierarchicalStreamReader.getNodeName().equals("status")) {
158                 responseDocument.setStatus(hierarchicalStreamReader.getValue());
159             } else if (hierarchicalStreamReader.getNodeName().equals("statusMessage")) {
160                 responseDocument.setStatusMessage(hierarchicalStreamReader.getValue());
161             } else if (hierarchicalStreamReader.getNodeName().equals("uuid")) {
162                 responseDocument.setUuid(hierarchicalStreamReader.getValue());
163             } else if (hierarchicalStreamReader.getNodeName().equals("content")) {
164                 String value = hierarchicalStreamReader.getValue();
165                 Content content = new Content(value);
166                 responseDocument.setContent(content);
167             } else if (hierarchicalStreamReader.getNodeName().equals("documentName")) {
168                 responseDocument.setDocumentName(hierarchicalStreamReader.getValue());
169             } else if (hierarchicalStreamReader.getNodeName().equals("documentTitle")) {
170                 responseDocument.setDocumentTitle(hierarchicalStreamReader.getValue());
171             } else if (hierarchicalStreamReader.getNodeName().equals("documentMimeType")) {
172                 responseDocument.setDocumentMimeType(hierarchicalStreamReader.getValue());
173             } else if (hierarchicalStreamReader.getNodeName().equals("additionalAttributes")) {
174                 setAdditionalAttributesObject(hierarchicalStreamReader, responseDocument, unmarshallingContext);
175             } else if (hierarchicalStreamReader.getNodeName().equals("version")) {
176                 responseDocument.setVersion(hierarchicalStreamReader.getValue());
177             } else if (hierarchicalStreamReader.getNodeName().equals("linkedDocuments")) {
178                 while (hierarchicalStreamReader.hasMoreChildren()) {
179                     hierarchicalStreamReader.moveDown();
180                     responseDocument.addLinkedDocument(
181                             (ResponseDocument) unmarshal(hierarchicalStreamReader, unmarshallingContext));
182                     hierarchicalStreamReader.moveUp();
183                 }
184             }
185             hierarchicalStreamReader.moveUp();
186         }
187         /*
188 
189 
190                 hierarchicalStreamReader.moveDown();
191                 if (hierarchicalStreamReader.getNodeName().equals("uuid")) {
192                     String value = hierarchicalStreamReader.getValue();
193                     responseDocument.setUuid(value);
194                 }
195 
196                 if (hierarchicalStreamReader.getNodeName().equals("content")) {
197                     String value = hierarchicalStreamReader.getValue();
198                     Content content = new Content(value);
199                     responseDocument.setContent(content);
200                 }
201 
202                  hierarchicalStreamReader.moveUp();
203                 if(hierarchicalStreamReader.getNodeName().equals("documentName"))
204                 {
205                    responseDocument.setDocumentName( hierarchicalStreamReader.getValue());
206                 }
207                  if(hierarchicalStreamReader.getNodeName().equals("documentTitle"))
208                 {
209                    responseDocument.setDocumentTitle( hierarchicalStreamReader.getValue());
210                 }
211                  if(hierarchicalStreamReader.getNodeName().equals("documentMimeType"))
212                 {
213                    responseDocument.setDocumentMimeType( hierarchicalStreamReader.getValue());
214                 }
215                 hierarchicalStreamReader.moveDown();
216 
217                 hierarchicalStreamReader.moveUp();
218                 if (hierarchicalStreamReader.hasMoreChildren()) {
219                     hierarchicalStreamReader.moveDown();
220                     if (hierarchicalStreamReader.getNodeName().equals("linkedDocuments")) {
221                         while (hierarchicalStreamReader.hasMoreChildren()) {
222                             hierarchicalStreamReader.moveDown();
223                             responseDocument.addLinkedDocument(
224                                     (ResponseDocument) unmarshal(hierarchicalStreamReader, unmarshallingContext));
225                             hierarchicalStreamReader.moveUp();
226                         }
227                     }
228                     hierarchicalStreamReader.moveUp();
229                 }
230         */
231         return responseDocument;
232     }
233 
234     @Override
235     public boolean canConvert(Class aClass) {
236         return aClass.equals(ResponseDocument.class);
237     }
238 }