Skip to content

Commit

Permalink
Removing properties and minor restructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
klakegg committed Sep 12, 2023
1 parent 7bcf7f8 commit 952c811
Show file tree
Hide file tree
Showing 73 changed files with 707 additions and 744 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

* Rewrite of Declaration.
* Rewrite of Expectation.
* no.difi.vefa.validator.api.Document.getDeclaration() returns a string, not an object.
* no.difi.vefa.validator.model.Document.getDeclaration() returns a string, not an object.
* Fixing methods in ValidatorBuilder returning void.
* Adding support for Piwik in sample application. Not turned on by default.
* Refactoring of validator-build.
Expand Down
19 changes: 7 additions & 12 deletions src/main/java/no/difi/vefa/validator/CheckerCacheLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import com.google.inject.Singleton;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import no.difi.vefa.validator.annotation.Type;
import no.difi.vefa.validator.api.Checker;
import no.difi.vefa.validator.api.CheckerFactory;
import no.difi.vefa.validator.lang.ValidatorException;

import java.util.List;
import java.util.Map;

/**
* @author erlend
Expand All @@ -19,26 +18,22 @@
@Singleton
public class CheckerCacheLoader extends CacheLoader<String, Checker> {

public static final int DEFAULT_SIZE = 250;

@Inject
private List<CheckerFactory> factories;
private Map<String, CheckerFactory> factories;

@Inject
private ValidatorEngine validatorEngine;

@Override
@NonNull
public Checker load(@NonNull String key) throws Exception {
public Checker load(@NonNull String key) throws ValidatorException {
try {
for (CheckerFactory factory : factories) {
for (String extension : factory.getClass().getAnnotation(Type.class).value()) {
if (key.toLowerCase().endsWith(extension)) {
return factory.prepare(validatorEngine.getResource(key), key.split("#")[1]);
}
for (var entry : factories.entrySet()) {
if (key.toLowerCase().endsWith(entry.getKey())) {
return entry.getValue().prepare(validatorEngine.getResource(key), key.split("#")[1]);
}
}
} catch (Exception e) {
} catch (ValidatorException e) {
throw new ValidatorException(String.format("Unable to load checker for '%s'.", key), e);
}

Expand Down
41 changes: 23 additions & 18 deletions src/main/java/no/difi/vefa/validator/ValidationInstance.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package no.difi.vefa.validator;

import lombok.extern.slf4j.Slf4j;
import no.difi.vefa.validator.api.*;
import no.difi.vefa.validator.api.Expectation;
import no.difi.vefa.validator.api.FlagFilterer;
import no.difi.vefa.validator.api.Section;
import no.difi.vefa.validator.api.Validation;
import no.difi.vefa.validator.lang.UnknownDocumentTypeException;
import no.difi.vefa.validator.lang.ValidatorException;
import no.difi.vefa.validator.properties.CombinedProperties;
import no.difi.vefa.validator.util.DeclarationIdentification;
import no.difi.vefa.validator.model.Detected;
import no.difi.vefa.validator.model.Document;
import no.difi.vefa.validator.model.Prop;
import no.difi.vefa.validator.model.Props;
import no.difi.xsd.vefa.validator._1.AssertionType;
import no.difi.xsd.vefa.validator._1.FileType;
import no.difi.xsd.vefa.validator._1.FlagType;
Expand All @@ -24,7 +29,7 @@ class ValidationInstance implements Validation {

private final ValidatorInstance validatorInstance;

private final Properties properties;
private final Props props;

private Configuration configuration;

Expand All @@ -45,20 +50,20 @@ class ValidationInstance implements Validation {

private List<Validation> children;

public static ValidationInstance of(ValidatorInstance validatorInstance, Document document, Properties properties) {
return new ValidationInstance(validatorInstance, document, properties);
public static ValidationInstance of(ValidatorInstance validatorInstance, Document document, Prop... props) {
return new ValidationInstance(validatorInstance, document, props);
}

/**
* Constructing new validator using validator instance and validation source containing document to validate.
*
* @param validatorInstance Instance of validator.
* @param document Source to validate.
* @param properties Properties for validation
* @param props Properties for validation
*/
private ValidationInstance(ValidatorInstance validatorInstance, Document document, Properties properties) {
private ValidationInstance(ValidatorInstance validatorInstance, Document document, Prop... props) {
this.validatorInstance = validatorInstance;
this.properties = new CombinedProperties(properties, validatorInstance.getProperties());
this.props = validatorInstance.getProps().update(props);

this.report = new Report();
this.report.setUuid(UUID.randomUUID().toString());
Expand Down Expand Up @@ -97,16 +102,16 @@ private ValidationInstance(ValidatorInstance validatorInstance, Document documen
}
}

private DeclarationIdentification loadDocument(Document document) throws ValidatorException, IOException {
private Detected loadDocument(Document document) throws ValidatorException, IOException {
// Use declaration implementations to detect declaration to use.
DeclarationIdentification declarationIdentifier = validatorInstance.detect(document);
Detected declarationIdentifier = validatorInstance.detect(document);

if (declarationIdentifier.equals(DeclarationIdentification.UNKNOWN))
if (declarationIdentifier.equals(Detected.UNKNOWN))
throw new UnknownDocumentTypeException("Unable to detect type of content.");

// Detect expectation
Expectation expectation = null;
if (properties.getBoolean("feature.expectation")) {
if (props.getBool("feature.expectation", false)) {
expectation = declarationIdentifier.expectations(document);

if (expectation != null)
Expand All @@ -130,7 +135,7 @@ private void loadConfiguration() throws UnknownDocumentTypeException {
// Get configuration using declaration
this.configuration = validatorInstance.getConfiguration(document.getDeclarations());

if (!properties.getBoolean("feature.suppress_notloaded"))
if (!props.getBool("feature.suppress_notloaded", false))
for (String notLoaded : configuration.getNotLoaded())
section.add("SYSTEM-007", String.format(
"Validation artifact '%s' not loaded.", notLoaded), FlagType.WARNING);
Expand Down Expand Up @@ -173,11 +178,11 @@ private void validate() {
/**
* Handling nested validation.
*/
private void nestedValidation(DeclarationIdentification declarationIdentification) throws ValidatorException {
private void nestedValidation(Detected detected) throws ValidatorException {
if (report.getFlag().compareTo(FlagType.FATAL) < 0) {
if (declarationIdentification.hasChildren() && properties.getBoolean("feature.nesting")) {
for (Document child : declarationIdentification.getChildren()) {
addChildValidation(ValidationInstance.of(validatorInstance, child, null));
if (detected.hasChildren() && props.getBool("feature.nesting", false)) {
for (Document child : detected.getChildren()) {
addChildValidation(ValidationInstance.of(validatorInstance, child));
}
}
}
Expand Down
38 changes: 19 additions & 19 deletions src/main/java/no/difi/vefa/validator/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
import com.google.inject.Inject;
import com.google.inject.Singleton;
import lombok.extern.slf4j.Slf4j;
import no.difi.vefa.validator.api.Document;
import no.difi.vefa.validator.api.Properties;
import no.difi.vefa.validator.api.Validation;
import no.difi.vefa.validator.model.Document;
import no.difi.vefa.validator.model.Prop;
import no.difi.xsd.vefa.validator._1.PackageType;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

Expand All @@ -34,44 +33,45 @@ public class Validator implements Closeable {
/**
* Validate file.
*
* @param file File to validate.
* @param file File to validate.
* @param props Optional properties.
* @return Validation result.
*/
public Validation validate(File file) throws IOException {
return validate(file.toPath());
public Validation validate(File file, Prop... props) throws IOException {
return validate(Document.of(file), props);
}

/**
* Validate file.
*
* @param file File to validate.
* @param file File to validate.
* @param props Optional properties.
* @return Validation result.
*/
public Validation validate(Path file) throws IOException {
try (InputStream inputStream = Files.newInputStream(file)) {
return validate(inputStream);
}
public Validation validate(Path file, Prop... props) throws IOException {
return validate(Document.of(file), props);
}

/**
* Validate content of stream.
*
* @param inputStream Stream containing content.
* @param props Optional properties.
* @return Validation result.
*/
public Validation validate(InputStream inputStream) throws IOException {
return validate(inputStream, null);
public Validation validate(InputStream inputStream, Prop... props) throws IOException {
return validate(Document.of(inputStream), props);
}

/**
* Validate content of stream.
* Validate document.
*
* @param inputStream Stream containing content.
* @param properties Properties used for individual validation.
* @param document File to validate.
* @param props Optional properties.
* @return Validation result.
*/
public Validation validate(InputStream inputStream, Properties properties) throws IOException {
return ValidationInstance.of(this.validatorInstance, Document.of(inputStream), properties);
public Validation validate(Document document, Prop... props) {
return validatorInstance.validate(document, props);
}

/**
Expand All @@ -80,7 +80,7 @@ public Validation validate(InputStream inputStream, Properties properties) throw
* @return List of packages.
*/
public List<PackageType> getPackages() {
return this.validatorInstance.getPackages();
return validatorInstance.getPackages();
}

@Override
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/no/difi/vefa/validator/ValidatorBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.google.inject.Guice;
import com.google.inject.Module;
import com.google.inject.util.Modules;
import no.difi.vefa.validator.api.Properties;
import no.difi.vefa.validator.api.Source;
import no.difi.vefa.validator.model.Prop;
import no.difi.vefa.validator.module.PropertiesModule;
import no.difi.vefa.validator.module.SourceModule;
import no.difi.vefa.validator.module.ValidatorModule;
Expand All @@ -19,7 +19,7 @@ public class ValidatorBuilder {

private Source source;

private Properties properties;
private Prop[] props;

/**
* Initiate creation of a new validator. Loads default plugins.
Expand All @@ -40,11 +40,11 @@ private ValidatorBuilder() {
/**
* Defines configuration to use for validator.
*
* @param properties Configuration
* @param props Configuration
* @return Builder object
*/
public ValidatorBuilder setProperties(Properties properties) {
this.properties = properties;
public ValidatorBuilder setProperties(Prop... props) {
this.props = props;
return this;
}

Expand All @@ -66,7 +66,7 @@ public ValidatorBuilder setSource(Source source) {
*/
public Validator build() {
List<Module> modules = new ArrayList<>();
modules.add(new PropertiesModule(properties));
modules.add(PropertiesModule.with(props));
modules.add(new SourceModule(source));

return Guice.createInjector(Modules.override(new ValidatorModule()).with(modules)).getInstance(Validator.class);
Expand Down
31 changes: 0 additions & 31 deletions src/main/java/no/difi/vefa/validator/ValidatorDefaults.java

This file was deleted.

26 changes: 8 additions & 18 deletions src/main/java/no/difi/vefa/validator/ValidatorEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@
import com.google.inject.Inject;
import com.google.inject.Singleton;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Unmarshaller;
import lombok.extern.slf4j.Slf4j;
import no.difi.vefa.validator.api.ArtifactHolder;
import no.difi.vefa.validator.api.SourceInstance;
import no.difi.vefa.validator.lang.ValidatorException;
import no.difi.vefa.validator.model.ArtifactHolder;
import no.difi.vefa.validator.model.Document;
import no.difi.vefa.validator.util.JaxbUtils;
import no.difi.xsd.vefa.validator._1.*;

import javax.xml.transform.stream.StreamSource;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -55,8 +52,7 @@ class ValidatorEngine implements Closeable {
* Loading a new validator engine loading configurations from current source.
*/
@Inject
public ValidatorEngine(SourceInstance sourceInstance, List<Configurations> configurations)
throws ValidatorException {
public ValidatorEngine(SourceInstance sourceInstance, List<Configurations> configurations) throws ValidatorException {
// Load configurations from ValidatorBuilder.
for (Configurations c : configurations)
loadConfigurations("", c);
Expand All @@ -65,9 +61,9 @@ public ValidatorEngine(SourceInstance sourceInstance, List<Configurations> confi
for (Map.Entry<String, ArtifactHolder> entry : sourceInstance.getContent().entrySet()) {
for (String filename : entry.getValue().getFilenames()) {
if (filename.startsWith("config") && filename.endsWith(".xml")) {
try (InputStream inputStream = entry.getValue().getInputStream(filename)) {
try {
content.put(entry.getKey(), entry.getValue());
loadConfigurations(entry.getKey(), inputStream);
loadConfigurations(entry.getKey(), entry.getValue().getDocument(filename));
} catch (ValidatorException e) {
throw new IOException(e.getMessage(), e);
}
Expand All @@ -87,16 +83,10 @@ public ValidatorEngine(SourceInstance sourceInstance, List<Configurations> confi
* Load configuration from stream of config.xml.
*
* @param configurationSource Identifier for resource.
* @param inputStream Stream of config.xml.
* @param document Contents of config.xml.
*/
private void loadConfigurations(String configurationSource, InputStream inputStream) throws ValidatorException {
try {
Unmarshaller unmarshaller = JAXB_CONTEXT.createUnmarshaller();
loadConfigurations(configurationSource,
unmarshaller.unmarshal(new StreamSource(inputStream), Configurations.class).getValue());
} catch (JAXBException e) {
throw new ValidatorException("Unable to read configurations.", e);
}
private void loadConfigurations(String configurationSource, Document document) throws ValidatorException {
loadConfigurations(configurationSource, document.unmarshal(JAXB_CONTEXT, Configurations.class));
}

/**
Expand Down
Loading

0 comments on commit 952c811

Please sign in to comment.