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
21
22
23
24
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231 return responseDocument;
232 }
233
234 @Override
235 public boolean canConvert(Class aClass) {
236 return aClass.equals(ResponseDocument.class);
237 }
238 }