Search in sources :

Example 26 with XPath

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);
    }
}
Also used : Element(org.jdom2.Element)

Example 27 with XPath

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;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) URISyntaxException(java.net.URISyntaxException) JDOMException(org.jdom2.JDOMException) ParseException(java.text.ParseException) CancellationException(java.util.concurrent.CancellationException) CompletionException(java.util.concurrent.CompletionException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TransformerException(javax.xml.transform.TransformerException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 28 with XPath

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);
    }
}
Also used : MCRNodeBuilder(org.mycore.common.xml.MCRNodeBuilder) MCRException(org.mycore.common.MCRException) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRBase(org.mycore.datamodel.metadata.MCRBase) Document(org.jdom2.Document) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRException(org.mycore.common.MCRException)

Example 29 with XPath

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);
}
Also used : XPathFactory(org.jdom2.xpath.XPathFactory) Text(org.jdom2.Text) Document(org.jdom2.Document) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRPersistentIdentifier(org.mycore.pi.MCRPersistentIdentifier)

Example 30 with XPath

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);
    }
}
Also used : Filter(org.jdom2.filter.Filter) Attribute(org.jdom2.Attribute) XmlException(gov.cms.qpp.conversion.xml.XmlException) DataConversionException(org.jdom2.DataConversionException)

Aggregations

Element (org.jdom2.Element)24 Document (org.jdom2.Document)11 IOException (java.io.IOException)4 Attribute (org.jdom2.Attribute)4 JDOMException (org.jdom2.JDOMException)4 MCRException (org.mycore.common.MCRException)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 URISyntaxException (java.net.URISyntaxException)3 Text (org.jdom2.Text)3 XPath (org.jdom2.xpath.XPath)3 XPathFactory (org.jdom2.xpath.XPathFactory)3 MCRNodeBuilder (org.mycore.common.xml.MCRNodeBuilder)3 MCRObject (org.mycore.datamodel.metadata.MCRObject)3 XmlException (gov.cms.qpp.conversion.xml.XmlException)2 List (java.util.List)2 TransformerException (javax.xml.transform.TransformerException)2 XPath (javax.xml.xpath.XPath)2 MCRPersistenceException (org.mycore.common.MCRPersistenceException)2 MCRPath (org.mycore.datamodel.niofs.MCRPath)2 MCRPersistentIdentifierException (org.mycore.pi.exceptions.MCRPersistentIdentifierException)2