Java Code Examples for org.springframework.expression.common.TemplateParserContext

The following examples show how to use org.springframework.expression.common.TemplateParserContext. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: spring-cloud-gray   Author: SpringCloud   File: ConfigurationUtils.java    License: Apache License 2.0 6 votes vote down vote up
private static Object getValue(SpelExpressionParser parser, BeanFactory beanFactory,
                               String entryValue) {
    Object value;
    String rawValue = entryValue;
    if (rawValue != null) {
        rawValue = rawValue.trim();
    }
    if (rawValue != null && rawValue.startsWith("#{") && entryValue.endsWith("}")) {
        // assume it's spel
        StandardEvaluationContext context = new StandardEvaluationContext();
        context.setBeanResolver(new BeanFactoryResolver(beanFactory));
        Expression expression = parser.parseExpression(entryValue,
                new TemplateParserContext());
        context.lookupVariable("api");
        value = expression.getValue(context);
    } else {
        value = entryValue;
    }
    return value;
}
 
Example #2
Source Project: konker-platform   Author: KonkerLabs   File: ExpressionEvaluationServiceImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public String evaluateTemplate(String expressionTemplate, Map<String, Object> evaluationContext) {
    Optional.ofNullable(expressionTemplate)
        .filter(template -> !template.isEmpty())
        .orElseThrow(() -> new IllegalArgumentException("Expression template cannot be null or empty"));

    Optional.ofNullable(evaluationContext)
        .orElseThrow(() -> new IllegalArgumentException("Evaluation context cannot be null"));

    Expression expression = new SpelExpressionParser().parseExpression(expressionTemplate
            ,new TemplateParserContext("@{","}"));

    StandardEvaluationContext standardEvaluationContext = new StandardEvaluationContext();
    try {
        standardEvaluationContext.registerFunction("urlEncode",
                Functions.class.getDeclaredMethod("urlEncode", new Class[] {String.class}));
    } catch (NoSuchMethodException e) {
        throw new EvaluationException("Fail to register function to evaluation context", e);
    }
    standardEvaluationContext.addPropertyAccessor(new MapAccessor());
    standardEvaluationContext.setVariables(evaluationContext);

    return expression.getValue(standardEvaluationContext,String.class);
}
 
Example #3
Source Project: spring-cloud-gateway   Author: spring-cloud   File: ShortcutConfigurable.java    License: Apache License 2.0 6 votes vote down vote up
static Object getValue(SpelExpressionParser parser, BeanFactory beanFactory,
		String entryValue) {
	Object value;
	String rawValue = entryValue;
	if (rawValue != null) {
		rawValue = rawValue.trim();
	}
	if (rawValue != null && rawValue.startsWith("#{") && entryValue.endsWith("}")) {
		// assume it's spel
		StandardEvaluationContext context = new StandardEvaluationContext();
		context.setBeanResolver(new BeanFactoryResolver(beanFactory));
		Expression expression = parser.parseExpression(entryValue,
				new TemplateParserContext());
		value = expression.getValue(context);
	}
	else {
		value = entryValue;
	}
	return value;
}
 
Example #4
Source Project: rice   Author: kuali   File: DefaultExpressionEvaluator.java    License: Educational Community License v2.0 6 votes vote down vote up
/**
 * Attempts to retrieve the {@link Expression} instance for the given expression template, if
 * not found one is created and added to the cache
 *
 * @param expressionTemplate template string for the expression
 * @return Expression instance
 */
protected Expression retrieveCachedExpression(String expressionTemplate) {
    Expression expression = null;

    // return from the expression from cache if present
    if (cachedExpressions.containsKey(expressionTemplate)) {
        return cachedExpressions.get(expressionTemplate);
    }

    // not in cache, create the expression object
    if (StringUtils.contains(expressionTemplate, UifConstants.EL_PLACEHOLDER_PREFIX)) {
        expression = parser.parseExpression(expressionTemplate, new TemplateParserContext(
                UifConstants.EL_PLACEHOLDER_PREFIX, UifConstants.EL_PLACEHOLDER_SUFFIX));
    } else {
        expression = parser.parseExpression(expressionTemplate);
    }

    synchronized (cachedExpressions) {
        cachedExpressions.put(expressionTemplate, expression);
    }

    return expression;
}
 
Example #5
Source Project: titus-control-plane   Author: Netflix   File: SpELMessageInterpolator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public String interpolate(String messageTemplate, Context context) {
    Expression expression = parser.parseExpression(messageTemplate, new TemplateParserContext());

    Object effectiveValue = context.getValidatedValue();
    if (context.getConstraintDescriptor().getAnnotation() instanceof FieldInvariant) {
        effectiveValue = new SpELFieldValidator.Root(effectiveValue);
    }
    return (String) expression.getValue(spelContextFactory.get(), effectiveValue);
}
 
Example #6
Source Project: springlets   Author: DISID   File: EntityExpressionSupport.java    License: Apache License 2.0 5 votes vote down vote up
public EntityExpressionSupport(ExpressionParser parser,
    TemplateParserContext templateParserContext, ConversionService conversionService,
    String defaultExpression) {
  this.parser = parser;
  this.templateParserContext = templateParserContext;
  this.defaultExpression = defaultExpression;
  this.conversionService = conversionService;
}
 
Example #7
Source Project: springlets   Author: DISID   File: Select2DataWithConversion.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create a response for select2 with data obtained from a request.
 *
 * @param page the data to show
 * @param idExpression the SpEl expression for the id field
 * @param conversionService to convert the data to String
 */
public Select2DataWithConversion(Page<T> page, String idExpression,
    ConversionService conversionService) {
  super(page);
  Assert.notNull(page, "A ConversionService is required");

  this.conversionService = conversionService;
  TemplateParserContext templateParserContext = new TemplateParserContext();
  ExpressionParser parser = new SpelExpressionParser();
  parseIdExpression = parser.parseExpression(idExpression, templateParserContext);
  // By default, the entire element will not be included in the response
  this.includeEntireElement = false;
}
 
Example #8
Source Project: spring-rest-exception-handler   Author: jirutka   File: SpelMessageInterpolator.java    License: Apache License 2.0 5 votes vote down vote up
public String interpolate(String messageTemplate, Map<String, Object> variables) {
    Assert.notNull(messageTemplate, "messageTemplate must not be null");

    try {
        Expression expression = parser().parseExpression(messageTemplate, new TemplateParserContext());

        return expression.getValue(evalContext, variables, String.class);

    } catch (ExpressionException ex) {
        LOG.error("Failed to interpolate message template: {}", messageTemplate, ex);
        return "";
    }
}
 
Example #9
Source Project: springlets   Author: DISID   File: EntityExpressionSupport.java    License: Apache License 2.0 4 votes vote down vote up
public EntityExpressionSupport(ExpressionParser parser,
    TemplateParserContext templateParserContext) {
  this(parser, templateParserContext, null, "#{toString()}");
}
 
Example #10
Source Project: springlets   Author: DISID   File: EntityExpressionSupport.java    License: Apache License 2.0 4 votes vote down vote up
public EntityExpressionSupport(ExpressionParser parser,
    TemplateParserContext templateParserContext, ConversionService conversionService) {
  this(parser, templateParserContext, conversionService, "#{toString()}");
}
 
Example #11
Source Project: springlets   Author: DISID   File: EntityExpressionSupport.java    License: Apache License 2.0 4 votes vote down vote up
public EntityExpressionSupport(ExpressionParser parser,
    TemplateParserContext templateParserContext, String defaultExpression) {
  this(parser, templateParserContext, null, defaultExpression);
}
 
Example #12
Source Project: springlets   Author: DISID   File: EntityMessagePrinter.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new instance with the given expression and expression parser.
 * @param messageCode the code of the message with the SpEL expression 
 *        to generate a String from the provided objects
 * @param messageSource to get the SpEL expression related to a given {@link Locale}
 * @param parser to parse the expression
 * @param templateParserContext context to use to parse the expression
 * @param defaultExpression expression to use if the expression to use in the conversion is empty
 */
public EntityMessagePrinter(String messageCode, MessageSource messageSource,
    ExpressionParser parser, TemplateParserContext templateParserContext,
    String defaultExpression) {
  super(parser, templateParserContext, defaultExpression);
  this.messageCode = messageCode;
  this.messageSource = messageSource;
}
 
Example #13
Source Project: springlets   Author: DISID   File: EntityMessagePrinter.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new instance with the given expression and expression parser.
 * @param messageCode the code of the message with the SpEL expression 
 *        to generate a String from the provided objects
 * @param messageSource to get the SpEL expression related to a given {@link Locale}
 * @param parser to parse the expression
 * @param templateParserContext context to use to parse the expression
 * @param conversionService the ConversionService to use to convert property values 
 * inside the expression
 * @param defaultExpression expression to use if the expression to use in the conversion is empty
 */
public EntityMessagePrinter(String messageCode, MessageSource messageSource,
    ExpressionParser parser, TemplateParserContext templateParserContext,
    ConversionService conversionService,
    String defaultExpression) {
  super(parser, templateParserContext, conversionService, defaultExpression);
  this.messageCode = messageCode;
  this.messageSource = messageSource;
}
 
Example #14
Source Project: springlets   Author: DISID   File: EntityToStringConverter.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new converter 
 * @param parser to parse the expression
 * @param templateParserContext context to use to parse the expression
 * @param messageSource to get the SpEL expression related to a given {@link Locale}
 * @param conversionService the ConversionService to use to convert property values 
 * inside the expression
 */
public EntityToStringConverter(ExpressionParser parser,
    TemplateParserContext templateParserContext, MessageSource messageSource,
    ConversionService conversionService) {
  super(parser, templateParserContext, conversionService);
  this.messageSource = messageSource;
}
 
Example #15
Source Project: springlets   Author: DISID   File: EntityPrinter.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new instance with the given expression and expression parser.
 * @param expression expression to generate a String from the provided objects
 * @param parser to parse the expression
 * @param templateParserContext context to use to parse the expression
 * @param conversionService the ConversionService to use to convert property values 
 * inside the expression
 * @param defaultExpression expression to use if the expression to use in the conversion is empty
 */
public EntityPrinter(String expression, ExpressionParser parser,
    TemplateParserContext templateParserContext, ConversionService conversionService,
    String defaultExpression) {
  super(parser, templateParserContext, conversionService, defaultExpression);
  this.expression = expression;
}
 
Example #16
Source Project: springlets   Author: DISID   File: Select2Data.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Create a response for select2 with data obtained from a request.
 * Uses SpEL expression templates 
 * (http://docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html#expressions-templating)
 * to create the select2 data properties
 * ('id' and 'text') from the attributes of the data bean to return  
 *
 * @param page the data to show
 * @param idExpression the SpEl expression for the id field
 * @param textExpression the SpEl expression for the text field
 */
public Select2Data(Page<T> page, String idExpression, String textExpression) {
  super(page);

  TemplateParserContext templateParserContext = new TemplateParserContext();
  ExpressionParser parser = new SpelExpressionParser();
  parseIdExpression = parser.parseExpression(idExpression, templateParserContext);
  parseTextExpression = parser.parseExpression(textExpression, templateParserContext);
}
 
Example #17
Source Project: springlets   Author: DISID   File: EntityToStringConverter.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new converter 
 * @param parser to parse the expression
 * @param templateParserContext context to use to parse the expression
 * @param messageSource to get the SpEL expression related to a given {@link Locale}
 * inside the expression
 */
public EntityToStringConverter(ExpressionParser parser,
    TemplateParserContext templateParserContext, MessageSource messageSource) {
  super(parser, templateParserContext);
  this.messageSource = messageSource;
}
 
Example #18
Source Project: springlets   Author: DISID   File: AbstractEntityPrinter.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new instance with the given expression parser.
 * @param parser to parse the expression
 * @param templateParserContext context to use to parse the expression
 * @param conversionService the ConversionService to use to convert property values 
 * inside the expression
 * @param defaultExpression expression to use if the expression to use in the conversion is empty
 */
public AbstractEntityPrinter(ExpressionParser parser, TemplateParserContext templateParserContext,
    ConversionService conversionService, String defaultExpression) {
  super(parser, templateParserContext, conversionService, defaultExpression);
}
 
Example #19
Source Project: springlets   Author: DISID   File: AbstractEntityPrinter.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new instance with the given expression parser.
 * @param parser to parse the expression
 * @param templateParserContext context to use to parse the expression
 * @param defaultExpression expression to use if the expression to use in the conversion is empty
 */
public AbstractEntityPrinter(ExpressionParser parser, TemplateParserContext templateParserContext,
    String defaultExpression) {
  super(parser, templateParserContext, defaultExpression);
}
 
Example #20
Source Project: springlets   Author: DISID   File: EntityPrinter.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new instance with the given expression and expression parser.
 * @param expression expression to generate a String from the provided objects
 * @param parser to parse the expression
 * @param templateParserContext context to use to parse the expression
 * @param defaultExpression expression to use if the expression to use in the conversion is empty
 */
public EntityPrinter(String expression, ExpressionParser parser,
    TemplateParserContext templateParserContext, String defaultExpression) {
  super(parser, templateParserContext, defaultExpression);
  this.expression = expression;
}
 
Example #21
Source Project: springlets   Author: DISID   File: EntityExpressionSupport.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Returns the {@link TemplateParserContext} to use to parse the expression.
 * @return the templateParserContext
 */
private TemplateParserContext getTemplateParserContext() {
  return templateParserContext;
}