Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/sample-config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<property name="remove">mspace maligngroup malignmark mstyle mpadded menclose maction</property>

<!-- remove these elements with their children -->
<property name="remove_all">mphantom merror</property>
<property name="remove_all">mphantom merror cerror</property>

<!-- keep attributes that bear semantics -->
<property name="keepAttributes">mathvariant=bold mathvariant=italic mathvariant=bold-italic mathvariant=double-struck mathvariant=bold-fraktur mathvariant=script mathvariant=bold-script mathvariant=fraktur mathvariant=sans-serif mathvariant=bold-sans-serif mathvariant=sans-serif-italic mathvariant=sans-serif-bold-italic mathvariant=monospace mathvariant=initial mathvariant=tailed mathvariant=looped mathvariant=stretched encoding</property>
Expand Down
12 changes: 11 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>cz.muni.fi.mir</groupId>
<artifactId>mathml-canonicalizer</artifactId>
<version>1.4.0</version>
<version>1.5.0</version>
<packaging>jar</packaging>

<name>MathMLCanonicalizer</name>
Expand Down Expand Up @@ -48,6 +48,11 @@
<name>Maros Kucbel</name>
<email>kocka@mail.muni.cz</email>
</developer>
<developer>
<id>lalik</id>
<name>Marian Lalik</name>
<email>xlalik@mail.muni.cz</email>
</developer>
</developers>

<profiles>
Expand Down Expand Up @@ -216,6 +221,11 @@
<artifactId>xmlunit-legacy</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.14.3</version>
</dependency>
<!-- TEST DEPENDENCIES -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
Expand All @@ -35,10 +36,7 @@
import javax.xml.validation.Validator;

import org.apache.commons.io.IOUtils;
import org.jdom2.Document;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter;
import org.xml.sax.SAXException;

import cz.muni.fi.mir.mathmlcanonicalization.modules.DOMModule;
Expand All @@ -47,6 +45,9 @@
import cz.muni.fi.mir.mathmlcanonicalization.modules.StreamModule;
import cz.muni.fi.mir.mathmlcanonicalization.utils.DTDManipulator;

import org.jsoup.*;
import org.jsoup.nodes.Document;

/**
* An input class for MathML canonicalization.
*
Expand All @@ -65,8 +66,13 @@ public final class MathMLCanonicalizer {
* Initializes canonicalizer with default settings
*
* @return itialized canonicalizer
* @throws SecurityException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalArgumentException
*/
public static MathMLCanonicalizer getDefaultCanonicalizer() {
public static MathMLCanonicalizer getDefaultCanonicalizer()
throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
try {
return new MathMLCanonicalizer(Settings.getStreamFromProperty("defaultConfig"));
} catch (ConfigException ex) {
Expand All @@ -85,10 +91,17 @@ public MathMLCanonicalizer() {
* Initializes canonicalizer using configuration file
*
* @param xmlConfigurationStream XML configuration constrained by XML Schema
* in cz.muni.fi.mir.mathmlcanonicalization.configuration.xsd resource file
* @throws ConfigException when configuration cannot be loaded
* in
* cz.muni.fi.mir.mathmlcanonicalization.configuration.xsd
* resource file
* @throws ConfigException when configuration cannot be loaded
* @throws SecurityException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalArgumentException
*/
public MathMLCanonicalizer(InputStream xmlConfigurationStream) throws ConfigException {
public MathMLCanonicalizer(InputStream xmlConfigurationStream) throws ConfigException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException, SecurityException {
if (xmlConfigurationStream == null) {
throw new NullPointerException("xmlConfigurationStream is null");
}
Expand Down Expand Up @@ -137,8 +150,13 @@ public MathMLCanonicalizer addModule(Module module) {
*
* @param moduleName the name of the module class
* @return the canonizer object to allow adding more modules at once
* @throws SecurityException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalArgumentException
*/
public MathMLCanonicalizer addModule(String moduleName) {
public MathMLCanonicalizer addModule(String moduleName)
throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
if (moduleName == null) {
throw new NullPointerException("moduleName");
}
Expand All @@ -150,7 +168,7 @@ public MathMLCanonicalizer addModule(String moduleName) {
+ ".modules." + moduleName;
Class<?> moduleClass = Class.forName(fullyQualified);

return addModule((Module) moduleClass.newInstance());
return addModule((Module) moduleClass.getDeclaredConstructor().newInstance());
} catch (ClassNotFoundException ex) {
LOGGER.log(Level.WARNING, "cannot load module " + moduleName, ex);
} catch (InstantiationException ex) {
Expand Down Expand Up @@ -181,9 +199,15 @@ private void validateXMLConfiguration(InputStream xmlConfigurationStream)

/**
* Loads configuration from XML file, overriding the properties.
*
* @throws SecurityException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalArgumentException
*/
private void loadXMLConfiguration(InputStream xmlConfigurationStream)
throws ConfigException, XMLStreamException {
throws ConfigException, XMLStreamException, IllegalArgumentException, InvocationTargetException,
NoSuchMethodException, SecurityException {
assert xmlConfigurationStream != null;
final XMLInputFactory inputFactory = Settings.defaultXmlInputFactory();
final XMLStreamReader reader = inputFactory.createXMLStreamReader(xmlConfigurationStream);
Expand All @@ -210,7 +234,7 @@ private void loadXMLConfiguration(InputStream xmlConfigurationStream)
+ ".modules." + attributeValue;
try {
Class<?> moduleClass = Class.forName(fullyQualified);
module = (Module) moduleClass.newInstance();
module = (Module) moduleClass.getDeclaredConstructor().newInstance();
} catch (InstantiationException ex) {
LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
throw new ConfigException("cannot instantiate module "
Expand Down Expand Up @@ -275,13 +299,14 @@ private void loadXMLConfiguration(InputStream xmlConfigurationStream)
/**
* Canonicalize an input MathML stream.
*
* @param in input stream to be canonicalized
* @param in input stream to be canonicalized
* @param out canonical output stream of input
* @throws JDOMException problem with DOM
* @throws IOException problem with streams
* @throws ModuleException some module cannot canonicalize the input
* @throws JDOMException problem with DOM
* @throws IOException problem with streams
* @throws ModuleException some module cannot canonicalize
* the input
* @throws javax.xml.stream.XMLStreamException an error with XML processing
* occurs
* occurs
*/
public void canonicalize(final InputStream in, final OutputStream out)
throws JDOMException, IOException, ModuleException, XMLStreamException {
Expand All @@ -303,13 +328,13 @@ public void canonicalize(final InputStream in, final OutputStream out)
streamModulesResult.writeTo(out);
return;
}
final InputStream input = streamModulesResult == null ? in : new ByteArrayInputStream(streamModulesResult.toByteArray());
final InputStream input = streamModulesResult == null ? in
: new ByteArrayInputStream(streamModulesResult.toByteArray());

final Document document = executeDomModules(input);

// convertong the JDOM representation back to stream
final XMLOutputter serializer = new XMLOutputter();
serializer.output(document, out);
out.write(document.html().getBytes("UTF-8"));
}

/**
Expand All @@ -318,21 +343,22 @@ public void canonicalize(final InputStream in, final OutputStream out)
*
* NB: maybe add another method which returns {@link org.w3c.dom.Document}?
*/
public Document canonicalize(final InputStream in) throws ModuleException, IOException, XMLStreamException, JDOMException {
public Document canonicalize(final InputStream in)
throws ModuleException, IOException, XMLStreamException, JDOMException {
if (in == null) {
throw new NullPointerException("Input stream is null");
}

ByteArrayOutputStream streamModulesResult = executeStreamModules(in);
final InputStream input = streamModulesResult == null ? in : new ByteArrayInputStream(streamModulesResult.toByteArray());
final InputStream input = streamModulesResult == null ? in
: new ByteArrayInputStream(streamModulesResult.toByteArray());

return executeDomModules(input);
}

private Document executeDomModules(final InputStream input) throws JDOMException, IOException, ModuleException {
// creating the JDOM representation from the stream
final SAXBuilder builder = Settings.setupSAXBuilder();
final Document document = builder.build(input);
final Document document = Jsoup.parse(input, null, "");

// calling JDOM modules
for (DOMModule module : domModules) {
Expand Down Expand Up @@ -369,7 +395,8 @@ private ByteArrayOutputStream executeStreamModules(final InputStream in)
return removeDtdsIfNecessary(outputStream);
}

private ByteArrayOutputStream removeDtdsIfNecessary(final ByteArrayOutputStream outputStream) throws XMLStreamException {
private ByteArrayOutputStream removeDtdsIfNecessary(final ByteArrayOutputStream outputStream)
throws XMLStreamException {
ByteArrayOutputStream result;

if (enforcingXHTMLPlusMathMLDTD) {
Expand Down Expand Up @@ -398,7 +425,7 @@ private InputStream injectDtdsIfNecessary(final InputStream in) {
* document.
*
* @return XHTML 1.1 plus MathML 2.0 plus SVG 1.1 DTD reference enforcement
* setting
* setting
*/
public boolean isEnforcingXHTMLPlusMathMLDTD() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import cz.muni.fi.mir.mathmlcanonicalization.modules.ModuleException;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -65,9 +66,16 @@ public final class MathMLCanonicalizerCommandLineTool {
/**
* @param args the command line arguments
* @throws javax.xml.stream.XMLStreamException an error with XML processing
* occurs
* occurs
* @throws SecurityException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalArgumentException
*/
public static void main(String[] args) throws TransformerConfigurationException, ParserConfigurationException, SAXException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, ConfigException, FileNotFoundException, JDOMException, ModuleException, XMLStreamException {
public static void main(String[] args) throws TransformerConfigurationException, ParserConfigurationException,
SAXException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException,
ConfigException, FileNotFoundException, JDOMException, ModuleException, XMLStreamException,
IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
final Options options = createOptions();
final CommandLineParser parser = new DefaultParser();
CommandLine line = null;
Expand Down Expand Up @@ -147,46 +155,43 @@ private static Options createOptions() {
.desc("Load configuration file.")
.argName("arg")
.hasArg()
.build()
);
.build());

options.addOption(Option
.builder(OPTION_INJECTION)
.longOpt(OPTION_INJECTION_LONG)
.desc("Enforce injection of XHTML 1.1 plus MathML 2.0 plus SVG 1.1 DTD reference into input documents.")
.hasArg(false)
.build()
);
.build());

options.addOption(Option
.builder(OPTION_OVERWRITE)
.longOpt(OPTION_OVERWRITE_LONG)
.desc("Overwrite input files by produced canonical outputs.")
.hasArg(false)
.build()
);
.build());

options.addOption(Option
.builder(OPTION_PRINT_DEFAULT_CONFIG)
.longOpt(OPTION_PRINT_DEFAULT_CONFIG_LONG)
.desc("Print default configuration that will be used if no config file is supplied.")
.hasArg(false)
.build()
);
.build());

options.addOption(Option
.builder(OPTION_HELP)
.longOpt(OPTION_HELP_LONG)
.desc("Print help (this screen).")
.hasArg(false)
.build()
);
.build());

return options;
}

private static void canonicalize(File file, InputStream config, boolean dtdInjectionMode, boolean overwrite) throws
ConfigException, FileNotFoundException, JDOMException, IOException, ModuleException, XMLStreamException {
private static void canonicalize(File file, InputStream config, boolean dtdInjectionMode, boolean overwrite)
throws ConfigException, FileNotFoundException, JDOMException, IOException, ModuleException,
XMLStreamException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
SecurityException {
assert file != null; // but config can be null
MathMLCanonicalizer mlcan;
if (config != null) {
Expand Down Expand Up @@ -255,7 +260,8 @@ private static void printHelp(Options options) {
formatter.printOptions(output, 80, options, 8, 8);
}

private static void printDefaultConfig() throws ParserConfigurationException, SAXException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
private static void printDefaultConfig() throws ParserConfigurationException, SAXException, IOException,
ClassNotFoundException, InstantiationException, IllegalAccessException {

final InputSource src = new InputSource(Settings.getStreamFromProperty("defaultConfig"));
final Document document = Settings.documentBuilderFactory().newDocumentBuilder().parse(src);
Expand Down
Loading