Search in sources :

Example 1 with UnknownContextVariableException

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();
}
Also used : CaseSyntax(net.sf.mmm.util.lang.api.CaseSyntax) Matcher(java.util.regex.Matcher) UnknownContextVariableException(com.devonfw.cobigen.impl.exceptions.UnknownContextVariableException)

Example 2 with UnknownContextVariableException

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());
    }
}
Also used : TriggerInterpreter(com.devonfw.cobigen.api.extension.TriggerInterpreter) Variables(com.devonfw.cobigen.impl.config.entity.Variables) Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) CobiGenRuntimeException(com.devonfw.cobigen.api.exception.CobiGenRuntimeException) ContextVariableResolver(com.devonfw.cobigen.impl.model.ContextVariableResolver) PathExpressionResolver(com.devonfw.cobigen.impl.config.resolver.PathExpressionResolver) UnknownContextVariableException(com.devonfw.cobigen.impl.exceptions.UnknownContextVariableException) Template(com.devonfw.cobigen.impl.config.entity.Template)

Aggregations

UnknownContextVariableException (com.devonfw.cobigen.impl.exceptions.UnknownContextVariableException)2 CobiGenRuntimeException (com.devonfw.cobigen.api.exception.CobiGenRuntimeException)1 TriggerInterpreter (com.devonfw.cobigen.api.extension.TriggerInterpreter)1 GenerationReportTo (com.devonfw.cobigen.api.to.GenerationReportTo)1 Template (com.devonfw.cobigen.impl.config.entity.Template)1 Trigger (com.devonfw.cobigen.impl.config.entity.Trigger)1 Variables (com.devonfw.cobigen.impl.config.entity.Variables)1 PathExpressionResolver (com.devonfw.cobigen.impl.config.resolver.PathExpressionResolver)1 ContextVariableResolver (com.devonfw.cobigen.impl.model.ContextVariableResolver)1 Matcher (java.util.regex.Matcher)1 CaseSyntax (net.sf.mmm.util.lang.api.CaseSyntax)1