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());
}
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;
}
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;
}
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());
}
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;
}
Aggregations