1 40 41 package org.dspace.content.crosswalk; 42 43 import java.io.InputStream ; 44 import java.io.IOException ; 45 import java.sql.SQLException ; 46 import java.util.Iterator ; 47 import java.util.List ; 48 import java.util.ArrayList ; 49 import java.util.HashMap ; 50 import java.util.Properties ; 51 import java.util.Enumeration ; 52 import java.io.ByteArrayOutputStream ; 53 import java.io.ByteArrayInputStream ; 54 import java.io.OutputStream ; 55 56 import java.sql.SQLException ; 57 import org.apache.log4j.Logger; 58 59 import org.dspace.core.Context; 60 import org.dspace.core.Constants; 61 import org.dspace.content.Item; 62 import org.dspace.content.DCDate; 63 import org.dspace.content.DCValue; 64 import org.dspace.content.DSpaceObject; 65 import org.dspace.content.packager.PackageParameters; 66 import org.dspace.content.packager.PackageDisseminator; 67 import org.dspace.content.packager.PackageException; 68 import org.dspace.authorize.AuthorizeException; 69 import org.dspace.core.ConfigurationManager; 70 import org.dspace.core.PluginManager; 71 72 import org.jdom.*; 73 import org.jdom.output.XMLOutputter; 74 import org.jdom.output.Format; 75 import org.jdom.input.SAXBuilder; 76 import org.jdom.input.JDOMParseException; 77 78 88 public class METSDisseminationCrosswalk 89 implements DisseminationCrosswalk 90 { 91 92 private static Logger log = Logger.getLogger(METSDisseminationCrosswalk.class); 93 94 private static final String METS_PACKAGER_PLUGIN = "METS"; 97 98 101 public static final Namespace MODS_NS = 102 Namespace.getNamespace("mods", "http://www.loc.gov/mods/v3"); 103 104 private static final Namespace XLINK_NS = 105 Namespace.getNamespace("xlink", "http://www.w3.org/TR/xlink"); 106 107 108 109 private static Namespace METS_NS = Namespace 110 .getNamespace("mets", "http://www.loc.gov/METS/"); 111 112 private static final Namespace namespaces[] = { METS_NS, MODS_NS, XLINK_NS }; 113 114 115 private static final String METS_XSD = "http://www.loc.gov/standards/mets/mets.xsd"; 116 117 private static final String schemaLocation = 118 METS_NS.getURI()+" "+METS_XSD; 119 120 private static XMLOutputter outputUgly = new XMLOutputter(); 121 private static XMLOutputter outputPretty = new XMLOutputter(Format.getPrettyFormat()); 122 private static SAXBuilder builder = new SAXBuilder(); 123 124 public Namespace[] getNamespaces() 125 { 126 return namespaces; 127 } 128 129 public String getSchemaLocation() 130 { 131 return schemaLocation; 132 } 133 134 public List disseminateList(DSpaceObject dso) 135 throws CrosswalkException, 136 IOException , SQLException , AuthorizeException 137 { 138 List result = new ArrayList (1); 139 result.add(disseminateElement(dso)); 140 return result; 141 } 142 143 public Element disseminateElement(DSpaceObject dso) 144 throws CrosswalkException, 145 IOException , SQLException , AuthorizeException 146 { 147 if (dso.getType() != Constants.ITEM) 148 throw new CrosswalkObjectNotSupported("METSDisseminationCrosswalk can only crosswalk an Item."); 149 Item item = (Item)dso; 150 151 PackageDisseminator dip = (PackageDisseminator) 152 PluginManager.getNamedPlugin(PackageDisseminator.class, METS_PACKAGER_PLUGIN); 153 if (dip == null) 154 throw new CrosswalkInternalException("Cannot find a disseminate plugin for package="+METS_PACKAGER_PLUGIN); 155 156 try 157 { 158 PackageParameters pparams = new PackageParameters(); 160 pparams.put("manifestOnly", "true"); 161 162 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 164 Context context = new Context(); 165 dip.disseminate(context, item, pparams, baos); 166 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 167 168 try 169 { 170 SAXBuilder builder = new SAXBuilder(); 171 Document metsDocument = builder.build(bais); 172 return metsDocument.getRootElement(); 173 } 174 catch (JDOMException je) 175 { 176 throw new MetadataValidationException("Error parsing METS: ",je); 177 } 178 } 179 catch (PackageException pe) 180 { 181 throw new CrosswalkInternalException("Failed making METS manifest in packager: ",pe); 182 } 183 } 184 185 public boolean canDisseminate(DSpaceObject dso) 186 { 187 return true; 188 } 189 190 public boolean preferList() 191 { 192 return false; 193 } 194 } 195 | Popular Tags |