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;
}
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"));
}
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;
}
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;
}
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");
}
Aggregations