1 34 35 package org.dspace.app.oai; 36 37 import java.util.Properties ; 38 import java.util.Date ; 39 import java.io.*; 40 import java.lang.*; 41 import java.sql.SQLException ; 42 import ORG.oclc.oai.server.crosswalk.Crosswalk; 43 import ORG.oclc.oai.server.verb.CannotDisseminateFormatException; 44 import ORG.oclc.oai.server.verb.ServerVerb; 45 import org.dspace.content.DCDate; 46 import org.dspace.content.DCValue; 47 import org.dspace.content.Item; 48 import org.dspace.content.Bundle; 49 import org.dspace.content.Bitstream; 50 import org.dspace.content.BitstreamFormat; 51 import org.dspace.storage.bitstore.BitstreamStorageManager; 52 import org.dspace.app.didl.UUIDFactory; 53 import org.dspace.core.Utils; 54 import org.dspace.search.HarvestedItemInfo; 55 import sun.misc.BASE64Encoder; 56 import org.dspace.core.*; 57 58 67 68 public class DIDLCrosswalk extends Crosswalk 69 { 70 public DIDLCrosswalk(Properties properties) 71 { 72 super("urn:mpeg:mpeg21:2002:02-DIDL-NS http://standards.iso.org/ittf/PubliclyAvailableStandards/MPEG-21_schema_files/did/didl.xsd "); 73 } 74 75 76 public boolean isAvailableFor(Object nativeItem) 77 { 78 return true; 80 } 81 82 83 public String createMetadata(Object nativeItem) 84 throws CannotDisseminateFormatException 85 { 86 Item item = ((HarvestedItemInfo) nativeItem).item; 87 88 Date d = ((HarvestedItemInfo) nativeItem).datestamp; 89 String ITEMDATE = new DCDate(d).toString(); 90 91 DCValue[] allDC = item.getDC(Item.ANY, Item.ANY, Item.ANY); 93 94 StringBuffer metadata = new StringBuffer (); 95 StringBuffer metadata1 = new StringBuffer (); 96 String itemhandle=item.getHandle(); 97 int maxsize= Integer.parseInt(ConfigurationManager.getProperty("oai.didl.maxresponse")); 98 String currdate=ServerVerb.createResponseDate(new Date ()); 99 100 metadata.append("<didl:DIDL ") 101 .append(" xmlns:didl=\"urn:mpeg:mpeg21:2002:02-DIDL-NS\" ") 102 .append(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ") 103 .append("xsi:schemaLocation=\"urn:mpeg:mpeg21:2002:02-DIDL-NS http://standards.iso.org/ittf/PubliclyAvailableStandards/MPEG-21_schema_files/did/didl.xsd \">") 104 .append ("<didl:DIDLInfo>") 105 .append ("<dcterms:created xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://purl.org/dc/terms/ \">") 106 .append (currdate) 107 .append ("</dcterms:created> </didl:DIDLInfo>" ) 108 .append("<didl:Item id=\"") 109 .append("uuid-" + UUIDFactory.generateUUID().toString()+"\">"); 110 metadata.append("<didl:Descriptor>") 111 .append("<didl:Statement mimeType=\"application/xml; charset=utf-8\">") 112 .append("<dii:Identifier xmlns:dii=\"urn:mpeg:mpeg21:2002:01-DII-NS\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"urn:mpeg:mpeg21:2002:01-DII-NS http://standards.iso.org/ittf/PubliclyAvailableStandards/MPEG-21_schema_files/dii/dii.xsd\">") 113 .append("urn:hdl:" + itemhandle) 114 .append("</dii:Identifier>") 115 .append("</didl:Statement>") 116 .append("</didl:Descriptor>"); 117 metadata.append("<didl:Descriptor>") 118 .append("<didl:Statement mimeType=\"application/xml; charset=utf-8\">"); 119 120 for (int i = 0; i < allDC.length; i++) 121 { 122 boolean description = allDC[i].element.equals("description"); 124 boolean provenance = allDC[i].qualifier != null && 125 allDC[i].qualifier.equals("provenance"); 126 127 if (!(description && provenance)) 128 { 129 String value = allDC[i].value; 131 132 int c = -1; 135 while ((c = value.indexOf("&", c + 1)) > -1) 136 { 137 value = value.substring(0, c) + 138 "&" + 139 value.substring(c + 1); 140 } 141 142 while ((c = value.indexOf("<")) > -1) 143 { 144 value = value.substring(0, c) + 145 "<" + 146 value.substring(c + 1); 147 } 148 149 while ((c = value.indexOf(">")) > -1) 150 { 151 value = value.substring(0, c) + 152 ">" + 153 value.substring(c + 1); 154 } 155 156 metadata1.append("<dc:") 157 .append(allDC[i].element) 158 .append(">") 159 .append(value) 160 .append("</dc:") 161 .append(allDC[i].element) 162 .append(">"); 163 } 164 } 165 166 metadata.append("<oai_dc:dc xmlns:oai_dc=\"http://www.openarchives.org/OAI/2.0/oai_dc/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd\">"); 167 168 metadata.append(metadata1); 169 170 metadata.append("</oai_dc:dc>") 171 .append("</didl:Statement>") 172 .append("</didl:Descriptor>"); 173 174 175 176 177 179 try 180 { 181 Bundle[] bundles= item.getBundles("ORIGINAL"); 182 183 if (bundles.length == 0) 184 { 185 metadata.append("<P>There are no files associated with this item.</P>"); 186 } 187 else 188 { 189 190 for (int i = 0; i < bundles.length; i++) 191 { 192 int flag=0; 193 Bitstream[] bitstreams = bundles[i].getBitstreams(); 194 195 196 for (int k = 0; k < bitstreams.length ; k++) 197 { 198 if (!bitstreams[k].getFormat().isInternal()) 200 { 201 if (flag==0) 202 { 203 flag=1; 204 } 205 206 metadata.append("<didl:Component id=" + "\"uuid-"+ UUIDFactory.generateUUID().toString() + "\">"); 207 208 if (bitstreams[k].getSize()> maxsize) 209 { 210 metadata.append("<didl:Resource ref=\""+ConfigurationManager.getProperty("dspace.url")+"/bitstream/"+itemhandle+"/"+bitstreams[k].getSequenceID()+"/"+bitstreams[k].getName() ); 211 metadata.append("\" mimeType=\""); 212 metadata.append(bitstreams[k].getFormat().getMIMEType()); 213 metadata.append("\">"); 214 metadata.append("</didl:Resource>"); 215 } 216 else 217 { 218 219 try 220 { 221 metadata.append("<didl:Resource mimeType=\""); 222 metadata.append(bitstreams[k].getFormat().getMIMEType()); 223 metadata.append("\" encoding=\"base64\">"); 224 225 229 int intSize = (int) bitstreams[k].getSize(); 230 231 byte[] buffer = new byte[intSize]; 232 233 Context contextl= new Context(); 234 BufferedInputStream bis=new BufferedInputStream(BitstreamStorageManager.retrieve(contextl,bitstreams[k].getID())); 235 int size=bis.read(buffer); 236 contextl.complete(); 237 238 BASE64Encoder encoder=(BASE64Encoder) Class.forName("sun.misc.BASE64Encoder").newInstance(); 239 String encoding = encoder.encodeBuffer(buffer); 240 metadata.append(encoding); 241 } 242 catch (Exception ex) 243 { 244 ex.printStackTrace(); 245 246 metadata.append("<didl:Resource ref=\""+ConfigurationManager.getProperty("dspace.url")+"/bitstream/"+itemhandle+"/"+bitstreams[k].getSequenceID()+"/"+bitstreams[k].getName() ); 247 metadata.append("\" mimeType=\""); 248 metadata.append(bitstreams[k].getFormat().getMIMEType()); 249 metadata.append("\">"); 250 } 251 252 metadata.append("</didl:Resource>"); 253 } 254 metadata.append("</didl:Component>"); 255 } 256 257 } 258 259 } 260 } 261 } 262 catch (SQLException sqle) 263 { 264 System.err.println("Caught exception:"+sqle.getCause()); 265 sqle.printStackTrace(); 266 } 267 268 270 metadata.append("</didl:Item>") 271 .append("</didl:DIDL>"); 272 273 return metadata.toString(); 274 } 275 } | Popular Tags |