Search in sources :

Example 1 with VariableAssignmentTo

use of com.devonfw.cobigen.api.to.VariableAssignmentTo in project cobigen by devonfw.

the class TypeScriptMatcher method getResolvedVariables.

/**
 * Resolves all variables for this trigger
 *
 * @param matcherType matcher type
 * @param matcherValue matcher value
 * @param stringToMatch String to match
 * @param variableAssignments variable assigments to be resolved
 * @return a {@link Map} from variable name to the resolved value
 * @throws InvalidConfigurationException if some of the matcher type and variable type combinations are not supported
 * @author mbrunnli (15.04.2013)
 */
public Map<String, String> getResolvedVariables(MatcherType matcherType, String matcherValue, String stringToMatch, List<VariableAssignmentTo> variableAssignments) throws InvalidConfigurationException {
    Map<String, String> resolvedVariables = new HashMap<>();
    for (VariableAssignmentTo va : variableAssignments) {
        VariableType variableType = Enum.valueOf(VariableType.class, va.getType().toUpperCase());
        switch(variableType) {
            case CONSTANT:
                resolvedVariables.put(va.getVarName(), va.getValue());
                break;
            case REGEX:
                String resolvedRegexValue = resolveRegexValue(matcherType, matcherValue, stringToMatch, va);
                resolvedVariables.put(va.getVarName(), resolvedRegexValue != null ? resolvedRegexValue : "");
                break;
        }
    }
    return resolvedVariables;
}
Also used : VariableAssignmentTo(com.devonfw.cobigen.api.to.VariableAssignmentTo) HashMap(java.util.HashMap)

Example 2 with VariableAssignmentTo

use of com.devonfw.cobigen.api.to.VariableAssignmentTo in project cobigen by devonfw.

the class OpenAPIMatcherTest method testMissingXRootPackageVariableNotMandatory.

/**
 * Test if the generation is successful and the report contains warnings, if a requested but not mandatory variable
 * isn't given.
 */
@Test
public void testMissingXRootPackageVariableNotMandatory() {
    ComponentDef componentDef = new ComponentDef();
    componentDef.setName("Tablemanagement");
    OpenAPIMatcher matcher = new OpenAPIMatcher();
    GenerationReportTo report = new GenerationReportTo();
    List<VariableAssignmentTo> vaOptionalXRootPackage = new ArrayList<>();
    vaOptionalXRootPackage.add(new VariableAssignmentTo("extension", "rootPackage", "x-rootpackage", false));
    matcher.resolveVariables(new MatcherTo("element", "ComponentDef", componentDef), vaOptionalXRootPackage, report);
    assertThat(report.getWarnings().get(0)).containsSequence(Constants.getMandatoryMessage(false, "x-rootpackage"));
    List<VariableAssignmentTo> vaMandatoryXRootPackage = new ArrayList<>();
    vaMandatoryXRootPackage.add(new VariableAssignmentTo("extension", "rootpackage", "x-rootpackage", true));
    matcher.resolveVariables(new MatcherTo("element", "ComponentDef", componentDef), vaMandatoryXRootPackage, report);
    assertThat(report.getErrors().get(0).getMessage()).containsSequence(Constants.getMandatoryMessage(true, "x-rootpackage"));
}
Also used : ComponentDef(com.devonfw.cobigen.openapiplugin.model.ComponentDef) VariableAssignmentTo(com.devonfw.cobigen.api.to.VariableAssignmentTo) GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) OpenAPIMatcher(com.devonfw.cobigen.openapiplugin.matcher.OpenAPIMatcher) ArrayList(java.util.ArrayList) MatcherTo(com.devonfw.cobigen.api.to.MatcherTo) Test(org.junit.Test)

Example 3 with VariableAssignmentTo

use of com.devonfw.cobigen.api.to.VariableAssignmentTo in project cobigen by devonfw.

the class OpenAPIMatcher method resolveVariables.

@Override
public Map<String, String> resolveVariables(MatcherTo matcher, List<VariableAssignmentTo> variableAssignments, GenerationReportTo report) throws InvalidConfigurationException {
    Map<String, String> resolvedVariables = new HashMap<>();
    VariableType variableType = null;
    for (VariableAssignmentTo va : variableAssignments) {
        try {
            variableType = Enum.valueOf(VariableType.class, va.getType().toUpperCase());
        } catch (InvalidConfigurationException e) {
            throw new CobiGenRuntimeException("Matcher or VariableAssignment type " + matcher.getType() + " not registered!", e);
        }
        switch(variableType) {
            case CONSTANT:
                resolvedVariables.put(va.getVarName(), va.getValue());
                break;
            case EXTENSION:
                Class<?> targetObject = matcher.getTarget().getClass();
                try {
                    Field field = targetObject.getDeclaredField("extensionProperties");
                    field.setAccessible(true);
                    Object extensionProperties = field.get(matcher.getTarget());
                    String attributeValue = getExtendedProperty((Map<String, Object>) extensionProperties, va.getValue());
                    resolvedVariables.put(va.getVarName(), attributeValue);
                } catch (NoSuchFieldException | SecurityException e) {
                    if (va.isMandatory()) {
                        String errorMessage = Constants.getMandatoryMessage(true, va.getValue());
                        report.addError(new CobiGenRuntimeException(errorMessage));
                        LOG.error(errorMessage);
                    } else {
                        String warningMessage = Constants.getMandatoryMessage(false, va.getValue());
                        report.addWarning(warningMessage);
                        resolvedVariables.put(va.getVarName(), "");
                        LOG.warn(warningMessage);
                    }
                } catch (IllegalArgumentException | IllegalAccessException e) {
                    throw new CobiGenRuntimeException("This is a programming error, please report an issue on github", e);
                }
                break;
            case PROPERTY:
                Class<?> target = matcher.getTarget().getClass();
                try {
                    Field field = target.getDeclaredField(va.getValue());
                    field.setAccessible(true);
                    Object o = field.get(matcher.getTarget());
                    resolvedVariables.put(va.getVarName(), o.toString());
                } catch (NoSuchFieldException | SecurityException e) {
                    LOG.warn("The property {} was requested in a variable assignment although the input does not provide this property. Setting it to empty", matcher.getValue());
                } catch (IllegalArgumentException | IllegalAccessException e) {
                    throw new CobiGenRuntimeException("This is a programming error, please report an issue on github", e);
                }
                break;
        }
    }
    return resolvedVariables;
}
Also used : VariableAssignmentTo(com.devonfw.cobigen.api.to.VariableAssignmentTo) CobiGenRuntimeException(com.devonfw.cobigen.api.exception.CobiGenRuntimeException) HashMap(java.util.HashMap) InvalidConfigurationException(com.devonfw.cobigen.api.exception.InvalidConfigurationException) Field(java.lang.reflect.Field)

Example 4 with VariableAssignmentTo

use of com.devonfw.cobigen.api.to.VariableAssignmentTo in project cobigen by devonfw.

the class JavaMatcher method getResolvedVariables.

/**
 * Resolves all variables for this trigger
 *
 * @param matcherType matcher type
 * @param matcherValue matcher value
 * @param stringToMatch String to match
 * @param variableAssignments variable assigments to be resolved
 * @return a {@link Map} from variable name to the resolved value
 * @throws InvalidConfigurationException if some of the matcher type and variable type combinations are not supported
 * @author mbrunnli (15.04.2013)
 */
public Map<String, String> getResolvedVariables(MatcherType matcherType, String matcherValue, String stringToMatch, List<VariableAssignmentTo> variableAssignments) throws InvalidConfigurationException {
    Map<String, String> resolvedVariables = new HashMap<>();
    for (VariableAssignmentTo va : variableAssignments) {
        VariableType variableType = Enum.valueOf(VariableType.class, va.getType().toUpperCase());
        switch(variableType) {
            case CONSTANT:
                resolvedVariables.put(va.getVarName(), va.getValue());
                break;
            case REGEX:
                String resolvedRegexValue = resolveRegexValue(matcherType, matcherValue, stringToMatch, va);
                resolvedVariables.put(va.getVarName(), resolvedRegexValue != null ? resolvedRegexValue : "");
                break;
        }
    }
    return resolvedVariables;
}
Also used : VariableAssignmentTo(com.devonfw.cobigen.api.to.VariableAssignmentTo) HashMap(java.util.HashMap)

Example 5 with VariableAssignmentTo

use of com.devonfw.cobigen.api.to.VariableAssignmentTo in project cobigen by devonfw.

the class JavaMatcherTests method resolveNonEmptyRegexVariable.

/**
 * Test method for
 * {@link com.devonfw.cobigen.javaplugin.matcher.JavaMatcher#getResolvedVariables(com.devonfw.cobigen.javaplugin.matcher.JavaMatcher.MatcherType, java.lang.String, java.lang.String, java.util.List)}
 * . tests if the algorithm handles non empty variables correctly (control-test to
 * {@link #resolveEmptyRegexVariable()})
 */
@SuppressWarnings("javadoc")
@Test
public void resolveNonEmptyRegexVariable() {
    List<VariableAssignmentTo> variables = new LinkedList<>();
    VariableAssignmentTo rootPackage = new VariableAssignmentTo("regex", "rootPackage", "1");
    VariableAssignmentTo domain = new VariableAssignmentTo("regex", "domain", "3");
    VariableAssignmentTo component = new VariableAssignmentTo("regex", "component", "4");
    VariableAssignmentTo detail = new VariableAssignmentTo("regex", "detail", "5");
    VariableAssignmentTo typeName = new VariableAssignmentTo("regex", "typeName", "6");
    variables.add(rootPackage);
    variables.add(domain);
    variables.add(component);
    variables.add(detail);
    variables.add(typeName);
    String inputValue = "de.tukl.abc.project.standard.datatype.common.api.subpackage.LongText";
    String regex = "((.+\\.)?([^.]+))\\.(datatype)\\.common\\.api(\\..*)?\\.([^.]+)";
    Map<String, String> result = this.javaMatcher.getResolvedVariables(null, regex, inputValue, variables);
    assertThat(result.get("detail")).as("value of detail").isEqualTo(".subpackage");
}
Also used : VariableAssignmentTo(com.devonfw.cobigen.api.to.VariableAssignmentTo) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

VariableAssignmentTo (com.devonfw.cobigen.api.to.VariableAssignmentTo)9 Test (org.junit.Test)5 GenerationReportTo (com.devonfw.cobigen.api.to.GenerationReportTo)3 MatcherTo (com.devonfw.cobigen.api.to.MatcherTo)3 OpenAPIMatcher (com.devonfw.cobigen.openapiplugin.matcher.OpenAPIMatcher)3 ComponentDef (com.devonfw.cobigen.openapiplugin.model.ComponentDef)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 LinkedList (java.util.LinkedList)2 CobiGenRuntimeException (com.devonfw.cobigen.api.exception.CobiGenRuntimeException)1 InvalidConfigurationException (com.devonfw.cobigen.api.exception.InvalidConfigurationException)1 Field (java.lang.reflect.Field)1