Search in sources :

Example 96 with Content

use of org.jdom2.Content in project mycore by MyCoRe-Org.

the class MCRMetsSave method updateOnFileAdd.

// TODO: should use mets-model api
/**
 * Alters the mets file
 *
 * @param mets
 *            the unmodified source
 * @param file
 *            the file to add
 * @return the modified mets or null if an exception occures
 */
private static Document updateOnFileAdd(Document mets, MCRPath file) {
    try {
        // check for file existance (if a derivate with mets.xml is uploaded
        String relPath = MCRXMLFunctions.encodeURIPath(file.getOwnerRelativePath().substring(1));
        // Check if file already exists -> if yes do nothing
        String fileExistPathString = "mets:mets/mets:fileSec/mets:fileGrp/mets:file/mets:FLocat[@xlink:href='" + relPath + "']";
        XPathExpression<Element> xpath = XPathFactory.instance().compile(fileExistPathString, Filters.element(), null, MCRConstants.METS_NAMESPACE, MCRConstants.XLINK_NAMESPACE);
        if (xpath.evaluate(mets).size() > 0) {
            String msgTemplate = "The File : '%s' already exists in mets.xml";
            LOGGER.warn(String.format(Locale.ROOT, msgTemplate, relPath));
            return null;
        } else {
            String msgTemplate = "The File : '%s' does not exists in mets.xml";
            LOGGER.warn(String.format(Locale.ROOT, msgTemplate, relPath));
        }
        // add to file section
        String contentType = MCRContentTypes.probeContentType(file);
        LOGGER.warn(MessageFormat.format("Content Type is : {0}", contentType));
        String fileGrpUSE = getFileGroupUse(file);
        String fileId = MessageFormat.format("{0}_{1}", fileGrpUSE.toLowerCase(Locale.ROOT), getFileBase(relPath));
        org.mycore.mets.model.files.File fileAsMetsFile = new org.mycore.mets.model.files.File(fileId, contentType);
        FLocat fLocat = new FLocat(LOCTYPE.URL, relPath);
        fileAsMetsFile.setFLocat(fLocat);
        Element fileSec = getFileGroup(mets, fileGrpUSE);
        fileSec.addContent(fileAsMetsFile.asElement());
        if (fileGrpUSE.equals(MCRMetsFileUse.DEFAULT.toString())) {
            updateOnImageFile(mets, fileId, relPath);
        } else {
            updateOnCustomFile(mets, fileId, relPath);
        }
    } catch (Exception ex) {
        LOGGER.error("Error occured while adding file {} to the existing mets file", file, ex);
        return null;
    }
    return mets;
}
Also used : Element(org.jdom2.Element) FLocat(org.mycore.mets.model.files.FLocat) File(org.mycore.mets.model.files.File) File(org.mycore.mets.model.files.File) URISyntaxException(java.net.URISyntaxException) JDOMException(org.jdom2.JDOMException) MCRPersistenceException(org.mycore.common.MCRPersistenceException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException)

Example 97 with Content

use of org.jdom2.Content in project mycore by MyCoRe-Org.

the class MCRMODSMetadataShareAgent method distributeMetadata.

/* (non-Javadoc)
     * @see org.mycore.datamodel.metadata.share.MCRMetadataShareAgent#inheritMetadata(org.mycore.datamodel.metadata.MCRObject)
     */
@Override
public void distributeMetadata(MCRObject holder) throws MCRPersistenceException, MCRAccessException {
    MCRMODSWrapper holderWrapper = new MCRMODSWrapper(holder);
    List<MCRMetaLinkID> children = holder.getStructure().getChildren();
    if (!children.isEmpty()) {
        LOGGER.info("Update inherited metadata");
        for (MCRMetaLinkID childIdRef : children) {
            MCRObjectID childId = childIdRef.getXLinkHrefID();
            if (MCRMODSWrapper.isSupported(childId)) {
                LOGGER.info("Update: {}", childIdRef);
                MCRObject child = MCRMetadataManager.retrieveMCRObject(childId);
                MCRMODSWrapper childWrapper = new MCRMODSWrapper(child);
                inheritToChild(holderWrapper, childWrapper);
                LOGGER.info("Saving: {}", childIdRef);
                try {
                    MCRMetadataManager.update(child);
                } catch (MCRPersistenceException | MCRAccessException e) {
                    throw new MCRPersistenceException("Error while updating inherited metadata", e);
                }
            }
        }
    }
    Collection<String> recipientIds = MCRLinkTableManager.instance().getSourceOf(holder.getId(), MCRLinkTableManager.ENTRY_TYPE_REFERENCE);
    for (String rId : recipientIds) {
        MCRObjectID recipientId = MCRObjectID.getInstance(rId);
        if (MCRMODSWrapper.isSupported(recipientId)) {
            LOGGER.info("distribute metadata to {}", rId);
            MCRObject recipient = MCRMetadataManager.retrieveMCRObject(recipientId);
            MCRMODSWrapper recipientWrapper = new MCRMODSWrapper(recipient);
            for (Element relatedItem : recipientWrapper.getLinkedRelatedItems()) {
                String holderId = relatedItem.getAttributeValue("href", MCRConstants.XLINK_NAMESPACE);
                if (holder.getId().toString().equals(holderId)) {
                    @SuppressWarnings("unchecked") Filter<Content> sharedMetadata = (Filter<Content>) Filters.element("part", MCRConstants.MODS_NAMESPACE).negate();
                    relatedItem.removeContent(sharedMetadata);
                    relatedItem.addContent(holderWrapper.getMODS().cloneContent());
                    LOGGER.info("Saving: {}", recipientId);
                    try {
                        MCRMetadataManager.update(recipient);
                    } catch (MCRPersistenceException | MCRAccessException e) {
                        throw new MCRPersistenceException("Error while updating shared metadata", e);
                    }
                }
            }
        }
    }
}
Also used : Element(org.jdom2.Element) MCRAccessException(org.mycore.access.MCRAccessException) MCRObject(org.mycore.datamodel.metadata.MCRObject) Filter(org.jdom2.filter.Filter) Content(org.jdom2.Content) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Example 98 with Content

use of org.jdom2.Content in project mycore by MyCoRe-Org.

the class MCRDFGLinkServlet method getMetsSource.

/**
 * Returns the mets document wrapped in a {@link MCRContent} object.
 *
 * @param job
 * @param useExistingMets
 * @return
 * @throws Exception
 */
private static MCRContent getMetsSource(MCRServletJob job, boolean useExistingMets, String derivate) throws Exception {
    MCRPath metsFile = MCRPath.getPath(derivate, "/mets.xml");
    try {
        job.getRequest().setAttribute("XSL.derivateID", derivate);
        job.getRequest().setAttribute("XSL.objectID", MCRLinkTableManager.instance().getSourceOf(derivate).iterator().next());
    } catch (Exception x) {
        LOGGER.warn("Unable to set \"XSL.objectID\" attribute to current request", x);
    }
    boolean metsExists = Files.exists(metsFile);
    if (metsExists && useExistingMets) {
        MCRContent content = new MCRPathContent(metsFile);
        content.setDocType("mets");
        return content;
    } else {
        Document mets = MCRMETSGeneratorFactory.create(metsFile.getParent()).generate().asDocument();
        return new MCRJDOMContent(mets);
    }
}
Also used : MCRPathContent(org.mycore.common.content.MCRPathContent) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRPath(org.mycore.datamodel.niofs.MCRPath) Document(org.jdom2.Document) MCRContent(org.mycore.common.content.MCRContent) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Example 99 with Content

use of org.jdom2.Content in project mycore by MyCoRe-Org.

the class MCRMETSServlet method getMetsSource.

/**
 * Returns the mets document wrapped in a {@link MCRContent} object.
 */
static MCRContent getMetsSource(MCRServletJob job, boolean useExistingMets, String derivate) throws Exception {
    MCRPath metsPath = MCRPath.getPath(derivate, "/mets.xml");
    try {
        job.getRequest().setAttribute("XSL.derivateID", derivate);
        String objectid = MCRLinkTableManager.instance().getSourceOf(derivate).iterator().next();
        if (objectid == null || objectid.length() == 0) {
            MCRDerivate derObj = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derivate));
            MCRObjectID ownerID = derObj.getOwnerID();
            objectid = ownerID.toString();
        }
        job.getRequest().setAttribute("XSL.objectID", objectid);
    } catch (Exception x) {
        LOGGER.warn("Unable to set \"XSL.objectID\" attribute to current request", x);
    }
    boolean metsExists = Files.exists(metsPath);
    if (metsExists && useExistingMets) {
        MCRContent content = new MCRPathContent(metsPath);
        content.setDocType("mets");
        return content;
    } else {
        Document mets = MCRMETSGeneratorFactory.create(MCRPath.getPath(derivate, "/")).generate().asDocument();
        if (!metsExists && STORE_METS_ON_GENERATE) {
            MCRMetsSave.saveMets(mets, MCRObjectID.getInstance(derivate));
        }
        return new MCRJDOMContent(mets);
    }
}
Also used : MCRPathContent(org.mycore.common.content.MCRPathContent) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath) Document(org.jdom2.Document) MCRContent(org.mycore.common.content.MCRContent) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 100 with Content

use of org.jdom2.Content in project mycore by MyCoRe-Org.

the class MCRSolrAltoExtractor method extract.

private void extract(Element root, SolrInputDocument document) {
    StringBuilder altoContent = new StringBuilder();
    for (Element stringElement : STRING_EXP.evaluate(root)) {
        String content = stringElement.getAttributeValue("CONTENT");
        String hpos = stringElement.getAttributeValue("HPOS");
        String vpos = stringElement.getAttributeValue("VPOS");
        String width = stringElement.getAttributeValue("WIDTH");
        String height = stringElement.getAttributeValue("HEIGHT");
        if (hpos == null || vpos == null || width == null || height == null) {
            continue;
        }
        String regEx = "\\.0";
        String altoWord = String.join("|", content, hpos.replaceAll(regEx, ""), vpos.replaceAll(regEx, ""), width.replaceAll(regEx, ""), height.replaceAll(regEx, ""));
        altoContent.append(content).append(' ');
        document.addField("alto_words", altoWord);
    }
    document.addField("alto_content", altoContent.toString().trim());
}
Also used : Element(org.jdom2.Element)

Aggregations

Element (org.jdom2.Element)77 Document (org.jdom2.Document)27 IOException (java.io.IOException)18 JDOMException (org.jdom2.JDOMException)16 File (java.io.File)11 MCRException (org.mycore.common.MCRException)11 Content (org.jdom2.Content)10 MCRContent (org.mycore.common.content.MCRContent)10 MCRJDOMContent (org.mycore.common.content.MCRJDOMContent)10 XMLOutputter (org.jdom2.output.XMLOutputter)9 SAXBuilder (org.jdom2.input.SAXBuilder)8 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 Attribute (org.jdom2.Attribute)6 MCRPath (org.mycore.datamodel.niofs.MCRPath)6 SAXException (org.xml.sax.SAXException)6 JsonElement (com.google.gson.JsonElement)5 Color (java.awt.Color)4 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)4 MCRObject (org.mycore.datamodel.metadata.MCRObject)4