AdvancedFilterValidator implements GenericMetaInfDataValidator, FilterValidator, DocumentViewXmlValidator, JcrPathValidator { @Override public Collection<ValidationMessage> validateMetaInfData(@NotNull InputStream input, @NotNull Path filePath, @NotNull Path basePath) throws IOException { Collection<ValidationMessage> messages = new LinkedList<>(); try { DocumentBuilder parser = factory.newDocumentBuilder(); ValidationMessageErrorHandler errorHandler = new ValidationMessageErrorHandler(defaultSeverity); parser.setErrorHandler(errorHandler); Document document = parser.parse(input, ""); messages.addAll(errorHandler.getValidationMessages()); DefaultWorkspaceFilter filter = new DefaultWorkspaceFilter(); try { filter.load(document.getDocumentElement()); for (Map.Entry<String, FilterValidator> entry : filterValidators.entrySet()) { messages.add(new ValidationMessage(ValidationMessageSeverity.DEBUG, "Validating with validator " + entry.getKey() + "...")); Collection<ValidationMessage> filterValidatorMessages = entry.getValue().validate(filter); if (filterValidatorMessages != null) { messages.addAll(ValidationViolation.wrapMessages(entry.getKey(), filterValidatorMessages, null, null, null, 0, 0)); } } } catch (ConfigurationException e) { messages.add(new ValidationMessage(defaultSeverity, MESSAGE_INVALID_FILTER_XML, e)); } return messages; } catch (ParserConfigurationException e) { throw new IllegalStateException("Could not create parser from factory", e); } catch (SAXException e) { throw new IllegalStateException("Could not parse filter.xml", e); } } AdvancedFilterValidator(@NotNull DocumentBuilderFactory factory, @NotNull ValidationMessageSeverity defaultSeverity, @NotNull ValidationMessageSeverity severityForUncoveredAncestorNodes, @NotNull ValidationMessageSeverity severityForUncoveredFilterRootAncestors, @NotNull ValidationMessageSeverity severityForOrphanedFilterEntries, boolean isSubPackage, @NotNull Collection<PackageInfo> dependenciesMetaInfo, @NotNull WorkspaceFilter filter, @NotNull Collection<String> validRoots); void setFilterValidators(Map<String, FilterValidator> filterValidators); @Override Collection<ValidationMessage> done(); @Override Collection<ValidationMessage> validate(@NotNull WorkspaceFilter filter); @Override @Nullable Collection<ValidationMessage> validateJcrPath(@NotNull NodeContext nodeContext,
            boolean isFolder); @Override @Nullable Collection<ValidationMessage> validate(@NotNull DocViewNode node, @NotNull NodeContext nodeContext,
            boolean isRoot); @Override Collection<ValidationMessage> validateMetaInfData(@NotNull InputStream input, @NotNull Path filePath, @NotNull Path basePath); @Override boolean shouldValidateMetaInfData(@NotNull Path filePath);  }
@Test public void testFilterWithInvalidElements() throws URISyntaxException, IOException, SAXException, ParserConfigurationException, ConfigurationException { try (InputStream input = this.getClass() .getResourceAsStream("/invalid-package/META-INF/vault/filter.xml")) { validator = new AdvancedFilterValidator( factory, ValidationMessageSeverity.WARN, AdvancedFilterValidatorFactory.DEFAULT_SEVERITY_FOR_UNCOVERED_ANCESTOR_NODES, ValidationMessageSeverity.ERROR, ValidationMessageSeverity.ERROR, false, dependenciesMetaInfo, filter, validRoots); Collection<ValidationMessage> messages = validator.validateMetaInfData(input, Paths.get("vault/filter.xml"), Paths.get("")); ValidationExecutorTest.assertViolation(messages, new ValidationMessage(ValidationMessageSeverity.WARN, "cvc-complex-type.3.2.2: Attribute 'mode' is not allowed to appear in element 'exclude'.", 20, 51, null), new ValidationMessage(ValidationMessageSeverity.WARN, "cvc-complex-type.2.4.a: Invalid content was found starting with element 'invalidelement'. One of '{exclude, include}' is expected.", 22, 27, null), new ValidationMessage(ValidationMessageSeverity.WARN, AdvancedFilterValidator.MESSAGE_INVALID_FILTER_XML)); } }