use of org.jdom2.xpath.XPath in project mycore by MyCoRe-Org.
the class MCRPersonTransformer method buildField.
void buildField(BibtexPerson person, Element parent) {
Element modsName = buildElement(xPath, null, parent);
String lastName = normalizeValue(person.getLast());
buildElement("mods:namePart[@type='family']", lastName, modsName);
String firstName = getFirstName(person);
if (!firstName.isEmpty()) {
buildElement("mods:namePart[@type='given']", firstName, modsName);
}
String lineage = person.getLineage();
if (lineage != null) {
buildElement("mods:namePart[@type='termsOfAddress']", lineage, modsName);
}
}
use of org.jdom2.xpath.XPath 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.XPath in project mycore by MyCoRe-Org.
the class MCRURNObjectXPathMetadataManager method insertIdentifier.
@Override
public void insertIdentifier(MCRDNBURN identifier, MCRBase obj, String additional) throws MCRPersistentIdentifierException {
String xpath = getProperties().get("Xpath");
Document xml = obj.createXML();
MCRNodeBuilder nb = new MCRNodeBuilder();
try {
nb.buildElement(xpath, identifier.asString(), xml);
MCRBase object = new MCRObject(xml);
MCRMetadataManager.update(object);
} catch (Exception e) {
throw new MCRException("Error while inscribing URN to " + obj.getId(), e);
}
}
use of org.jdom2.xpath.XPath 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.XPath in project qpp-conversion-tool by CMSgov.
the class ValidationApiAcceptance method verifyDetail.
private void verifyDetail(Detail detail) {
String xPath = detail.getPath();
Filter filter = xPath.contains("@") ? Filters.attribute() : Filters.element();
try {
Object found = evaluateXpath(detail.getPath(), filter);
if (filter.equals(Filters.attribute())) {
Attribute attribute = (Attribute) found;
assertThat(attribute.getIntValue()).isEqualTo(CANNED_VALUE);
assertThat(detail.getMessage()).startsWith(ValidationServiceImpl.SV_LABEL);
} else {
assertThat(found).isNotNull();
}
} catch (XmlException | DataConversionException ex) {
fail("This xpath could not be found: " + detail.getPath(), ex);
}
}
Aggregations