@Override public Object convert( Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) { try { if (currentExchange.get() == null) { throw new IllegalStateException("Current exchange has not been set for ExpressionMapper"); } Expression exp; // Resolve the language being used for this expression and evaluate Exchange exchange = currentExchange.get(); Language expLang = exchange.getContext().resolveLanguage(getLanguagePart()); String scheme = getSchemePart(); if (scheme != null && (scheme.equalsIgnoreCase("classpath") || scheme.equalsIgnoreCase("file") || scheme.equalsIgnoreCase("http"))) { String path = getPathPart(); try { exp = expLang.createExpression(resolveScript(scheme + ":" + path)); } catch (IOException e) { throw new IllegalStateException("Expression script specified but not found", e); } } else { exp = expLang.createExpression(getExpressionPart()); } return exp.evaluate(exchange, destinationClass); } finally { done(); } }
private Expression createFileLanguageExpression(String expression) { Language language; // only use file language if the name is complex (eg. using $) if (expression.contains("$")) { language = getCamelContext().resolveLanguage("file"); } else { language = getCamelContext().resolveLanguage("constant"); } return language.createExpression(expression); }
/** * Asserts that the given language name and expression evaluates to the given value on a specific * exchange */ protected void assertExpression( Exchange exchange, String languageName, String expressionText, Object expectedValue) { Language language = assertResolveLanguage(languageName); Expression expression = language.createExpression(expressionText); assertNotNull( "No Expression could be created for text: " + expressionText + " language: " + language, expression); assertExpression(expression, exchange, expectedValue); }