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() : "");
}
};
}
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;
}
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());
}
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);
}
}
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;
}
};
}
Aggregations