Search in sources :

Example 36 with XPathExpression

use of javax.xml.xpath.XPathExpression in project ats-framework by Axway.

the class XmlUtilities method getByXpath.

/**
     * Get values matching passed XPath expression
     * @param node
     * @param expression XPath expression
     * @return matching values
     * @throws Exception
     */
private static String[] getByXpath(Node node, String expression) throws Exception {
    XPathFactory xPathFactory = XPathFactory.newInstance();
    XPath xPath = xPathFactory.newXPath();
    XPathExpression xPathExpression = xPath.compile(expression);
    NodeList nlist = (NodeList) xPathExpression.evaluate(node, XPathConstants.NODESET);
    int nodeListSize = nlist.getLength();
    List<String> values = new ArrayList<String>(nodeListSize);
    for (int index = 0; index < nlist.getLength(); index++) {
        Node aNode = nlist.item(index);
        values.add(aNode.getTextContent());
    }
    return values.toArray(new String[values.size()]);
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList)

Example 37 with XPathExpression

use of javax.xml.xpath.XPathExpression in project claw-compiler by C2SM-RCM.

the class XnodeUtil method getFromXpath.

/**
   * Get a list of T elements from an xpath query executed from the
   * given element.
   *
   * @param from  Element to start from.
   * @param query XPath query to be executed.
   * @return List of all array references found. List is empty if nothing is
   * found.
   */
private static List<Xnode> getFromXpath(Xnode from, String query) {
    List<Xnode> elements = new ArrayList<>();
    try {
        XPathExpression ex = XPathFactory.newInstance().newXPath().compile(query);
        NodeList output = (NodeList) ex.evaluate(from.element(), XPathConstants.NODESET);
        for (int i = 0; i < output.getLength(); i++) {
            Element element = (Element) output.item(i);
            elements.add(new Xnode(element));
        }
    } catch (XPathExpressionException ignored) {
    }
    return elements;
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList)

Example 38 with XPathExpression

use of javax.xml.xpath.XPathExpression in project perun by CESNET.

the class AbstractPublicationSystemStrategy method getValueFromXpath.

/**
	 * Get xml Node and xpath expression to get value from node by this xpath.
	 *
	 * @param node node for getting value from
	 * @param xpathExpression expression for xpath to looking for value in node
	 * @param resultType type of resulting / expected object (string number node nodelist ...)
	 * @return object extracted from node by xpath
	 * @throws InternalErrorException
	 */
protected Object getValueFromXpath(Node node, String xpathExpression, QName resultType) throws InternalErrorException {
    //Prepare xpath expression
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr;
    try {
        expr = xpath.compile(xpathExpression);
    } catch (XPathExpressionException ex) {
        throw new InternalErrorException("Error when compiling xpath query.", ex);
    }
    Object result;
    try {
        result = expr.evaluate(node, resultType);
    } catch (XPathExpressionException ex) {
        throw new InternalErrorException("Error when evaluate xpath query on node.", ex);
    }
    return result;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 39 with XPathExpression

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

the class TestCalDav method testAppleCaldavProxyFunctions.

/**
     * http://svn.calendarserver.org/repository/calendarserver/CalendarServer/trunk/doc/Extensions/caldav-proxy.txt
     * This is an Apple standard implemented by Apple Mac OSX at least up to Yosemite and offers a fairly simple
     * sharing model for calendars.  The model is simpler than Zimbra's native model and there are mismatches,
     * for instance Zimbra requires the proposed delegate to accept shares.
     */
@Test
public void testAppleCaldavProxyFunctions() throws ServiceException, IOException {
    Account sharer = users[3].create();
    Account sharee1 = users[1].create();
    Account sharee2 = users[2].create();
    ZMailbox mboxSharer = TestUtil.getZMailbox(sharer.getName());
    ZMailbox mboxSharee1 = TestUtil.getZMailbox(sharee1.getName());
    ZMailbox mboxSharee2 = TestUtil.getZMailbox(sharee2.getName());
    setZimbraPrefAppleIcalDelegationEnabled(mboxSharer, true);
    setZimbraPrefAppleIcalDelegationEnabled(mboxSharee1, true);
    setZimbraPrefAppleIcalDelegationEnabled(mboxSharee2, true);
    // Test PROPPATCH to "calendar-proxy-read" URL
    setGroupMemberSet(TestCalDav.getCalendarProxyReadUrl(sharer), sharer, sharee2);
    // Test PROPPATCH to "calendar-proxy-write" URL
    setGroupMemberSet(TestCalDav.getCalendarProxyWriteUrl(sharer), sharer, sharee1);
    // verify that adding new members to groups triggered notification messages
    List<ZMessage> msgs = TestUtil.waitForMessages(mboxSharee1, "in:inbox subject:\"Share Created: Calendar shared by \"", 1, 10000);
    assertNotNull(String.format("Notification msgs for %s", sharee1.getName()), msgs);
    assertEquals(String.format("num msgs for %s", sharee1.getName()), 1, msgs.size());
    msgs = TestUtil.waitForMessages(mboxSharee2, "in:inbox subject:\"Share Created: Calendar shared by \"", 1, 10000);
    assertNotNull(String.format("Notification msgs for %s", sharee2.getName()), msgs);
    assertEquals(String.format("num msgs for %s", sharee2.getName()), 1, msgs.size());
    // Simulate acceptance of the shares (would normally need to be done in ZWC
    createCalendarMountPoint(mboxSharee1, sharer);
    createCalendarMountPoint(mboxSharee2, sharer);
    Document doc = delegateForExpandProperty(sharee1);
    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);
        NodeList result = (NodeList) xPathExpr.evaluate(doc, XPathConstants.NODESET);
        assertEquals(String.format("num XPath nodes for %s for %s", xpathS, sharee1.getName()), 1, result.getLength());
        String text = (String) xPathExpr.evaluate(doc, XPathConstants.STRING);
        assertEquals("HREF for account owner", UrlNamespace.getPrincipalUrl(sharee1).replaceAll("@", "%40"), text);
        xpathS = "/D:multistatus/D:response/D:propstat/D:prop/CS:calendar-proxy-write-for/D:response/D:href/text()";
        xPathExpr = xpath.compile(xpathS);
        result = (NodeList) xPathExpr.evaluate(doc, XPathConstants.NODESET);
        assertEquals(String.format("num XPath nodes for %s for %s", xpathS, sharee1.getName()), 1, result.getLength());
        text = (String) xPathExpr.evaluate(doc, XPathConstants.STRING);
        assertEquals("HREF for sharer", UrlNamespace.getPrincipalUrl(sharer).replaceAll("@", "%40"), text);
    } catch (XPathExpressionException e1) {
        ZimbraLog.test.debug("xpath problem", e1);
    }
    // Check that proxy write has sharee1 in it
    doc = groupMemberSetExpandProperty(sharer, sharee1, true);
    // Check that proxy read has sharee2 in it
    doc = groupMemberSetExpandProperty(sharer, sharee2, false);
    String davBaseName = "notAllowed@There";
    String url = String.format("%s%s", getFolderUrl(sharee1, "Shared Calendar").replaceAll(" ", "%20").replaceAll("@", "%40"), davBaseName);
    HttpMethodExecutor exe = doIcalPut(url, sharee1, simpleEvent(sharer), HttpStatus.SC_MOVED_TEMPORARILY);
    String location = null;
    for (Header hdr : exe.respHeaders) {
        if ("Location".equals(hdr.getName())) {
            location = hdr.getValue();
        }
    }
    assertNotNull("Location Header not returned when creating", location);
    url = String.format("%s%s", getFolderUrl(sharee1, "Shared Calendar").replaceAll(" ", "%20").replaceAll("@", "%40"), location.substring(location.lastIndexOf('/') + 1));
    doIcalPut(url, sharee1, simpleEvent(sharer), HttpStatus.SC_CREATED);
}
Also used : ZMessage(com.zimbra.client.ZMessage) XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) Account(com.zimbra.cs.account.Account) ZMailbox(com.zimbra.client.ZMailbox) Header(org.apache.commons.httpclient.Header) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 40 with XPathExpression

use of javax.xml.xpath.XPathExpression in project cloudstack by apache.

the class OvmObject method xmlToList.

public List<String> xmlToList(String path, Document xmlDocument) throws Ovm3ResourceException {
    List<String> list = new ArrayList<String>();
    XPathFactory factory = javax.xml.xpath.XPathFactory.newInstance();
    XPath xPath = factory.newXPath();
    try {
        XPathExpression xPathExpression = xPath.compile(path);
        NodeList nodeList = (NodeList) xPathExpression.evaluate(xmlDocument, XPathConstants.NODESET);
        for (int ind = 0; ind < nodeList.getLength(); ind++) {
            if (!nodeList.item(ind).getTextContent().isEmpty()) {
                list.add("" + nodeList.item(ind).getTextContent());
            } else {
                list.add("" + nodeList.item(ind).getNodeValue());
            }
        }
        return list;
    } catch (XPathExpressionException e) {
        throw new Ovm3ResourceException("Problem parsing XML to List: ", e);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList)

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