Skip to content

Commit f0d2b0b

Browse files
physikerweltvstange
authored andcommitted
Xsd (#26)
* Export interface to MathML schema * Help others to work the MathML XSD Schema.
1 parent 4d65f56 commit f0d2b0b

2 files changed

Lines changed: 57 additions & 6 deletions

File tree

  • mathml-core/src

mathml-core/src/main/java/com/formulasearchengine/mathmltools/mml/MathDoc.java

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.formulasearchengine.mathmltools.io.XmlDocumentReader;
55
import com.formulasearchengine.mathmltools.utils.mml.CSymbol;
66
import com.formulasearchengine.mathmltools.xml.PartialLocalEntityResolver;
7+
import com.sun.org.apache.xerces.internal.dom.DOMInputImpl;
78
import org.apache.commons.lang3.NotImplementedException;
89
import org.apache.logging.log4j.LogManager;
910
import org.apache.logging.log4j.Logger;
@@ -103,14 +104,22 @@ public static String tryFixHeader(String inputXMLString, String prefix) {
103104
return inputXMLString;
104105
}
105106

106-
private static Validator getXsdValidator() throws ParserConfigurationException, IOException, SAXException, URISyntaxException {
107+
public static DOMInputImpl getMathMLSchema() {
108+
SchemaInput schemaInput = new SchemaInput().invoke();
109+
InputSource inputSource = schemaInput.getInputSource();
110+
final DOMInputImpl input = new DOMInputImpl();
111+
input.setByteStream(inputSource.getByteStream());
112+
input.setPublicId(inputSource.getPublicId());
113+
input.setSystemId(inputSource.getSystemId());
114+
return input;
115+
}
116+
117+
private static Validator getXsdValidator() {
107118
if (v == null) {
108-
SchemaFactory schemaFactory = SchemaFactory.newInstance(Languages.W3C_XML_SCHEMA_NS_URI);
109-
final PartialLocalEntityResolver resolver = new PartialLocalEntityResolver();
110-
schemaFactory.setResourceResolver(resolver);
119+
SchemaInput schemaInput = new SchemaInput().invoke();
120+
SchemaFactory schemaFactory = schemaInput.getSchemaFactory();
121+
InputSource inputSource = schemaInput.getInputSource();
111122
v = new JAXPValidator(Languages.W3C_XML_SCHEMA_NS_URI, schemaFactory);
112-
final InputSource inputSource = resolver.resolveEntity("math", MATHML3_XSD);
113-
assert inputSource != null;
114123
final StreamSource streamSource = new StreamSource(inputSource.getByteStream());
115124
streamSource.setPublicId(inputSource.getPublicId());
116125
streamSource.setSystemId(inputSource.getSystemId());
@@ -197,4 +206,26 @@ List<CSymbol> getCSymbols() {
197206
public Document getDom() {
198207
return dom;
199208
}
209+
210+
private static class SchemaInput {
211+
private SchemaFactory schemaFactory;
212+
private InputSource inputSource;
213+
214+
SchemaFactory getSchemaFactory() {
215+
return schemaFactory;
216+
}
217+
218+
InputSource getInputSource() {
219+
return inputSource;
220+
}
221+
222+
SchemaInput invoke() {
223+
schemaFactory = SchemaFactory.newInstance(Languages.W3C_XML_SCHEMA_NS_URI);
224+
final PartialLocalEntityResolver resolver = new PartialLocalEntityResolver();
225+
schemaFactory.setResourceResolver(resolver);
226+
inputSource = resolver.resolveEntity("math", MATHML3_XSD);
227+
assert inputSource != null;
228+
return this;
229+
}
230+
}
200231
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.formulasearchengine.mathmltools.mml;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import com.sun.org.apache.xerces.internal.xs.XSImplementation;
5+
import com.sun.org.apache.xerces.internal.xs.XSLoader;
6+
import com.sun.org.apache.xerces.internal.xs.XSModel;
7+
import org.junit.jupiter.api.Test;
8+
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
9+
10+
class MathDocTest {
11+
12+
@Test
13+
void getXsdValidator() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
14+
final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
15+
final XSImplementation impl = (XSImplementation) registry.getDOMImplementation("XS-Loader");
16+
XSLoader loader = impl.createXSLoader(null);
17+
XSModel xsd = loader.load(MathDoc.getMathMLSchema());
18+
assertEquals(2,xsd.getNamespaces().getLength());
19+
}
20+
}

0 commit comments

Comments
 (0)