diff --git a/src/main/java/de/kosit/validationtool/impl/xml/RemoteResolvingStrategy.java b/src/main/java/de/kosit/validationtool/impl/xml/RemoteResolvingStrategy.java index 3be4a65..ebebeb5 100644 --- a/src/main/java/de/kosit/validationtool/impl/xml/RemoteResolvingStrategy.java +++ b/src/main/java/de/kosit/validationtool/impl/xml/RemoteResolvingStrategy.java @@ -1,7 +1,13 @@ package de.kosit.validationtool.impl.xml; +import javax.xml.validation.SchemaFactory; + public class RemoteResolvingStrategy extends StrictLocalResolvingStrategy { - - + @Override + public SchemaFactory createSchemaFactory() { + final SchemaFactory schemaFactory = super.createSchemaFactory(); + allowExternalSchema(schemaFactory, "https,http,file"); + return schemaFactory; + } } diff --git a/src/test/java/de/kosit/validationtool/impl/Helper.java b/src/test/java/de/kosit/validationtool/impl/Helper.java index d0ca817..1fa0837 100644 --- a/src/test/java/de/kosit/validationtool/impl/Helper.java +++ b/src/test/java/de/kosit/validationtool/impl/Helper.java @@ -19,7 +19,6 @@ package de.kosit.validationtool.impl; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; @@ -84,7 +83,6 @@ public class Helper { return new ContentRepository(strategy, Simple.REPOSITORY_URI); } - public static URI getSchemaLocation() { return SCHEMA; } @@ -99,6 +97,15 @@ public class Helper { public static final URI SCENARIOS_ILLFORMED = ROOT.resolve("scenarios-illformed.xml"); } + public static class Resolving { + + public static final URI ROOT = EXAMPLES_DIR.resolve("resolving/"); + + public static final URI SCHEMA_WITH_REMOTE_REFERENCE = ROOT.resolve("withRemote.xsd"); + + public static final URI SCHEMA_WITH_REFERENCE = ROOT.resolve("main.xsd"); + } + public static final URI MODEL_ROOT = Paths.get("src/main/model").toUri(); public static final URI ASSERTION_SCHEMA = MODEL_ROOT.resolve("xsd/assertions.xsd"); @@ -135,16 +142,6 @@ public class Helper { return c.readXml(url.toURI(), type); } - /** - * Lädt das default test repository mit Artefacten für Unit-Tests - * - * @return ein {@link ContentRepository} - */ - public static ContentRepository loadTestRepository() { - return new ContentRepository(ResolvingMode.STRICT_RELATIVE.getStrategy(), - new File("src/test/resources/examples/repository").toURI()); - } - public static String serialize(final XdmNode node) { try ( final StringWriter writer = new StringWriter() ) { final Processor processor = Helper.getTestProcessor(); diff --git a/src/test/java/de/kosit/validationtool/impl/TestObjectFactory.java b/src/test/java/de/kosit/validationtool/impl/TestObjectFactory.java index f239d5b..e647843 100644 --- a/src/test/java/de/kosit/validationtool/impl/TestObjectFactory.java +++ b/src/test/java/de/kosit/validationtool/impl/TestObjectFactory.java @@ -1,94 +1,19 @@ package de.kosit.validationtool.impl; -import java.io.Reader; -import java.io.UnsupportedEncodingException; -import java.net.URI; -import java.net.URLEncoder; -import java.nio.charset.StandardCharsets; +import de.kosit.validationtool.impl.xml.StrictLocalResolvingStrategy; -import javax.xml.transform.Result; -import javax.xml.transform.TransformerException; - -import net.sf.saxon.Configuration; -import net.sf.saxon.expr.XPathContext; -import net.sf.saxon.lib.CollectionFinder; -import net.sf.saxon.lib.FeatureKeys; -import net.sf.saxon.lib.OutputURIResolver; -import net.sf.saxon.lib.ResourceCollection; -import net.sf.saxon.lib.UnparsedTextURIResolver; import net.sf.saxon.s9api.Processor; -import net.sf.saxon.trans.XPathException; /** * @author Andreas Penski */ public class TestObjectFactory { - private static class SecureUriResolver implements CollectionFinder, OutputURIResolver, UnparsedTextURIResolver { - - public static final String MESSAGE = "Configuration error. Resolving ist not allowed"; - - @Override - public OutputURIResolver newInstance() { - return this; - } - - @Override - public Result resolve(final String href, final String base) throws TransformerException { - throw new IllegalStateException(MESSAGE); - } - - @Override - public void close(final Result result) throws TransformerException { - throw new IllegalStateException(MESSAGE); - } - - @Override - public Reader resolve(final URI absoluteURI, final String encoding, final Configuration config) throws XPathException { - throw new IllegalStateException(MESSAGE); - } - - @Override - public ResourceCollection findCollection(final XPathContext context, final String collectionURI) throws XPathException { - throw new IllegalStateException(MESSAGE); - } - } - - private static final String DISSALLOW_DOCTYPE_DECL_FEATURE = "http://apache.org/xml/features/disallow-doctype-decl"; - - private static final String LOAD_EXTERNAL_DTD_FEATURE = "http://apache.org/xml/features/nonvalidating/load-external-dtd"; - - private static final String FEATURE_SECURE_PROCESSING = "http://javax.xml.XMLConstants/feature/secure-processing"; - private static Processor processor; - private static String encode(final String input) { - try { - return URLEncoder.encode(input, StandardCharsets.UTF_8.name()); - } catch (final UnsupportedEncodingException e) { - throw new IllegalStateException("Error encoding property while initializing saxon", e); - } - } - public static Processor createProcessor() { if (processor == null) { - processor = new Processor(false); - // verhindere global im Prinzip alle resolving strategien - final SecureUriResolver resolver = new SecureUriResolver(); - processor.getUnderlyingConfiguration().setCollectionFinder(resolver); - processor.getUnderlyingConfiguration().setOutputURIResolver(resolver); - processor.getUnderlyingConfiguration().setUnparsedTextURIResolver(resolver); - - // grundsätzlich Feature-konfiguration: - processor.setConfigurationProperty(FeatureKeys.DTD_VALIDATION, false); - processor.setConfigurationProperty(FeatureKeys.ENTITY_RESOLVER_CLASS, ""); - processor.setConfigurationProperty(FeatureKeys.XINCLUDE, false); - processor.setConfigurationProperty(FeatureKeys.ALLOW_EXTERNAL_FUNCTIONS, false); - - // Konfiguration des zu verwendenden Parsers, wenn Saxon selbst einen erzeugen muss, bspw. beim XSL parsen - processor.setConfigurationProperty(FeatureKeys.XML_PARSER_FEATURE + encode(FEATURE_SECURE_PROCESSING), true); - processor.setConfigurationProperty(FeatureKeys.XML_PARSER_FEATURE + encode(DISSALLOW_DOCTYPE_DECL_FEATURE), true); - processor.setConfigurationProperty(FeatureKeys.XML_PARSER_FEATURE + encode(LOAD_EXTERNAL_DTD_FEATURE), false); + processor = new StrictLocalResolvingStrategy().getProcessor(); } return processor; } diff --git a/src/test/java/de/kosit/validationtool/impl/xml/BaseResolverTest.java b/src/test/java/de/kosit/validationtool/impl/xml/BaseResolverConfigurationTest.java similarity index 98% rename from src/test/java/de/kosit/validationtool/impl/xml/BaseResolverTest.java rename to src/test/java/de/kosit/validationtool/impl/xml/BaseResolverConfigurationTest.java index 51a2871..a37718d 100644 --- a/src/test/java/de/kosit/validationtool/impl/xml/BaseResolverTest.java +++ b/src/test/java/de/kosit/validationtool/impl/xml/BaseResolverConfigurationTest.java @@ -23,7 +23,7 @@ import lombok.RequiredArgsConstructor; * * @author Andreas Penski */ -public class BaseResolverTest { +public class BaseResolverConfigurationTest { @RequiredArgsConstructor private class TestResolvingStrategy extends StrictRelativeResolvingStrategy { diff --git a/src/test/java/de/kosit/validationtool/impl/xml/RemoteResolvingStrategyTest.java b/src/test/java/de/kosit/validationtool/impl/xml/RemoteResolvingStrategyTest.java new file mode 100644 index 0000000..a60244c --- /dev/null +++ b/src/test/java/de/kosit/validationtool/impl/xml/RemoteResolvingStrategyTest.java @@ -0,0 +1,41 @@ +package de.kosit.validationtool.impl.xml; + +import static org.assertj.core.api.Assertions.assertThat; + +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import de.kosit.validationtool.api.ResolvingConfigurationStrategy; +import de.kosit.validationtool.impl.Helper.Resolving; + +/** + * Tests {@link RemoteResolvingStrategy}. + * + * @author Andreas Penski + */ +public class RemoteResolvingStrategyTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void testRemoteSchemaResolving() throws Exception { + final ResolvingConfigurationStrategy s = new RemoteResolvingStrategy(); + final SchemaFactory schemaFactory = s.createSchemaFactory(); + final Schema schema = schemaFactory.newSchema(Resolving.SCHEMA_WITH_REMOTE_REFERENCE.toURL()); + assertThat(schema).isNotNull(); + } + + @Test + public void testLocalSchemaResolving() throws Exception { + final ResolvingConfigurationStrategy s = new StrictLocalResolvingStrategy(); + final SchemaFactory schemaFactory = s.createSchemaFactory(); + final Schema schema = schemaFactory.newSchema(Resolving.SCHEMA_WITH_REFERENCE.toURL()); + assertThat(schema).isNotNull(); + } + +} diff --git a/src/test/java/de/kosit/validationtool/impl/SaxonSecurityTest.java b/src/test/java/de/kosit/validationtool/impl/xml/SaxonSecurityTest.java similarity index 96% rename from src/test/java/de/kosit/validationtool/impl/SaxonSecurityTest.java rename to src/test/java/de/kosit/validationtool/impl/xml/SaxonSecurityTest.java index 70ac28c..0a63475 100644 --- a/src/test/java/de/kosit/validationtool/impl/SaxonSecurityTest.java +++ b/src/test/java/de/kosit/validationtool/impl/xml/SaxonSecurityTest.java @@ -17,7 +17,7 @@ * under the License. */ -package de.kosit.validationtool.impl; +package de.kosit.validationtool.impl.xml; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; @@ -35,9 +35,10 @@ import org.junit.Test; import lombok.extern.slf4j.Slf4j; import de.kosit.validationtool.api.InputFactory; +import de.kosit.validationtool.impl.Helper; import de.kosit.validationtool.impl.Helper.Simple; +import de.kosit.validationtool.impl.TestObjectFactory; import de.kosit.validationtool.impl.model.Result; -import de.kosit.validationtool.impl.xml.RelativeUriResolver; import de.kosit.validationtool.model.reportInput.XMLSyntaxError; import net.sf.saxon.s9api.Processor; diff --git a/src/test/java/de/kosit/validationtool/impl/xml/StrictLocalResolvingTest.java b/src/test/java/de/kosit/validationtool/impl/xml/StrictLocalResolvingTest.java new file mode 100644 index 0000000..7be18d8 --- /dev/null +++ b/src/test/java/de/kosit/validationtool/impl/xml/StrictLocalResolvingTest.java @@ -0,0 +1,43 @@ +package de.kosit.validationtool.impl.xml; + +import static org.assertj.core.api.Assertions.assertThat; + +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; + +import org.hamcrest.Matchers; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.xml.sax.SAXParseException; + +import de.kosit.validationtool.api.ResolvingConfigurationStrategy; +import de.kosit.validationtool.impl.Helper.Resolving; + +/** + * Tests {@link StrictLocalResolvingStrategy} + * + * @author Andreas Penski + */ +public class StrictLocalResolvingTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void testRemoteSchemaResolving() throws Exception { + this.expectedException.expect(SAXParseException.class); + this.expectedException.expectMessage(Matchers.containsString("schema_reference")); + final ResolvingConfigurationStrategy s = new StrictLocalResolvingStrategy(); + final SchemaFactory schemaFactory = s.createSchemaFactory(); + schemaFactory.newSchema(Resolving.SCHEMA_WITH_REMOTE_REFERENCE.toURL()); + } + + @Test + public void testLocalSchemaResolving() throws Exception { + final ResolvingConfigurationStrategy s = new StrictLocalResolvingStrategy(); + final SchemaFactory schemaFactory = s.createSchemaFactory(); + final Schema schema = schemaFactory.newSchema(Resolving.SCHEMA_WITH_REFERENCE.toURL()); + assertThat(schema).isNotNull(); + } +} diff --git a/src/test/java/de/kosit/validationtool/impl/xml/StrictRelativeResolvingTest.java b/src/test/java/de/kosit/validationtool/impl/xml/StrictRelativeResolvingTest.java new file mode 100644 index 0000000..c6a1fff --- /dev/null +++ b/src/test/java/de/kosit/validationtool/impl/xml/StrictRelativeResolvingTest.java @@ -0,0 +1,45 @@ +package de.kosit.validationtool.impl.xml; + +import static org.assertj.core.api.Assertions.assertThat; + +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; + +import org.hamcrest.Matchers; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.xml.sax.SAXParseException; + +import de.kosit.validationtool.api.ResolvingConfigurationStrategy; +import de.kosit.validationtool.impl.Helper.Resolving; + +/** + * Tests {@link StrictRelativeResolvingStrategy}. + * + * @author Andreas Penski + */ +public class StrictRelativeResolvingTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void testRemoteSchemaResolving() throws Exception { + this.expectedException.expect(SAXParseException.class); + this.expectedException.expectMessage(Matchers.containsString("schema_reference")); + final ResolvingConfigurationStrategy s = new StrictLocalResolvingStrategy(); + final SchemaFactory schemaFactory = s.createSchemaFactory(); + schemaFactory.newSchema(Resolving.SCHEMA_WITH_REMOTE_REFERENCE.toURL()); + } + + @Test + public void testLocalSchemaResolving() throws Exception { + final ResolvingConfigurationStrategy s = new StrictLocalResolvingStrategy(); + final SchemaFactory schemaFactory = s.createSchemaFactory(); + final Schema schema = schemaFactory.newSchema(Resolving.SCHEMA_WITH_REFERENCE.toURL()); + assertThat(schema).isNotNull(); + } + + // TODO loading schema from location outside of the repository - this is still possible yet +} diff --git a/src/test/resources/examples/resolving/main.xsd b/src/test/resources/examples/resolving/main.xsd new file mode 100644 index 0000000..16234da --- /dev/null +++ b/src/test/resources/examples/resolving/main.xsd @@ -0,0 +1,12 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/examples/resolving/resources/reference.xsd b/src/test/resources/examples/resolving/resources/reference.xsd new file mode 100644 index 0000000..6f92671 --- /dev/null +++ b/src/test/resources/examples/resolving/resources/reference.xsd @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/examples/resolving/withRemote.xsd b/src/test/resources/examples/resolving/withRemote.xsd new file mode 100644 index 0000000..89c51b9 --- /dev/null +++ b/src/test/resources/examples/resolving/withRemote.xsd @@ -0,0 +1,14 @@ + + + + + + + + + + + \ No newline at end of file