Search in sources :

Example 36 with XPathExpression

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

the class MCRTransferPackageUtil method getMCRObjects.

/**
 * Gets the list of mycore object identifiers from the given directory.
 *
 * @param targetDirectory directory where the *.tar was unpacked
 * @return list of object which lies within the directory
 */
public static List<String> getMCRObjects(Path targetDirectory) throws JDOMException, IOException {
    Path order = targetDirectory.resolve(MCRTransferPackage.IMPORT_CONFIG_FILENAME);
    Document xml = new SAXBuilder().build(order.toFile());
    Element config = xml.getRootElement();
    XPathExpression<Text> exp = MCRConstants.XPATH_FACTORY.compile("order/object/text()", Filters.text());
    return exp.evaluate(config).stream().map(Text::getText).collect(Collectors.toList());
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) SAXBuilder(org.jdom2.input.SAXBuilder) Element(org.jdom2.Element) Text(org.jdom2.Text) Document(org.jdom2.Document)

Example 37 with XPathExpression

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

the class MCRMetsSave method getFileGroup.

private static Element getFileGroup(Document mets, String fileGrpUSE) {
    // alter the mets document
    XPathExpression<Element> xpath;
    String fileGroupXPathString = String.format(Locale.ROOT, "mets:mets/mets:fileSec/mets:fileGrp[@USE='%s']", fileGrpUSE);
    xpath = XPathFactory.instance().compile(fileGroupXPathString, Filters.element(), null, MCRConstants.METS_NAMESPACE);
    Element element = xpath.evaluateFirst(mets);
    if (element == null) {
        // section does not exist
        Element fileGroupElement = new FileGrp(fileGrpUSE).asElement();
        String fileSectionPath = "mets:mets/mets:fileSec";
        xpath = XPathFactory.instance().compile(fileSectionPath, Filters.element(), null, MCRConstants.METS_NAMESPACE);
        Element fileSectionElement = xpath.evaluateFirst(mets);
        if (fileSectionElement == null) {
            throw new MCRPersistenceException("There is no fileSection in mets.xml!");
        }
        fileSectionElement.addContent(fileGroupElement);
        element = fileGroupElement;
    }
    return element;
}
Also used : Element(org.jdom2.Element) FileGrp(org.mycore.mets.model.files.FileGrp) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Example 38 with XPathExpression

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

the class MCRMetsSave method searchFileInGroup.

/**
 * Searches a file in a group, which matches a filename.
 *
 * @param mets the mets file to search
 * @param path the path to the alto file (e.g. "alto/alto_file.xml" when searching in DEFAULT_FILE_GROUP_USE or
 *             "image_file.jpg" when searchin in ALTO_FILE_GROUP_USE)
 * @param searchFileGroup
 * @return the id of the matching file or null if there is no matching file
 */
private static String searchFileInGroup(Document mets, String path, String searchFileGroup) {
    // first check all files in default file group
    XPathExpression<Element> xpath;
    String relatedFileExistPathString = String.format(Locale.ROOT, "mets:mets/mets:fileSec/mets:fileGrp[@USE='%s']/mets:file/mets:FLocat", searchFileGroup);
    xpath = XPathFactory.instance().compile(relatedFileExistPathString, Filters.element(), null, MCRConstants.METS_NAMESPACE, MCRConstants.XLINK_NAMESPACE);
    List<Element> fileLocList = xpath.evaluate(mets);
    String matchId = null;
    // iterate over all files
    path = getCleanPath(path);
    for (Element fileLoc : fileLocList) {
        Attribute hrefAttribute = fileLoc.getAttribute("href", MCRConstants.XLINK_NAMESPACE);
        String hrefAttributeValue = hrefAttribute.getValue();
        String hrefPath = getCleanPath(removeExtension(hrefAttributeValue));
        if (hrefPath.equals(removeExtension(path))) {
            matchId = ((Element) fileLoc.getParent()).getAttributeValue("ID");
            break;
        }
    }
    return matchId;
}
Also used : Attribute(org.jdom2.Attribute) Element(org.jdom2.Element)

Example 39 with XPathExpression

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

the class MCRMetsSave method getDefaultSmLink.

/**
 * Build the default smLink. The PhysicalSubDiv is simply linked to the root chapter of the mets document.
 *
 * @param mets the mets document
 * @param div  the PhysicalSubDiv which should be linked
 * @return the default smLink
 */
private static SmLink getDefaultSmLink(Document mets, PhysicalSubDiv div) {
    XPathExpression<Attribute> attributeXpath;
    attributeXpath = XPathFactory.instance().compile("mets:mets/mets:structMap[@TYPE='LOGICAL']/mets:div/@ID", Filters.attribute(), null, MCRConstants.METS_NAMESPACE);
    Attribute idAttribute = attributeXpath.evaluateFirst(mets);
    String rootID = idAttribute.getValue();
    return new SmLink(rootID, div.getId());
}
Also used : SmLink(org.mycore.mets.model.struct.SmLink) Attribute(org.jdom2.Attribute)

Example 40 with XPathExpression

use of org.jdom2.xpath.XPathExpression 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)

Aggregations

Element (org.jdom2.Element)43 Document (org.jdom2.Document)23 XPathExpression (org.jdom2.xpath.XPathExpression)13 IOException (java.io.IOException)9 XPathFactory (org.jdom2.xpath.XPathFactory)9 SAXBuilder (org.jdom2.input.SAXBuilder)7 Attribute (org.jdom2.Attribute)6 JDOMException (org.jdom2.JDOMException)5 Test (org.junit.Test)5 Text (org.jdom2.Text)4 XMLOutputter (org.jdom2.output.XMLOutputter)4 MCRObject (org.mycore.datamodel.metadata.MCRObject)4 MCRPath (org.mycore.datamodel.niofs.MCRPath)4 URISyntaxException (java.net.URISyntaxException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 HashMap (java.util.HashMap)2 MCRPersistenceException (org.mycore.common.MCRPersistenceException)2 MCRNodeBuilder (org.mycore.common.xml.MCRNodeBuilder)2 MCRCategory (org.mycore.datamodel.classifications2.MCRCategory)2