From 7ca3ef90f3b1585d51c94a89a52d5ec47c30b9b7 Mon Sep 17 00:00:00 2001 From: "Andreas Penski (init)" Date: Wed, 29 Apr 2020 10:29:30 +0200 Subject: [PATCH] (enhance) ContentRepository is only based on uri and strategy --- .../config/ConfigurationBuilder.java | 5 +--- .../config/ConfigurationLoader.java | 5 +--- .../impl/ContentRepository.java | 30 +++++++++++++------ .../validationtool/impl/ObjectFactory.java | 5 ++-- .../impl/ContentRepositoryTest.java | 5 +++- .../de/kosit/validationtool/impl/Helper.java | 20 ++++--------- .../impl/ScenarioRepositoryTest.java | 2 +- .../tasks/ComputeAcceptanceActionTest.java | 4 +-- .../impl/tasks/TestBagBuilder.java | 4 +-- 9 files changed, 38 insertions(+), 42 deletions(-) diff --git a/src/main/java/de/kosit/validationtool/config/ConfigurationBuilder.java b/src/main/java/de/kosit/validationtool/config/ConfigurationBuilder.java index e1ec359..f81e967 100644 --- a/src/main/java/de/kosit/validationtool/config/ConfigurationBuilder.java +++ b/src/main/java/de/kosit/validationtool/config/ConfigurationBuilder.java @@ -172,10 +172,7 @@ public class ConfigurationBuilder { if (this.processor == null) { this.processor = resolving.createProcessor(); } - final ContentRepository contentRepository = new ContentRepository(this.processor, this.repository, - resolving.createResolver(this.repository)); - contentRepository.setSchemaFactory(resolving.createSchemaFactory()); - contentRepository.setResolvingConfigurationStrategy(resolving); + final ContentRepository contentRepository = new ContentRepository(this.resolvingConfigurationStrategy, this.repository); final List list = initializeScenarios(contentRepository); final Scenario fallbackScenario = initializeFallback(contentRepository); diff --git a/src/main/java/de/kosit/validationtool/config/ConfigurationLoader.java b/src/main/java/de/kosit/validationtool/config/ConfigurationLoader.java index 589979a..cff00fc 100644 --- a/src/main/java/de/kosit/validationtool/config/ConfigurationLoader.java +++ b/src/main/java/de/kosit/validationtool/config/ConfigurationLoader.java @@ -118,10 +118,7 @@ public class ConfigurationLoader { public Configuration build() { final ResolvingConfigurationStrategy resolving = getResolvingConfigurationStrategy(); final Processor processor = resolving.createProcessor(); - final ContentRepository contentRepository = new ContentRepository(processor, getScenarioRepository(), - resolving.createResolver(this.getScenarioRepository())); - contentRepository.setSchemaFactory(resolving.createSchemaFactory()); - contentRepository.setResolvingConfigurationStrategy(resolving); + final ContentRepository contentRepository = new ContentRepository(resolving, getScenarioRepository()); final Scenarios def = loadScenarios(contentRepository.getScenarioSchema(), processor); final List scenarios = initializeScenarios(def, contentRepository); diff --git a/src/main/java/de/kosit/validationtool/impl/ContentRepository.java b/src/main/java/de/kosit/validationtool/impl/ContentRepository.java index 139e776..9d69706 100644 --- a/src/main/java/de/kosit/validationtool/impl/ContentRepository.java +++ b/src/main/java/de/kosit/validationtool/impl/ContentRepository.java @@ -42,7 +42,6 @@ import org.xml.sax.SAXException; import lombok.Getter; import lombok.RequiredArgsConstructor; -import lombok.Setter; import lombok.extern.slf4j.Slf4j; import de.kosit.validationtool.api.ResolvingConfigurationStrategy; @@ -78,13 +77,27 @@ public class ContentRepository { private final URIResolver resolver; - @Setter - private SchemaFactory schemaFactory; + private final SchemaFactory schemaFactory; - @Setter @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) { try { return new StreamSource(resource.openStream(), resource.toURI().getRawPath()); @@ -119,10 +132,9 @@ public class ContentRepository { final CollectingErrorEventHandler listener = new CollectingErrorEventHandler(); try { xsltCompiler.setErrorListener(listener); - final URIResolver resolver = getResolver(); - if (resolver != null) { + if (getResolver() != null) { // otherwise use default resolver - xsltCompiler.setURIResolver(resolver); + xsltCompiler.setURIResolver(getResolver()); } return xsltCompiler.compile(resolveInRepository(uri)); @@ -275,7 +287,7 @@ public class ContentRepository { } public List 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()); } diff --git a/src/main/java/de/kosit/validationtool/impl/ObjectFactory.java b/src/main/java/de/kosit/validationtool/impl/ObjectFactory.java index 8c96eae..f1917ba 100644 --- a/src/main/java/de/kosit/validationtool/impl/ObjectFactory.java +++ b/src/main/java/de/kosit/validationtool/impl/ObjectFactory.java @@ -25,7 +25,6 @@ import java.net.URI; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; -import javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; @@ -144,8 +143,8 @@ public class ObjectFactory { Transformer transformer = null; try { final TransformerFactory transformerFactory = TransformerFactory.newInstance(); - transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); - transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, ""); // Compliant + // transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); + // transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, ""); // Compliant transformer = transformerFactory.newTransformer(); if (prettyPrint) { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); diff --git a/src/test/java/de/kosit/validationtool/impl/ContentRepositoryTest.java b/src/test/java/de/kosit/validationtool/impl/ContentRepositoryTest.java index 9a54bee..fe1959c 100644 --- a/src/test/java/de/kosit/validationtool/impl/ContentRepositoryTest.java +++ b/src/test/java/de/kosit/validationtool/impl/ContentRepositoryTest.java @@ -114,7 +114,8 @@ public class ContentRepositoryTest { @Test 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")); assertThat(xsltExecutable).isNotNull(); } @@ -122,6 +123,7 @@ public class ContentRepositoryTest { @Test public void testLoadSchema() { final URL main = RelativeUriResolverTest.class.getClassLoader().getResource("loading/main.xsd"); + assert main != null; final Schema schema = this.repository.createSchema(main, new ClassPathResourceResolver("/loading")); assertThat(schema).isNotNull(); } @@ -129,6 +131,7 @@ public class ContentRepositoryTest { @Test public void testLoadSchemaPackaged() throws URISyntaxException { final URL main = RelativeUriResolverTest.class.getClassLoader().getResource("packaged/main.xsd"); + assert main != null; final Schema schema = this.repository.createSchema(main, new ClassPathResourceResolver(RelativeUriResolverTest.class.getClassLoader().getResource("packaged/").toURI())); assertThat(schema).isNotNull(); diff --git a/src/test/java/de/kosit/validationtool/impl/Helper.java b/src/test/java/de/kosit/validationtool/impl/Helper.java index ad78c94..941429d 100644 --- a/src/test/java/de/kosit/validationtool/impl/Helper.java +++ b/src/test/java/de/kosit/validationtool/impl/Helper.java @@ -53,6 +53,7 @@ import net.sf.saxon.s9api.XdmNode; */ public class Helper { + public static class Simple { public static final URI ROOT = EXAMPLES_DIR.resolve("simple/"); @@ -83,12 +84,9 @@ public class Helper { public static final ContentRepository createContentRepository() { final ResolvingConfigurationStrategy strategy = ResolvingMode.STRICT_RELATIVE.getStrategy(); - final ContentRepository rep = new ContentRepository(TestObjectFactory.createProcessor(), Simple.REPOSITORY_URI, - strategy.createResolver(Simple.REPOSITORY_URI)); - rep.setResolvingConfigurationStrategy(strategy); - rep.setSchemaFactory(strategy.createSchemaFactory()); - return rep; + return new ContentRepository(strategy, Simple.REPOSITORY_URI); } + public static URI getSchemaLocation() { 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 MODEL_ROOT = Paths.get("src/main/model").toUri(); 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 EXAMPLES_DIR = TEST_ROOT.resolve("examples/"); public static final URI ASSERTIONS = EXAMPLES_DIR.resolve("assertions/tests-xrechnung.xml"); - - public static final URL JAR_REPOSITORY = Helper.class.getClassLoader().getResource("xrechnung/repository/"); - - - - - /** * Lädt ein XML-Dokument von der gegebenen URL * @@ -154,7 +143,8 @@ public class Helper { * @return ein {@link ContentRepository} */ 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) { diff --git a/src/test/java/de/kosit/validationtool/impl/ScenarioRepositoryTest.java b/src/test/java/de/kosit/validationtool/impl/ScenarioRepositoryTest.java index ceb8e85..cb11310 100644 --- a/src/test/java/de/kosit/validationtool/impl/ScenarioRepositoryTest.java +++ b/src/test/java/de/kosit/validationtool/impl/ScenarioRepositoryTest.java @@ -139,6 +139,6 @@ public class ScenarioRepositoryTest { } 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<>()); } } diff --git a/src/test/java/de/kosit/validationtool/impl/tasks/ComputeAcceptanceActionTest.java b/src/test/java/de/kosit/validationtool/impl/tasks/ComputeAcceptanceActionTest.java index 541d1c9..3bb04b2 100644 --- a/src/test/java/de/kosit/validationtool/impl/tasks/ComputeAcceptanceActionTest.java +++ b/src/test/java/de/kosit/validationtool/impl/tasks/ComputeAcceptanceActionTest.java @@ -10,7 +10,7 @@ import org.junit.Test; import de.kosit.validationtool.api.AcceptRecommendation; 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 net.sf.saxon.s9api.XPathExecutable; @@ -105,6 +105,6 @@ public class ComputeAcceptanceActionTest { 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<>()); } } diff --git a/src/test/java/de/kosit/validationtool/impl/tasks/TestBagBuilder.java b/src/test/java/de/kosit/validationtool/impl/tasks/TestBagBuilder.java index f7a5d34..716023f 100644 --- a/src/test/java/de/kosit/validationtool/impl/tasks/TestBagBuilder.java +++ b/src/test/java/de/kosit/validationtool/impl/tasks/TestBagBuilder.java @@ -18,7 +18,6 @@ import de.kosit.validationtool.impl.ContentRepository; import de.kosit.validationtool.impl.Helper; import de.kosit.validationtool.impl.ResolvingMode; import de.kosit.validationtool.impl.Scenario; -import de.kosit.validationtool.impl.TestObjectFactory; import de.kosit.validationtool.impl.model.Result; import de.kosit.validationtool.impl.tasks.CheckAction.Bag; import de.kosit.validationtool.model.reportInput.CreateReportInput; @@ -74,8 +73,7 @@ public class TestBagBuilder { } private static Schema createSchema(final URL toURL) { - final ContentRepository contentRepository = new ContentRepository(TestObjectFactory.createProcessor(), null, null); - contentRepository.setSchemaFactory(ResolvingMode.STRICT_RELATIVE.getStrategy().createSchemaFactory()); + final ContentRepository contentRepository = new ContentRepository(ResolvingMode.STRICT_RELATIVE.getStrategy(), null); return contentRepository.createSchema(toURL); }