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          {
69              hierarchicalStreamWriter.startNode("documentName");
70              hierarchicalStreamWriter.setValue(responseDocument.getDocumentName());
71              hierarchicalStreamWriter.endNode();
72          }
73          if(responseDocument.getDocumentTitle() != null)
74          {
75              hierarchicalStreamWriter.startNode("documentTitle");
76              hierarchicalStreamWriter.setValue(responseDocument.getDocumentTitle());
77              hierarchicalStreamWriter.endNode();
78          }
79          if(responseDocument.getDocumentMimeType() != null)
80          {
81              hierarchicalStreamWriter.startNode("documentMimeType");
82              hierarchicalStreamWriter.setValue(responseDocument.getDocumentMimeType());
83              hierarchicalStreamWriter.endNode();
84          }
85          if (responseDocument.getVersion() != null) {
86              hierarchicalStreamWriter.startNode("version");
87              hierarchicalStreamWriter.setValue(responseDocument.getVersion());
88              hierarchicalStreamWriter.endNode();
89          }
90  
91  
92  
93          if (responseDocument.getAdditionalAttributes() != null) {
94              AdditionalAttributeConverter addConverter = new AdditionalAttributeConverter();
95              AdditionalAttributes additionalAttributes = responseDocument.getAdditionalAttributes();
96              addConverter.marshal(additionalAttributes, hierarchicalStreamWriter, marshallingContext);
97          }
98  
99          List<ResponseDocument> linkedResponseDocuments = responseDocument.getLinkedDocuments();
100         if (linkedResponseDocuments != null && linkedResponseDocuments.size() > 0) {
101             hierarchicalStreamWriter.startNode("linkedDocuments");
102             for (Iterator<ResponseDocument> iterator = linkedResponseDocuments.iterator(); iterator.hasNext(); ) {
103                 hierarchicalStreamWriter.startNode("linkedDocument");
104                 ResponseDocument respDoc = iterator.next();
105                 marshal(respDoc, hierarchicalStreamWriter, marshallingContext);
106                 List<ResponseDocument> instanceResposeDocuments = responseDocument.getLinkedInstanceDocuments();
107                 if (instanceResposeDocuments != null && instanceResposeDocuments.size() > 0) {
108                     hierarchicalStreamWriter.startNode("linkedDocuments");
109                     for (Iterator<ResponseDocument> instanceIterator = instanceResposeDocuments
110                             .iterator(); instanceIterator.hasNext(); ) {
111                         hierarchicalStreamWriter.startNode("linkedDocument");
112                         ResponseDocument instanceDoc = instanceIterator.next();
113                         marshalInstance(instanceDoc, hierarchicalStreamWriter, marshallingContext);
114                         hierarchicalStreamWriter.endNode();
115                     }
116                     hierarchicalStreamWriter.endNode();
117                 }
118                 hierarchicalStreamWriter.endNode();
119             }
120             hierarchicalStreamWriter.endNode();
121         }
122     }
123 
124     private void setAdditionalAttributesObject(HierarchicalStreamReader hierarchicalStreamReader,
125                                                  ResponseDocument responseDocument,
126                                                  UnmarshallingContext unmarshallingContext) {
127           AdditionalAttributeConverter addConverter = new AdditionalAttributeConverter();
128           AdditionalAttributes additionalAttributes = (AdditionalAttributes) addConverter
129                   .unmarshal(hierarchicalStreamReader, unmarshallingContext);
130           responseDocument.setAdditionalAttributes(additionalAttributes);
131       }
132 
133     private void marshalInstance(ResponseDocument instanceDoc, HierarchicalStreamWriter hierarchicalStreamWriter,
134                                  MarshallingContext marshallingContext) {
135         ResponseDocument responseDocument = (ResponseDocument) instanceDoc;
136         hierarchicalStreamWriter.addAttribute("id", responseDocument.getId());
137         hierarchicalStreamWriter.addAttribute("category", responseDocument.getCategory());
138         hierarchicalStreamWriter.addAttribute("type", responseDocument.getType());
139         hierarchicalStreamWriter.addAttribute("format", responseDocument.getFormat());
140         hierarchicalStreamWriter.startNode("content");
141         hierarchicalStreamWriter.setValue(responseDocument.getContent().getContent());
142         hierarchicalStreamWriter.endNode();
143         hierarchicalStreamWriter.startNode("uuid");
144         hierarchicalStreamWriter.setValue(responseDocument.getUuid());
145         hierarchicalStreamWriter.endNode();
146     }
147 
148     @Override
149     public Object unmarshal(HierarchicalStreamReader hierarchicalStreamReader,
150                             UnmarshallingContext unmarshallingContext) {
151 
152         ResponseDocument responseDocument = new ResponseDocument();
153         responseDocument.setId(hierarchicalStreamReader.getAttribute("id"));
154         responseDocument.setCategory(hierarchicalStreamReader.getAttribute("category"));
155         responseDocument.setType(hierarchicalStreamReader.getAttribute("type"));
156         responseDocument.setFormat(hierarchicalStreamReader.getAttribute("format"));
157 
158 
159         while (hierarchicalStreamReader.hasMoreChildren()) {
160             hierarchicalStreamReader.moveDown();
161             if(hierarchicalStreamReader.getNodeName().equals("status")){
162                 responseDocument.setStatus(hierarchicalStreamReader.getValue());
163             }
164             else if(hierarchicalStreamReader.getNodeName().equals("statusMessage")){
165                 responseDocument.setStatusMessage(hierarchicalStreamReader.getValue());
166             }
167 
168             else if (hierarchicalStreamReader.getNodeName().equals("uuid")) {
169                 responseDocument.setUuid(hierarchicalStreamReader.getValue());
170             }
171 
172             else if (hierarchicalStreamReader.getNodeName().equals("content")) {
173                 String value = hierarchicalStreamReader.getValue();
174                 Content content = new Content(value);
175                 responseDocument.setContent(content);
176             }
177 
178             else if (hierarchicalStreamReader.getNodeName().equals("documentName")) {
179                 responseDocument.setDocumentName(hierarchicalStreamReader.getValue());
180             }
181             else if (hierarchicalStreamReader.getNodeName().equals("documentTitle")) {
182                 responseDocument.setDocumentTitle(hierarchicalStreamReader.getValue());
183             }
184 
185             else if (hierarchicalStreamReader.getNodeName().equals("documentMimeType")) {
186                 responseDocument.setDocumentMimeType(hierarchicalStreamReader.getValue());
187             }
188             else if (hierarchicalStreamReader.getNodeName().equals("additionalAttributes")) {
189                 setAdditionalAttributesObject(hierarchicalStreamReader, responseDocument, unmarshallingContext);
190             }
191             
192             else if (hierarchicalStreamReader.getNodeName().equals("version")) {
193                 responseDocument.setVersion(hierarchicalStreamReader.getValue());
194             }
195 
196             else if (hierarchicalStreamReader.getNodeName().equals("linkedDocuments")) {
197                 while (hierarchicalStreamReader.hasMoreChildren()) {
198                     hierarchicalStreamReader.moveDown();
199                     responseDocument.addLinkedDocument(
200                             (ResponseDocument) unmarshal(hierarchicalStreamReader, unmarshallingContext));
201                     hierarchicalStreamReader.moveUp();
202                 }
203             }
204             hierarchicalStreamReader.moveUp();
205         }
206         /*
207 
208 
209                 hierarchicalStreamReader.moveDown();
210                 if (hierarchicalStreamReader.getNodeName().equals("uuid")) {
211                     String value = hierarchicalStreamReader.getValue();
212                     responseDocument.setUuid(value);
213                 }
214 
215                 if (hierarchicalStreamReader.getNodeName().equals("content")) {
216                     String value = hierarchicalStreamReader.getValue();
217                     Content content = new Content(value);
218                     responseDocument.setContent(content);
219                 }
220 
221                  hierarchicalStreamReader.moveUp();
222                 if(hierarchicalStreamReader.getNodeName().equals("documentName"))
223                 {
224                    responseDocument.setDocumentName( hierarchicalStreamReader.getValue());
225                 }
226                  if(hierarchicalStreamReader.getNodeName().equals("documentTitle"))
227                 {
228                    responseDocument.setDocumentTitle( hierarchicalStreamReader.getValue());
229                 }
230                  if(hierarchicalStreamReader.getNodeName().equals("documentMimeType"))
231                 {
232                    responseDocument.setDocumentMimeType( hierarchicalStreamReader.getValue());
233                 }
234                 hierarchicalStreamReader.moveDown();
235 
236                 hierarchicalStreamReader.moveUp();
237                 if (hierarchicalStreamReader.hasMoreChildren()) {
238                     hierarchicalStreamReader.moveDown();
239                     if (hierarchicalStreamReader.getNodeName().equals("linkedDocuments")) {
240                         while (hierarchicalStreamReader.hasMoreChildren()) {
241                             hierarchicalStreamReader.moveDown();
242                             responseDocument.addLinkedDocument(
243                                     (ResponseDocument) unmarshal(hierarchicalStreamReader, unmarshallingContext));
244                             hierarchicalStreamReader.moveUp();
245                         }
246                     }
247                     hierarchicalStreamReader.moveUp();
248                 }
249         */
250         return responseDocument;
251     }
252 
253     @Override
254     public boolean canConvert(Class aClass) {
255         return aClass.equals(ResponseDocument.class);
256     }
257 }