Search in sources :

Example 1 with XPath

use of org.jaxen.XPath in project tdi-studio-se by Talend.

the class MsDynamicsWrapper method retrievePolicy.

/**
     * Retrieves the MS CRM 2011 policy
     * 
     * @return the policy string
     * @throws JaxenException issue with the response format
     * @throws IOException generic IO issue
     * @throws SOAPException issue with SOAP invocation
     */
public String retrievePolicy() throws IOException, SOAPException, JaxenException {
    String msg = FileUtil.readStringFromClasspath("/org/talend/mscrm/login/passport/RetrievePolicy.xml", MsDynamicsWrapper.class);
    SOAPMessage response = soap.execute(HTTPS + host + CRM_DISCOVERY_ENDPOINT, msg);
    XPath xp = soap.createXPath("//crm:Policy/text()", response);
    xp.addNamespace("crm", CRM_DISCOVERY_XMLNS);
    Text result = (Text) xp.selectSingleNode(response.getSOAPBody());
    return result.getValue();
}
Also used : XPath(org.jaxen.XPath) Text(javax.xml.soap.Text) SOAPMessage(javax.xml.soap.SOAPMessage)

Example 2 with XPath

use of org.jaxen.XPath in project tdi-studio-se by Talend.

the class MsDynamicsWrapper method login.

/**
     * Retrieves data from the CRM 2011 Online
     * 
     * @param entity CRM 2011 entity (e.g. account or opportunity)
     * @param columns Entity fields (e.g. accountid, name etc.)
     * @param csvFile name of the CSV file where the results will be stored
     * @return number of rows retrieved
     * @throws JaxenException issue with the response format
     * @throws IOException generic IO issue
     * @throws SOAPException issue with SOAP invocation
     */
// public int retrieveMultiple(String entity, String[] columns, String csvFile) throws IOException, SOAPException,
// JaxenException {
// int cnt = 0;
// CSVWriter cw = FileUtil.createUtf8CsvEscapingWriter(new File(csvFile));
// try {
// int pageNumber = 1;
// String cookie = "";
// boolean hasNext = true;
// while (hasNext) {
// List<Map<String, String>> ret = new ArrayList<Map<String, String>>();
// RetrievePageInfo info = retrievePage(entity, columns, pageNumber++, cookie, ret);
// for (Map<String, String> m : ret) {
// String[] row = new String[columns.length];
// for (int i = 0; i < columns.length; i++) {
// row[i] = m.get(columns[i]);
// }
// cw.writeNext(row);
// }
// cnt += ret.size();
// cw.flush();
// cookie = info.getPageCookie();
// if ("0".equalsIgnoreCase(info.getMoreRecords()))
// hasNext = false;
// }
// } finally {
// cw.close();
// }
// return cnt;
// }
/**
     * Paging information holder
     */
// private class RetrievePageInfo {
//
// private String pageCookie;
//
// private String moreRecords;
//
// public RetrievePageInfo(String cookie, String more) {
// setPageCookie(cookie);
// setMoreRecords(more);
// }
//
// public String getPageCookie() {
// return pageCookie;
// }
//
// public void setPageCookie(String pageCookie) {
// this.pageCookie = pageCookie;
// }
//
// public String getMoreRecords() {
// return moreRecords;
// }
//
// public void setMoreRecords(String moreRecords) {
// this.moreRecords = moreRecords;
// }
// }
/**
     * Retrieves a single page of RetrieveMultiple result
     * 
     * @param entity CRM 2011 entity (e.g. account or opportunity)
     * @param columns Entity fields (e.g. accountid, name etc.)
     * @param pageNumber the result page number (1..N)
     * @param cookie API paging cookie
     * @param ret the List of Maps that will be populated with the data
     * @return the RetrievePageInfo structure that describes the status of the retrieval
     * @throws JaxenException issue with the response format
     * @throws IOException generic IO issue
     * @throws SOAPException issue with SOAP invocation
     */
// protected RetrievePageInfo retrievePage(String entity, String[] columns, int pageNumber, String cookie,
// List<Map<String, String>> ret) throws IOException, SOAPException, JaxenException {
// String msg = FileUtil.readStringFromClasspath("/com/gooddata/msdynamics/RetrieveMultiple.xml",
// MsDynamicsWrapper.class);
// msg = msg.replace(CRM_ORGANIZATION_PLACEHOLDER, getOrganization());
// msg = msg.replace(CRM_TICKET_PLACEHOLDER, getCrmTicket());
// msg = msg.replace(CRM_ENTITY_PLACEHOLDER, entity);
// msg = msg.replace(CRM_PAGE_NUMBER_PLACEHOLDER, Integer.toString(pageNumber));
// msg = msg.replace(CRM_PAGE_COUNT_PLACEHOLDER, Integer.toString(PAGE_COUNT));
// if (cookie != null && cookie.length() > 0) {
// msg = msg.replace(CRM_PAGE_COOKIE_PLACEHOLDER, "<ns4:PageCookie><![CDATA[" + cookie + "]]></ns4:PageCookie>");
// } else {
// msg = msg.replace(CRM_PAGE_COOKIE_PLACEHOLDER, "");
// }
// String columnsElement = "";
// for (int i = 0; i < columns.length; i++) {
// columnsElement += "<ns4:Attribute>" + columns[i] + "</ns4:Attribute>";
// }
// msg = msg.replace(CRM_ATTRIBUTES_PLACEHOLDER, columnsElement);
// SOAPMessage response = soap.execute(HTTPS + host + CRM_ENDPOINT, msg);
// XPath xp = soap.createXPath("//crm:RetrieveMultipleResult", response);
// xp.addNamespace("crm", RESULT_XMLNS);
// List result = xp.selectNodes(response.getSOAPBody());
// if (result != null && result.size() == 1) {
// SOAPElement e = (SOAPElement) result.get(0);
// String more = e.getAttribute("MoreRecords");
// String newCookie = e.getAttribute("PagingCookie");
// if (more != null && more.length() > 0 && newCookie != null && newCookie.length() > 0) {
// xp = soap.createXPath("//crm:BusinessEntity", response);
// xp.addNamespace("crm", ENTITY_XMLNS);
// result = xp.selectNodes(response.getSOAPBody());
// for (Object o : result) {
// e = (SOAPElement) o;
// Map<String, String> instance = new HashMap<String, String>();
// Iterator elements = e.getChildElements();
// while (elements.hasNext()) {
// SOAPElement name = (SOAPElement) elements.next();
// String value = name.getFirstChild().getNodeValue();
// instance.put(name.getElementName().getLocalName(), value);
// }
// ret.add(instance);
// }
// return new RetrievePageInfo(newCookie, more);
// } else {
// throw new SOAPException("RetrieveMultiple: Invalid response. The response doesn't contain either "
// + "the MoreRecords or the PagingCookie attributes.");
// }
//
// } else {
// throw new SOAPException("RetrieveMultiple: Invalid response. The response doesn't contain "
// + "the RetrieveMultipleResult element.");
// }
// }
/**
     * Logs into the MS CRM 2011 Online
     * 
     * @return the Live ID token
     * @throws JaxenException issue with the response format
     * @throws IOException generic IO issue
     * @throws SOAPException issue with SOAP invocation
     */
public String login() throws IOException, SOAPException, JaxenException {
    String msg = FileUtil.readStringFromClasspath("/org/talend/mscrm/login/passport/LiveIdLogin.xml", MsDynamicsWrapper.class);
    msg = msg.replaceAll(LIVE_ID_SERVER_PLACEHOLDER, getHost());
    msg = msg.replaceAll(LIVE_ID_USERNAME_PLACEHOLDER, getUsername());
    msg = msg.replaceAll(LIVE_ID_PASSWORD_PLACEHOLDER, getPassword());
    msg = msg.replaceAll(LIVE_ID_POLICY_PLACEHOLDER, getPolicy());
    SOAPMessage response = soap.execute(HTTPS + LIVE_ID_HOST + LIVE_ID_ENDPOINT, msg);
    XPath xp = soap.createXPath("//wsse:BinarySecurityToken/text()", response);
    xp.addNamespace("wsse", WSSE_XMLNS);
    Node result = (Node) xp.selectSingleNode(response.getSOAPBody());
    return result.getValue();
}
Also used : XPath(org.jaxen.XPath) Node(javax.xml.soap.Node) SOAPMessage(javax.xml.soap.SOAPMessage)

Example 3 with XPath

use of org.jaxen.XPath in project intellij-community by JetBrains.

the class XPathEvalAction method evaluateExpression.

private boolean evaluateExpression(EvalExpressionDialog.Context context, XmlElement contextNode, Editor editor, Config cfg) {
    final Project project = editor.getProject();
    try {
        final XPathSupport support = XPathSupport.getInstance();
        final XPath xpath = support.createXPath((XmlFile) contextNode.getContainingFile(), context.input.expression, context.input.namespaces);
        xpath.setVariableContext(new CachedVariableContext(context.input.variables, xpath, contextNode));
        // evaluate the expression on the whole document
        final Object result = xpath.evaluate(contextNode);
        LOG.debug("result = " + result);
        LOG.assertTrue(result != null, "null result?");
        if (result instanceof List<?>) {
            final List<?> list = (List<?>) result;
            if (!list.isEmpty()) {
                if (cfg.HIGHLIGHT_RESULTS) {
                    highlightResult(contextNode, editor, list);
                }
                if (cfg.SHOW_USAGE_VIEW) {
                    showUsageView(editor, xpath, contextNode, list);
                }
                if (!cfg.SHOW_USAGE_VIEW && !cfg.HIGHLIGHT_RESULTS) {
                    final String s = StringUtil.pluralize("match", list.size());
                    Messages.showInfoMessage(project, "Expression produced " + list.size() + " " + s, "XPath Result");
                }
            } else {
                return Messages.showOkCancelDialog(project, "Sorry, your expression did not return any result", "XPath Result", "OK", "Edit Expression", Messages.getInformationIcon()) != Messages.OK;
            }
        } else if (result instanceof String) {
            Messages.showMessageDialog("'" + result.toString() + "'", "XPath result (String)", Messages.getInformationIcon());
        } else if (result instanceof Number) {
            Messages.showMessageDialog(result.toString(), "XPath result (Number)", Messages.getInformationIcon());
        } else if (result instanceof Boolean) {
            Messages.showMessageDialog(result.toString(), "XPath result (Boolean)", Messages.getInformationIcon());
        } else {
            LOG.error("Unknown XPath result: " + result);
        }
    } catch (XPathSyntaxException e) {
        LOG.debug(e);
        // TODO: Better layout of the error message with non-fixed size fonts
        return Messages.showOkCancelDialog(project, e.getMultilineMessage(), "XPath syntax error", "Edit Expression", "Cancel", Messages.getErrorIcon()) == Messages.OK;
    } catch (SAXPathException e) {
        LOG.debug(e);
        Messages.showMessageDialog(project, e.getMessage(), "XPath error", Messages.getErrorIcon());
    }
    return false;
}
Also used : XPath(org.jaxen.XPath) Project(com.intellij.openapi.project.Project) CachedVariableContext(org.intellij.plugins.xpathView.util.CachedVariableContext) XPathSyntaxException(org.jaxen.XPathSyntaxException) XPathSupport(org.intellij.plugins.xpathView.support.XPathSupport) SAXPathException(org.jaxen.saxpath.SAXPathException) List(java.util.List) ArrayList(java.util.ArrayList)

Example 4 with XPath

use of org.jaxen.XPath in project Asqatasun by Asqatasun.

the class KbCsvMojo method getUrls.

private List<String> getUrls(String url) throws JDOMException, JaxenException, IOException {
    SAXBuilder builder = new SAXBuilder();
    EntityResolver resolver = new XhtmlEntityResolver();
    builder.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    builder.setEntityResolver(resolver);
    Document document = builder.build(new URL(url));
    XPath xpath = new JDOMXPath("//*[@id='resultat']//*[@href]/@href");
    List<Attribute> results = xpath.selectNodes(document);
    List<String> urls = new ArrayList<String>();
    for (Attribute attr : results) {
        urls.add(attr.getValue());
    }
    return urls;
}
Also used : XPath(org.jaxen.XPath) JDOMXPath(org.jaxen.jdom.JDOMXPath) SAXBuilder(org.jdom.input.SAXBuilder) JDOMXPath(org.jaxen.jdom.JDOMXPath) Attribute(org.jdom.Attribute) ArrayList(java.util.ArrayList) EntityResolver(org.xml.sax.EntityResolver) Document(org.jdom.Document) URL(java.net.URL)

Example 5 with XPath

use of org.jaxen.XPath in project tdi-studio-se by Talend.

the class MsDynamicsWrapper method retrieveCrmTicket.

/**
     * Retrieves the CRM ticket
     * 
     * @return CRM ticket
     * @throws JaxenException issue with the response format
     * @throws IOException generic IO issue
     * @throws SOAPException issue with SOAP invocation
     */
public String retrieveCrmTicket() throws IOException, SOAPException, JaxenException {
    String msg = FileUtil.readStringFromClasspath("/org/talend/mscrm/login/passport/RetrieveCrmTicket.xml", MsDynamicsWrapper.class);
    msg = msg.replace(CRM_ORGANIZATION_PLACEHOLDER, getOrganization());
    msg = msg.replace(LIVE_ID_TICKET_PLACEHOLDER, getLiveId());
    SOAPMessage response = soap.execute(HTTPS + host + CRM_DISCOVERY_ENDPOINT, msg);
    XPath xp = soap.createXPath("//crm:CrmTicket/text()", response);
    xp.addNamespace("crm", CRM_DISCOVERY_XMLNS);
    Text result = (Text) xp.selectSingleNode(response.getSOAPBody());
    return result.getNodeValue();
}
Also used : XPath(org.jaxen.XPath) Text(javax.xml.soap.Text) SOAPMessage(javax.xml.soap.SOAPMessage)

Aggregations

XPath (org.jaxen.XPath)6 SOAPMessage (javax.xml.soap.SOAPMessage)3 ArrayList (java.util.ArrayList)2 Text (javax.xml.soap.Text)2 Project (com.intellij.openapi.project.Project)1 URL (java.net.URL)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Node (javax.xml.soap.Node)1 XPathSupport (org.intellij.plugins.xpathView.support.XPathSupport)1 CachedVariableContext (org.intellij.plugins.xpathView.util.CachedVariableContext)1 SimpleNamespaceContext (org.jaxen.SimpleNamespaceContext)1 XPathSyntaxException (org.jaxen.XPathSyntaxException)1 DOMXPath (org.jaxen.dom.DOMXPath)1 JDOMXPath (org.jaxen.jdom.JDOMXPath)1 SAXPathException (org.jaxen.saxpath.SAXPathException)1 Attribute (org.jdom.Attribute)1 Document (org.jdom.Document)1 SAXBuilder (org.jdom.input.SAXBuilder)1 EntityResolver (org.xml.sax.EntityResolver)1