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

📄 metsmanifest.java

📁 dspace 用j2ee架构的一个数字图书馆.开源程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                {                    return xmlData.getChildren();                }            }            else if ((mdRef = mdSec.getChild("mdRef", metsNS)) != null)            {                String mimeType = mdRef.getAttributeValue("MIMETYPE");                if (mimeType != null && mimeType.equalsIgnoreCase("text/xml"))                {                    Document mdd = parser.build(callback.getInputStream(mdRef));                    List result = new ArrayList(1);                    result.add(mdd.getRootElement());                    return result;                }                else                {                    log.warn("Ignoring mdRef section because MIMETYPE is not XML, but: "+mimeType);                    return new ArrayList(0);                }            }            else                throw new MetadataValidationException("Invalid METS Manifest: ?mdSec element with neither mdRef nor mdWrap child.");        }        catch (JDOMException je)        {            throw new MetadataValidationException("Error parsing or validating metadata section in mdRef or binData within "+mdSec.toString(), je);        }    }    /**     * Return contents of *md element as stream.     * Gets content, dereferecing mdRef if necessary, or decoding     * a binData element if necessary.     * @return Stream containing contents of metadata section.  Never returns null.     * @throws MetadataValidationException if METS format does not contain any metadata.     */    public InputStream getMdContentAsStream(Element mdSec, Mdref callback)        throws MetadataValidationException, IOException, SQLException, AuthorizeException    {        Element mdRef = null;        Element mdWrap = mdSec.getChild("mdWrap", metsNS);        if (mdWrap != null)        {            Element xmlData = mdWrap.getChild("xmlData", metsNS);            if (xmlData == null)            {                Element bin = mdWrap.getChild("binData", metsNS);                if (bin == null)                    throw new MetadataValidationException("Invalid METS Manifest: mdWrap element with neither xmlData nor binData child.");                else                {                    byte value[] = Base64.decodeBase64(bin.getText().getBytes());                    return new ByteArrayInputStream(value);                }            }            else            {                XMLOutputter outputPretty = new XMLOutputter(Format.getPrettyFormat());                return new ByteArrayInputStream(                        outputPretty.outputString(xmlData.getChildren()).getBytes());            }        }        else if ((mdRef = mdSec.getChild("mdRef", metsNS)) != null)        {            return callback.getInputStream(mdRef);        }        else            throw new MetadataValidationException("Invalid METS Manifest: ?mdSec element with neither mdRef nor mdWrap child.");    }    // special call to crosswalk the guts of a metadata *Sec (dmdSec, amdSec)    // because mdRef and mdWrap have to be handled differently.    // It's a lot like getMdContentAsXml but cannot use that because xwalk    // should be called with root element OR list depending on what was given.    private void crosswalkMdContent(Element mdSec, Mdref callback,                IngestionCrosswalk xwalk, Context context, DSpaceObject dso)        throws CrosswalkException, IOException, SQLException, AuthorizeException    {        List xml = getMdContentAsXml(mdSec,callback);        // if we get inappropriate metadata, e.g. PREMIS for Item, let it go.        try        {            xwalk.ingest(context, dso, xml);        }        catch (CrosswalkObjectNotSupported e)        {            log.warn("Skipping metadata for inappropriate type of object: Object="+dso.toString()+", error="+e.toString());        }    }    // return first <div> of first <structMap>;    // in DSpace profile, this is where item-wide dmd and other metadata    // lives as IDrefs.    private Element getFirstDiv()        throws MetadataValidationException    {        Element sm = mets.getChild("structMap", metsNS);        if (sm == null)            throw new MetadataValidationException("METS document is missing the required structMap element.");        Element result = sm.getChild("div", metsNS);        if (result == null)            throw new MetadataValidationException("METS document is missing the required first div element in first structMap.");        log.debug("Got firstDiv result="+result.toString());        return (Element)result;    }    // return a single Element node found by one-off path.    // use only when path varies each time you call it.    private Element getElementByXPath(String path, boolean nullOk)        throws MetadataValidationException    {        try        {            XPath xpath = XPath.newInstance(path);            xpath.addNamespace(metsNS);            xpath.addNamespace(xlinkNS);            Object result = xpath.selectSingleNode(mets);            if (result == null && nullOk)                return null;            else if (result instanceof Element)                return (Element)result;            else                throw new MetadataValidationException("METSManifest: Failed to resolve XPath, path=\""+path+"\"");        }        catch (JDOMException je)        {            throw new MetadataValidationException("METSManifest: Failed to resolve XPath, path=\""+path+"\"", je);        }    }    // Find crosswalk for the indicated metadata type (e.g. "DC", "MODS")    // The crosswalk plugin name MAY be indirected in config file,    // through an entry like    //  mets.submission.crosswalk.{mdType} = {pluginName}    //   e.g.    //  mets.submission.crosswalk.DC = mysite-QDC    private IngestionCrosswalk getCrosswalk(String type)    {        String xwalkName = ConfigurationManager.getProperty(CONFIG_METADATA_PREFIX + type);        if (xwalkName == null)            xwalkName = type;        return (IngestionCrosswalk)          PluginManager.getNamedPlugin(IngestionCrosswalk.class, xwalkName);    }    /**     * Gets all dmdSec elements containing metadata for the DSpace Item.     *     * @return array of Elements, each a dmdSec.  May be empty but NOT null.     * @throws MetadataValidationException if the METS is missing a reference to item-wide     *          DMDs in the correct place.     */    public Element[] getItemDmds()        throws MetadataValidationException    {        // div@DMDID is actually IDREFS, a space-separated list of IDs:        Element firstDiv = getFirstDiv();        String dmds = firstDiv.getAttributeValue("DMDID");        if (dmds == null)            throw new MetadataValidationException("Invalid METS: Missing reference to Item descriptive metadata, first div on first structmap must have a DMDID attribute.");        String dmdID[] = dmds.split("\\s+");        Element result[] = new Element[dmdID.length];        for (int i = 0; i < dmdID.length; ++i)            result[i] = getElementByXPath("mets:dmdSec[@ID=\""+dmdID[i]+"\"]", false);        return result;    }    /**     * Return rights metadata section(s) relevant to item as a whole.     * @return array of rightsMd elements, possibly empty but never null.     * @throws MetadataValidationException if METS is invalid, e.g. referenced amdSec is missing.     */    public Element[] getItemRightsMD()        throws MetadataValidationException    {        // div@ADMID is actually IDREFS, a space-separated list of IDs:        Element firstDiv = getFirstDiv();        String amds = firstDiv.getAttributeValue("ADMID");        if (amds == null)        {            log.debug("getItemRightsMD: No ADMID references found.");            return new Element[0];        }        String amdID[] = amds.split("\\s+");        List resultList = new ArrayList();        for (int i = 0; i < amdID.length; ++i)        {            List rmds = getElementByXPath("mets:amdSec[@ID=\""+amdID[i]+"\"]", false).                            getChildren("rightsMD", metsNS);            if (rmds.size() > 0)                resultList.addAll(rmds);        }        return (Element[])resultList.toArray(new Element[resultList.size()]);    }    /**     * Invokes appropriate crosswalks on Item-wide descriptive metadata.     */    public void crosswalkItem(Context context, Item item, Element dmd, Mdref callback)        throws MetadataValidationException,               CrosswalkException, IOException, SQLException, AuthorizeException    {        String type = getMdType(dmd);        IngestionCrosswalk xwalk = getCrosswalk(type);        if (xwalk == null)            throw new MetadataValidationException("Cannot process METS Manifest: "+                "No crosswalk found for MDTYPE="+type);        crosswalkMdContent(dmd, callback, xwalk, context, item);    }    /**     * Crosswalk the metadata associated with a particular <code>file</code>     * element into the bitstream it corresponds to.     * @param context a dspace context.     * @param bs bitstream target of the crosswalk     * @param fileId value of ID attribute in the file element responsible     *  for the contents of that bitstream.     */    public void crosswalkBitstream(Context context, Bitstream bitstream,                                   String fileId, Mdref callback)        throws MetadataValidationException,               CrosswalkException, IOException, SQLException, AuthorizeException    {        Element file = getElementByXPath("descendant::mets:file[@ID=\""+fileId+"\"]", false);        if (file == null)            throw new MetadataValidationException("Failed in Bitstream crosswalk, Could not find file element with ID="+fileId);        // In DSpace METS SIP spec, admin metadata is only "highly        // recommended", not "required", so it is OK if there is no ADMID.        String amds = file.getAttributeValue("ADMID");        if (amds == null)        {            log.warn("Got no bitstream ADMID, file@ID="+fileId);            return;        }        String amdID[] = amds.split("\\s+");        for (int i = 0; i < amdID.length; ++i)        {            List techMDs = getElementByXPath("mets:amdSec[@ID=\""+amdID[i]+"\"]", false).                                 getChildren("techMD", metsNS);            Iterator ti = techMDs.iterator();            while (ti.hasNext())            {                Element techMD = (Element)ti.next();                if (techMD != null)                {                    String type = getMdType(techMD);                    IngestionCrosswalk xwalk = getCrosswalk(type);                    log.debug("Got bitstream techMD of type="+type+", for file ID="+fileId);                                         if (xwalk == null)                        throw new MetadataValidationException("Cannot process METS Manifest: "+                            "No crosswalk found for techMD MDTYPE="+type);                    crosswalkMdContent(techMD, callback, xwalk, context, bitstream);                }            }        }    }    /**     * Find Handle (if any) identifier labelling this manifest.     * @return handle (never null)     * @throws MetadataValidationException if no handle available.     */    public String getHandle()        throws MetadataValidationException    {        // TODO: XXX Make configurable? Handle optionally passed in?        // FIXME: Not sure if OBJID is really the right place        String handle = mets.getAttributeValue("OBJID");        if (handle != null && handle.startsWith("hdl:"))        {            return handle.substring(4);        }        else        {            throw new MetadataValidationException("Item has no valid Handle (OBJID)");        }    }}

⌨️ 快捷键说明

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