Search in sources :

Example 6 with Language

use of org.apache.camel.spi.Language in project camel by apache.

the class GenericFileDefaultSorter method sortByFileLanguage.

/**
     * Returns a new sort by file language expression
     *
     * @param context    the camel context
     * @param expression the file language expression
     * @param reverse    true to reverse order
     * @param ignoreCase ignore case if comparing strings
     * @param nested     nested comparator for sub group sorting, can be null
     * @return the comparator
     */
public static Comparator<Exchange> sortByFileLanguage(final CamelContext context, final String expression, final boolean reverse, final boolean ignoreCase, final Comparator<Exchange> nested) {
    // the expression should be enclosed by ${ }
    String text = expression;
    if (!expression.startsWith("${")) {
        text = "${" + text;
    }
    if (!expression.endsWith("}")) {
        text = text + "}";
    }
    Language language = context.resolveLanguage("file");
    final Expression exp = language.createExpression(text);
    return new Comparator<Exchange>() {

        public int compare(Exchange o1, Exchange o2) {
            Object result1 = exp.evaluate(o1, Object.class);
            Object result2 = exp.evaluate(o2, Object.class);
            int answer = ObjectHelper.compare(result1, result2, ignoreCase);
            // if equal then sub sort by nested comparator
            if (answer == 0 && nested != null) {
                answer = nested.compare(o1, o2);
            }
            return reverse ? -1 * answer : answer;
        }

        public String toString() {
            return expression + (nested != null ? ";" + nested.toString() : "");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Language(org.apache.camel.spi.Language) Expression(org.apache.camel.Expression) Comparator(java.util.Comparator)

Example 7 with Language

use of org.apache.camel.spi.Language in project camel by apache.

the class ExpressionDefinition method createPredicate.

public Predicate createPredicate(CamelContext camelContext) {
    if (predicate == null) {
        if (getExpressionType() != null) {
            predicate = getExpressionType().createPredicate(camelContext);
        } else if (getExpressionValue() != null) {
            predicate = new ExpressionToPredicateAdapter(getExpressionValue());
        } else if (getExpression() != null) {
            ObjectHelper.notNull("language", getLanguage());
            Language language = camelContext.resolveLanguage(getLanguage());
            if (language == null) {
                throw new NoSuchLanguageException(getLanguage());
            }
            String exp = getExpression();
            // should be true by default
            boolean isTrim = getTrim() == null || getTrim();
            // trim if configured to trim
            if (exp != null && isTrim) {
                exp = exp.trim();
            }
            // resolve the expression as it may be an external script from the classpath/file etc
            exp = ResourceHelper.resolveOptionalExternalScript(camelContext, exp);
            predicate = language.createPredicate(exp);
            configurePredicate(camelContext, predicate);
        }
    }
    return predicate;
}
Also used : Language(org.apache.camel.spi.Language) ExpressionToPredicateAdapter(org.apache.camel.util.ExpressionToPredicateAdapter) NoSuchLanguageException(org.apache.camel.NoSuchLanguageException)

Example 8 with Language

use of org.apache.camel.spi.Language in project camel by apache.

the class DefaultCamelContextResolverTest method testLanguageWithFallbackNames.

@Test
public void testLanguageWithFallbackNames() throws Exception {
    Language language = context.resolveLanguage("green");
    assertNotNull("Language not found in registry", language);
    assertTrue("Wrong instance type of the Language", language instanceof SampleLanguage);
    assertTrue("Here we should have the fallback Language", ((SampleLanguage) language).isFallback());
}
Also used : Language(org.apache.camel.spi.Language) Test(org.junit.Test)

Example 9 with Language

use of org.apache.camel.spi.Language in project camel by apache.

the class DefaultAnnotationExpressionFactory method createExpression.

public Expression createExpression(CamelContext camelContext, Annotation annotation, LanguageAnnotation languageAnnotation, Class<?> expressionReturnType) {
    String languageName = languageAnnotation.language();
    if (languageName == null) {
        throw new IllegalArgumentException("Cannot determine the language from the annotation: " + annotation);
    }
    Language language = camelContext.resolveLanguage(languageName);
    if (language == null) {
        throw new IllegalArgumentException("Cannot find the language: " + languageName + " on the classpath");
    }
    String expression = getExpressionFromAnnotation(annotation);
    if (expressionReturnType == Boolean.class || expressionReturnType == boolean.class) {
        Predicate predicate = language.createPredicate(expression);
        return PredicateToExpressionAdapter.toExpression(predicate);
    } else {
        return language.createExpression(expression);
    }
}
Also used : Language(org.apache.camel.spi.Language) Predicate(org.apache.camel.Predicate)

Example 10 with Language

use of org.apache.camel.spi.Language in project camel by apache.

the class XPathBuilder method createSimpleFunction.

private XPathFunction createSimpleFunction() {
    return new XPathFunction() {

        @SuppressWarnings("rawtypes")
        public Object evaluate(List list) throws XPathFunctionException {
            if (!list.isEmpty()) {
                Object value = list.get(0);
                if (value != null) {
                    String text = exchange.get().getContext().getTypeConverter().convertTo(String.class, value);
                    Language simple = exchange.get().getContext().resolveLanguage("simple");
                    Expression exp = simple.createExpression(text);
                    Object answer = exp.evaluate(exchange.get(), Object.class);
                    return answer;
                }
            }
            return null;
        }
    };
}
Also used : Language(org.apache.camel.spi.Language) XPathExpression(javax.xml.xpath.XPathExpression) Expression(org.apache.camel.Expression) XPathFunction(javax.xml.xpath.XPathFunction) NodeList(org.w3c.dom.NodeList) List(java.util.List)

Aggregations

Language (org.apache.camel.spi.Language)44 Expression (org.apache.camel.Expression)15 Test (org.junit.Test)13 Exchange (org.apache.camel.Exchange)6 CamelContext (org.apache.camel.CamelContext)5 Predicate (org.apache.camel.Predicate)5 NoSuchLanguageException (org.apache.camel.NoSuchLanguageException)4 SqlTest (org.apache.camel.builder.sql.SqlTest)4 DefaultExchange (org.apache.camel.impl.DefaultExchange)4 File (java.io.File)3 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)3 IOException (java.io.IOException)2 NoFactoryAvailableException (org.apache.camel.NoFactoryAvailableException)2 SimpleRegistry (org.apache.camel.impl.SimpleRegistry)2 LanguageResolver (org.apache.camel.spi.LanguageResolver)2 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1