📄 metsmanifest.java
字号:
/* * METSManifest.java * * Version: $Revision: 1.1 $ * * Date: $Date: 2006/03/17 00:04:38 $ * * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts * Institute of Technology. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of the Hewlett-Packard Company nor the name of the * Massachusetts Institute of Technology nor the names of their * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. */package org.dspace.content.packager;import java.io.ByteArrayInputStream;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.sql.SQLException;import java.util.ArrayList;import java.util.Enumeration;import java.util.Iterator;import java.util.List;import org.apache.commons.codec.binary.Base64;import org.apache.log4j.Logger;import org.dspace.authorize.AuthorizeException;import org.dspace.content.Bitstream;import org.dspace.content.DSpaceObject;import org.dspace.content.Item;import org.dspace.content.crosswalk.CrosswalkException;import org.dspace.content.crosswalk.CrosswalkObjectNotSupported;import org.dspace.content.crosswalk.MetadataValidationException;import org.dspace.content.crosswalk.IngestionCrosswalk;import org.dspace.core.ConfigurationManager;import org.dspace.core.Constants;import org.dspace.core.Context;import org.dspace.core.PluginManager;import org.jdom.Document;import org.jdom.Element;import org.jdom.JDOMException;import org.jdom.Namespace;import org.jdom.input.SAXBuilder;import org.jdom.output.Format;import org.jdom.output.XMLOutputter;import org.jdom.xpath.XPath;/** * <P> * Manage the METS manifest document for METS importer classes, * such as the package importer <code>org.dspace.content.packager.MetsSubmission</code> * and the federated importer <code>org.dspace.app.mets.FederatedMETSImport</code> * </P> * <P> * It can parse the METS document, build an internal model, and give the importers * access to that model. It also crosswalks * all of the descriptive and administrative metadata in the METS * manifest into the target DSpace Item, under control of the importer. * </P> * * <P> * It reads the following DSpace Configuration entries: * </P> * <UL> * <LI>Local XML schema (XSD) declarations, in the general format: * <br><code>mets.xsd.<em>identifier</em> = <em>namespace</em> <em>xsd-URL</em></code> * <br> eg. <code>mets.xsd.dc = http://purl.org/dc/elements/1.1/ dc.xsd</code> * <br>Add a separate config entry for each schema. * </LI> * <p><LI>Crosswalk plugin mappings: * These tell it the name of the crosswalk plugin to invoke for metadata sections * with a particular value of <code>MDTYPE</code> (or <code>OTHERMDTYPE</code>) * By default, the crosswalk mechanism will look for a plugin with the * same name as the metadata type (e.g. <code>"MODS"</code>, * <code>"DC"</code>). This example line invokes the <code>QDC</code> * plugin when <code>MDTYPE="DC"</code> * <br><code>mets.submission.crosswalk.DC = QDC </code> * <br> general format is: * <br><code>mets.submission.crosswalk.<em>mdType</em> = <em>pluginName</em> </code> * </LI> * </UL> * * * @author Robert Tansley * @author WeiHua Huang * @author Rita Lee * @author Larry Stone * @see org.dspace.content.packager.MetsSubmission * @see org.dspace.app.mets.FederatedMETSImport */public class METSManifest{ /** * Callback interface to retrieve data streams in mdRef elements. * "Package" or file reader returns an input stream for the * given relative path, e.g. to dereference <code>mdRef</code> elements. */ public interface Mdref { /** * Make the contents of an external resource mentioned in * an <code>mdRef</code> element available as an <code>InputStream</code>. * The implementation must use the information in the * <code>mdRef</code> element, and the state in the object that * implements this interface, to find the actual metadata content. * <p> * For example, an implementation that ingests a directory of * files on the local filesystem would get a relative pathname * out of the <code>mdRef</code> and open that file. * * @param mdRef JDOM element of mdRef in the METS manifest. * @return stream containing the metadata mentioned in mdRef. * @throw MetadataValidationException if the mdRef is unacceptable or missing required information. * @throw IOException if it is returned by services called by this method. * @throw SQLException if it is returned by services called by this method. * @throw AuthorizeException if it is returned by services called by this method. */ public InputStream getInputStream(Element mdRef) throws MetadataValidationException, IOException, SQLException, AuthorizeException; } /** log4j category */ private static Logger log = Logger.getLogger(METSManifest.class); /** Canonical filename of METS manifest within a package or as a bitstream. */ public final static String MANIFEST_FILE = "mets.xml"; /** Prefix of DSpace configuration lines that map METS metadata type to * crosswalk plugin names. */ private final static String CONFIG_METADATA_PREFIX = "mets.submission.crosswalk."; /** prefix of config lines identifying local XML Schema (XSD) files */ private final static String CONFIG_XSD_PREFIX = "mets.xsd."; /** Dublin core element namespace */ private static Namespace dcNS = Namespace .getNamespace("http://purl.org/dc/elements/1.1/"); /** Dublin core term namespace (for qualified DC) */ private static Namespace dcTermNS = Namespace .getNamespace("http://purl.org/dc/terms/"); /** METS namespace -- includes "mets" prefix for use in XPaths */ public static Namespace metsNS = Namespace .getNamespace("mets", "http://www.loc.gov/METS/"); /** XLink namespace -- includes "xlink" prefix prefix for use in XPaths */ private static Namespace xlinkNS = Namespace .getNamespace("xlink", "http://www.w3.org/1999/xlink"); /** root element of the current METS manifest. */ private Element mets = null; /** all mdRef elements in the manifest */ private List mdFiles = null; /** <file> elements in "original" filegroup (bundle) */ private List contentFiles = null; /** builder to use for mdRef streams, inherited from create() */ private SAXBuilder parser = null; // Create list of local schemas at load time, since it depends only // on the DSpace configuration. private static String localSchemas; static { String dspace_dir = ConfigurationManager.getProperty("dspace.dir"); File xsdPath1 = new File(dspace_dir+"/config/schemas/"); File xsdPath2 = new File(dspace_dir+"/config/"); Enumeration pe = ConfigurationManager.propertyNames(); StringBuffer result = new StringBuffer(); while (pe.hasMoreElements()) { // config lines have the format: // mets.xsd.{identifier} = {namespace} {xsd-URL} // e.g. // mets.xsd.dc = http://purl.org/dc/elements/1.1/ dc.xsd // (filename is relative to {dspace_dir}/config/schemas/) String key = (String)pe.nextElement(); if (key.startsWith(CONFIG_XSD_PREFIX)) { String spec = ConfigurationManager.getProperty(key); String val[] = spec.trim().split("\\s+"); if (val.length == 2) { File xsd = new File(xsdPath1, val[1]); if (!xsd.exists()) xsd = new File(xsdPath2, val[1]); if (!xsd.exists()) log.warn("Schema file not found for config entry=\""+spec+"\""); else { try { String u = xsd.toURL().toString(); if (result.length() > 0) result.append(" "); result.append(val[0]).append(" ").append(u); } catch (java.net.MalformedURLException e) { log.warn("Skipping badly formed XSD URL: "+e.toString()); } } } else log.warn("Schema config entry has wrong format, entry=\""+spec+"\""); } } localSchemas = result.toString(); log.debug("Got local schemas = \""+localSchemas+"\""); } /** * Default constructor, only called internally. * @param builder XML parser (for parsing mdRef'd files and binData) * @param mets parsed METS document */ private METSManifest(SAXBuilder builder, Element mets) { super(); this.mets = mets; parser = builder; } /** * Create a new manifest object from a serialized METS XML document. * Parse document read from the input stream, optionally validating. * @param is input stream containing serialized XML * @param validate if true, enable XML validation using schemas * in document. Also validates any sub-documents. * @throws MetadataValidationException if there is any error parsing * or validating the METS. * @return new METSManifest object. */ public static METSManifest create(InputStream is, boolean validate) throws IOException, MetadataValidationException { SAXBuilder builder = new SAXBuilder(validate); // Set validation feature if (validate) builder.setFeature("http://apache.org/xml/features/validation/schema", true); // Tell the parser where local copies of schemas are, to speed up // validation. Local XSDs are identified in the configuration file. if (localSchemas.length() > 0) builder.setProperty( "http://apache.org/xml/properties/schema/external-schemaLocation", localSchemas); // Parse the METS file Document metsDocument; try { metsDocument = builder.build(is); // XXX for temporary debugging
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -