Example usage for javax.xml.xpath XPathFactory newXPath

List of usage examples for javax.xml.xpath XPathFactory newXPath

Introduction

In this page you can find the example usage for javax.xml.xpath XPathFactory newXPath.

Prototype

public abstract XPath newXPath();

Source Link

Document

Return a new XPath using the underlying object model determined when the XPathFactory was instantiated.

Usage

From source file:org.apereo.portal.layout.simple.SimpleLayout.java

@Override
public Enumeration getNodeIds() throws PortalException {
    Vector v = new Vector();
    try {//from w  w  w  .  j a va  2s  .  c o m
        String expression = "*";
        XPathFactory fac = XPathFactory.newInstance();
        XPath xpath = fac.newXPath();
        NodeList nl = (NodeList) xpath.evaluate(expression, layout, XPathConstants.NODESET);
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element e = (Element) node;
                v.add(e.getAttribute("ID"));
            }
        }
    } catch (Exception e) {
        log.error("Exception getting node ids.", e);
    }
    return v.elements();
}

From source file:org.apereo.portal.layout.simple.SimpleLayout.java

@Override
public String getRootId() {
    String rootNode = null;//  ww w . j a va 2 s .  c  o  m
    try {

        String expression = "/layout/folder";
        XPathFactory fac = XPathFactory.newInstance();
        XPath xpath = fac.newXPath();
        Element rootNodeE = (Element) xpath.evaluate(expression, layout, XPathConstants.NODE);

        rootNode = rootNodeE.getAttribute("ID");
    } catch (Exception e) {
        log.error("Error getting root id.", e);
    }
    return rootNode;
}

From source file:org.apereo.portal.portlets.marketplace.PortletMarketplaceController.java

private List<PortletTab> getPortletTabInfo(DistributedUserLayout layout, String fname) {
    final String XPATH_TAB = "/layout/folder/folder[@hidden = 'false' and @type = 'regular']";
    final String XPATH_COUNT_COLUMNS = "count(./folder[@hidden = \"false\"])";
    final String XPATH_COUNT_NON_EDITABLE_COLUMNS = "count(./folder[@hidden = \"false\" and @*[local-name() = \"editAllowed\"] = \"false\"])";
    final String XPATH_GET_TAB_PORTLET_FMT = ".//channel[@hidden = \"false\" and @fname = \"%s\"]";

    Document doc = layout.getLayout();

    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();

    try {// w  w w .j a  va  2s .  c o  m
        XPathExpression tabExpr = xpath.compile(XPATH_TAB);
        NodeList list = (NodeList) tabExpr.evaluate(doc, XPathConstants.NODESET);

        // Count columns and non-editable columns...
        XPathExpression columnCountExpr = xpath.compile(XPATH_COUNT_COLUMNS);
        XPathExpression nonEditableCountExpr = xpath.compile(XPATH_COUNT_NON_EDITABLE_COLUMNS);

        // get the list of tabs...
        String xpathStr = format(XPATH_GET_TAB_PORTLET_FMT, fname);
        XPathExpression portletExpr = xpath.compile(xpathStr);

        List<PortletTab> tabs = new ArrayList<>();
        for (int i = 0; i < list.getLength(); i++) {
            Node tab = list.item(i);
            String tabName = ((Element) tab).getAttribute("name");
            String tabId = ((Element) tab).getAttribute("ID");

            // check if tab is editable...
            Number columns = (Number) columnCountExpr.evaluate(tab, XPathConstants.NUMBER);
            Number nonEditColumns = (Number) nonEditableCountExpr.evaluate(tab, XPathConstants.NUMBER);
            // tab is not editable...  skip it...
            if (columns.intValue() > 0 && columns.intValue() == nonEditColumns.intValue()) {
                continue;
            }

            // get all instances of this portlet on this tab...
            List<String> layoutIds = new ArrayList<>();
            NodeList fnameListPerTab = (NodeList) portletExpr.evaluate(tab, XPathConstants.NODESET);
            for (int j = 0; j < fnameListPerTab.getLength(); j++) {
                Node channel = fnameListPerTab.item(j);

                String layoutId = ((Element) channel).getAttribute("ID");
                layoutIds.add(layoutId);
            }

            PortletTab tabInfo = new PortletTab(tabName, tabId, layoutIds);
            tabs.add(tabInfo);
        }

        return tabs;

    } catch (XPathExpressionException e) {
        logger.error("Error evaluating xpath", e);
    }

    return null;
}

From source file:org.apereo.portal.xml.XmlUtilitiesImplTest.java

@Test
public void testGetUniqueXPath() throws Exception {
    final Document testDoc = loadTestDocument();

    final XPathFactory xPathFactory = XPathFactory.newInstance();
    final XPath xPath = xPathFactory.newXPath();
    final XPathExpression xPathExpression = xPath.compile("//*[@ID='11']");

    final Node node = (Node) xPathExpression.evaluate(testDoc, XPathConstants.NODE);

    final XmlUtilitiesImpl xmlUtilities = new XmlUtilitiesImpl();
    final String nodePath = xmlUtilities.getUniqueXPath(node);

    assertEquals("/layout/folder[2]/folder[3]", nodePath);
}

From source file:org.artifactory.webapp.wicket.util.DescriptionExtractor.java

private String executeQuery(String query) {
    try {//  w ww  .j a  va2 s .  c o  m
        XPathFactory xFactory = XPathFactory.newInstance();
        XPath xpath = xFactory.newXPath();
        xpath.setNamespaceContext(new SchemaNamespaceContext());
        XPathExpression expr = xpath.compile(query);
        Object description = expr.evaluate(doc, XPathConstants.STRING);
        return description.toString().trim();
    } catch (XPathExpressionException e) {
        throw new RuntimeException("Failed to execute xpath query: " + query, e);
    }
}

From source file:org.asqatasun.processor.DOMHandlerImpl.java

private void initialize() {
    if (docInitialised) {
        return;//www  .  jav  a 2 s . c  o m
    }
    XPathFactory xPathfactory = XPathFactory.newInstance();
    xpath = xPathfactory.newXPath();

    try {

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        //@TODO verify the namespace property is necessary in our context
        //            factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        document = builder.parse(new ByteArrayInputStream(ssp.getDOM().getBytes("UTF-8")));
        docInitialised = true;
    } catch (IOException ex) {
        LOGGER.error(ssp.getURI() + "cannot be initialised due to " + ex.getMessage());
        throw new RuntimeException(ex);
    } catch (SAXException ex) {
        LOGGER.error(ssp.getURI() + "cannot be initialised due to " + ex.getMessage());
        throw new RuntimeException(ex);
    } catch (ParserConfigurationException ex) {
        LOGGER.error(ssp.getURI() + "cannot be initialised due to " + ex.getMessage());
        throw new RuntimeException(ex);
    }
    processRemarkService.initializeService(document, ssp.getDOM());
}

From source file:org.atricore.idbus.capabilities.sso.support.core.signature.JSR105SamlR2SignerImpl.java

protected NodeList evaluateXPath(Document doc, String expression) throws SamlR2SignatureException {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    xpath.setNamespaceContext(getNamespaceContext());

    NodeList nl;//from   w  w  w.  j  a  v  a 2  s .  co  m
    try {
        XPathExpression expr = xpath.compile(expression);

        nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        throw new SamlR2SignatureException(e);
    }

    return nl;
}

From source file:org.auscope.gridtools.RegistryQueryClient.java

/**
 * Runs an XPath query on the local (cached) MDS file.
 * //  w  w  w.  j  a v  a2s.co  m
 * @param query The XPath query to run
 * @return A <code>NodeList</code> containing the nodes/elements selected
 *         by the query or null on error.
 */
private NodeList turboMDSquery(String query) {
    checkCache();

    NodeList myNodeList = null;
    XPathFactory xPath = XPathFactory.newInstance();

    try {
        InputSource inpXml = new InputSource(MDS_CACHE_FILE);
        myNodeList = (NodeList) xPath.newXPath().evaluate(query, inpXml, XPathConstants.NODESET);
    } catch (Exception e) {
        logger.error(e.getMessage());
    }

    return myNodeList;
}

From source file:org.broadleafcommerce.common.extensibility.context.merge.MergePoint.java

public MergePoint(MergeHandler handler, Document doc1, Document doc2) {
    this.handler = handler;
    this.doc1 = doc1;
    this.doc2 = doc2;
    XPathFactory factory = XPathFactory.newInstance();
    xPath = factory.newXPath();
}

From source file:org.codice.ddf.security.interceptor.AnonymousInterceptor.java

private String retrieveXmlValue(String xml, String xpathStmt) {
    String result = null;//from  w w w  .  j av a2s .  c  om
    Document document = createDocument(xml);
    XPathFactory xFactory = XPathFactory.newInstance();
    XPath xpath = xFactory.newXPath();

    try {
        XPathExpression expr = xpath.compile(xpathStmt);
        result = (String) expr.evaluate(document, XPathConstants.STRING);
    } catch (XPathExpressionException e) {
        LOGGER.warn("Error processing XPath statement on policy XML.");
    }
    return result;
}