use of org.eclipse.elk.core.validation.GraphValidationException in project elk by eclipse.
the class ElkUtil method applyVisitorsWithValidation.
/**
* Apply the given graph element visitors to the content of the given graph. If validators are involved
* and at least one error is found, a {@link GraphValidationException} is thrown.
*
* @param graph the graph the visitors shall be applied to.
* @param visitors the visitors to apply.
* @throws GraphValidationException if an error is found while validating the graph
*/
public static void applyVisitorsWithValidation(final ElkNode graph, final IGraphElementVisitor... visitors) throws GraphValidationException {
applyVisitors(graph, visitors);
// Gather validator results and generate an error message
List<GraphIssue> allIssues = null;
for (int i = 0; i < visitors.length; i++) {
if (visitors[i] instanceof IValidatingGraphElementVisitor) {
Collection<GraphIssue> issues = ((IValidatingGraphElementVisitor) visitors[i]).getIssues();
if (!issues.isEmpty()) {
if (allIssues == null) {
allIssues = new ArrayList<GraphIssue>(issues);
} else {
allIssues.addAll(issues);
}
}
}
}
// TODO print warnings to log if there are no errors
if (allIssues != null && allIssues.stream().anyMatch(issue -> issue.getSeverity() == GraphIssue.Severity.ERROR)) {
StringBuilder message = new StringBuilder();
for (GraphIssue issue : allIssues) {
if (message.length() > 0) {
message.append("\n");
}
message.append(issue.getSeverity()).append(": ").append(issue.getMessage()).append("\n\tat ");
ElkUtil.printElementPath(issue.getElement(), message);
}
throw new GraphValidationException(message.toString(), allIssues);
}
}
Aggregations