Skip to content

Commit

Permalink
fix: silence a Saxon warning (Schematron XSLT)
Browse files Browse the repository at this point in the history
The Schematron XSLT built in Jing as an `/..` XPath expression which
always evaluates to the empty sequence.
Saxon 9.8 consequently raises a SXWN9000 warning, when the validators
are created (statically, during EPUBCheck's intializations).

This PR initalizes Jing's transformer factory with a static error listener
that ignores this warning.

This is tested as an integration test (since the warning is produced
on the standard error stream at initialization time), run via the
Maven failsafe plugin.

Fixes #859
  • Loading branch information
rdeltour committed Nov 22, 2018
1 parent f572a86 commit 5045d78
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,19 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xml-maven-plugin</artifactId>
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/com/adobe/epubcheck/xml/XMLValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.net.URISyntaxException;
import java.net.URL;

import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;

import org.idpf.epubcheck.util.saxon.ColumnNumberFunction;
Expand All @@ -54,10 +55,12 @@

import net.sf.saxon.Configuration;
import net.sf.saxon.TransformerFactoryImpl;
import net.sf.saxon.lib.FeatureKeys;
import net.sf.saxon.lib.StandardErrorListener;
import net.sf.saxon.sxpath.IndependentContext;
import net.sf.saxon.sxpath.XPathStaticContext;
import net.sf.saxon.trans.SymbolicName;
import net.sf.saxon.om.StandardNames;
import net.sf.saxon.trans.XPathException;


public class XMLValidator
Expand Down Expand Up @@ -193,6 +196,20 @@ public void initTransformerFactory(TransformerFactory factory)
{
configuration.registerExtensionFunction(new SystemIdFunction());
}
// Used to silence Saxon's warning about an XPath expression in Jing's built-in Schematron XSLT
// See issue #859
factory.setAttribute(FeatureKeys.XSLT_STATIC_ERROR_LISTENER_CLASS, SilencingErrorListener.class.getName());
}
}
}

public static class SilencingErrorListener extends StandardErrorListener {

@Override
public void warning(TransformerException exception) {
XPathException xe = XPathException.makeXPathException(exception);
if (!"SXWN9000".equals(xe.getErrorCodeLocalPart())) {
super.warning(exception);
}
}
}
Expand Down
59 changes: 59 additions & 0 deletions src/test/java/com/adobe/epubcheck/cli/CheckerIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.adobe.epubcheck.cli;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.io.InputStream;

import org.junit.Test;

import com.google.common.collect.ObjectArrays;

public class CheckerIT
{

private static final String[] cmd = new String[] { "java", "-jar", "target/epubcheck.jar" };
private static String valid30EPUB = "src/test/resources/30/epub/valid/";

@Test
public void testValidEPUB()
{
try
{
Process process = run(valid30EPUB + "lorem.epub");
InputStream stderr = process.getErrorStream();
process.waitFor();
assertEmpty(stderr);
assertEquals(0, process.exitValue());
} catch (Exception e)
{
fail(e.getMessage());
}
}

private static Process run(String epub)
{
ProcessBuilder builder = new ProcessBuilder(ObjectArrays.concat(cmd, epub));
try
{
return builder.start();
} catch (IOException e)
{
fail(e.getMessage());
return null;
}
}

private static void assertEmpty(InputStream inputStream)
{
try
{
if (inputStream.read() == -1) return;
fail("stream is not empty");
} catch (IOException e)
{
fail(e.getMessage());
}
}
}

0 comments on commit 5045d78

Please sign in to comment.