📄 metsexport.java
字号:
/* * METSExport.java * * Version: $Revision: 1.12 $ * * Date: $Date: 2005/10/18 19:46:13 $ * * 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.app.mets;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.URLEncoder;import java.sql.SQLException;import java.util.Date;import java.util.Properties;import org.apache.commons.cli.CommandLine;import org.apache.commons.cli.CommandLineParser;import org.apache.commons.cli.HelpFormatter;import org.apache.commons.cli.Options;import org.apache.commons.cli.PosixParser;import org.dspace.authorize.AuthorizeException;import org.dspace.authorize.AuthorizeManager;import org.dspace.content.Bitstream;import org.dspace.content.BitstreamFormat;import org.dspace.content.Bundle;import org.dspace.content.Collection;import org.dspace.content.DCValue;import org.dspace.content.DSpaceObject;import org.dspace.content.Item;import org.dspace.content.ItemIterator;import org.dspace.core.ConfigurationManager;import org.dspace.core.Constants;import org.dspace.core.Context;import org.dspace.core.Utils;import org.dspace.handle.HandleManager;import org.dspace.app.webui.util.UIUtil;import edu.harvard.hul.ois.mets.Agent;import edu.harvard.hul.ois.mets.AmdSec;import edu.harvard.hul.ois.mets.BinData;import edu.harvard.hul.ois.mets.Checksumtype;import edu.harvard.hul.ois.mets.Div;import edu.harvard.hul.ois.mets.DmdSec;import edu.harvard.hul.ois.mets.FLocat;import edu.harvard.hul.ois.mets.FileGrp;import edu.harvard.hul.ois.mets.FileSec;import edu.harvard.hul.ois.mets.Loctype;import edu.harvard.hul.ois.mets.MdWrap;import edu.harvard.hul.ois.mets.Mdtype;import edu.harvard.hul.ois.mets.Mets;import edu.harvard.hul.ois.mets.MetsHdr;import edu.harvard.hul.ois.mets.Name;import edu.harvard.hul.ois.mets.RightsMD;import edu.harvard.hul.ois.mets.Role;import edu.harvard.hul.ois.mets.StructMap;import edu.harvard.hul.ois.mets.Type;import edu.harvard.hul.ois.mets.XmlData;import edu.harvard.hul.ois.mets.helper.Base64;import edu.harvard.hul.ois.mets.helper.MetsException;import edu.harvard.hul.ois.mets.helper.MetsValidator;import edu.harvard.hul.ois.mets.helper.MetsWriter;import edu.harvard.hul.ois.mets.helper.PCData;import edu.harvard.hul.ois.mets.helper.PreformedXML;/** * Tool for exporting DSpace AIPs with the metadata serialised in METS format * * @author Robert Tansley * @version $Revision: 1.12 $ */public class METSExport{ private static int licenseFormat = -1; private static Properties dcToMODS; public static void main(String[] args) throws Exception { Context context = new Context(); init(context); // create an options object and populate it CommandLineParser parser = new PosixParser(); Options options = new Options(); options.addOption("c", "collection", true, "Handle of collection to export"); options.addOption("i", "item", true, "Handle of item to export"); options.addOption("a", "all", false, "Export all items in the archive"); options.addOption("d", "destination", true, "Destination directory"); options.addOption("h", "help", false, "Help"); CommandLine line = parser.parse(options, args); if (line.hasOption('h')) { HelpFormatter myhelp = new HelpFormatter(); myhelp.printHelp("metsexport", options); System.out .println("\nExport a collection: metsexport -c hdl:123.456/789"); System.out .println("Export an item: metsexport -i hdl:123.456/890"); System.out.println("Export everything: metsexport -a"); System.exit(0); } String dest = ""; if (line.hasOption('d')) { dest = line.getOptionValue('d'); // Make sure it ends with a file separator if (!dest.endsWith(File.separator)) { dest = dest + File.separator; } } if (line.hasOption('i')) { String handle = getHandleArg(line.getOptionValue('i')); // Exporting a single item DSpaceObject o = HandleManager.resolveToObject(context, handle); if ((o != null) && o instanceof Item) { writeAIP(context, (Item) o, dest); System.exit(0); } else { System.err.println(line.getOptionValue('i') + " is not a valid item Handle"); System.exit(1); } } ItemIterator items = null; if (line.hasOption('c')) { String handle = getHandleArg(line.getOptionValue('c')); // Exporting a collection's worth of items DSpaceObject o = HandleManager.resolveToObject(context, handle); if ((o != null) && o instanceof Collection) { items = ((Collection) o).getItems(); } else { System.err.println(line.getOptionValue('c') + " is not a valid collection Handle"); System.exit(1); } } if (line.hasOption('a')) { items = Item.findAll(context); } if (items == null) { System.err.println("Nothing to export specified!"); System.exit(1); } while (items.hasNext()) { writeAIP(context, items.next(), dest); } context.abort(); } /** * Initialise various variables, read in config etc. * * @param context * DSpace context */ private static void init(Context context) throws SQLException, IOException { // Don't init again if initialised already if (licenseFormat != -1) { return; } // Find the License format BitstreamFormat bf = BitstreamFormat.findByShortDescription(context, "License"); licenseFormat = bf.getID(); // get path to DC->MODS map info file String configFile = ConfigurationManager.getProperty("dspace.dir") + File.separator + "config" + File.separator + "dc2mods.cfg"; // Read it in InputStream is = new FileInputStream(configFile); dcToMODS = new Properties(); dcToMODS.load(is); } /** * Write out the AIP for the given item to the given directory. A new * directory will be created with the Handle (URL-encoded) as the directory * name, and inside, a mets.xml file written, together with the bitstreams. * * @param context * DSpace context to use * @param item * Item to write * @param dest * destination directory */ public static void writeAIP(Context context, Item item, String dest) throws SQLException, IOException, AuthorizeException, MetsException { System.out.println("Exporting item hdl:" + item.getHandle()); // Create aip directory java.io.File aipDir = new java.io.File(dest + URLEncoder.encode("hdl:" + item.getHandle(), "UTF-8")); if (!aipDir.mkdir()) { // Couldn't make the directory for some reason throw new IOException("Couldn't create " + aipDir.toString()); } // Write the METS file FileOutputStream out = new FileOutputStream(aipDir.toString() + java.io.File.separator + "mets.xml"); writeMETS(context, item, out, false); out.close(); // Write bitstreams Bundle[] bundles = item.getBundles(); for (int i = 0; i < bundles.length; i++) { Bitstream[] bitstreams = bundles[i].getBitstreams(); for (int b = 0; b < bitstreams.length; b++) { // Skip license bitstream and unauthorized resources if ((bitstreams[b].getFormat().getID() != licenseFormat) && AuthorizeManager.authorizeActionBoolean(context, bitstreams[b], Constants.READ)) { out = new FileOutputStream(aipDir.toString() + java.io.File.separator + bitstreams[b].getName()); InputStream in = bitstreams[b].retrieve(); Utils.bufferedCopy(in, out); out.close(); in.close(); } } } } /** * Write METS metadata corresponding to the metadata for an item * * @param context * DSpace context * @param item * DSpace item to create METS object for * @param os * A stream to write METS package to (UTF-8 encoding will be used) * @param fullURL * if <code>true</code>, the <FLocat> values for each * bitstream will be the full URL for that bitstream. Otherwise, * only the filename itself will be used. */ public static void writeMETS(Context context, Item item, OutputStream os, boolean fullURL) throws SQLException, IOException, AuthorizeException { try { init(context); // Create the METS file Mets mets = new Mets(); // Top-level stuff mets.setOBJID("hdl:" + item.getHandle()); mets.setLABEL("DSpace Item"); mets.setSchema("mods", "http://www.loc.gov/mods/v3", "http://www.loc.gov/standards/mods/v3/mods-3-0.xsd"); // MetsHdr MetsHdr metsHdr = new MetsHdr(); metsHdr.setCREATEDATE(new Date()); // FIXME: CREATEDATE is now: // maybe should be item create
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -