public Object convertSourceToTargetClass(Object source, Class<?> targetClass) throws Exception { String targetStateId = (String) source; if (!StringUtils.hasText(targetStateId)) { return null; } ExpressionParser parser = flowBuilderContext.getExpressionParser(); Expression expression = parser.parseExpression(targetStateId, new FluentParserContext().template().evaluate(RequestContext.class).expectResult(String.class)); return new DefaultTargetStateResolver(expression); } }
/** * Create a new parser context, initially with all context attributes as null. Post construction, call one or more * of the fluent builder methods to configure this context. * @see #evaluate(Class) * @see #expectResult(Class) * @see #variable(ExpressionVariable) * @see #template() */ public FluentParserContext() { init(); }
private Expression parseFieldExpression(String field, boolean useResultTypeHint) { FluentParserContext parserContext = new FluentParserContext().evaluate(boundObject.getClass()); if (useResultTypeHint) { parserContext.expectResult(String.class); } return expressionParser.parseExpression(field, parserContext); }
/** * Hook method subclasses can override to return a specialized expression evaluating transition criteria * implementation. * @param encodedCriteria the encoded transition criteria expression * @param parser the parser that should parse the expression * @return the transition criteria object * @throws ConversionExecutionException when something goes wrong */ protected TransitionCriteria createBooleanExpressionTransitionCriteria(String encodedCriteria, ExpressionParser parser) throws ConversionExecutionException { Expression expression = parser.parseExpression(encodedCriteria, new FluentParserContext().template().evaluate(RequestContext.class)); return new DefaultTransitionCriteria(expression); } }
private DefaultMapping parseFlowInputMapping(InputModel input) { ExpressionParser parser = getLocalContext().getExpressionParser(); String name = input.getName(); String value = null; if (StringUtils.hasText(input.getValue())) { value = input.getValue(); } else { value = "flowScope." + name; } Expression source = parser.parseExpression(name, new FluentParserContext().evaluate(MutableAttributeMap.class)); Expression target = parser.parseExpression(value, new FluentParserContext().evaluate(RequestContext.class)); DefaultMapping mapping = new DefaultMapping(source, target); parseAndSetMappingConversionExecutor(input, mapping); parseAndSetMappingRequired(input, mapping); return mapping; }
/** * Create expression expression. * * @param expression the expression * @param expectedType the expected type * @return the expression */ public Expression createExpression(final String expression, final Class expectedType) { val parserContext = new FluentParserContext().expectResult(expectedType); return getSpringExpressionParser().parseExpression(expression, parserContext); }
public void testTemplateEnclosedCompositeNotSupported() { String exp = "${hello ${flag} ${flag} ${flag}}"; try { parser.parseExpression(exp, new FluentParserContext().template()); fail("Should've failed - not intended use"); } catch (ParserException e) { } }
public void testTemplateNestedVariables() { String expressionString = "#{value}#{max}"; Expression exp = parser.parseExpression( expressionString, new FluentParserContext().template().variable( new ExpressionVariable("max", "#{maximum}#{var}", new FluentParserContext().template() .variable(new ExpressionVariable("var", "'bar'"))))); TestBean target = new TestBean(); assertEquals("foo2bar", exp.getValue(target)); }
public void testVariablesWithCoersion() { Expression exp = parser.parseExpression("max", new FluentParserContext().variable(new ExpressionVariable("max", "maximum", new FluentParserContext().expectResult(Long.class)))); TestBean target = new TestBean(); assertEquals(2L, exp.getValue(target)); }
@Override public EvaluateAction createEvaluateAction(final String expression) { if (this.flowBuilderServices == null) { LOGGER.error("Flow builder services is not configured correctly."); return null; } val ctx = new FluentParserContext(); val action = this.flowBuilderServices.getExpressionParser().parseExpression(expression, ctx); val newAction = new EvaluateAction(action, null); LOGGER.trace("Created evaluate action for expression [{}]", action.getExpressionString()); return newAction; }
private DefaultMapping parseSubflowInputMapping(InputModel input) { ExpressionParser parser = getLocalContext().getExpressionParser(); String name = input.getName(); String value = null; if (StringUtils.hasText(input.getValue())) { value = input.getValue(); } else { value = name; } Expression source = parser.parseExpression(value, new FluentParserContext().evaluate(RequestContext.class)); Expression target = parser.parseExpression(name, new FluentParserContext().evaluate(MutableAttributeMap.class)); DefaultMapping mapping = new DefaultMapping(source, target); parseAndSetMappingConversionExecutor(input, mapping); parseAndSetMappingRequired(input, mapping); return mapping; }
private Action parseSetAction(SetModel set) { Expression nameExpression = getLocalContext().getExpressionParser().parseExpression(set.getName(), new FluentParserContext().evaluate(RequestContext.class)); FluentParserContext valueParserContext = new FluentParserContext().evaluate(RequestContext.class); if (StringUtils.hasText(set.getType())) { valueParserContext.expectResult(toClass(set.getType())); } Expression valueExpression = getLocalContext().getExpressionParser().parseExpression(set.getValue(), valueParserContext); return new SetAction(nameExpression, valueExpression); }
/** * Create expression expression. * * @param flow the flow * @param expression the expression * @param expectedType the expected type * @return the expression */ protected Expression createExpression(final Flow flow, final String expression, final Class expectedType) { final ParserContext parserContext = new FluentParserContext() .expectResult(expectedType); return getSpringExpressionParser().parseExpression(expression, parserContext); }
public void testParseTemplateExpression() { String expressionString = "text text text #{value} text text text#{value}"; Expression exp = parser.parseExpression(expressionString, new FluentParserContext().template()); TestBean target = new TestBean(); assertEquals("text text text foo text text textfoo", exp.getValue(target)); }
/** * Hook method subclasses can override to return a specialized expression evaluating transition criteria * implementation. * @param encodedCriteria the encoded transition criteria expression * @param parser the parser that should parse the expression * @return the transition criteria object * @throws ConversionExecutionException when something goes wrong */ protected TransitionCriteria createBooleanExpressionTransitionCriteria(String encodedCriteria, ExpressionParser parser) throws ConversionExecutionException { Expression expression = parser.parseExpression(encodedCriteria, new FluentParserContext().template().evaluate( RequestContext.class)); return new DefaultTransitionCriteria(expression); } }
public void testParseTemplateExpressionWithVariables() { String expressionString = "#{value}#{max}"; Expression exp = parser.parseExpression(expressionString, new FluentParserContext().template().variable(new ExpressionVariable("max", "maximum"))); TestBean target = new TestBean(); assertEquals("foo2", exp.getValue(target)); }
private Action parseRenderAction(RenderModel render) { String[] fragmentExpressionStrings = StringUtils.commaDelimitedListToStringArray(render.getFragments()); fragmentExpressionStrings = StringUtils.trimArrayElements(fragmentExpressionStrings); ParserContext context = new FluentParserContext().template().evaluate(RequestContext.class) .expectResult(String.class); Expression[] fragments = new Expression[fragmentExpressionStrings.length]; for (int i = 0; i < fragmentExpressionStrings.length; i++) { String fragment = fragmentExpressionStrings[i]; fragments[i] = getLocalContext().getExpressionParser().parseExpression(fragment, context); } return new RenderAction(fragments); }
private DefaultMapping parseFlowOutputMapping(OutputModel output) { ExpressionParser parser = getLocalContext().getExpressionParser(); String name = output.getName(); String value = null; if (StringUtils.hasText(output.getValue())) { value = output.getValue(); } else { value = name; } Expression source = parser.parseExpression(value, new FluentParserContext().evaluate(RequestContext.class)); Expression target = parser.parseExpression(name, new FluentParserContext().evaluate(MutableAttributeMap.class)); DefaultMapping mapping = new DefaultMapping(source, target); parseAndSetMappingConversionExecutor(output, mapping); parseAndSetMappingRequired(output, mapping); return mapping; }
private Action parseEvaluateAction(EvaluateModel evaluate) { FluentParserContext evaluateExpressionParserContext = new FluentParserContext().evaluate(RequestContext.class); if (StringUtils.hasText(evaluate.getResultType())) { evaluateExpressionParserContext.expectResult(toClass(evaluate.getResultType())); } Expression evaluateExpression = getLocalContext().getExpressionParser().parseExpression( evaluate.getExpression(), evaluateExpressionParserContext); Expression resultExpression = null; if (StringUtils.hasText(evaluate.getResult())) { resultExpression = getLocalContext().getExpressionParser().parseExpression(evaluate.getResult(), new FluentParserContext().evaluate(RequestContext.class)); } return new EvaluateAction(evaluateExpression, resultExpression); }
public void testParseSimpleEvalExpressionNoEvalContextWithTypeCoersion() { String expressionString = "3 + 4"; Expression exp = parser.parseExpression(expressionString, new FluentParserContext().expectResult(Long.class)); assertEquals(7L, exp.getValue(null)); }