* @param model The model to validate.
* @throws ModelValidationException If any validation errors are encountered.
*/
protected void validate(EnunciateFreemarkerModel model) throws ModelValidationException {
debug("Validating the model...");
Messager messager = getMessager();
ValidatorChain validator = new ValidatorChain();
EnunciateConfiguration config = this.enunciate.getConfig();
Set<String> disabledRules = new TreeSet<String>(config.getDisabledRules());
if (this.enunciate.isModuleEnabled("rest")) {
//if the REST module is enabled, disable the validation rule that
//fails if the module is not enabled.
disabledRules.add("disabled.rest.module");
}
Validator coreValidator = config.getValidator();
if (coreValidator instanceof ConfigurableRules) {
((ConfigurableRules) coreValidator).disableRules(disabledRules);
}
validator.addValidator("core", coreValidator);
debug("Default validator added to the chain.");
for (DeploymentModule module : config.getEnabledModules()) {
Validator moduleValidator = module.getValidator();
if (moduleValidator != null) {
if (moduleValidator instanceof ConfigurableRules) {
((ConfigurableRules)moduleValidator).disableRules(disabledRules);
}
validator.addValidator(module.getName(), moduleValidator);
debug("Validator for module %s added to the chain.", module.getName());
}
}
if (!config.isAllowEmptyNamespace()) {
validator.addValidator("emptyns", new EmptyNamespaceValidator());
}
ValidationResult validationResult = validate(model, validator);
if (validationResult.hasWarnings()) {
warn("Validation result has warnings.");
for (ValidationMessage warning : validationResult.getWarnings()) {
if (!disabledRules.contains("all.warnings") && !disabledRules.contains(String.valueOf(warning.getLabel()) + ".warnings")) {
StringBuilder text = new StringBuilder();
if (warning.getLabel() != null) {
text.append('[').append(warning.getLabel()).append("] ");
}
text.append(warning.getText());
if (warning.getPosition() != null) {
messager.printWarning(warning.getPosition(), text.toString());
}
else {
messager.printWarning(text.toString());
}
}
}
}
if (validationResult.hasErrors()) {
warn("Validation result has errors.");
for (ValidationMessage error : validationResult.getErrors()) {
StringBuilder text = new StringBuilder();
if (error.getLabel() != null) {
text.append('[').append(error.getLabel()).append("] ");
}
text.append(error.getText());
if (error.getPosition() != null) {
messager.printError(error.getPosition(), text.toString());
}
else {
messager.printError(text.toString());
}
}
throw new ModelValidationException();
}