Merge remote-tracking branch 'origin/configuration_api' into configuration_api

This commit is contained in:
Andreas Penski 2020-05-06 16:17:54 +02:00
commit 62550c42d8
20 changed files with 493 additions and 113 deletions

View file

@ -4,9 +4,17 @@ import de.kosit.validationtool.impl.ContentRepository;
import de.kosit.validationtool.impl.model.Result;
/**
* Internal interface for creating object builders.
*
* @author Andreas Penski
*/
public interface Builder<T> {
interface Builder<T> {
/**
* Creates an object based on artifacts provided via a defined {@link ContentRepository}.
*
* @param repository the {@link ContentRepository}
* @return the result of building the object
*/
Result<T, String> build(ContentRepository repository);
}

View file

@ -3,6 +3,7 @@ package de.kosit.validationtool.config;
import static de.kosit.validationtool.impl.DateFactory.createTimestamp;
import java.net.URI;
import java.nio.file.Path;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
@ -75,6 +76,12 @@ public class ConfigurationBuilder {
return this;
}
/**
* Add a specific nam to this configuration
*
* @param name the name of the configuration
* @return this
*/
public ConfigurationBuilder name(final String name) {
this.name = name;
return this;
@ -103,11 +110,25 @@ public class ConfigurationBuilder {
return date(date != null ? LocalDate.ofEpochDay(date.getTime()) : null);
}
/**
* Adds a {@link Scenario} to this list of know scenarios. Note: order of calling this methods defines order of
* scenarios when determining the target scenario for a given xml file.
*
* @param scenarioBuilder the {@link ScenarioBuilder} building the {@link Scenario}
* @return this
*/
public ConfigurationBuilder with(final ScenarioBuilder scenarioBuilder) {
this.scenarios.add(scenarioBuilder);
return this;
}
/**
* Sets a specific fallback scenario configuration. Note: calling this more than once is possible, but the last call
* will define the actual fallback scenario used. There can be only one
*
* @param builder the {@link FallbackBuilder}
* @return this
*/
public ConfigurationBuilder with(final FallbackBuilder builder) {
if (this.fallbackBuilder != null) {
log.warn("Overriding previously created fallback scenario");
@ -116,6 +137,12 @@ public class ConfigurationBuilder {
return this;
}
/**
* Adds a description to this configuration.
*
* @param description the descriptioin
* @return this
*/
public ConfigurationBuilder description(final String description) {
this.description = description;
return this;
@ -219,6 +246,12 @@ public class ConfigurationBuilder {
return new ReportBuilder().name(name);
}
/**
* Builds the actual {@link Configuration} by validating all builder inputs and constructing neccessary objects.
*
* @return a valid configuration
* @throws IllegalStateException when the configuration is not valid/complete
*/
public Configuration build() {
final ResolvingConfigurationStrategy resolving = getResolvingConfigurationStrategy();
if (this.processor == null) {
@ -269,7 +302,7 @@ public class ConfigurationBuilder {
}
private List<Scenario> initializeScenarios(final ContentRepository contentRepository) {
if (this.scenarios.size() == 0) {
if (this.scenarios.isEmpty()) {
throw new IllegalStateException("No scenario specified");
}
return this.scenarios.stream().map(s -> {
@ -291,6 +324,13 @@ public class ConfigurationBuilder {
return this.resolvingMode.getStrategy();
}
/**
* Sets a specific resolving mode, for resolving xml artifacts for this configuration. See {@link ResolvingMode} for
* details.
*
* @param mode the mode
* @return this
*/
public ConfigurationBuilder resolvingMode(final ResolvingMode mode) {
this.resolvingMode = mode;
return this;
@ -307,8 +347,24 @@ public class ConfigurationBuilder {
return this;
}
/**
* Set a specific repository location for resolving artifacts for scenarios.
*
* @param repository the repository location
* @return this
*/
public ConfigurationBuilder useRepository(final URI repository) {
this.repository = repository;
return this;
}
/**
* Set a specific repository location for resolving artifacts for scenarios.
*
* @param repository the repository location
* @return this
*/
public ConfigurationBuilder useRepository(final Path repository) {
return useRepository(repository.toUri());
}
}

View file

@ -12,7 +12,6 @@ import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
@ -29,7 +28,8 @@ import net.sf.saxon.s9api.XPathExecutable;
* @author Andreas Penski
*/
@RequiredArgsConstructor
@Data
@Getter
@Setter
@Slf4j
class XPathBuilder implements Builder<XPathExecutable> {
@ -42,9 +42,15 @@ class XPathBuilder implements Builder<XPathExecutable> {
private XPathExecutable executable;
@Setter(AccessLevel.PACKAGE)
@Getter(AccessLevel.PACKAGE)
private Map<String, String> namespaces;
Map<String, String> getNamespaces() {
if (this.namespaces == null) {
this.namespaces = new HashMap<>();
}
return this.namespaces;
}
/**
* Returns the xpath expression.
*
@ -66,7 +72,7 @@ class XPathBuilder implements Builder<XPathExecutable> {
}
try {
if (this.executable == null) {
this.executable = repository.createXPath(this.xpath, this.namespaces);
this.executable = repository.createXPath(this.xpath, getNamespaces());
} else {
this.xpath = extractExpression();
extractNamespaces();
@ -81,19 +87,15 @@ class XPathBuilder implements Builder<XPathExecutable> {
}
private void extractNamespaces() {
if (this.namespaces == null) {
this.namespaces = new HashMap<>();
}
final Map<String, String> ns = new HashMap<>();
final Iterator<String> iterator = this.executable.getUnderlyingExpression().getInternalExpression().getRetainedStaticContext()
.iteratePrefixes();
final Iterable<String> iterable = () -> iterator;
StreamSupport.stream(iterable.spliterator(), false).filter(e -> !ArrayUtils.contains(IGNORED_PREFIXES, e))
.filter(StringUtils::isNotBlank).forEach(e -> {
ns.put(e, this.executable.getUnderlyingExpression().getInternalExpression().getRetainedStaticContext()
.getURIForPrefix(e, false));
});
this.namespaces.putAll(ns);
.filter(StringUtils::isNotBlank).forEach(e -> ns.put(e, this.executable.getUnderlyingExpression().getInternalExpression()
.getRetainedStaticContext().getURIForPrefix(e, false)));
getNamespaces().putAll(ns);
}

View file

@ -143,7 +143,7 @@ public class ContentRepository {
throw new IllegalStateException("Can not compile xslt executable for uri " + uri, e);
} finally {
if (!listener.hasErrors() && listener.hasEvents()) {
log.warn("Received warnings while loading a xslt script {}", uri);
log.warn("Received warnings or errors while loading a xslt script {}", uri);
listener.getErrors().forEach(e -> e.log(log));
}
}

View file

@ -4,6 +4,7 @@ import lombok.Getter;
import lombok.RequiredArgsConstructor;
import de.kosit.validationtool.api.ResolvingConfigurationStrategy;
import de.kosit.validationtool.impl.xml.RemoteResolvingStrategy;
import de.kosit.validationtool.impl.xml.StrictLocalResolvingStrategy;
import de.kosit.validationtool.impl.xml.StrictRelativeResolvingStrategy;
@ -24,7 +25,7 @@ public enum ResolvingMode {
STRICT_LOCAL(new StrictLocalResolvingStrategy()),
JDK_SUPPORTED(null),
ALLOW_REMOTE(new RemoteResolvingStrategy()),
CUSTOM(null);

View file

@ -1,7 +1,13 @@
package de.kosit.validationtool.impl.xml;
import javax.xml.validation.SchemaFactory;
public class RemoteResolvingStrategy extends StrictLocalResolvingStrategy {
@Override
public SchemaFactory createSchemaFactory() {
final SchemaFactory schemaFactory = super.createSchemaFactory();
allowExternalSchema(schemaFactory, "https,http,file");
return schemaFactory;
}
}