use of com.devonfw.cobigen.impl.exceptions.UnknownContextVariableException in project cobigen by devonfw.
the class Variables method resolveVariables.
/**
* Resolves all variables from the given {@code string}.
*
* @param string the string to resolve.
* @param pattern the {@link Pattern} with the variable syntax. The {@link Matcher#group(int) group(1)} has to match
* the actual variable name.
* @param supportCase {@code true} for the new case transformation by example, {@code false} otherwise.
* @param replacementForDot the character used as replacement for the dot character ('.') or '\0' for no replacement.
* @return the given {@code string} with all variables resolved.
*/
private String resolveVariables(String string, Pattern pattern, boolean supportCase, char replacementForDot) {
Matcher m = pattern.matcher(string.toString());
StringBuffer out = new StringBuffer();
while (m.find()) {
String variableKey = m.group(1);
if (!supportCase && (variableKey.startsWith(PREFIX_VARIABLES))) {
variableKey = variableKey.substring(PREFIX_VARIABLES.length());
}
// this is considered as the empty string but null instead of "" is required for free-marker
if (!containsKey(variableKey)) {
throw new UnknownContextVariableException(variableKey);
}
String variableValue = get(variableKey);
if (variableValue != null) {
boolean containsDot = variableValue.contains(".");
if (containsDot && (replacementForDot != '\0')) {
if (supportCase) {
variableValue = variableValue.replace('.', DUMMY_LETTER_FOR_DOT);
} else {
variableValue = variableValue.replace('.', replacementForDot);
}
}
if (supportCase) {
CaseSyntax syntax = CaseSyntax.ofExample(variableKey, true);
variableValue = syntax.convert(variableValue);
if (containsDot) {
variableValue = variableValue.replace(DUMMY_LETTER_FOR_DOT, replacementForDot);
}
} else {
variableValue = resolveFunction(variableValue, m.group(2));
}
m.appendReplacement(out, variableValue);
} else {
m.appendReplacement(out, "");
}
}
m.appendTail(out);
return out.toString();
}
use of com.devonfw.cobigen.impl.exceptions.UnknownContextVariableException in project cobigen by devonfw.
the class ConfigurationInterpreterImpl method resolveTemplateDestinationPath.
@Override
public Path resolveTemplateDestinationPath(Path targetRootPath, TemplateTo template, Object input) {
Trigger trigger = this.configurationHolder.readContextConfiguration().getTrigger(template.getTriggerId());
InputValidator.validateTrigger(trigger);
TriggerInterpreter triggerInterpreter = PluginRegistry.getTriggerInterpreter(trigger.getType());
// the GenerationReportTo won't be further processed
Variables variables = new ContextVariableResolver(input, trigger).resolveVariables(triggerInterpreter, new GenerationReportTo());
Template templateEty = this.configurationHolder.readTemplatesConfiguration(trigger).getTemplate(template.getId());
try {
String resolvedDestinationPath = new PathExpressionResolver(variables).evaluateExpressions(templateEty.getUnresolvedTargetPath());
return targetRootPath.resolve(resolvedDestinationPath).normalize();
} catch (UnknownContextVariableException e) {
throw new CobiGenRuntimeException("Could not resolve path '" + templateEty.getUnresolvedTargetPath() + "' for input '" + (input instanceof Object[] ? Arrays.toString((Object[]) input) : input.toString()) + "' and template '" + templateEty.getAbsoluteTemplatePath() + "'. Available variables: " + variables.toString());
}
}
Aggregations