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