(enhance) ContentRepository is only based on uri and strategy

This commit is contained in:
Andreas Penski (init) 2020-04-29 10:29:30 +02:00
parent 35c0797898
commit 7ca3ef90f3
9 changed files with 38 additions and 42 deletions

View file

@ -172,10 +172,7 @@ public class ConfigurationBuilder {
if (this.processor == null) { if (this.processor == null) {
this.processor = resolving.createProcessor(); this.processor = resolving.createProcessor();
} }
final ContentRepository contentRepository = new ContentRepository(this.processor, this.repository, final ContentRepository contentRepository = new ContentRepository(this.resolvingConfigurationStrategy, this.repository);
resolving.createResolver(this.repository));
contentRepository.setSchemaFactory(resolving.createSchemaFactory());
contentRepository.setResolvingConfigurationStrategy(resolving);
final List<Scenario> list = initializeScenarios(contentRepository); final List<Scenario> list = initializeScenarios(contentRepository);
final Scenario fallbackScenario = initializeFallback(contentRepository); final Scenario fallbackScenario = initializeFallback(contentRepository);

View file

@ -118,10 +118,7 @@ public class ConfigurationLoader {
public Configuration build() { public Configuration build() {
final ResolvingConfigurationStrategy resolving = getResolvingConfigurationStrategy(); final ResolvingConfigurationStrategy resolving = getResolvingConfigurationStrategy();
final Processor processor = resolving.createProcessor(); final Processor processor = resolving.createProcessor();
final ContentRepository contentRepository = new ContentRepository(processor, getScenarioRepository(), final ContentRepository contentRepository = new ContentRepository(resolving, getScenarioRepository());
resolving.createResolver(this.getScenarioRepository()));
contentRepository.setSchemaFactory(resolving.createSchemaFactory());
contentRepository.setResolvingConfigurationStrategy(resolving);
final Scenarios def = loadScenarios(contentRepository.getScenarioSchema(), processor); final Scenarios def = loadScenarios(contentRepository.getScenarioSchema(), processor);
final List<Scenario> scenarios = initializeScenarios(def, contentRepository); final List<Scenario> scenarios = initializeScenarios(def, contentRepository);

View file

@ -42,7 +42,6 @@ import org.xml.sax.SAXException;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import de.kosit.validationtool.api.ResolvingConfigurationStrategy; import de.kosit.validationtool.api.ResolvingConfigurationStrategy;
@ -78,13 +77,27 @@ public class ContentRepository {
private final URIResolver resolver; private final URIResolver resolver;
@Setter private final SchemaFactory schemaFactory;
private SchemaFactory schemaFactory;
@Setter
@Getter @Getter
private ResolvingConfigurationStrategy resolvingConfigurationStrategy; private final ResolvingConfigurationStrategy resolvingConfigurationStrategy;
/**
* Creates a new {@link ContentRepository} based on configured security and resolving strategy and the specified
* repository location.
*
* @param strategy the security and resolving strategy
* @param repository the repository.
*/
public ContentRepository(final ResolvingConfigurationStrategy strategy, final URI repository) {
this.repository = repository;
this.resolvingConfigurationStrategy = strategy;
this.processor = this.resolvingConfigurationStrategy.createProcessor();
this.resolver = this.resolvingConfigurationStrategy.createResolver(repository);
this.schemaFactory = this.resolvingConfigurationStrategy.createSchemaFactory();
}
@SuppressWarnings("java:S2095")
private static Source resolve(final URL resource) { private static Source resolve(final URL resource) {
try { try {
return new StreamSource(resource.openStream(), resource.toURI().getRawPath()); return new StreamSource(resource.openStream(), resource.toURI().getRawPath());
@ -119,10 +132,9 @@ public class ContentRepository {
final CollectingErrorEventHandler listener = new CollectingErrorEventHandler(); final CollectingErrorEventHandler listener = new CollectingErrorEventHandler();
try { try {
xsltCompiler.setErrorListener(listener); xsltCompiler.setErrorListener(listener);
final URIResolver resolver = getResolver(); if (getResolver() != null) {
if (resolver != null) {
// otherwise use default resolver // otherwise use default resolver
xsltCompiler.setURIResolver(resolver); xsltCompiler.setURIResolver(getResolver());
} }
return xsltCompiler.compile(resolveInRepository(uri)); return xsltCompiler.compile(resolveInRepository(uri));
@ -275,7 +287,7 @@ public class ContentRepository {
} }
public List<Transformation> createSchematronTransformations(final ScenarioType s) { public List<Transformation> createSchematronTransformations(final ScenarioType s) {
return s.getValidateWithSchematron() != null && s.getValidateWithSchematron().isEmpty() ? Collections.emptyList() return s.getValidateWithSchematron().isEmpty() ? Collections.emptyList()
: s.getValidateWithSchematron().stream().map(this::createSchematronTransformation).collect(Collectors.toList()); : s.getValidateWithSchematron().stream().map(this::createSchematronTransformation).collect(Collectors.toList());
} }

View file

@ -25,7 +25,6 @@ import java.net.URI;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
@ -144,8 +143,8 @@ public class ObjectFactory {
Transformer transformer = null; Transformer transformer = null;
try { try {
final TransformerFactory transformerFactory = TransformerFactory.newInstance(); final TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); // transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, ""); // Compliant // transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, ""); // Compliant
transformer = transformerFactory.newTransformer(); transformer = transformerFactory.newTransformer();
if (prettyPrint) { if (prettyPrint) {
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");

View file

@ -114,7 +114,8 @@ public class ContentRepositoryTest {
@Test @Test
public void loadFromJar() throws URISyntaxException { public void loadFromJar() throws URISyntaxException {
this.repository = new ContentRepository(TestObjectFactory.createProcessor(), Helper.JAR_REPOSITORY.toURI(), null); assert Helper.JAR_REPOSITORY != null;
this.repository = new ContentRepository(ResolvingMode.STRICT_RELATIVE.getStrategy(), Helper.JAR_REPOSITORY.toURI());
final XsltExecutable xsltExecutable = this.repository.loadXsltScript(URI.create("resources/eRechnung/report.xsl")); final XsltExecutable xsltExecutable = this.repository.loadXsltScript(URI.create("resources/eRechnung/report.xsl"));
assertThat(xsltExecutable).isNotNull(); assertThat(xsltExecutable).isNotNull();
} }
@ -122,6 +123,7 @@ public class ContentRepositoryTest {
@Test @Test
public void testLoadSchema() { public void testLoadSchema() {
final URL main = RelativeUriResolverTest.class.getClassLoader().getResource("loading/main.xsd"); final URL main = RelativeUriResolverTest.class.getClassLoader().getResource("loading/main.xsd");
assert main != null;
final Schema schema = this.repository.createSchema(main, new ClassPathResourceResolver("/loading")); final Schema schema = this.repository.createSchema(main, new ClassPathResourceResolver("/loading"));
assertThat(schema).isNotNull(); assertThat(schema).isNotNull();
} }
@ -129,6 +131,7 @@ public class ContentRepositoryTest {
@Test @Test
public void testLoadSchemaPackaged() throws URISyntaxException { public void testLoadSchemaPackaged() throws URISyntaxException {
final URL main = RelativeUriResolverTest.class.getClassLoader().getResource("packaged/main.xsd"); final URL main = RelativeUriResolverTest.class.getClassLoader().getResource("packaged/main.xsd");
assert main != null;
final Schema schema = this.repository.createSchema(main, final Schema schema = this.repository.createSchema(main,
new ClassPathResourceResolver(RelativeUriResolverTest.class.getClassLoader().getResource("packaged/").toURI())); new ClassPathResourceResolver(RelativeUriResolverTest.class.getClassLoader().getResource("packaged/").toURI()));
assertThat(schema).isNotNull(); assertThat(schema).isNotNull();

View file

@ -53,6 +53,7 @@ import net.sf.saxon.s9api.XdmNode;
*/ */
public class Helper { public class Helper {
public static class Simple { public static class Simple {
public static final URI ROOT = EXAMPLES_DIR.resolve("simple/"); public static final URI ROOT = EXAMPLES_DIR.resolve("simple/");
@ -83,12 +84,9 @@ public class Helper {
public static final ContentRepository createContentRepository() { public static final ContentRepository createContentRepository() {
final ResolvingConfigurationStrategy strategy = ResolvingMode.STRICT_RELATIVE.getStrategy(); final ResolvingConfigurationStrategy strategy = ResolvingMode.STRICT_RELATIVE.getStrategy();
final ContentRepository rep = new ContentRepository(TestObjectFactory.createProcessor(), Simple.REPOSITORY_URI, return new ContentRepository(strategy, Simple.REPOSITORY_URI);
strategy.createResolver(Simple.REPOSITORY_URI));
rep.setResolvingConfigurationStrategy(strategy);
rep.setSchemaFactory(strategy.createSchemaFactory());
return rep;
} }
public static URI getSchemaLocation() { public static URI getSchemaLocation() {
return ROOT.resolve("repository/simple.xsd"); return ROOT.resolve("repository/simple.xsd");
} }
@ -103,27 +101,18 @@ public class Helper {
public static final URI SCENARIOS_ILLFORMED = ROOT.resolve("scenarios-illformed.xml"); public static final URI SCENARIOS_ILLFORMED = ROOT.resolve("scenarios-illformed.xml");
} }
public static final URI MODEL_ROOT = Paths.get("src/main/model").toUri(); public static final URI MODEL_ROOT = Paths.get("src/main/model").toUri();
public static final URI ASSERTION_SCHEMA = MODEL_ROOT.resolve("xsd/assertions.xsd"); public static final URI ASSERTION_SCHEMA = MODEL_ROOT.resolve("xsd/assertions.xsd");
public static final URI TEST_ROOT = Paths.get("src/test/resources").toUri(); public static final URI TEST_ROOT = Paths.get("src/test/resources").toUri();
public static final URI EXAMPLES_DIR = TEST_ROOT.resolve("examples/"); public static final URI EXAMPLES_DIR = TEST_ROOT.resolve("examples/");
public static final URI ASSERTIONS = EXAMPLES_DIR.resolve("assertions/tests-xrechnung.xml"); public static final URI ASSERTIONS = EXAMPLES_DIR.resolve("assertions/tests-xrechnung.xml");
public static final URL JAR_REPOSITORY = Helper.class.getClassLoader().getResource("xrechnung/repository/"); public static final URL JAR_REPOSITORY = Helper.class.getClassLoader().getResource("xrechnung/repository/");
/** /**
* Lädt ein XML-Dokument von der gegebenen URL * Lädt ein XML-Dokument von der gegebenen URL
* *
@ -154,7 +143,8 @@ public class Helper {
* @return ein {@link ContentRepository} * @return ein {@link ContentRepository}
*/ */
public static ContentRepository loadTestRepository() { public static ContentRepository loadTestRepository() {
return new ContentRepository(TestObjectFactory.createProcessor(), new File("src/test/resources/examples/repository").toURI(), null); return new ContentRepository(ResolvingMode.STRICT_RELATIVE.getStrategy(),
new File("src/test/resources/examples/repository").toURI());
} }
public static String serialize(final Document doc) { public static String serialize(final Document doc) {

View file

@ -139,6 +139,6 @@ public class ScenarioRepositoryTest {
} }
private static XPathExecutable createXpath(final String expression) { private static XPathExecutable createXpath(final String expression) {
return new ContentRepository(TestObjectFactory.createProcessor(), null, null).createXPath(expression, new HashMap<>()); return new ContentRepository(ResolvingMode.STRICT_RELATIVE.getStrategy(), null).createXPath(expression, new HashMap<>());
} }
} }

View file

@ -10,7 +10,7 @@ import org.junit.Test;
import de.kosit.validationtool.api.AcceptRecommendation; import de.kosit.validationtool.api.AcceptRecommendation;
import de.kosit.validationtool.impl.ContentRepository; import de.kosit.validationtool.impl.ContentRepository;
import de.kosit.validationtool.impl.TestObjectFactory; import de.kosit.validationtool.impl.ResolvingMode;
import de.kosit.validationtool.impl.tasks.CheckAction.Bag; import de.kosit.validationtool.impl.tasks.CheckAction.Bag;
import net.sf.saxon.s9api.XPathExecutable; import net.sf.saxon.s9api.XPathExecutable;
@ -105,6 +105,6 @@ public class ComputeAcceptanceActionTest {
private static XPathExecutable createXpath(final String expression) { private static XPathExecutable createXpath(final String expression) {
return new ContentRepository(TestObjectFactory.createProcessor(), null, null).createXPath(expression, new HashMap<>()); return new ContentRepository(ResolvingMode.STRICT_RELATIVE.getStrategy(), null).createXPath(expression, new HashMap<>());
} }
} }

View file

@ -18,7 +18,6 @@ import de.kosit.validationtool.impl.ContentRepository;
import de.kosit.validationtool.impl.Helper; import de.kosit.validationtool.impl.Helper;
import de.kosit.validationtool.impl.ResolvingMode; import de.kosit.validationtool.impl.ResolvingMode;
import de.kosit.validationtool.impl.Scenario; import de.kosit.validationtool.impl.Scenario;
import de.kosit.validationtool.impl.TestObjectFactory;
import de.kosit.validationtool.impl.model.Result; import de.kosit.validationtool.impl.model.Result;
import de.kosit.validationtool.impl.tasks.CheckAction.Bag; import de.kosit.validationtool.impl.tasks.CheckAction.Bag;
import de.kosit.validationtool.model.reportInput.CreateReportInput; import de.kosit.validationtool.model.reportInput.CreateReportInput;
@ -74,8 +73,7 @@ public class TestBagBuilder {
} }
private static Schema createSchema(final URL toURL) { private static Schema createSchema(final URL toURL) {
final ContentRepository contentRepository = new ContentRepository(TestObjectFactory.createProcessor(), null, null); final ContentRepository contentRepository = new ContentRepository(ResolvingMode.STRICT_RELATIVE.getStrategy(), null);
contentRepository.setSchemaFactory(ResolvingMode.STRICT_RELATIVE.getStrategy().createSchemaFactory());
return contentRepository.createSchema(toURL); return contentRepository.createSchema(toURL);
} }