⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 metsexport.java

📁 dspace 用j2ee架构的一个数字图书馆.开源程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                                               // date?            // Agent            Agent agent = new Agent();            agent.setROLE(Role.CUSTODIAN);            agent.setTYPE(Type.ORGANIZATION);            Name name = new Name();            name.getContent()                    .add(                            new PCData(ConfigurationManager                                    .getProperty("dspace.name")));            agent.getContent().add(name);            metsHdr.getContent().add(agent);            mets.getContent().add(metsHdr);            DmdSec dmdSec = new DmdSec();            dmdSec.setID("DMD_hdl_" + item.getHandle());            MdWrap mdWrap = new MdWrap();            mdWrap.setMDTYPE(Mdtype.MODS);            XmlData xmlData = new XmlData();            createMODS(item, xmlData);            mdWrap.getContent().add(xmlData);            dmdSec.getContent().add(mdWrap);            mets.getContent().add(dmdSec);            // amdSec            AmdSec amdSec = new AmdSec();            amdSec.setID("TMD_hdl_" + item.getHandle());            // FIXME: techMD here            // License as <rightsMD><mdWrap><binData>base64encoded</binData>...            InputStream licenseStream = findLicense(context, item);            if (licenseStream != null)            {                RightsMD rightsMD = new RightsMD();                MdWrap rightsMDWrap = new MdWrap();                rightsMDWrap.setMIMETYPE("text/plain");                rightsMDWrap.setMDTYPE(Mdtype.OTHER);                rightsMDWrap.setOTHERMDTYPE("TEXT");                BinData binData = new BinData();                Base64 base64 = new Base64(licenseStream);                binData.getContent().add(base64);                rightsMDWrap.getContent().add(binData);                rightsMD.getContent().add(rightsMDWrap);                amdSec.getContent().add(rightsMD);            }            // FIXME: History data???? Nooooo!!!!            mets.getContent().add(amdSec);            // fileSec            FileSec fileSec = new FileSec();            boolean fileSecEmpty = true;            Bundle[] bundles = item.getBundles();            for (int i = 0; i < bundles.length; i++)            {                Bitstream[] bitstreams = bundles[i].getBitstreams();                // First: we skip the license bundle, since it's included                // elsewhere                if (bitstreams[0].getFormat().getID() == licenseFormat)                {                    continue;                }                // Create a fileGrp                FileGrp fileGrp = new FileGrp();                // Bundle name for USE attribute                if ((bundles[i].getName() != null)                        && !bundles[i].getName().equals(""))                {                    fileGrp.setUSE(bundles[i].getName());                }                for (int bits = 0; bits < bitstreams.length; bits++)                {                    // What's the persistent(-ish) ID?                    String bitstreamPID = ConfigurationManager                            .getProperty("dspace.url")                            + "/bitstream/"                            + item.getHandle()                            + "/"                            + bitstreams[bits].getSequenceID()                            + "/"                            + UIUtil.encodeBitstreamName(bitstreams[bits].getName(),                                    "UTF-8");                    edu.harvard.hul.ois.mets.File file = new edu.harvard.hul.ois.mets.File();                    /*                     * ID: we use the unique part of the persistent ID, i.e. the                     * Handle + sequence number, but with _'s instead of /'s so                     * it's a legal xsd:ID.                     */                    String xmlIDstart = item.getHandle().replaceAll("/", "_")                            + "_";                    file.setID(xmlIDstart + bitstreams[bits].getSequenceID());                    String groupID = "GROUP_" + xmlIDstart                            + bitstreams[bits].getSequenceID();                    /*                     * If we're in THUMBNAIL or TEXT bundles, the bitstream is                     * extracted text or a thumbnail, so we use the name to work                     * out which bitstream to be in the same group as                     */                    if ((bundles[i].getName() != null)                            && (bundles[i].getName().equals("THUMBNAIL") || bundles[i]                                    .getName().equals("TEXT")))                    {                        // Try and find the original bitstream, and chuck the                        // derived                        // bitstream in the same group                        Bitstream original = findOriginalBitstream(item,                                bitstreams[bits]);                        if (original != null)                        {                            groupID = "GROUP_" + xmlIDstart                                    + original.getSequenceID();                        }                    }                    file.setGROUPID(groupID);                    file.setOWNERID(bitstreamPID);                    // FIXME: ADMID should point to appropriate TechMD section                    // above                    file                            .setMIMETYPE(bitstreams[bits].getFormat()                                    .getMIMEType());                    // FIXME: CREATED: no date                    file.setSIZE(bitstreams[bits].getSize());                    file.setCHECKSUM(bitstreams[bits].getChecksum());                    file.setCHECKSUMTYPE(Checksumtype.MD5);                    // FLocat: filename is as in records, or full URL                    // FIXME: Duplicate filenames and characters illegal to                    // local OS may cause problems                    FLocat flocat = new FLocat();                    flocat.setLOCTYPE(Loctype.URL);                    if (fullURL)                    {                        flocat.setXlinkHref(bitstreamPID);                    }                    else                    {                        flocat.setXlinkHref(bitstreams[bits].getName());                    }                    // Add FLocat to File, and File to FileGrp                    file.getContent().add(flocat);                    fileGrp.getContent().add(file);                }                // Add fileGrp to fileSec                fileSec.getContent().add(fileGrp);                fileSecEmpty = false;            }            // Add fileSec to document            if (!fileSecEmpty)            {                mets.getContent().add(fileSec);            }                        // FIXME: Add Structmap here, but it is empty and we won't use it now.            StructMap structMap = new StructMap();            Div div = new Div();            structMap.getContent().add(div);            mets.getContent().add(structMap);                        mets.validate(new MetsValidator());            mets.write(new MetsWriter(os));        }        catch (MetsException e)        {            // We don't pass up a MetsException, so callers don't need to            // know the details of the METS toolkit            e.printStackTrace();            throw new IOException(e.getMessage());        }    }    /**     * Utility to find the license bitstream from an item     *      * @param context     *            DSpace context     * @param item     *            the item     * @return the license as a string     *      * @throws IOException     *             if the license bitstream can't be read     */    private static InputStream findLicense(Context context, Item item)            throws SQLException, IOException, AuthorizeException    {        Bundle[] bundles = item.getBundles();        for (int i = 0; i < bundles.length; i++)        {            // Assume license will be in its own bundle            Bitstream[] bitstreams = bundles[i].getBitstreams();            if (bitstreams[0].getFormat().getID() == licenseFormat)            {                // Read the license into a string                return bitstreams[0].retrieve();            }        }        // Oops! No license!        return null;    }    /**     * For a bitstream that's a thumbnail or extracted text, find the     * corresponding bitstream in the ORIGINAL bundle     *      * @param item     *            the item we're dealing with     * @param derived     *            the derived bitstream     *      * @return the corresponding original bitstream (or null)     */    private static Bitstream findOriginalBitstream(Item item, Bitstream derived)    				throws SQLException    {        Bundle[] bundles = item.getBundles();        // Filename of original will be filename of the derived bitstream        // minus the extension (last 4 chars - .jpg or .txt)        String originalFilename = derived.getName().substring(0,                derived.getName().length() - 4);        // First find "original" bundle        for (int i = 0; i < bundles.length; i++)        {            if ((bundles[i].getName() != null)                    && bundles[i].getName().equals("ORIGINAL"))            {                // Now find the corresponding bitstream                Bitstream[] bitstreams = bundles[i].getBitstreams();                for (int bsnum = 0; bsnum < bitstreams.length; bsnum++)                {                    if (bitstreams[bsnum].getName().equals(originalFilename))                    {                        return bitstreams[bsnum];                    }                }            }        }        // Didn't find it        return null;    }    /**     * Create MODS metadata from the DC in the item, and add to the given     * XmlData METS object.     *      * @param item     *            the item     * @param xmlData     *            xmlData to add MODS to.     */    private static void createMODS(Item item, XmlData xmlData)    {        DCValue[] dc = item.getDC(Item.ANY, Item.ANY, Item.ANY);        StringBuffer modsXML = new StringBuffer();        for (int i = 0; i < dc.length; i++)        {            // Get the property name - element[.qualifier]            String propName = ((dc[i].qualifier == null) ? dc[i].element                    : (dc[i].element + "." + dc[i].qualifier));            String modsMapping = dcToMODS.getProperty(propName);            if (modsMapping == null)            {                System.err.println("WARNING: No MODS mapping for " + propName);            }            else            {                // Replace '%s' with DC value (with entities encoded)                modsXML.append(modsMapping.replaceAll("%s", Utils                        .addEntities(dc[i].value)));                modsXML.append("\n"); // For readability            }        }        PreformedXML pXML = new PreformedXML(modsXML.toString());        xmlData.getContent().add(pXML);    }    /**     * Get the handle from the command line in the form 123.456/789. Doesn't     * matter if incoming handle has 'hdl:' or 'http://hdl....' before it.     *      * @param original     *            Handle as passed in by user     * @return Handle as can be looked up in our table     */    private static String getHandleArg(String original)    {        if (original.startsWith("hdl:"))        {            return original.substring(4);        }        if (original.startsWith("http://hdl.handle.net/"))        {            return original.substring(22);        }        return original;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -