use of org.jdom2.xpath.XPathExpression in project mycore by MyCoRe-Org.
the class MCRMoveToRelatedItemIfExists method getRelatedItemIfExists.
Element getRelatedItemIfExists(Element parent) {
XPathExpression<Element> xPath = XPathFactory.instance().compile(xPathOfRelatedItem, Filters.element(), null, MCRConstants.getStandardNamespaces());
Element fixedParent = xPath.evaluateFirst(parent);
return fixedParent != null ? fixedParent : parent;
}
use of org.jdom2.xpath.XPathExpression in project mycore by MyCoRe-Org.
the class MCRXMLFunctions method getTreeByPath.
/**
* The method return a org.w3c.dom.NodeList as subpath of the doc input
* NodeList selected by a path as String.
*
* @param doc
* the input org.w3c.dom.Nodelist
* @param path
* the path of doc as String
* @return a subpath of doc selected by path as org.w3c.dom.NodeList
*/
public static NodeList getTreeByPath(NodeList doc, String path) {
NodeList n = null;
DocumentBuilder documentBuilder = MCRDOMUtils.getDocumentBuilderUnchecked();
try {
// build path selection
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile(path);
// select part
Document document = documentBuilder.newDocument();
if (doc.item(0).getNodeName().equals("#document")) {
// LOGGER.debug("NodeList is a document.");
Node child = doc.item(0).getFirstChild();
if (child != null) {
Node node = doc.item(0).getFirstChild();
Node imp = document.importNode(node, true);
document.appendChild(imp);
} else {
document.appendChild(doc.item(0));
}
}
n = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
} catch (Exception e) {
LOGGER.error("Error while getting tree by path {}", path, e);
} finally {
MCRDOMUtils.releaseDocumentBuilder(documentBuilder);
}
return n;
}
use of org.jdom2.xpath.XPathExpression in project mycore by MyCoRe-Org.
the class MCRRestAPIObjectsHelper method createXMLForSubdirectories.
private static void createXMLForSubdirectories(MCRPath mcrPath, Element currentElement, int currentDepth, int maxDepth) {
if (currentDepth < maxDepth) {
XPathExpression<Element> xp = XPathFactory.instance().compile("./children/child[@type='directory']", Filters.element());
for (Element e : xp.evaluate(currentElement)) {
String name = e.getChildTextNormalize("name");
try {
MCRPath pChild = (MCRPath) mcrPath.resolve(name);
Document doc = MCRPathXML.getDirectoryXML(pChild);
Element eChildren = doc.getRootElement().getChild("children");
if (eChildren != null) {
e.addContent(eChildren.detach());
createXMLForSubdirectories(pChild, e, currentDepth + 1, maxDepth);
}
} catch (IOException ex) {
// ignore
}
}
}
}
use of org.jdom2.xpath.XPathExpression in project mycore by MyCoRe-Org.
the class MCRURNObjectXPathMetadataManager method getIdentifier.
@Override
public Optional<MCRPersistentIdentifier> getIdentifier(MCRBase obj, String additional) throws MCRPersistentIdentifierException {
String xpath = getProperties().get("Xpath");
Document xml = obj.createXML();
XPathFactory xpfac = XPathFactory.instance();
XPathExpression<Text> xp = xpfac.compile(xpath, Filters.text());
List<Text> evaluate = xp.evaluate(xml);
if (evaluate.size() > 1) {
throw new MCRPersistentIdentifierException("Got " + evaluate.size() + " matches for " + obj.getId() + " with xpath " + xpath + "");
}
if (evaluate.size() == 0) {
return Optional.empty();
}
Text identifierText = evaluate.listIterator().next();
String identifierString = identifierText.getTextNormalize();
Optional<MCRDNBURN> parsedIdentifierOptional = PARSER.parse(identifierString);
return parsedIdentifierOptional.map(MCRPersistentIdentifier.class::cast);
}
use of org.jdom2.xpath.XPathExpression in project mycore by MyCoRe-Org.
the class MCRURNResolver method resolve.
@Override
public Stream<String> resolve(MCRDNBURN identifier) throws MCRIdentifierUnresolvableException {
Document pidefDocument = MCRDNBPIDefProvider.get(identifier);
XPathExpression<Element> compile = XPathFactory.instance().compile(".//pidef:resolving_information/pidef:url_info/pidef:url", Filters.element(), null, MCRConstants.PIDEF_NAMESPACE);
return compile.evaluate(pidefDocument).stream().map(Element::getTextTrim);
}
Aggregations