Skip to content

Commit

Permalink
Xsd (#26)
Browse files Browse the repository at this point in the history
* Export interface to MathML schema
* Help others to work the MathML XSD Schema.
  • Loading branch information
physikerwelt authored and vstange committed Apr 30, 2018
1 parent 4d65f56 commit f0d2b0b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -197,4 +206,26 @@ List<CSymbol> 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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit f0d2b0b

Please sign in to comment.