@Override public HandledResult handle(ValidationContext ctx, ValidationError error) { final String el = error.element().getLocalPart(); final Set<String> alts = error.alternatives(); final Location loc = error.location(); final Position pos = ctx.searchBackward(loc.getLineNumber() - 1, loc.getColumnNumber(), Pattern.compile(String.format("<%s[ >/]", el))); final HandledResult result = HandledResult.from(error) .addPrimaryMessage(this.primaryMessageKey, el); if (pos != null) { result.line(pos.line).column(pos.col); } if (!alts.isEmpty()) { final List<SchemaElement> path = ctx.mapDocLocationToSchemaPath(error.element(), error.position()); if (path.isEmpty()) { result.possiblyMalformed(true); } Set<String> otherElements = ctx.elementsForElement(path).stream() .map(SchemaElement::name) .collect(Collectors.toSet()); result.addPrimaryMessage(this.optionsMessageKey, Util.asSortedList(alts).stream() .map(String::toLowerCase) .map(Util.possiblyUnderscoredName(otherElements)) .collect(Collectors.toList())); } return result; }
private void formatResult(final StringBuilder out, final ErrorHandler.HandledResult result) { final int linum = result.line(); final int maxLinumWidth = ("" + linum + CONTEXT_LINES).length(); final List<PrefixedLine> preambleLines = preambleLines(linum, maxLinumWidth); final List<PrefixedLine> postambleLines = postambleLines(linum, maxLinumWidth); final List<PrefixedLine> allLines = new ArrayList<>(); allLines.addAll(preambleLines); allLines.addAll(postambleLines); final int removeSpaces = smallestPrefixWhitespace(allLines); out.append('\n') .append(Util.withPrefix(" ", ambleString(preambleLines, removeSpaces))) .append(alignPointerMessage(maxLinumWidth + result.column() + 2 - removeSpaces, result.primaryMessages())) .append("\n") .append(Util.withPrefix(" ", ambleString(postambleLines, removeSpaces))); if (!result.secondaryMessages().isEmpty()) { result.secondaryMessages().forEach(m -> out.append("\n").append(Util.withPrefix(" ", m.toString())).append("\n")); } if (!result.secondaryResults().isEmpty()) { result.secondaryResults().forEach(r -> formatResult(out, r)); } else { out.append("\n"); } if (result.originalMessage() != null) { out.append(Util.withPrefix(" ", I18N.lookup(I18N.Key.ORIGINAL_ERROR))).append("\n") .append(Util.withPrefix(" > ", Util.indentLinesAfterNth(2, WRAPPED_LINE_INDENT, Util.wrapString(WRAPPED_LINE_WIDTH, result.originalMessage())))) .append("\n\n"); } }
@Override public HandledResult handle(ValidationContext ctx, ValidationError error) { final Location loc = error.location(); final String attr = error.attribute().getLocalPart(); final Position pos = ctx.searchForward(loc.getLineNumber() - 1, loc.getColumnNumber(), Pattern.compile(attr + "\\s*=")); final HandledResult result = HandledResult.from(error) .addPrimaryMessage(I18N.Key.ATTRIBUTE_INVALID_VALUE, error.attributeValue(), attr); if (pos != null) { result.line(pos.line).column(pos.col); } return result; } }
result.line(pos.line).column(pos.col);
result.line(pos.line).column(pos.col + 1);