Search in sources :

Example 1 with XPathExpression

use of org.apache.camel.model.language.XPathExpression in project camel by apache.

the class ExpressionClauseSupport method xpath.

/**
     * Evaluates an <a href="http://camel.apache.org/xpath.html">XPath
     * expression</a> with the specified set of namespace prefixes and URIs
     *
     * @param text the expression to be evaluated
     * @param namespaces the namespace prefix and URIs to use
     * @return the builder to continue processing the DSL
     */
public T xpath(String text, Map<String, String> namespaces) {
    XPathExpression expression = new XPathExpression(text);
    expression.setNamespaces(namespaces);
    setExpressionType(expression);
    return result;
}
Also used : XPathExpression(org.apache.camel.model.language.XPathExpression) JXPathExpression(org.apache.camel.model.language.JXPathExpression)

Example 2 with XPathExpression

use of org.apache.camel.model.language.XPathExpression in project camel by apache.

the class ExpressionClauseSupport method xpath.

/**
     * Evaluates an <a href="http://camel.apache.org/xpath.html">XPath
     * expression</a> with the specified result type and set of namespace
     * prefixes and URIs on the supplied header name's contents
     *
     * @param text the expression to be evaluated
     * @param resultType the return type expected by the expression
     * @param namespaces the namespace prefix and URIs to use
     * @param headerName the name of the header to apply the expression to
     * @return the builder to continue processing the DSL
     */
public T xpath(String text, Class<?> resultType, Namespaces namespaces, String headerName) {
    XPathExpression expression = new XPathExpression(text);
    expression.setResultType(resultType);
    expression.setNamespaces(namespaces.getNamespaces());
    expression.setHeaderName(headerName);
    setExpressionType(expression);
    return result;
}
Also used : XPathExpression(org.apache.camel.model.language.XPathExpression) JXPathExpression(org.apache.camel.model.language.JXPathExpression)

Example 3 with XPathExpression

use of org.apache.camel.model.language.XPathExpression in project camel by apache.

the class ExpressionNodeHelper method toExpressionDefinition.

/**
     * Determines which {@link ExpressionDefinition} describes the given expression best possible.
     * <p/>
     * This implementation will use types such as {@link SimpleExpression}, {@link XPathExpression} etc.
     * if the given expression is detect as such a type.
     *
     * @param expression the expression
     * @return a definition which describes the expression
     */
public static ExpressionDefinition toExpressionDefinition(Expression expression) {
    if (expression instanceof SimpleBuilder) {
        SimpleBuilder builder = (SimpleBuilder) expression;
        // we keep the original expression by using the constructor that accepts an expression
        SimpleExpression answer = new SimpleExpression(builder);
        answer.setExpression(builder.getText());
        answer.setResultType(builder.getResultType());
        return answer;
    } else if (expression instanceof XPathBuilder) {
        XPathBuilder builder = (XPathBuilder) expression;
        // we keep the original expression by using the constructor that accepts an expression
        XPathExpression answer = new XPathExpression(builder);
        answer.setExpression(builder.getText());
        answer.setResultType(builder.getResultType());
        return answer;
    } else if (expression instanceof ValueBuilder) {
        // ValueBuilder wraps the actual expression so unwrap
        ValueBuilder builder = (ValueBuilder) expression;
        expression = builder.getExpression();
    }
    if (expression instanceof ExpressionDefinition) {
        return (ExpressionDefinition) expression;
    }
    return new ExpressionDefinition(expression);
}
Also used : XPathExpression(org.apache.camel.model.language.XPathExpression) ValueBuilder(org.apache.camel.builder.ValueBuilder) SimpleBuilder(org.apache.camel.builder.SimpleBuilder) XPathBuilder(org.apache.camel.builder.xml.XPathBuilder) ExpressionDefinition(org.apache.camel.model.language.ExpressionDefinition) SimpleExpression(org.apache.camel.model.language.SimpleExpression)

Example 4 with XPathExpression

use of org.apache.camel.model.language.XPathExpression in project camel by apache.

the class MockXpathTest method testXpathAssertion.

public void testXpathAssertion() throws Exception {
    MockEndpoint result = getMockEndpoint("mock:result");
    result.expectedMessageCount(1);
    XPathExpression xpath = new XPathExpression("/foo = 'bar'");
    xpath.setResultType(Boolean.class);
    result.allMessages().body().matches(xpath);
    template.sendBody("direct:start", "<foo>bar</foo>");
    assertMockEndpointsSatisfied();
}
Also used : XPathExpression(org.apache.camel.model.language.XPathExpression)

Example 5 with XPathExpression

use of org.apache.camel.model.language.XPathExpression in project camel by apache.

the class SpringDataFormatPartialTest method testPartialMarshal.

@Test
public void testPartialMarshal() throws Exception {
    PurchaseOrder bean = new PurchaseOrder();
    bean.setName("Beer");
    bean.setAmount(23);
    bean.setPrice(2.5);
    MockEndpoint mock = resolveMandatoryEndpoint("mock:marshal", MockEndpoint.class);
    mock.expectedMessageCount(1);
    XPathExpression xpath = new XPathExpression("count(//*[namespace-uri() = 'http://example.camel.org/apache' and local-name() = 'po']) = 1");
    xpath.setResultType(Boolean.class);
    mock.allMessages().body().matches(xpath);
    template.sendBody("direct:marshal", bean);
    mock.assertIsSatisfied();
    //To make sure there is no XML declaration.
    assertFalse("There should have no XML declaration.", mock.getExchanges().get(0).getIn().getBody(String.class).startsWith("<?xml version="));
}
Also used : XPathExpression(org.apache.camel.model.language.XPathExpression) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Aggregations

XPathExpression (org.apache.camel.model.language.XPathExpression)11 JXPathExpression (org.apache.camel.model.language.JXPathExpression)5 SimpleBuilder (org.apache.camel.builder.SimpleBuilder)2 ValueBuilder (org.apache.camel.builder.ValueBuilder)2 XPathBuilder (org.apache.camel.builder.xml.XPathBuilder)2 ExpressionDefinition (org.apache.camel.model.language.ExpressionDefinition)2 SimpleExpression (org.apache.camel.model.language.SimpleExpression)2 Expression (org.apache.camel.Expression)1 Predicate (org.apache.camel.Predicate)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1 Test (org.junit.Test)1