Example usage for javax.xml.xpath XPathExpression evaluate

List of usage examples for javax.xml.xpath XPathExpression evaluate

Introduction

In this page you can find the example usage for javax.xml.xpath XPathExpression evaluate.

Prototype

public Object evaluate(InputSource source, QName returnType) throws XPathExpressionException;

Source Link

Document

Evaluate the compiled XPath expression in the context of the specified InputSource and return the result as the specified type.

Usage

From source file:cz.mzk.editor.server.fedora.utils.FedoraUtils.java

/**
 * Find first page pid.//from  ww  w.  j ava  2  s  .c om
 * 
 * @param pid
 *        the pid
 * @return the string
 */
public static String findFirstPagePid(String pid) {

    ArrayList<String> pids = new ArrayList<String>();
    try {
        String command = configuration.getFedoraHost() + "/get/" + pid + "/RELS-EXT";
        InputStream is = RESTHelper.get(command, configuration.getFedoraLogin(),
                configuration.getFedoraPassword(), true);
        Document contentDom = XMLUtils.parseDocument(is);
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile("/RDF/Description/*");
        NodeList nodes = (NodeList) expr.evaluate(contentDom, XPathConstants.NODESET);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node childnode = nodes.item(i);
            String nodeName = childnode.getNodeName();
            if (nodeName.contains(FedoraRelationship.hasPage.getStringRepresentation())
                    || nodeName.contains(FedoraRelationship.isOnPage.getStringRepresentation())) {
                return childnode.getAttributes().getNamedItem("rdf:resource").getNodeValue().split("uuid:")[1];
            } else if (!nodeName.contains("hasModel") && childnode.hasAttributes()
                    && childnode.getAttributes().getNamedItem("rdf:resource") != null) {
                pids.add(childnode.getAttributes().getNamedItem("rdf:resource").getNodeValue().split("/")[1]);
            }
        }
        for (String relpid : pids) {
            return FedoraUtils.findFirstPagePid(relpid);
        }
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return null;
}

From source file:cz.mzk.editor.server.fedora.utils.FedoraUtils.java

/**
 * @param foxmlDocument/*from   w w w .j  a  v a2 s.c  o  m*/
 * @param oldContent
 */
private static void addOldOcr(Document foxmlDocument, String oldContent) {
    try {
        String lastContLocXPath = "//foxml:datastream[@ID=\'" + TEXT_OCR.getValue()
                + "\']/foxml:datastreamVersion[last()]/foxml:contentLocation[last()]";

        XPathExpression all = makeNSAwareXpath().compile(lastContLocXPath);
        NodeList listOfstream = (NodeList) all.evaluate(foxmlDocument, XPathConstants.NODESET);
        Element lastContLocElement = null;
        if (listOfstream.getLength() != 0) {
            lastContLocElement = (Element) listOfstream.item(0);
        }
        Element localContElement = foxmlDocument.createElement("foxml:content");

        localContElement.setTextContent(oldContent);
        lastContLocElement.appendChild(localContElement);

    } catch (XPathExpressionException e) {
        LOGGER.warn("XPath failure", e);
    }
}

From source file:eu.europa.esig.dss.DSSXMLUtils.java

/**
 * Returns the NodeList corresponding to the XPath query.
 *
 * @param xmlNode     The node where the search should be performed.
 * @param xPathString XPath query string
 * @return/*from w w  w  . j  ava  2s  . c  o  m*/
 * @throws XPathExpressionException
 */
public static NodeList getNodeList(final Node xmlNode, final String xPathString) {

    try {

        final XPathExpression expr = createXPathExpression(xPathString);
        final NodeList evaluated = (NodeList) expr.evaluate(xmlNode, XPathConstants.NODESET);
        return evaluated;
    } catch (XPathExpressionException e) {
        throw new DSSException(e);
    }
}

From source file:eu.europa.esig.dss.DSSXMLUtils.java

/**
 * Returns the number of found elements based on the XPath query.
 *
 * @param xmlNode//from  w ww  . ja v a 2 s .  c o  m
 * @param xPathString
 * @return
 */
public static int count(final Node xmlNode, final String xPathString) {
    try {
        final XPathExpression xPathExpression = createXPathExpression(xPathString);
        final Double number = (Double) xPathExpression.evaluate(xmlNode, XPathConstants.NUMBER);
        return number.intValue();
    } catch (XPathExpressionException e) {
        throw new DSSException(e);
    }
}

From source file:eu.europa.esig.dss.DSSXMLUtils.java

/**
 * Returns the String value of the corresponding to the XPath query.
 *
 * @param xmlNode     The node where the search should be performed.
 * @param xPathString XPath query string
 * @return string value of the XPath query
 * @throws XPathExpressionException//w  w w  .  j  a va  2 s  .c o  m
 */
public static String getValue(final Node xmlNode, final String xPathString) {

    try {

        final XPathExpression xPathExpression = createXPathExpression(xPathString);
        final String string = (String) xPathExpression.evaluate(xmlNode, XPathConstants.STRING);
        return string.trim();
    } catch (XPathExpressionException e) {
        throw new DSSException(e);
    }
}

From source file:cz.mzk.editor.server.fedora.utils.FedoraUtils.java

/**
 * Removes the elements./*  w  ww.  j a  v  a 2s  .  c o m*/
 * 
 * @param parent
 *        the parent
 * @param doc
 *        the doc
 * @throws XPathExpressionException
 */
public static List<Element> removeElements(Element parent, Document doc, String xPath)
        throws XPathExpressionException {
    XPathExpression expr = makeNSAwareXpath().compile(xPath);
    NodeList nodes = null;
    List<Element> removedNodes = null;
    try {
        nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
        removedNodes = new ArrayList<Element>(nodes.getLength());
        for (int i = 0, lastIndex = nodes.getLength() - 1; i <= lastIndex; i++) {
            removedNodes.add((Element) parent.removeChild(nodes.item(i)));
        }
    } catch (XPathExpressionException e) {
        LOGGER.error("Unable to remove elements", e);
    }
    return removedNodes;
}

From source file:cz.mzk.editor.server.fedora.utils.FedoraUtils.java

/**
 * Create new relations part./* w ww. ja  v  a2s.  c om*/
 * 
 * @param detail
 *        the detail
 * @return content content of the new relations part
 */
public static String createNewRealitonsPart(DigitalObjectDetail detail) {
    if (detail.getAllItems() == null)
        return null;

    Document relsExt = null;

    try {
        relsExt = fedoraAccess.getRelsExt(detail.getUuid());
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        XPathExpression all = makeNSAwareXpath().compile("/rdf:RDF/rdf:Description");
        NodeList nodes1 = (NodeList) all.evaluate(relsExt, XPathConstants.NODESET);
        Element parent = null;
        if (nodes1.getLength() != 0) {
            parent = (Element) nodes1.item(0);
        }

        int modelId = 0;
        boolean changed = false;
        List<DigitalObjectModel> models = NamedGraphModel.getChildren(detail.getModel());
        for (List<DigitalObjectDetail> data : detail.getAllItems()) {
            if (data != null) { // is changed
                changed = true;
                String xPathString = NamedGraphModel.getRelationship(detail.getModel(), models.get(modelId))
                        .getXPathNamespaceAwareQuery();
                removeElements(parent, relsExt, xPathString);
            }
            modelId++;
        }
        if (!changed)
            return null;

        boolean lameNS = false;
        String rdfDescXPath = "/rdf:RDF/rdf:Description";
        Element rdfDescEl = FoxmlUtils.getElement(relsExt, rdfDescXPath);
        Element policyEl = null;
        if (rdfDescEl != null && rdfDescEl.getElementsByTagName("policy").getLength() > 0)
            policyEl = (Element) rdfDescEl.getElementsByTagName("policy").item(0);
        if (policyEl != null && policyEl.getAttribute(XMLNS_ATTR) != null) {
            lameNS = policyEl.getAttribute("xmlns").equals(FedoraNamespaces.KRAMERIUS_URI);
        }
        int i = 0;
        for (List<DigitalObjectDetail> data : detail.getAllItems()) {
            FedoraRelationship relationship = NamedGraphModel.getRelationship(detail.getModel(), models.get(i));
            if (data != null) {
                String relation = relationship.getStringRepresentation();
                for (DigitalObjectDetail obj : data) {

                    Element newEl = relsExt.createElement((lameNS ? "" : RELS_EXT_PART_KRAM).concat(relation));
                    if (lameNS)
                        newEl.setAttribute(XMLNS_ATTR, XMLNS_ATTR_CONTENT);
                    newEl.setAttribute(RDF_RESOURCE_ATTR, Constants.FEDORA_INFO_PREFIX + obj.getUuid());

                    rdfDescEl.appendChild(newEl);
                }
            } else {
                List<Element> removedElements = removeElements(parent, relsExt,
                        relationship.getXPathNamespaceAwareQuery());
                for (Element el : removedElements)
                    rdfDescEl.appendChild(el);
            }
            i++;
        }

    } catch (XPathExpressionException e) {
        LOGGER.warn("XPath failure", e);
    }

    String content = null;
    try {
        content = getStringFromDocument(relsExt, true);
    } catch (TransformerException e) {
        LOGGER.warn("Document transformer failure", e);
        e.printStackTrace();
    }
    return content;
}

From source file:org.jboss.windup.decorator.archive.PomDescriptionDecorator.java

protected String extractStringValue(XPathExpression expression, Document doc) throws XPathExpressionException {
    return (String) expression.evaluate(doc, XPathConstants.STRING);
}

From source file:com.dianping.phoenix.dev.core.tools.scanner.ServiceMetaScanner.java

@Override
protected List<ServicePortEntry> doScan(Document doc) throws Exception {
    List<ServicePortEntry> resList = new ArrayList<ServicePortEntry>();
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr = xpath.compile("//service");

    Object xmlRes = expr.evaluate(doc, XPathConstants.NODESET);

    NodeList nodes = (NodeList) xmlRes;

    for (int i = 0; i < nodes.getLength(); i++) {
        String serviceName = nodes.item(i).getAttributes().getNamedItem("name").getNodeValue();
        XPathExpression portExpr = xpath.compile("//service[@name='" + serviceName + "']/port/text()");
        Object portRes = portExpr.evaluate(doc, XPathConstants.NODESET);
        NodeList ports = (NodeList) portRes;
        int projectPort = -1;
        if (ports.getLength() >= 1) {
            projectPort = Integer.parseInt(ports.item(0).getNodeValue());
        }//from w w w.  j  a v a2s  .c  o  m

        if (projectPort > 0 && StringUtils.isNotBlank(serviceName)) {
            resList.add(new ServicePortEntry(serviceName, projectPort));
        }
    }

    return resList;
}

From source file:com.dianping.maven.plugin.tools.misc.scanner.ProjectMetaScanner.java

@Override
protected List<ProjectPortEntry> doScan(Document doc) throws Exception {
    List<ProjectPortEntry> resList = new ArrayList<ProjectPortEntry>();
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr = xpath.compile("//project");

    Object xmlRes = expr.evaluate(doc, XPathConstants.NODESET);

    NodeList nodes = (NodeList) xmlRes;

    for (int i = 0; i < nodes.getLength(); i++) {
        String projectName = nodes.item(i).getAttributes().getNamedItem("name").getNodeValue();
        XPathExpression portExpr = xpath.compile("//project[@name='" + projectName + "']/port/text()");
        Object portRes = portExpr.evaluate(doc, XPathConstants.NODESET);
        NodeList ports = (NodeList) portRes;
        int projectPort = -1;
        if (ports.getLength() >= 1) {
            projectPort = Integer.parseInt(ports.item(0).getNodeValue());
        }/*from w  ww . jav  a 2  s  .c o  m*/

        if (projectPort > 0 && StringUtils.isNotBlank(projectName)) {
            resList.add(new ProjectPortEntry(projectName, projectPort));
        }
    }

    return resList;
}