/** * Replaces each occurence of ${SOME_VALUE} with the varible SOME_VALUE in 'variables' * If any error occures during parsing, i.e: variable doesn't exist, bad syntax, etc... * a {@link PlaceholderResolutionException} is thrown, otherwise the new string after all replacements have been * made will be returned. * * @param variables The variables map to match placeholders against * @param value the string, possibly containing placeholders * @return the value after placeholder replacement have been made * @throws PlaceholderResolutionException if an empty placeholder is found * or a place holder with no suitable value in 'variables' */ public static String replacePlaceholders(final Map<String, String> variables, final String value) throws PlaceholderResolutionException { PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${","}"); return helper.replacePlaceholders(value, new PlaceholderResolver() { public String resolvePlaceholder(String key) { if (key.isEmpty()) { throw new PlaceholderResolutionException("Placeholder in '" + value + "' has to have a length of at least 1"); } String result = variables.get(key); if (result == null) { throw new PlaceholderResolutionException("Missing value for placeholder: '" + key + "' in '" + value + "'"); } return result; } }); }
public StaticStringValueResolver(final Map<String, String> values) { this.helper = new PropertyPlaceholderHelper("${", "}", ":", false); this.resolver = new PlaceholderResolver() { @Override public String resolvePlaceholder(String placeholderName) { return values.get(placeholderName); } }; }
@Override public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception { if (response.getContentType() == null) { response.setContentType(getContentType()); } Map<String, Object> map = new HashMap<String, Object>(model); map.put("path", request.getContextPath()); PlaceholderResolver resolver = new ExpressionResolver(getExpressions(), map); String result = this.helper.replacePlaceholders(this.template, resolver); response.getWriter().append(result); }
@Override public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception { if (response.getContentType() == null) { response.setContentType(getContentType()); } Map<String, Object> map = new HashMap<String, Object>(model); map.put("path", request.getContextPath()); PlaceholderResolver resolver = new ExpressionResolver(this.expressions, map); String result = this.helper.replacePlaceholders(this.template, resolver); response.getWriter().append(result); }
public SpelView(String template) { this.template = template; this.context.addPropertyAccessor(new MapAccessor()); this.helper = new PropertyPlaceholderHelper("${", "}"); this.resolver = new PlaceholderResolver() { public String resolvePlaceholder(String name) { Expression expression = parser.parseExpression(name); Object value = expression.getValue(context); return value==null ? null : value.toString(); } }; }
/** * replace placeholder in properties file with value; * @param placeholders placeholder to replace */ public final void replacePlaceholders(Map<String, String> placeholders) { PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders); PlaceholderResolver resolver = new PlaceholderConfigurerResolver(placeholders); for (Object key : this.properties.keySet()) { Object val = this.properties.get(key); this.properties.setProperty(key.toString(), helper.replacePlaceholders(val.toString(), resolver)); } }
@SuppressWarnings("unchecked") private PlaceholderResolver initVarResolver(MessageHeaders headers) { String name = DestinationVariableMethodArgumentResolver.DESTINATION_TEMPLATE_VARIABLES_HEADER; Map<String, String> vars = (Map<String, String>) headers.get(name); return new DestinationVariablePlaceholderResolver(vars); }
/** * Parse the given String value for placeholder resolution. * @param strVal the String value to parse * @param props the Properties to resolve placeholders against * @param visitedPlaceholders the placeholders that have already been visited * during the current resolution attempt (ignored in this version of the code) * @deprecated as of Spring 3.0, in favor of using {@link #resolvePlaceholder} * with {@link org.springframework.util.PropertyPlaceholderHelper}. * Only retained for compatibility with Spring 2.5 extensions. */ @Deprecated protected String parseStringValue(String strVal, Properties props, Set<?> visitedPlaceholders) { PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper( placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders); PlaceholderResolver resolver = new PropertyPlaceholderConfigurerResolver(props); return helper.replacePlaceholders(strVal, resolver); }