Search in sources :

Example 81 with XPathExpression

use of javax.xml.xpath.XPathExpression in project Asqatasun by Asqatasun.

the class HeritrixConfigurationModifier method getNodeFromXpath.

protected Node getNodeFromXpath(Document document) throws XPathExpressionException {
    if (this.xpathExpression == null) {
        this.xpathExpression = buildXpathExpression();
    }
    XPathExpression xPathExpression = xpath.compile(this.xpathExpression);
    Object result = xPathExpression.evaluate(document, XPathConstants.NODESET);
    NodeList nodeList = (NodeList) result;
    if (nodeList.getLength() == 1) {
        return nodeList.item(0);
    }
    return null;
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) NodeList(org.w3c.dom.NodeList)

Example 82 with XPathExpression

use of javax.xml.xpath.XPathExpression in project uPortal by Jasig.

the class PortletMarketplaceController method getPortletTabInfo.

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 {
        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;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) XPathFactory(javax.xml.xpath.XPathFactory)

Example 83 with XPathExpression

use of javax.xml.xpath.XPathExpression in project zm-mailbox by Zimbra.

the class TestCalDav method groupMemberSetExpandProperty.

public static Document groupMemberSetExpandProperty(Account acct, Account member, boolean proxyWrite) throws IOException, ServiceException {
    String url = proxyWrite ? TestCalDav.getCalendarProxyWriteUrl(acct) : TestCalDav.getCalendarProxyReadUrl(acct);
    url = url.replaceAll("@", "%40");
    String href = proxyWrite ? UrlNamespace.getCalendarProxyWriteUrl(acct, acct) : UrlNamespace.getCalendarProxyReadUrl(acct, acct);
    href = href.replaceAll("@", "%40");
    ReportMethod method = new ReportMethod(url);
    addBasicAuthHeaderForUser(method, acct);
    HttpClient client = new HttpClient();
    TestCalDav.HttpMethodExecutor executor;
    method.addRequestHeader("Content-Type", MimeConstants.CT_TEXT_XML);
    method.setRequestEntity(new ByteArrayRequestEntity(TestCalDav.expandPropertyGroupMemberSet.getBytes(), MimeConstants.CT_TEXT_XML));
    executor = new TestCalDav.HttpMethodExecutor(client, method, HttpStatus.SC_MULTI_STATUS);
    String respBody = new String(executor.responseBodyBytes, MimeConstants.P_CHARSET_UTF8);
    Document doc = W3cDomUtil.parseXMLToDoc(respBody);
    org.w3c.dom.Element docElement = doc.getDocumentElement();
    assertEquals("Report node name", DavElements.P_MULTISTATUS, docElement.getLocalName());
    XPath xpath = XPathFactory.newInstance().newXPath();
    xpath.setNamespaceContext(TestCalDav.NamespaceContextForXPath.forCalDAV());
    XPathExpression xPathExpr;
    try {
        String xpathS = "/D:multistatus/D:response/D:href/text()";
        xPathExpr = xpath.compile(xpathS);
        String text = (String) xPathExpr.evaluate(doc, XPathConstants.STRING);
        assertEquals("HREF for response", href, text);
        xpathS = "/D:multistatus/D:response/D:propstat/D:prop/D:group-member-set/D:response/D:href/text()";
        xPathExpr = xpath.compile(xpathS);
        text = (String) xPathExpr.evaluate(doc, XPathConstants.STRING);
        assertEquals("HREF for sharee", UrlNamespace.getPrincipalUrl(member).replaceAll("@", "%40"), text);
    } catch (XPathExpressionException e1) {
        ZimbraLog.test.debug("xpath problem", e1);
    }
    return doc;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Document(org.w3c.dom.Document) HttpClient(org.apache.commons.httpclient.HttpClient) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity)

Example 84 with XPathExpression

use of javax.xml.xpath.XPathExpression in project zm-mailbox by Zimbra.

the class TestCalDav method waitForItemInCalendarCollectionByUID.

/**
     * @param acct
     * @param UID - null or empty if don't care
     * @param expected - false if don't expect a matching item to be in collection within timeout time
     * @return href of first matching item found
     * @throws ServiceException
     * @throws IOException
     */
public static String waitForItemInCalendarCollectionByUID(String url, Account acct, String UID, boolean expected, int timeout_millis) throws ServiceException, IOException {
    int orig_timeout_millis = timeout_millis;
    while (timeout_millis > 0) {
        Document doc = calendarQuery(url, acct);
        XPath xpath = XPathFactory.newInstance().newXPath();
        xpath.setNamespaceContext(TestCalDav.NamespaceContextForXPath.forCalDAV());
        XPathExpression xPathExpr;
        try {
            xPathExpr = xpath.compile("/D:multistatus/D:response/D:href/text()");
            NodeList result = (NodeList) xPathExpr.evaluate(doc, XPathConstants.NODESET);
            if (1 <= result.getLength()) {
                for (int ndx = 0; ndx < result.getLength(); ndx++) {
                    Node item = result.item(ndx);
                    String nodeValue = item.getNodeValue();
                    if ((Strings.isNullOrEmpty(UID)) || (nodeValue.contains(UID))) {
                        if (!expected) {
                            fail(String.format("item with UID '%s' unexpectedly arrived in collection '%s' within %d millisecs", Strings.nullToEmpty(UID), url, orig_timeout_millis - timeout_millis));
                        }
                        return nodeValue;
                    }
                }
            }
        } catch (XPathExpressionException e1) {
            ZimbraLog.test.debug("xpath problem", e1);
        }
        try {
            if (timeout_millis > TestUtil.DEFAULT_WAIT) {
                Thread.sleep(TestUtil.DEFAULT_WAIT);
                timeout_millis = timeout_millis - TestUtil.DEFAULT_WAIT;
            } else {
                Thread.sleep(timeout_millis);
                timeout_millis = 0;
            }
        } catch (InterruptedException e) {
            ZimbraLog.test.debug("sleep got interrupted", e);
        }
    }
    if (expected) {
        fail(String.format("item with UID '%s' didn't arrive in collection '%s' within %d millisecs", Strings.nullToEmpty(UID), url, orig_timeout_millis));
    }
    return null;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document)

Example 85 with XPathExpression

use of javax.xml.xpath.XPathExpression in project zm-mailbox by Zimbra.

the class TestCalDav method checkPropFindSupportedCalendarComponentSet.

public void checkPropFindSupportedCalendarComponentSet(Account user, String fullurl, String shorturl, String[] compNames) throws Exception {
    PropFindMethod method = new PropFindMethod(fullurl);
    addBasicAuthHeaderForUser(method, user);
    HttpClient client = new HttpClient();
    TestCalDav.HttpMethodExecutor executor;
    String respBody;
    method.addRequestHeader("Content-Type", MimeConstants.CT_TEXT_XML);
    method.setRequestEntity(new ByteArrayRequestEntity(propFindSupportedCalendarComponentSet.getBytes(), MimeConstants.CT_TEXT_XML));
    executor = new TestCalDav.HttpMethodExecutor(client, method, HttpStatus.SC_MULTI_STATUS);
    respBody = new String(executor.responseBodyBytes, MimeConstants.P_CHARSET_UTF8);
    Document doc = W3cDomUtil.parseXMLToDoc(respBody);
    XPath xpath = XPathFactory.newInstance().newXPath();
    xpath.setNamespaceContext(TestCalDav.NamespaceContextForXPath.forCalDAV());
    XPathExpression xPathExpr;
    String text;
    NodeList result;
    xPathExpr = xpath.compile("/D:multistatus/D:response/D:href/text()");
    result = (NodeList) xPathExpr.evaluate(doc, XPathConstants.NODESET);
    text = (String) xPathExpr.evaluate(doc, XPathConstants.STRING);
    assertEquals("HREF", shorturl.replaceAll("@", "%40"), text);
    xPathExpr = xpath.compile("/D:multistatus/D:response/D:propstat/D:status/text()");
    text = (String) xPathExpr.evaluate(doc, XPathConstants.STRING);
    assertEquals("status", "HTTP/1.1 200 OK", text);
    xPathExpr = xpath.compile("/D:multistatus/D:response/D:propstat/D:prop/C:supported-calendar-component-set/C:comp");
    result = (NodeList) xPathExpr.evaluate(doc, XPathConstants.NODESET);
    assertEquals("Number of comp nodes under supported-calendar-component-set", compNames.length, result.getLength());
    List<String> names = Arrays.asList(compNames);
    for (int ndx = 0; ndx < result.getLength(); ndx++) {
        org.w3c.dom.Element child = (org.w3c.dom.Element) result.item(ndx);
        String name = child.getAttribute("name");
        assertNotNull("comp should have a 'name' attribute", name);
        assertTrue(String.format("comp 'name' attribute '%s' should be one of the expected names", name), names.contains(name));
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) NodeList(org.w3c.dom.NodeList) Element(com.zimbra.common.soap.Element) Document(org.w3c.dom.Document) HttpClient(org.apache.commons.httpclient.HttpClient) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity)

Aggregations

XPathExpression (javax.xml.xpath.XPathExpression)98 XPath (javax.xml.xpath.XPath)69 NodeList (org.w3c.dom.NodeList)56 Document (org.w3c.dom.Document)48 XPathExpressionException (javax.xml.xpath.XPathExpressionException)40 XPathFactory (javax.xml.xpath.XPathFactory)40 Node (org.w3c.dom.Node)38 DocumentBuilder (javax.xml.parsers.DocumentBuilder)24 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)19 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)13 Element (org.w3c.dom.Element)12 PBXNativeTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXNativeTarget)11 PBXTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXTarget)11 ImmutableMap (com.google.common.collect.ImmutableMap)11 IOException (java.io.IOException)11 Path (java.nio.file.Path)11 PBXFileReference (com.facebook.buck.apple.xcode.xcodeproj.PBXFileReference)10 InputSource (org.xml.sax.InputSource)9