(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) {
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<Scenario> list = initializeScenarios(contentRepository);
final Scenario fallbackScenario = initializeFallback(contentRepository);

View file

@ -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<Scenario> scenarios = initializeScenarios(def, contentRepository);

View file

@ -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<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());
}

View file

@ -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");

View file

@ -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();

View file

@ -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) {

View file

@ -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<>());
}
}

View file

@ -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<>());
}
}

View file

@ -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);
}