diff --git a/mathml-core/src/main/java/com/formulasearchengine/mathmltools/mml/MathDoc.java b/mathml-core/src/main/java/com/formulasearchengine/mathmltools/mml/MathDoc.java index 1a09545..c9f84fd 100644 --- a/mathml-core/src/main/java/com/formulasearchengine/mathmltools/mml/MathDoc.java +++ b/mathml-core/src/main/java/com/formulasearchengine/mathmltools/mml/MathDoc.java @@ -4,6 +4,7 @@ import com.formulasearchengine.mathmltools.io.XmlDocumentReader; import com.formulasearchengine.mathmltools.utils.mml.CSymbol; import com.formulasearchengine.mathmltools.xml.PartialLocalEntityResolver; +import com.sun.org.apache.xerces.internal.dom.DOMInputImpl; import org.apache.commons.lang3.NotImplementedException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -103,14 +104,22 @@ public static String tryFixHeader(String inputXMLString, String prefix) { return inputXMLString; } - private static Validator getXsdValidator() throws ParserConfigurationException, IOException, SAXException, URISyntaxException { + public static DOMInputImpl getMathMLSchema() { + SchemaInput schemaInput = new SchemaInput().invoke(); + InputSource inputSource = schemaInput.getInputSource(); + final DOMInputImpl input = new DOMInputImpl(); + input.setByteStream(inputSource.getByteStream()); + input.setPublicId(inputSource.getPublicId()); + input.setSystemId(inputSource.getSystemId()); + return input; + } + + private static Validator getXsdValidator() { if (v == null) { - SchemaFactory schemaFactory = SchemaFactory.newInstance(Languages.W3C_XML_SCHEMA_NS_URI); - final PartialLocalEntityResolver resolver = new PartialLocalEntityResolver(); - schemaFactory.setResourceResolver(resolver); + SchemaInput schemaInput = new SchemaInput().invoke(); + SchemaFactory schemaFactory = schemaInput.getSchemaFactory(); + InputSource inputSource = schemaInput.getInputSource(); v = new JAXPValidator(Languages.W3C_XML_SCHEMA_NS_URI, schemaFactory); - final InputSource inputSource = resolver.resolveEntity("math", MATHML3_XSD); - assert inputSource != null; final StreamSource streamSource = new StreamSource(inputSource.getByteStream()); streamSource.setPublicId(inputSource.getPublicId()); streamSource.setSystemId(inputSource.getSystemId()); @@ -197,4 +206,26 @@ List getCSymbols() { public Document getDom() { return dom; } + + private static class SchemaInput { + private SchemaFactory schemaFactory; + private InputSource inputSource; + + SchemaFactory getSchemaFactory() { + return schemaFactory; + } + + InputSource getInputSource() { + return inputSource; + } + + SchemaInput invoke() { + schemaFactory = SchemaFactory.newInstance(Languages.W3C_XML_SCHEMA_NS_URI); + final PartialLocalEntityResolver resolver = new PartialLocalEntityResolver(); + schemaFactory.setResourceResolver(resolver); + inputSource = resolver.resolveEntity("math", MATHML3_XSD); + assert inputSource != null; + return this; + } + } } diff --git a/mathml-core/src/test/java/com/formulasearchengine/mathmltools/mml/MathDocTest.java b/mathml-core/src/test/java/com/formulasearchengine/mathmltools/mml/MathDocTest.java new file mode 100644 index 0000000..1c1dc32 --- /dev/null +++ b/mathml-core/src/test/java/com/formulasearchengine/mathmltools/mml/MathDocTest.java @@ -0,0 +1,20 @@ +package com.formulasearchengine.mathmltools.mml; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import com.sun.org.apache.xerces.internal.xs.XSImplementation; +import com.sun.org.apache.xerces.internal.xs.XSLoader; +import com.sun.org.apache.xerces.internal.xs.XSModel; +import org.junit.jupiter.api.Test; +import org.w3c.dom.bootstrap.DOMImplementationRegistry; + +class MathDocTest { + + @Test + void getXsdValidator() throws InstantiationException, IllegalAccessException, ClassNotFoundException { + final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); + final XSImplementation impl = (XSImplementation) registry.getDOMImplementation("XS-Loader"); + XSLoader loader = impl.createXSLoader(null); + XSModel xsd = loader.load(MathDoc.getMathMLSchema()); + assertEquals(2,xsd.getNamespaces().getLength()); + } +}