1 40 41 package org.dspace.content.crosswalk; 42 43 import java.io.IOException ; 44 import java.sql.SQLException ; 45 import java.util.Iterator ; 46 import java.util.List ; 47 import java.util.ArrayList ; 48 import java.util.Properties ; 49 import java.util.Enumeration ; 50 import java.net.URLEncoder ; 51 52 import java.sql.SQLException ; 53 54 import org.apache.log4j.Logger; 55 56 import org.dspace.core.Context; 57 import org.dspace.core.Constants; 58 import org.dspace.core.ConfigurationManager; 59 import org.dspace.content.Bitstream; 60 import org.dspace.content.BitstreamFormat; 61 import org.dspace.content.FormatIdentifier; 62 import org.dspace.content.Bundle; 63 import org.dspace.content.Item; 64 import org.dspace.content.DSpaceObject; 65 import org.dspace.authorize.AuthorizeException; 66 67 import org.jdom.*; 68 69 81 public class PREMISCrosswalk 82 implements IngestionCrosswalk, DisseminationCrosswalk 83 { 84 85 private static Logger log = Logger.getLogger(PREMISCrosswalk.class); 86 87 private static final Namespace PREMIS_NS = 88 Namespace.getNamespace("premis", "http://www.loc.gov/standards/premis"); 89 90 private String schemaLocation = 92 PREMIS_NS.getURI()+" http://www.loc.gov/standards/premis/PREMIS-v1-0.xsd"; 93 94 private static final Namespace XLINK_NS = 95 Namespace.getNamespace("xlink", "http://www.w3.org/TR/xlink"); 96 97 private static final Namespace namespaces[] = { PREMIS_NS }; 98 99 100 101 public void ingest(Context context, DSpaceObject dso, Element root) 102 throws CrosswalkException, IOException , SQLException , AuthorizeException 103 { 104 if (!(root.getName().equals("premis"))) 105 throw new MetadataValidationException("Wrong root element for PREMIS: "+root.toString()); 106 ingest(context, dso, root.getChildren()); 107 } 108 109 public void ingest(Context context, DSpaceObject dso, List ml) 110 throws CrosswalkException, IOException , SQLException , AuthorizeException 111 { 112 if (dso.getType() != Constants.BITSTREAM) 114 throw new CrosswalkObjectNotSupported("Wrong target object type, PREMISCrosswalk can only crosswalk to a Bitstream."); 115 116 Bitstream bitstream = (Bitstream)dso; 117 String MIMEType = null; 118 String bsName = null; 119 Iterator mi = ml.iterator(); 120 while (mi.hasNext()) 121 { 122 Element me = (Element)mi.next(); 123 124 if (me.getName().equals("premis")) 126 ingest(context, dso, me.getChildren()); 127 128 else if (me.getName().equals("object")) 130 { 131 Element on = me.getChild("originalName", PREMIS_NS); 133 if (on != null) 134 bsName = on.getTextTrim(); 135 136 Element oc = me.getChild("objectCharacteristics", PREMIS_NS); 140 if (oc != null) 141 { 142 String ssize = oc.getChildTextTrim("size", PREMIS_NS); 143 if (ssize != null) 144 { 145 try 146 { 147 int size = Integer.parseInt(ssize); 148 if (bitstream.getSize() != size) 149 throw new MetadataValidationException( 150 "Bitstream size ("+String.valueOf(bitstream.getSize())+ 151 ") does not match size in PREMIS ("+ssize+"), rejecting it."); 152 } 153 catch (NumberFormatException ne) 154 { 155 throw new MetadataValidationException("Bad number value in PREMIS object/objectCharacteristics/size: "+ssize, ne); 156 } 157 } 158 Element fixity = oc.getChild("fixity", PREMIS_NS); 159 if (fixity != null) 160 { 161 String alg = fixity.getChildTextTrim("messageDigestAlgorithm", PREMIS_NS); 162 String md = fixity.getChildTextTrim("messageDigest", PREMIS_NS); 163 String b_alg = bitstream.getChecksumAlgorithm(); 164 String b_md = bitstream.getChecksum(); 165 if (alg != null && md != null && 166 b_alg != null && b_md != null && 167 alg.equals(b_alg)) 168 { 169 if (md.equals(b_md)) 170 log.debug("Bitstream checksum agrees with PREMIS: "+bitstream.getName()); 171 else 172 throw new MetadataValidationException("Bitstream "+alg+" Checksum does not match value in PREMIS ("+b_md+" != "+md+"), for bitstream: "+bitstream.getName()); 173 } 174 else 175 log.warn("Cannot test checksum on bitstream="+bitstream.getName()+ 176 ", algorithm in PREMIS is different: "+alg); 177 } 178 179 Element format = oc.getChild("format", PREMIS_NS); 182 if (format != null) 183 { 184 Element fd = format.getChild("formatDesignation", PREMIS_NS); 185 if (fd != null) 186 MIMEType = fd.getChildTextTrim("formatName", PREMIS_NS); 187 } 188 } 189 190 if (bsName != null) 192 { 193 bitstream.setName(bsName); 194 log.debug("Changing bitstream id="+String.valueOf(bitstream.getID())+"name and source to: "+bsName); 195 } 196 197 BitstreamFormat bf = (MIMEType == null) ? null : 201 BitstreamFormat.findByMIMEType(context, MIMEType); 202 if (bf == null) 203 bf = FormatIdentifier.guessFormat(context, bitstream); 204 if (bf != null) 205 bitstream.setFormat(bf); 206 } 207 else 208 log.debug("Skipping element: "+me.toString()); 209 } 210 bitstream.update(); 211 } 212 213 214 215 public Namespace[] getNamespaces() 216 { 217 return namespaces; 218 } 219 220 public String getSchemaLocation() 221 { 222 return schemaLocation; 223 } 224 225 public boolean canDisseminate(DSpaceObject dso) 226 { 227 return true; 228 } 229 230 public Element disseminateElement(DSpaceObject dso) 231 throws CrosswalkException, 232 IOException , SQLException , AuthorizeException 233 { 234 if (dso.getType() != Constants.BITSTREAM) 235 throw new CrosswalkObjectNotSupported("PREMISCrosswalk can only crosswalk a Bitstream."); 236 Bitstream bitstream = (Bitstream)dso; 237 238 Element premis = new Element("premis", PREMIS_NS); 239 Element object = new Element("object", PREMIS_NS); 240 premis.addContent(object); 241 242 Element oid = new Element("objectIdentifier", PREMIS_NS); 244 Element oit = new Element("objectIdentifierType", PREMIS_NS); 245 oit.setText("URL"); 246 oid.addContent(oit); 247 Element oiv = new Element("objectIdentifierValue", PREMIS_NS); 248 249 String sid = String.valueOf(bitstream.getSequenceID()); 254 String baseUrl = ConfigurationManager.getProperty("dspace.url"); 255 String handle = null; 256 Bundle[] bn = bitstream.getBundles(); 258 if (bn.length > 0) 259 { 260 Item bi[] = bn[0].getItems(); 261 if (bi.length > 0) 262 handle = bi[0].getHandle(); 263 } 264 String bsName = bitstream.getName(); 266 if (bsName == null) 267 { 268 String ext[] = bitstream.getFormat().getExtensions(); 269 bsName = "bitstream_"+sid+ (ext.length > 0 ? ext[0] : ""); 270 } 271 if (handle != null && baseUrl != null) 272 oiv.setText(baseUrl 273 + "/bitstream/" 274 + URLEncoder.encode(handle, "UTF-8") 275 + "/" 276 + sid 277 + "/" 278 + URLEncoder.encode(bsName, "UTF-8")); 279 else 280 oiv.setText(URLEncoder.encode(bsName, "UTF-8")); 281 282 oid.addContent(oiv); 283 object.addContent(oid); 284 285 Element oc = new Element("objectCategory", PREMIS_NS); 287 oc.setText("File"); 288 object.addContent(oc); 289 290 Element ochar = new Element("objectCharacteristics", PREMIS_NS); 291 object.addContent(ochar); 292 293 String cks = bitstream.getChecksum(); 295 String cka = bitstream.getChecksumAlgorithm(); 296 if (cks != null && cka != null) 297 { 298 Element fixity = new Element("fixity", PREMIS_NS); 299 Element mda = new Element("messageDigestAlgorithm", PREMIS_NS); 300 mda.setText(cka); 301 fixity.addContent(mda); 302 Element md = new Element("messageDigest", PREMIS_NS); 303 md.setText(cks); 304 fixity.addContent(md); 305 ochar.addContent(fixity); 306 } 307 308 Element size = new Element("size", PREMIS_NS); 310 size.setText(String.valueOf(bitstream.getSize())); 311 ochar.addContent(size); 312 313 Element format = new Element("format", PREMIS_NS); 319 Element formatDes = new Element("formatDesignation", PREMIS_NS); 320 Element formatName = new Element("formatName", PREMIS_NS); 321 formatName.setText(bitstream.getFormat().getMIMEType()); 322 formatDes.addContent(formatName); 323 format.addContent(formatDes); 324 ochar.addContent(format); 325 326 String oname = bitstream.getName(); 328 if (oname == null) 329 oname = bitstream.getSource(); 330 if (oname != null) 331 { 332 Element on = new Element("originalName", PREMIS_NS); 333 on.setText(oname); 334 object.addContent(on); 335 } 336 337 return premis; 338 } 339 340 public List disseminateList(DSpaceObject dso) 341 throws CrosswalkException, 342 IOException , SQLException , AuthorizeException 343 { 344 List result = new ArrayList (1); 345 result.add(disseminateElement(dso)); 346 return result; 347 } 348 349 public boolean preferList() 350 { 351 return false; 352 } 353 } 354 | Popular Tags |