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 hierarchicalStreamWriter.startNode("uuid");
70 hierarchicalStreamWriter.setValue(requestDocument.getUuid());
71 hierarchicalStreamWriter.endNode();
72 }
73 if (requestDocument.getDocumentName() != null) {
74 hierarchicalStreamWriter.startNode("documentName");
75 hierarchicalStreamWriter.setValue(requestDocument.getDocumentName());
76 hierarchicalStreamWriter.endNode();
77 }
78 if (requestDocument.getDocumentTitle() != null) {
79 hierarchicalStreamWriter.startNode("documentTitle");
80 hierarchicalStreamWriter.setValue(requestDocument.getDocumentTitle());
81 hierarchicalStreamWriter.endNode();
82 }
83 if (requestDocument.getDocumentMimeType() != null) {
84 hierarchicalStreamWriter.startNode("documentMimeType");
85 hierarchicalStreamWriter.setValue(requestDocument.getDocumentMimeType());
86 hierarchicalStreamWriter.endNode();
87 }
88
89
90
91 AdditionalAttributes additionalAttributes = requestDocument.getAdditionalAttributes();
92 addConverter.marshal(additionalAttributes, hierarchicalStreamWriter, marshallingContext);
93
94 List<RequestDocument> linkedRequestDocuments = requestDocument.getLinkedRequestDocuments();
95 if (linkedRequestDocuments != null && linkedRequestDocuments.size() > 0) {
96 hierarchicalStreamWriter.startNode("linkedIngestDocuments");
97
98 for (Iterator<RequestDocument> iterator = linkedRequestDocuments.iterator(); iterator.hasNext(); ) {
99 hierarchicalStreamWriter.startNode("linkedIngestDocument");
100 RequestDocument requestDoc = iterator.next();
101 marshal(requestDoc, hierarchicalStreamWriter, marshallingContext);
102 hierarchicalStreamWriter.endNode();
103 }
104
105 hierarchicalStreamWriter.endNode();
106 }
107 }
108
109 @Override
110 public Object unmarshal(HierarchicalStreamReader hierarchicalStreamReader,
111 UnmarshallingContext unmarshallingContext) {
112 RequestDocument requestDocument = new RequestDocument();
113 requestDocument.setId(hierarchicalStreamReader.getAttribute("id"));
114 requestDocument.setCategory(hierarchicalStreamReader.getAttribute("category"));
115 requestDocument.setType(hierarchicalStreamReader.getAttribute("type"));
116 requestDocument.setFormat(hierarchicalStreamReader.getAttribute("format"));
117 while (hierarchicalStreamReader.hasMoreChildren()) {
118 hierarchicalStreamReader.moveDown();
119
120 if (hierarchicalStreamReader.getNodeName().equals("uuid")) {
121 requestDocument.setUuid(hierarchicalStreamReader.getValue());
122 } else if (hierarchicalStreamReader.getNodeName().equals("documentName")) {
123 requestDocument.setDocumentName(hierarchicalStreamReader.getValue());
124 } else if (hierarchicalStreamReader.getNodeName().equals("documentTitle")) {
125 requestDocument.setDocumentTitle(hierarchicalStreamReader.getValue());
126 } else if (hierarchicalStreamReader.getNodeName().equals("documentMimeType")) {
127 requestDocument.setDocumentMimeType(hierarchicalStreamReader.getValue());
128 } else if (hierarchicalStreamReader.getNodeName().equals("content")) {
129 setContentObject(hierarchicalStreamReader, requestDocument);
130 } else if (hierarchicalStreamReader.getNodeName().equals("additionalAttributes")) {
131 setAdditionalAttributesObject(hierarchicalStreamReader, requestDocument, unmarshallingContext);
132 } else if (hierarchicalStreamReader.getNodeName().equals("linkedIngestDocuments")) {
133 setLinkedRequestDocuments(hierarchicalStreamReader, requestDocument, unmarshallingContext);
134 }
135 hierarchicalStreamReader.moveUp();
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 return requestDocument;
158 }
159
160
161 private void setLinkedRequestDocuments(HierarchicalStreamReader hierarchicalStreamReader,
162 RequestDocument requestDocument, UnmarshallingContext unmarshallingContext) {
163 while (hierarchicalStreamReader.hasMoreChildren()) {
164 hierarchicalStreamReader.moveDown();
165 requestDocument.addLinkedRequestDocument((RequestDocument) unmarshal(hierarchicalStreamReader, unmarshallingContext));
166 hierarchicalStreamReader.moveUp();
167 }
168
169 }
170
171 private void setAdditionalAttributesObject(HierarchicalStreamReader hierarchicalStreamReader,
172 RequestDocument requestDocument,
173 UnmarshallingContext unmarshallingContext) {
174 AdditionalAttributeConverter addConverter = new AdditionalAttributeConverter();
175 AdditionalAttributes additionalAttributes = (AdditionalAttributes) addConverter
176 .unmarshal(hierarchicalStreamReader, unmarshallingContext);
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 requestDocument.setAdditionalAttributes(additionalAttributes);
199 }
200
201
202 private void setContentObject(HierarchicalStreamReader hierarchicalStreamReader, RequestDocument requestDocument) {
203 String value = hierarchicalStreamReader.getValue();
204 if (isValidXml(value)) {
205 Content content = new Content(value);
206 requestDocument.setContent(content);
207 }
208 }
209
210 @Override
211 public boolean canConvert(Class aClass) {
212 return aClass.equals(RequestDocument.class);
213 }
214
215 public boolean isValidXml(String xmlContent) {
216 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
217 try {
218 DocumentBuilder builder = factory.newDocumentBuilder();
219 InputSource is = new InputSource(new StringReader(xmlContent));
220 builder.parse(is);
221 return true;
222 } catch (Exception e) {
223 throw new IllegalArgumentException(
224 "Invalid XML content in RequestDocument. Try to replace (if any) '&' with '&' and '<' with '<'");
225 }
226 }
227 }