mirror of
https://github.com/itplr-kosit/validator.git
synced 2026-05-25 16:55:39 +00:00
(wip) Umsetzung OHNE Nutzung eines java.nio.Filesystem
This commit is contained in:
parent
7af9b1fdbd
commit
99930a2b0a
14 changed files with 312 additions and 188 deletions
|
|
@ -42,38 +42,6 @@ import lombok.extern.slf4j.Slf4j;
|
|||
@Slf4j
|
||||
class ClassPathResourceResolver implements LSResourceResolver {
|
||||
|
||||
private final URI base;
|
||||
|
||||
/**
|
||||
* Instantiiert einen neuen resolver mit angegebenen Basispfad
|
||||
*
|
||||
* @param basePath der Basispfad
|
||||
*/
|
||||
public ClassPathResourceResolver(String basePath) {
|
||||
if (!StringUtils.startsWith(basePath, "/")) {
|
||||
throw new IllegalArgumentException("Base path must start with a slash");
|
||||
}
|
||||
base = URI.create(basePath + (basePath.endsWith("/") == basePath.length() > 1 ? "" : "/"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
|
||||
final URL resource = ClassPathResourceResolver.class.getResource(base.resolve(systemId).toASCIIString());
|
||||
if (resource != null) {
|
||||
try {
|
||||
InputStream in = resource.openStream();
|
||||
final LSInputImpl input = new LSInputImpl(publicId, systemId, baseURI);
|
||||
input.setByteStream(in);
|
||||
return input;
|
||||
|
||||
} catch (IOException e) {
|
||||
log.error("Error loading schema resource from {}", resource, e);
|
||||
}
|
||||
}
|
||||
// not found
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple {@link LSInput}-Implementierung, die einen Stream liefern kann
|
||||
*/
|
||||
|
|
@ -100,11 +68,12 @@ class ClassPathResourceResolver implements LSResourceResolver {
|
|||
|
||||
/**
|
||||
* Instantiierung einer neue Instanz.
|
||||
*
|
||||
* @param publicId die publicId
|
||||
* @param systemId die systemId
|
||||
* @param baseURI die baseURI
|
||||
*/
|
||||
public LSInputImpl(String publicId, String systemId, String baseURI) {
|
||||
public LSInputImpl(final String publicId, final String systemId, final String baseURI) {
|
||||
this.publicId = publicId;
|
||||
this.systemId = systemId;
|
||||
this.baseURI = baseURI;
|
||||
|
|
@ -112,7 +81,48 @@ class ClassPathResourceResolver implements LSResourceResolver {
|
|||
|
||||
@Override
|
||||
public boolean getCertifiedText() {
|
||||
return certifiedText;
|
||||
return this.certifiedText;
|
||||
}
|
||||
}
|
||||
|
||||
private final URI base;
|
||||
|
||||
/**
|
||||
* Instantiiert einen neuen resolver mit angegebenen Basispfad
|
||||
*
|
||||
* @param basePath der Basispfad
|
||||
*/
|
||||
public ClassPathResourceResolver(final String basePath) {
|
||||
if (!StringUtils.startsWith(basePath, "/")) {
|
||||
throw new IllegalArgumentException("Base path must start with a slash");
|
||||
}
|
||||
this.base = URI.create(basePath + (basePath.endsWith("/") == basePath.length() > 1 ? "" : "/"));
|
||||
}
|
||||
|
||||
public ClassPathResourceResolver(final URI jarUri) {
|
||||
this.base = jarUri;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LSInput resolveResource(final String type, final String namespaceURI, final String publicId, final String systemId,
|
||||
final String baseURI) {
|
||||
|
||||
final URI resolved = RelativeUriResolver.resolve(URI.create(systemId), this.base);
|
||||
if (resolved != null) {
|
||||
try {
|
||||
final URL resource = resolved.isAbsolute() ? resolved.toURL()
|
||||
: ClassPathResourceResolver.class.getResource(resolved.toASCIIString());
|
||||
final InputStream in = resource.openStream();
|
||||
final LSInputImpl input = new LSInputImpl(publicId, systemId, resolved.toASCIIString());
|
||||
input.setByteStream(in);
|
||||
return input;
|
||||
|
||||
} catch (final IOException e) {
|
||||
log.error("Error loading schema resource from {}", resolved, e);
|
||||
}
|
||||
}
|
||||
// not found
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,7 +39,12 @@ import lombok.Getter;
|
|||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import net.sf.saxon.s9api.*;
|
||||
import net.sf.saxon.s9api.Processor;
|
||||
import net.sf.saxon.s9api.SaxonApiException;
|
||||
import net.sf.saxon.s9api.XPathCompiler;
|
||||
import net.sf.saxon.s9api.XPathExecutable;
|
||||
import net.sf.saxon.s9api.XsltCompiler;
|
||||
import net.sf.saxon.s9api.XsltExecutable;
|
||||
|
||||
/**
|
||||
* Repository für verschiedene XML Artefakte zur Vearbeitung der Prüfszenarien.
|
||||
|
|
@ -57,25 +62,25 @@ public class ContentRepository {
|
|||
|
||||
private Schema reportInputSchema;
|
||||
|
||||
private static Source resolve(URL resource) {
|
||||
private static Source resolve(final URL resource) {
|
||||
try {
|
||||
return new StreamSource(resource.openStream(), resource.toURI().getRawPath());
|
||||
} catch (IOException | URISyntaxException e) {
|
||||
} catch (final IOException | URISyntaxException e) {
|
||||
throw new IllegalStateException("Can not load schema for resource " + resource.getPath(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private static Schema createSchema(Source[] schemaSources, LSResourceResolver resourceResolver) {
|
||||
private static Schema createSchema(final Source[] schemaSources, final LSResourceResolver resourceResolver) {
|
||||
try {
|
||||
SchemaFactory sf = ObjectFactory.createSchemaFactory();
|
||||
final SchemaFactory sf = ObjectFactory.createSchemaFactory();
|
||||
sf.setResourceResolver(resourceResolver);
|
||||
return sf.newSchema(schemaSources);
|
||||
} catch (SAXException e) {
|
||||
} catch (final SAXException e) {
|
||||
throw new IllegalArgumentException("Can not load schema from sources " + schemaSources[0].getSystemId(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private static Schema createSchema(Source[] schemaSources) {
|
||||
private static Schema createSchema(final Source[] schemaSources) {
|
||||
return createSchema(schemaSources, null);
|
||||
}
|
||||
|
||||
|
|
@ -85,16 +90,16 @@ public class ContentRepository {
|
|||
* @param uri die URI der XSL Definition
|
||||
* @return ein XSLT Executable
|
||||
*/
|
||||
public XsltExecutable loadXsltScript(URI uri) {
|
||||
public XsltExecutable loadXsltScript(final URI uri) {
|
||||
log.info("Loading XSLT script from {}", uri);
|
||||
final XsltCompiler xsltCompiler = getProcessor().newXsltCompiler();
|
||||
final CollectingErrorEventHandler listener = new CollectingErrorEventHandler();
|
||||
try {
|
||||
xsltCompiler.setErrorListener(listener);
|
||||
xsltCompiler.setURIResolver(new RelativeUriResolver(repository));
|
||||
xsltCompiler.setURIResolver(new RelativeUriResolver(this.repository));
|
||||
|
||||
return xsltCompiler.compile(resolve(uri));
|
||||
} catch (SaxonApiException e) {
|
||||
} catch (final SaxonApiException e) {
|
||||
listener.getErrors().forEach(event -> event.log(log));
|
||||
throw new IllegalStateException("Can not compile xslt executable for uri " + uri, e);
|
||||
} finally {
|
||||
|
|
@ -111,9 +116,13 @@ public class ContentRepository {
|
|||
* @param url die url
|
||||
* @return das erzeugte Schema
|
||||
*/
|
||||
public Schema createSchema(URL url) {
|
||||
public static Schema createSchema(final URL url) {
|
||||
return createSchema(url, null);
|
||||
}
|
||||
|
||||
public static Schema createSchema(final URL url, final LSResourceResolver resourceResolver) {
|
||||
log.info("Load schema from source {}", url.getPath());
|
||||
return createSchema(new Source[] { resolve(url) });
|
||||
return createSchema(new Source[] { resolve(url) }, resourceResolver);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -121,7 +130,7 @@ public class ContentRepository {
|
|||
*
|
||||
* @return Scenario-Schema
|
||||
*/
|
||||
public Schema getScenarioSchema() {
|
||||
public static Schema getScenarioSchema() {
|
||||
return createSchema(ContentRepository.class.getResource("/xsd/scenarios.xsd"));
|
||||
}
|
||||
|
||||
|
|
@ -131,11 +140,11 @@ public class ContentRepository {
|
|||
* @return ReportInput-Schema
|
||||
*/
|
||||
public Schema getReportInputSchema() {
|
||||
if (reportInputSchema == null) {
|
||||
if (this.reportInputSchema == null) {
|
||||
final Source source = resolve(ContentRepository.class.getResource("/xsd/createReportInput.xsd"));
|
||||
reportInputSchema = createSchema(new Source[] { source }, new ClassPathResourceResolver("/xsd"));
|
||||
this.reportInputSchema = createSchema(new Source[] { source }, new ClassPathResourceResolver("/xsd"));
|
||||
}
|
||||
return reportInputSchema;
|
||||
return this.reportInputSchema;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -144,12 +153,13 @@ public class ContentRepository {
|
|||
* @param uris die uris in String-Repräsentation
|
||||
* @return das Schema
|
||||
*/
|
||||
public Schema createSchema(Collection<String> uris) {
|
||||
public Schema createSchema(final Collection<String> uris) {
|
||||
return createSchema(uris.stream().map(s -> resolve(URI.create(s))).toArray(Source[]::new));
|
||||
}
|
||||
|
||||
private Source resolve(URI source) {
|
||||
return new StreamSource(repository.resolve(source).toASCIIString());
|
||||
private Source resolve(final URI source) {
|
||||
final URI resolved = RelativeUriResolver.resolve(source, this.repository);
|
||||
return new StreamSource(resolved.toASCIIString());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -159,17 +169,37 @@ public class ContentRepository {
|
|||
* @param namespaces optionale Namespace-Mappings
|
||||
* @return ein kompiliertes Executable
|
||||
*/
|
||||
public XPathExecutable createXPath(String expression, Map<String, String> namespaces) {
|
||||
public XPathExecutable createXPath(final String expression, final Map<String, String> namespaces) {
|
||||
try {
|
||||
final XPathCompiler compiler = getProcessor().newXPathCompiler();
|
||||
if (namespaces != null) {
|
||||
namespaces.entrySet().forEach(n -> compiler.declareNamespace(n.getKey(), n.getValue()));
|
||||
namespaces.forEach(compiler::declareNamespace);
|
||||
}
|
||||
return compiler.compile(expression);
|
||||
} catch (SaxonApiException e) {
|
||||
} catch (final SaxonApiException e) {
|
||||
throw new IllegalStateException(String.format("Can not compile xpath match expression '%s'",
|
||||
StringUtils.isNotBlank(expression) ? expression : "EMPTY EXPRESSION"), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Zeigt an, ob diese URI in ein JAR zeigt.
|
||||
*
|
||||
* @param uri der URI
|
||||
* @return true wenn innerhalb eines JARs
|
||||
*/
|
||||
public static boolean isJarResource(final URI uri) {
|
||||
return isJarResource(uri.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Zeigt an, ob dieser Pfad in ein JAR zeigt.
|
||||
*
|
||||
* @param path der Pfad (URI-Format)
|
||||
* @return true wenn innerhalb eines JARs
|
||||
*/
|
||||
public static boolean isJarResource(final String path) {
|
||||
return StringUtils.startsWithIgnoreCase(path, "jar:") && path.split("!").length == 2;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,6 @@ import java.io.Reader;
|
|||
import java.net.URI;
|
||||
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.URIResolver;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
|
|
@ -48,38 +47,49 @@ public class RelativeUriResolver implements URIResolver, UnparsedTextURIResolver
|
|||
private final URI baseUri;
|
||||
|
||||
@Override
|
||||
public Source resolve(final String href, final String base) throws TransformerException {
|
||||
final URI resolved = URI.create(base).resolve(href);
|
||||
public Source resolve(final String href, final String base) {
|
||||
final URI resolved = resolve(URI.create(href), URI.create(base));
|
||||
if (isUnderBaseUri(resolved)) {
|
||||
try {
|
||||
return new StreamSource(resolved.toURL().openStream());
|
||||
return new StreamSource(resolved.toURL().openStream(), resolved.toASCIIString());
|
||||
} catch (final IOException e) {
|
||||
|
||||
throw new IllegalStateException(String.format("Can not resolve required %s", href), e);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
String.format("The resolved transformation artifact %s is not within the configured repository %s", resolved, baseUri));
|
||||
throw new IllegalStateException(String
|
||||
.format("The resolved transformation artifact %s is not within the configured repository %s", resolved, this.baseUri));
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isUnderBaseUri(URI resolved) {
|
||||
String base = baseUri.toASCIIString().replaceAll("file:/+", "");
|
||||
String r = resolved.toASCIIString().replaceAll("file:/+", "");
|
||||
static URI resolve(final URI href, final URI base) {
|
||||
final boolean jarURI = isJarURI(base);
|
||||
final URI tmpBase = jarURI ? URI.create(base.toASCIIString().substring(4)) : base;
|
||||
final URI result = tmpBase.resolve(href);
|
||||
return jarURI ? URI.create("jar:" + result.toString()) : result;
|
||||
}
|
||||
|
||||
static boolean isJarURI(final URI uri) {
|
||||
return uri.isOpaque() && uri.getScheme().equals("jar");
|
||||
}
|
||||
|
||||
private boolean isUnderBaseUri(final URI resolved) {
|
||||
final String base = this.baseUri.toASCIIString().replaceAll("file:/+", "");
|
||||
final String r = resolved.toASCIIString().replaceAll("file:/+", "");
|
||||
return r.startsWith(base);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Reader resolve(URI absoluteURI, String encoding, Configuration config) throws XPathException {
|
||||
public Reader resolve(final URI absoluteURI, final String encoding, final Configuration config) throws XPathException {
|
||||
if (isUnderBaseUri(absoluteURI)) {
|
||||
try {
|
||||
return new InputStreamReader(absoluteURI.toURL().openStream(), encoding);
|
||||
} catch (IOException e) {
|
||||
} catch (final IOException e) {
|
||||
throw new IllegalStateException(String.format("Can not resolve required %s", absoluteURI), e);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
String.format("The resolved transformation artifact %s is not within the configured repository %s", absoluteURI, baseUri));
|
||||
throw new IllegalStateException(String.format(
|
||||
"The resolved transformation artifact %s is not within the configured repository %s", absoluteURI, this.baseUri));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -73,7 +73,7 @@ public class ScenarioRepository {
|
|||
@Getter
|
||||
private Scenarios scenarios;
|
||||
|
||||
private static boolean isSupportedDocument(XdmNode doc) {
|
||||
private static boolean isSupportedDocument(final XdmNode doc) {
|
||||
final XdmNode root = findRoot(doc);
|
||||
final String frameworkVersion = root.getAttributeValue(new QName("frameworkVersion"));
|
||||
return startsWith(frameworkVersion, SUPPORTED_MAJOR_VERSION)
|
||||
|
|
@ -91,8 +91,8 @@ public class ScenarioRepository {
|
|||
throw new IllegalArgumentException("Kein root element gefunden");
|
||||
}
|
||||
|
||||
private static void checkVersion(URI scenarioDefinition) {
|
||||
DocumentParseAction p = new DocumentParseAction();
|
||||
private static void checkVersion(final URI scenarioDefinition) {
|
||||
final DocumentParseAction p = new DocumentParseAction();
|
||||
try {
|
||||
final Result<XdmNode, XMLSyntaxError> result = p.parseDocument(InputFactory.read(scenarioDefinition.toURL()));
|
||||
if (result.isValid() && !isSupportedDocument(result.getObject())) {
|
||||
|
|
@ -101,16 +101,17 @@ public class ScenarioRepository {
|
|||
scenarioDefinition, SUPPORTED_MAJOR_VERSION_SCHEMA));
|
||||
|
||||
}
|
||||
} catch (MalformedURLException e) {
|
||||
} catch (final MalformedURLException e) {
|
||||
throw new IllegalStateException("Error reading definition file");
|
||||
}
|
||||
}
|
||||
|
||||
public XsltExecutable getNoScenarioReport() {
|
||||
if (noScenarioReport == null) {
|
||||
noScenarioReport = repository.loadXsltScript(URI.create(scenarios.getNoScenarioReport().getResource().getLocation()));
|
||||
if (this.noScenarioReport == null) {
|
||||
this.noScenarioReport = this.repository
|
||||
.loadXsltScript(URI.create(this.scenarios.getNoScenarioReport().getResource().getLocation()));
|
||||
}
|
||||
return noScenarioReport;
|
||||
return this.noScenarioReport;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -118,18 +119,18 @@ public class ScenarioRepository {
|
|||
*
|
||||
* @param config die Konfiguration
|
||||
*/
|
||||
public void initialize(CheckConfiguration config) {
|
||||
ConversionService conversionService = new ConversionService();
|
||||
public void initialize(final CheckConfiguration config) {
|
||||
final ConversionService conversionService = new ConversionService();
|
||||
checkVersion(config.getScenarioDefinition());
|
||||
log.info("Loading scenarios from {}", config.getScenarioDefinition());
|
||||
CollectingErrorEventHandler handler = new CollectingErrorEventHandler();
|
||||
this.scenarios = conversionService.readXml(config.getScenarioDefinition(), Scenarios.class, repository.getScenarioSchema(),
|
||||
final CollectingErrorEventHandler handler = new CollectingErrorEventHandler();
|
||||
this.scenarios = conversionService.readXml(config.getScenarioDefinition(), Scenarios.class, ContentRepository.getScenarioSchema(),
|
||||
handler);
|
||||
if (!handler.hasErrors()) {
|
||||
log.info("Loaded scenarios for {} by {} from {}. The following scenarios are available:\n\n{}", scenarios.getName(),
|
||||
scenarios.getAuthor(), scenarios.getDate(), summarizeScenarios());
|
||||
log.info("Loaded scenarios for {} by {} from {}. The following scenarios are available:\n\n{}", this.scenarios.getName(),
|
||||
this.scenarios.getAuthor(), this.scenarios.getDate(), summarizeScenarios());
|
||||
log.info("Loading scenario content from {}", config.getScenarioRepository());
|
||||
getScenarios().getScenario().forEach(s -> s.initialize(repository, false));
|
||||
getScenarios().getScenario().forEach(s -> s.initialize(this.repository, false));
|
||||
} else {
|
||||
throw new IllegalStateException(String.format("Can not load scenarios from %s due to %s", config.getScenarioDefinition(),
|
||||
handler.getErrorDescription()));
|
||||
|
|
@ -140,8 +141,8 @@ public class ScenarioRepository {
|
|||
}
|
||||
|
||||
private String summarizeScenarios() {
|
||||
StringBuilder b = new StringBuilder();
|
||||
scenarios.getScenario().forEach(s -> {
|
||||
final StringBuilder b = new StringBuilder();
|
||||
this.scenarios.getScenario().forEach(s -> {
|
||||
b.append(s.getName());
|
||||
b.append("\n");
|
||||
});
|
||||
|
|
@ -154,9 +155,10 @@ public class ScenarioRepository {
|
|||
* @param document das Eingabedokument
|
||||
* @return ein Ergebnis-Objekt zur weiteren Verarbeitung
|
||||
*/
|
||||
public Result<ScenarioType, String> selectScenario(XdmNode document) {
|
||||
public Result<ScenarioType, String> selectScenario(final XdmNode document) {
|
||||
Result<ScenarioType, String> result = new Result<>();
|
||||
final List<ScenarioType> collect = scenarios.getScenario().stream().filter(s -> match(document, s)).collect(Collectors.toList());
|
||||
final List<ScenarioType> collect = this.scenarios.getScenario().stream().filter(s -> match(document, s))
|
||||
.collect(Collectors.toList());
|
||||
if (collect.size() == 1) {
|
||||
result = new Result<>(collect.get(0));
|
||||
} else if (collect.isEmpty()) {
|
||||
|
|
@ -168,19 +170,19 @@ public class ScenarioRepository {
|
|||
|
||||
}
|
||||
|
||||
private boolean match(XdmNode document, ScenarioType scenario) {
|
||||
private static boolean match(final XdmNode document, final ScenarioType scenario) {
|
||||
try {
|
||||
final XPathSelector selector = scenario.getSelector();
|
||||
selector.setContextItem(document);
|
||||
return selector.effectiveBooleanValue();
|
||||
} catch (SaxonApiException e) {
|
||||
} catch (final SaxonApiException e) {
|
||||
log.error("Error evaluating xpath expression", e);
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
void initialize(Scenarios def) {
|
||||
void initialize(final Scenarios def) {
|
||||
this.scenarios = def;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue