/** * Get an exception reporting that an element of a given type and name has * already been declared in this scope. * @param reader the stream reader * @param name the name that was redeclared * @return the exception */ public static XMLStreamException duplicateNamedElement(final XMLExtendedStreamReader reader, final String name) { final XMLStreamException ex = ControllerLogger.ROOT_LOGGER.duplicateNamedElement(name, reader.getLocation()); return new XMLStreamValidationException(ex.getMessage(), ValidationError.from(ex, ErrorType.DUPLICATE_ELEMENT) .element(reader.getName()) .attribute(QName.valueOf("name")) .attributeValue(name), ex); }
private static XMLStreamException wrapMissingRequiredAttribute(final XMLStreamException ex, final XMLStreamReader reader, final Set<String> required) { return new XMLStreamValidationException(ex.getMessage(), ValidationError.from(ex, ErrorType.REQUIRED_ATTRIBUTE_MISSING) .element(reader.getName()) .alternatives(required), ex); }
@Override public HandledResult handle(ValidationContext ctx, ValidationError err) { String msg = err.fallbackMessage(); if (msg == null) { msg = err.message(); if (Pattern.matches(".*\\n at.*", msg)) { final String[] parts = msg.split("\\n"); msg = parts[0]; } } return HandledResult.from(err) .addPrimaryMessage(I18N.Key.PASSTHRU, Util.stripPeriod(msg)); } }
public static ValidationError from(final ValidationError error, final ErrorType type) { return new ValidationError(type, error.message(), error.location()); }
/** * Get an exception reporting that an attribute of a given name has already * been declared in this scope. * @param reader the stream reader * @param name the name that was redeclared * @return the exception */ public static XMLStreamException duplicateAttribute(final XMLExtendedStreamReader reader, final String name) { final XMLStreamException ex = ControllerLogger.ROOT_LOGGER.duplicateAttribute(name, reader.getLocation()); return new XMLStreamValidationException(ex.getMessage(), ValidationError.from(ex, ErrorType.DUPLICATE_ATTRIBUTE) .element(reader.getName()) .attribute(new QName(name)), ex); }
/** * Get an exception reporting an unexpected XML attribute. * @param reader the stream reader * @param index the attribute index * @param possibleAttributes attributes that are expected on this element * @return the exception */ public static XMLStreamException unexpectedAttribute(final XMLExtendedStreamReader reader, final int index, Set<String> possibleAttributes) { final XMLStreamException ex = ControllerLogger.ROOT_LOGGER.unexpectedAttribute(reader.getAttributeName(index), asStringList(possibleAttributes), reader.getLocation()); return new XMLStreamValidationException(ex.getMessage(), ValidationError.from(ex, ErrorType.UNEXPECTED_ATTRIBUTE) .element(reader.getName()) .attribute(reader.getAttributeName(index)) .alternatives(possibleAttributes), ex); }
@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; }
@Override public HandledResult handle(ValidationContext ctx, ValidationError error) { final String el = error.element().getLocalPart(); final String attr = error.attribute() != null ? error.attribute().getLocalPart() : null; final String attrValue = error.attributeValue(); final List<DocElement> docPath = ctx.pathToDocElement(error.element(), error.position()); if (attr != null) { result.addPrimaryMessage(I18N.Key.ELEMENT_WITH_ATTRIBUTE_DUPLICATED, el, attr, attrValue); ctx.docElementSiblings(docPath, e -> e.qname().equals(error.element()) && (attr == null || attrValue.equals(e.attributes().get(attr))));
@Override public HandledResult handle(ValidationContext ctx, ValidationError error) { final QName el = error.element(); final String elName = el.getLocalPart(); final List<List<SchemaElement>> altElements = ctx.alternateElementsForElement(el); ctx.pathToDocElement(e -> e.qname().equals(el) && e.encloses(error.position())); ValidationError.from(error, ErrorType.DUPLICATE_ELEMENT) .element(error.element())); if (error.alternatives().isEmpty()) { List<DocElement> pathToDocElement = ctx.pathToDocElement(el, error.position()); otherElements = Util.asSortedList(error.alternatives());
error = ValidationError.from(exception, ErrorType.DUPLICATE_ATTRIBUTE) .attribute(QName.valueOf(dupAttribute.get())); } else { error = ValidationError.from(exception, ErrorType.UNKNOWN_ERROR); final Optional<String> strippedMessage = stripMessageCode(message); if (strippedMessage.isPresent()) { error.fallbackMessage(strippedMessage.get());
/** * Get an exception reporting an unexpected XML element. * @param reader the stream reader * @return the exception */ public static XMLStreamException unexpectedElement(final XMLExtendedStreamReader reader) { final XMLStreamException ex = ControllerLogger.ROOT_LOGGER.unexpectedElement(reader.getName(), reader.getLocation()); return new XMLStreamValidationException(ex.getMessage(), ValidationError.from(ex, ErrorType.UNEXPECTED_ELEMENT) .element(reader.getName()), ex); }
@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; } }
@Override public HandledResult handle(ValidationContext ctx, ValidationError error) { final String attr = error.attribute().getLocalPart(); final String el = error.element() == null ? null : error.element().getLocalPart(); final Location loc = error.location(); final Pattern attrPattern = Pattern.compile(String.format("\\s%s\\s*=", attr)); final Position pos = ctx.searchBackward(loc.getLineNumber() - 1, loc.getColumnNumber(), attrPattern);
@Override public HandledResult handle(ValidationContext ctx, ValidationError error) { final String el = error.element().getLocalPart(); final Set<String> alts = error.alternatives(); final HandledResult result = HandledResult.from(error) .addPrimaryMessage(I18N.Key.ATTRIBUTE_REQUIRED_MISSING, el); if (!alts.isEmpty()) { final List<SchemaElement> path = ctx.mapDocLocationToSchemaPath(error.element(), error.position()); if (path.isEmpty()) { result.possiblyMalformed(true); } final Set<String> attributesForElement = ctx.attributesForElement(path); result.addPrimaryMessage(I18N.Key.ATTRIBUTE_REQUIRED_MISSING_LIST, Util.asSortedList(alts).stream() .map(String::toLowerCase) .map(Util.possiblyUnderscoredName(attributesForElement)) .collect(Collectors.toList())); } return result; } }
@Override public HandledResult handle(ValidationContext ctx, ValidationError error) { final String el = error.element().getLocalPart(); final Set<String> alts = error.alternatives(); final String alt = alts.stream().findFirst().orElse(null); final HandledResult result = HandledResult.from(error); if (alt == null) { result.addPrimaryMessage(I18N.Key.ELEMENT_UNSUPPORTED_NO_ALT, el); } else { result.addPrimaryMessage(I18N.Key.ELEMENT_UNSUPPORTED, el, alt); } return result; } }
public XMLStreamValidationException(final String message, final ValidationError validationError, final Throwable nested) { super(message, validationError.location(), nested); this.validationError = validationError; }
public static ValidationError from(final XMLStreamException error, final ErrorType type) { return new ValidationError(type, error.getMessage(), error.getLocation()); }
/** * Get an exception reporting that an attribute of a given name has already * been declared in this scope. * @param reader the stream reader * @param name the name that was redeclared * @return the exception */ public static XMLStreamException duplicateAttribute(final XMLExtendedStreamReader reader, final String name) { final XMLStreamException ex = ControllerLogger.ROOT_LOGGER.duplicateAttribute(name, reader.getLocation()); return new XMLStreamValidationException(ex.getMessage(), ValidationError.from(ex, ErrorType.DUPLICATE_ATTRIBUTE) .element(reader.getName()) .attribute(new QName(name)), ex); }
/** * Get an exception reporting an unexpected XML attribute. * @param reader the stream reader * @param index the attribute index * @param possibleAttributes attributes that are expected on this element * @return the exception */ public static XMLStreamException unexpectedAttribute(final XMLExtendedStreamReader reader, final int index, Set<String> possibleAttributes) { final XMLStreamException ex = ControllerLogger.ROOT_LOGGER.unexpectedAttribute(reader.getAttributeName(index), asStringList(possibleAttributes), reader.getLocation()); return new XMLStreamValidationException(ex.getMessage(), ValidationError.from(ex, ErrorType.UNEXPECTED_ATTRIBUTE) .element(reader.getName()) .attribute(reader.getAttributeName(index)) .alternatives(possibleAttributes), ex); }