#59 fix unparsed text uri resolving

This commit is contained in:
Andreas Penski 2020-07-29 12:13:18 +00:00
parent 13de7f00a4
commit 0717e22d33
10 changed files with 58 additions and 16 deletions

View file

@ -7,6 +7,7 @@ import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import net.sf.saxon.lib.UnparsedTextURIResolver;
import net.sf.saxon.s9api.Processor;
/**
@ -28,7 +29,7 @@ public interface ResolvingConfigurationStrategy {
/**
* Creates a preconfigured {@link SchemaFactory} for loading {@link javax.xml.validation.Schema} objects. The
* implementation is responsible for xml security. Take care
*
*
* @return preconfigured {@link SchemaFactory}
*/
SchemaFactory createSchemaFactory();
@ -37,9 +38,9 @@ public interface ResolvingConfigurationStrategy {
* Returns a preconfigured {@link Processor Saxon Processor} for various tasks within the Validator. The validator
* leverages the saxon s9api for internal processing e.g. xml reading and writing. So this is the main object to secure
* for reading, transforming and writing xml files.
*
*
* Note: you need exactly one instance for all validator related processing.
*
*
* @return a preconfigured {@link Processor}
*/
Processor getProcessor();
@ -52,12 +53,20 @@ public interface ResolvingConfigurationStrategy {
* This URIResolver is used to dereference the URIs appearing in <code>xsl:import</code>, <code>xsl:include</code>, and
* <code>xsl:import-schema</code> declarations.
* </p>
*
*
* @param scenarioRepository an optional repository, your implementation might not need this
* @return a preconfigured {@link URIResolver}
*/
URIResolver createResolver(URI scenarioRepository);
/**
* Creates a specific implementation for resolving objects referenced via XSLT's <code>unparsed-text()</code> function.
*
* @param scenarioRepository an optional repository, your implementation might not need this
* @return a preconfigured {@link net.sf.saxon.lib.UnparsedTextURIResolver} or null for using saxons default
*/
UnparsedTextURIResolver createUnparsedTextURIResolver(URI scenarioRepository);
/**
* Creates a preconfigured {@link Validator } instance for a given schema for xml file validation. The implementation
* takes care about security and reference resolving strategies.

View file

@ -52,6 +52,7 @@ import de.kosit.validationtool.model.scenarios.ResourceType;
import de.kosit.validationtool.model.scenarios.ScenarioType;
import de.kosit.validationtool.model.scenarios.ValidateWithSchematron;
import net.sf.saxon.lib.UnparsedTextURIResolver;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XPathCompiler;
@ -77,6 +78,8 @@ public class ContentRepository {
private final URIResolver resolver;
private final UnparsedTextURIResolver unparsedTextURIResolver;
private final SchemaFactory schemaFactory;
@Getter
@ -94,6 +97,7 @@ public class ContentRepository {
this.resolvingConfigurationStrategy = strategy;
this.processor = this.resolvingConfigurationStrategy.getProcessor();
this.resolver = this.resolvingConfigurationStrategy.createResolver(repository);
this.unparsedTextURIResolver = this.resolvingConfigurationStrategy.createUnparsedTextURIResolver(repository);
this.schemaFactory = this.resolvingConfigurationStrategy.createSchemaFactory();
}
@ -251,14 +255,18 @@ public class ContentRepository {
}
/**
* Erzeugt einen resolver für dieses Repository, der nur relativ auflösen kann
* Returns the {@link URIResolver} to use for resolving xml artifacts.
*
* @return ein neuer Resolver
* @return the resolver
*/
public URIResolver getResolver() {
return this.resolver;
}
public UnparsedTextURIResolver getUnparsedTextURIResolver() {
return this.unparsedTextURIResolver;
}
/**
* Gibt eine Transformation zurück.
*

View file

@ -85,7 +85,8 @@ public class DefaultCheck implements Check {
this.checkSteps.add(new SchemaValidationAction(content.getResolvingConfigurationStrategy(), processor));
this.checkSteps.add(new SchematronValidationAction(content.getResolver(), this.conversionService));
this.checkSteps.add(new ValidateReportInputAction(this.conversionService, content.getReportInputSchema()));
this.checkSteps.add(new CreateReportAction(processor, this.conversionService, content.getResolver()));
this.checkSteps.add(
new CreateReportAction(processor, this.conversionService, content.getResolver(), content.getUnparsedTextURIResolver()));
this.checkSteps.add(new ComputeAcceptanceAction());
}

View file

@ -28,7 +28,6 @@ import javax.xml.bind.Marshaller;
import javax.xml.bind.util.JAXBSource;
import javax.xml.transform.URIResolver;
import lombok.extern.slf4j.Slf4j;
import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.EntityResolver;
@ -41,6 +40,7 @@ import org.xml.sax.XMLReader;
import org.xml.sax.helpers.AttributesImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import de.kosit.validationtool.impl.CollectingErrorEventHandler;
import de.kosit.validationtool.impl.ConversionService;
@ -48,6 +48,7 @@ import de.kosit.validationtool.impl.EngineInformation;
import de.kosit.validationtool.impl.Scenario;
import de.kosit.validationtool.model.reportInput.XMLSyntaxError;
import net.sf.saxon.lib.UnparsedTextURIResolver;
import net.sf.saxon.s9api.BuildingContentHandler;
import net.sf.saxon.s9api.DocumentBuilder;
import net.sf.saxon.s9api.Processor;
@ -175,6 +176,8 @@ public class CreateReportAction implements CheckAction {
private final URIResolver resolver;
private final UnparsedTextURIResolver unparsedTextURIResolver;
private static XsltExecutable loadFromScenario(final Scenario object) {
return object.getReportTransformation().getExecutable();
}
@ -198,7 +201,9 @@ public class CreateReportAction implements CheckAction {
final CollectingErrorEventHandler e = new CollectingErrorEventHandler();
transformer.setMessageListener(e);
transformer.setURIResolver(this.resolver);
// transformer.getUnderlyingController().setUnparsedTextURIResolver(resolver);
if (this.unparsedTextURIResolver != null) {
transformer.getUnderlyingController().setUnparsedTextURIResolver(this.unparsedTextURIResolver);
}
if (parsedDocument != null) {
transformer.setParameter(new QName("input-document"), parsedDocument);
}

View file

@ -20,7 +20,6 @@
package de.kosit.validationtool.impl.xml;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
@ -32,6 +31,8 @@ import javax.xml.transform.stream.StreamSource;
import lombok.RequiredArgsConstructor;
import net.sf.saxon.Configuration;
import net.sf.saxon.lib.StandardUnparsedTextResolver;
import net.sf.saxon.lib.UnparsedTextURIResolver;
import net.sf.saxon.trans.XPathException;
/**
@ -41,7 +42,7 @@ import net.sf.saxon.trans.XPathException;
* @author Andreas Penski
*/
@RequiredArgsConstructor()
public class RelativeUriResolver implements URIResolver {
public class RelativeUriResolver implements URIResolver, UnparsedTextURIResolver {
/** the base uri */
private final URI baseUri;
@ -93,6 +94,16 @@ public class RelativeUriResolver implements URIResolver {
return r.startsWith(base);
}
// from UnparsedTextURIResolver
@Override
public Reader resolve(final URI absoluteURI, final String encoding, final Configuration config) throws XPathException {
if (isUnderBaseUri(absoluteURI, this.baseUri)) {
return new StandardUnparsedTextResolver().resolve(absoluteURI, encoding, config);
} else {
throw new XPathException(String.format("The resolved transformation artifact %s is not within the configured repository %s",
absoluteURI, this.baseUri));
}
}
}

View file

@ -109,6 +109,11 @@ public class StrictRelativeResolvingStrategy extends BaseResolvingStrategy {
return new RelativeUriResolver(repositoryURI);
}
@Override
public UnparsedTextURIResolver createUnparsedTextURIResolver(final URI scenarioRepository) {
return new RelativeUriResolver(scenarioRepository);
}
@Override
public Validator createValidator(final Schema schema) {
if (schema == null) {