@Inject public FormatStringDecorator(@Assisted Decorator decorator, Engine templateEngine) { final String formatString = (String) requireNonNull(decorator.config().get(CK_FORMAT_STRING), CK_FORMAT_STRING + " cannot be null"); this.targetField = (String) requireNonNull(decorator.config().get(CK_TARGET_FIELD), CK_TARGET_FIELD + " cannot be null"); requireAllFields = (boolean) requireNonNull(decorator.config().get(CK_REQUIRE_ALL_FIELDS), CK_REQUIRE_ALL_FIELDS + " cannot be null"); template = requireNonNull(templateEngine, "templateEngine").getTemplate(formatString); usedVariables = template.getUsedVariableDescriptions(); }
@Override public SearchResponse apply(SearchResponse searchResponse) { final List<ResultMessageSummary> summaries = searchResponse.messages().stream() .map(summary -> { if (requireAllFields && !usedVariables.stream().allMatch(variable -> summary.message().containsKey(variable.name))) { return summary; } final String formattedString = template.transform(summary.message(), Locale.ENGLISH); if (formattedString == null) { return summary; } final Message message = new Message(ImmutableMap.copyOf(summary.message())); message.addField(targetField, formattedString); return summary.toBuilder().message(message.getFields()).build(); }) .collect(Collectors.toList()); return searchResponse.toBuilder().messages(summaries).build(); } }
/** * Gets a template for a certain source. * * @param template * the template source * @param sourceName * the template name * @return the prepared template */ public Template getTemplate(String template, String sourceName) { return new InterpretedTemplate(template, sourceName, this); }
/** * Transforms a template into an expanded output using the given model. * * @param model * the model used to evaluate expressions inside the template * @return the expanded output */ public String transform(Map<String, Object> model, Locale locale) { return transform(model, locale, engine.getModelAdaptor(), null); }
templateImpl = compiledTemplates.get(template); if (templateImpl == null) { templateImpl = compiler.compile(template, sourceName, this); compiledTemplates.put(template, templateImpl); templateImpl = interpretedTemplates.get(template); if (templateImpl == null) { templateImpl = new InterpretedTemplate(template, sourceName, this); interpretedTemplates.put(template, templateImpl); templateImpl = new InterpretedTemplate(template, sourceName, this);
/** * Gets all variables used in the given template. */ public synchronized Set<String> getUsedVariables(String template) { Template templateImpl = getTemplate(template, null); return templateImpl.getUsedVariables(); }
protected void addSpecialVariables(ForEachToken feToken, Map<String, Object> model) { String suffix = feToken.getVarName(); addSpecialVariables(feToken, model, suffix); // special _it variable as an alias for run variable in inner loop model.put(SPECIAL_ITERATOR_VARIABLE, model.get(feToken.getVarName())); addSpecialVariables(feToken, model, SPECIAL_ITERATOR_VARIABLE); }
@Override public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + (renderer != null ? renderer.hashCode() : 0); result = 31 * result + (parameters != null ? parameters.hashCode() : 0); result = 31 * result + (context != null ? context.hashCode() : 0); return result; } }
@Override @SuppressWarnings("unchecked") public synchronized Set<String> getUsedVariables() { if (this.usedVariables != null) { return this.usedVariables; } this.usedVariables = new TreeSet<String>(); final List<VariableDescription> variableDescriptions = this.getUsedVariableDescriptions(); for (VariableDescription variableDescription : variableDescriptions) { this.usedVariables.add(variableDescription.name); } return this.usedVariables; }
public synchronized List<ErrorEntry> getStaticErrors(String template) { Template templateImpl = getTemplate(template, null); return templateImpl.getStaticErrors(); }
@Override public void log(TemplateContext context, Token token, Action action) { if (token instanceof ExpressionToken) { String variable = ((ExpressionToken) token).getExpression(); if (!isLocal(variable)) { usedVariables.add(variable); } } }
@Override public synchronized String transform(Map<String, Object> model, Locale locale, ModelAdaptor modelAdaptor, ProcessListener processListener) { TemplateContext context = new TemplateContext(template, locale, sourceName, new ScopedMap(model), modelAdaptor, engine, engine.getErrorHandler(), processListener); String transformed = transformCompiled(context); return transformed; }
String transformInternal(String template, Locale locale, String sourceName, Map<String, Object> model, ModelAdaptor modelAdaptor, ProcessListener processListener) { Template templateImpl = getTemplate(template, sourceName); String output = templateImpl.transform(model, locale, modelAdaptor, processListener); return output; }
/** * Transforms a template into an expanded output using the given model. * * @param model * the model used to evaluate expressions inside the template * @return the expanded output */ public String transform(Map<String, Object> model, Locale locale, ProcessListener processListener) { return transform(model, locale, engine.getModelAdaptor(), processListener); }
/** * Gets all variables used in the given template as a detailed description. * */ public synchronized List<VariableDescription> getUsedVariableDescriptions(String template) { Template templateImpl = getTemplate(template, null); return templateImpl.getUsedVariableDescriptions(); }
/** * Gets all variables used in the given template. * * @deprecated use {@link #getUsedVariableDescriptions(String)} instead */ @Deprecated() public synchronized Set<String> getUsedVariables(String template) { Template templateImpl = getTemplate(template, null); return templateImpl.getUsedVariables(); }
String transformInternal(String template, Locale locale, String sourceName, Map<String, Object> model, ModelAdaptor modelAdaptor, ProcessListener processListener) { if (template == null) { return null; } Template templateImpl = getTemplate(template, sourceName); String output = templateImpl.transform(model, locale, modelAdaptor, processListener); return output; }
/** * Transforms a template into an expanded output using the given model. * * @param model * the model used to evaluate expressions inside the template * @return the expanded output */ public String transform(Map<String, Object> model, Locale locale, ProcessListener processListener) { return transform(model, locale, engine.getModelAdaptor(), processListener); }
/** * Transforms a template into an expanded output using the given model. * * @param model * the model used to evaluate expressions inside the template * @return the expanded output */ public String transform(Map<String, Object> model, Locale locale) { return transform(model, locale, engine.getModelAdaptor(), null); }