001/* 002 * Copyright 2011 The Kuali Foundation. 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.ole.docstore.model.xstream.ingest; 017 018import com.thoughtworks.xstream.converters.Converter; 019import com.thoughtworks.xstream.converters.MarshallingContext; 020import com.thoughtworks.xstream.converters.UnmarshallingContext; 021import com.thoughtworks.xstream.io.HierarchicalStreamReader; 022import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 023import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes; 024import org.kuali.ole.docstore.model.xmlpojo.ingest.Content; 025import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument; 026import org.xml.sax.InputSource; 027 028import javax.xml.parsers.DocumentBuilder; 029import javax.xml.parsers.DocumentBuilderFactory; 030import java.io.StringReader; 031import java.util.Iterator; 032import java.util.List; 033 034/** 035 * Created by IntelliJ IDEA. 036 * User: pvsubrah 037 * Date: 9/9/11 038 * Time: 10:39 AM 039 * To change this template use File | Settings | File Templates. 040 */ 041public class RequestDocumentConverter 042 implements Converter { 043 @Override 044 public void marshal(Object o, HierarchicalStreamWriter hierarchicalStreamWriter, 045 MarshallingContext marshallingContext) { 046 AdditionalAttributeConverter addConverter = new AdditionalAttributeConverter(); 047 RequestDocument requestDocument = (RequestDocument) o; 048 if (requestDocument.getId() != null) { 049 hierarchicalStreamWriter.addAttribute("id", requestDocument.getId()); 050 } 051 if (requestDocument.getCategory() != null) { 052 hierarchicalStreamWriter.addAttribute("category", requestDocument.getCategory()); 053 } 054 if (requestDocument.getType() != null) { 055 hierarchicalStreamWriter.addAttribute("type", requestDocument.getType()); 056 } 057 if (requestDocument.getFormat() != null) { 058 hierarchicalStreamWriter.addAttribute("format", requestDocument.getFormat()); 059 } 060 061 if (requestDocument.getContent() != null && requestDocument.getContent().getContent() != null) { 062 hierarchicalStreamWriter.startNode("content"); 063 hierarchicalStreamWriter.setValue(requestDocument.getContent().getContent()); 064 hierarchicalStreamWriter.endNode(); 065 } 066 067 //for License 068 if (requestDocument.getUuid() != null) { 069 hierarchicalStreamWriter.startNode("uuid"); 070 hierarchicalStreamWriter.setValue(requestDocument.getUuid()); 071 hierarchicalStreamWriter.endNode(); 072 } 073 if (requestDocument.getDocumentName() != null) { 074 hierarchicalStreamWriter.startNode("documentName"); 075 hierarchicalStreamWriter.setValue(requestDocument.getDocumentName()); 076 hierarchicalStreamWriter.endNode(); 077 } 078 if (requestDocument.getDocumentTitle() != null) { 079 hierarchicalStreamWriter.startNode("documentTitle"); 080 hierarchicalStreamWriter.setValue(requestDocument.getDocumentTitle()); 081 hierarchicalStreamWriter.endNode(); 082 } 083 if (requestDocument.getDocumentMimeType() != null) { 084 hierarchicalStreamWriter.startNode("documentMimeType"); 085 hierarchicalStreamWriter.setValue(requestDocument.getDocumentMimeType()); 086 hierarchicalStreamWriter.endNode(); 087 } 088 089 //ends here... 090 091 AdditionalAttributes additionalAttributes = requestDocument.getAdditionalAttributes(); 092 addConverter.marshal(additionalAttributes, hierarchicalStreamWriter, marshallingContext); 093 094 List<RequestDocument> linkedRequestDocuments = requestDocument.getLinkedRequestDocuments(); 095 if (linkedRequestDocuments != null && linkedRequestDocuments.size() > 0) { 096 hierarchicalStreamWriter.startNode("linkedIngestDocuments"); 097 098 for (Iterator<RequestDocument> iterator = linkedRequestDocuments.iterator(); iterator.hasNext(); ) { 099 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 if (hierarchicalStreamReader.hasMoreChildren()) { 139 hierarchicalStreamReader.moveDown(); 140 if (hierarchicalStreamReader.getNodeName().equals("additionalAttributes")) { 141 setAdditionalAttributesObject(hierarchicalStreamReader, requestDocument); 142 } 143 if (hierarchicalStreamReader.getNodeName().equals("linkedIngestDocuments")) { 144 setLinkedRequestDocuments(hierarchicalStreamReader, requestDocument, unmarshallingContext); 145 } 146 hierarchicalStreamReader.moveUp(); 147 } 148 if (hierarchicalStreamReader.hasMoreChildren()) { 149 hierarchicalStreamReader.moveDown(); 150 if (hierarchicalStreamReader.getNodeName().equals("linkedIngestDocuments")) { 151 setLinkedRequestDocuments(hierarchicalStreamReader, requestDocument, unmarshallingContext); 152 } 153 hierarchicalStreamReader.moveUp(); 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 if (hierarchicalStreamReader.getNodeName().equals("dateEntered")) { 180 additionalAttributes.setDateEntered(hierarchicalStreamReader.getValue()); 181 } 182 else if (hierarchicalStreamReader.getNodeName().equals("lastUpdated")) { 183 additionalAttributes.setLastUpdated(hierarchicalStreamReader.getValue()); 184 } 185 else if (hierarchicalStreamReader.getNodeName().equals("fastAddFlag")) { 186 additionalAttributes.setFastAddFlag(hierarchicalStreamReader.getValue()); 187 } 188 else if (hierarchicalStreamReader.getNodeName().equals("supressFromPublic")) { 189 additionalAttributes.setSupressFromPublic(hierarchicalStreamReader.getValue()); 190 } 191 else if (hierarchicalStreamReader.getNodeName().equals("harvestable")) { 192 additionalAttributes.setHarvestable(hierarchicalStreamReader.getValue()); 193 } 194 else if (hierarchicalStreamReader.getNodeName().equals("status")) { 195 additionalAttributes.setStatus(hierarchicalStreamReader.getValue()); 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}