Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make SVG content conformance checking informative-only #1365

Merged
merged 3 commits into from
Dec 1, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ private void initialize()
severities.put(MessageId.RSC_021, Severity.ERROR);
severities.put(MessageId.RSC_022, Severity.INFO);
severities.put(MessageId.RSC_023, Severity.WARNING);
severities.put(MessageId.RSC_024, Severity.USAGE);
severities.put(MessageId.RSC_025, Severity.USAGE);

// Scripting
severities.put(MessageId.SCP_001, Severity.USAGE);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/adobe/epubcheck/messages/MessageId.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ public enum MessageId implements Comparable<MessageId>
RSC_021("RSC-021"),
RSC_022("RSC-022"),
RSC_023("RSC-023"),
RSC_024("RSC-024"),
RSC_025("RSC-025"),

// Messages relating to scripting
SCP_001("SCP-001"),
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/adobe/epubcheck/ops/OPSChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public class OPSChecker extends PublicationResourceChecker
.putAll(Predicates.and(mimetype("application/xhtml+xml"), version(EPUBVersion.VERSION_2)),
XMLValidators.XHTML_20_NVDL, XMLValidators.XHTML_20_SCH, XMLValidators.IDUNIQUE_20_SCH)
.putAll(Predicates.and(mimetype("application/xhtml+xml"), version(EPUBVersion.VERSION_3)),
XMLValidators.XHTML_30_RNC, XMLValidators.XHTML_30_SCH)
XMLValidators.XHTML_30_RNC, XMLValidators.XHTML_30_SCH, XMLValidators.SVG_INFORMATIVE_30_NVDL)
.putAll(Predicates.and(mimetype("image/svg+xml"), version(EPUBVersion.VERSION_2)),
XMLValidators.SVG_20_NVDL, XMLValidators.IDUNIQUE_20_SCH)
.putAll(Predicates.and(mimetype("image/svg+xml"), version(EPUBVersion.VERSION_3)),
XMLValidators.SVG_30_NVDL)
XMLValidators.SVG_30_NVDL, XMLValidators.SVG_INFORMATIVE_30_NVDL)
.putAll(
and(or(profile(EPUBProfile.DICT), hasPubType(OPFData.DC_TYPE_DICT)),
mimetype("application/xhtml+xml"), version(EPUBVersion.VERSION_3)),
Expand Down
46 changes: 44 additions & 2 deletions src/main/java/com/adobe/epubcheck/xml/XMLParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
Expand Down Expand Up @@ -68,6 +69,46 @@

public class XMLParser extends DefaultHandler implements LexicalHandler, DeclHandler
{

private static final class UsageErrorHandler implements ErrorHandler
{
private final ValidationContext context;

public UsageErrorHandler(ValidationContext context)
{
this.context = context;
}

@Override
public void warning(SAXParseException exception)
throws SAXException
{
context.report.message(MessageId.RSC_024,
EPUBLocation.create(context.path, exception.getLineNumber(), exception.getColumnNumber()),
exception.getMessage());
}

@Override
public void error(SAXParseException exception)
throws SAXException
{
context.report.message(MessageId.RSC_025,
EPUBLocation.create(context.path, exception.getLineNumber(), exception.getColumnNumber()),
exception.getMessage());
}

@Override
public void fatalError(SAXParseException exception)
throws SAXException
{
context.report.message(MessageId.RSC_016,
EPUBLocation.create(context.path, exception.getLineNumber(), exception.getColumnNumber()),
exception.getMessage());

}

}

private static final String SAXPROP_LEXICAL_HANDLER = "http://xml.org/sax/properties/lexical-handler";
private static final String SAXPROP_DECL_HANDLER = "http://xml.org/sax/properties/declaration-handler";
private SAXParser parser;
Expand Down Expand Up @@ -158,8 +199,9 @@ public void addXMLHandler(XMLHandler handler)
public void addValidator(XMLValidator xv)
{
PropertyMapBuilder propertyMapBuilder = new PropertyMapBuilder();
propertyMapBuilder.put(ValidateProperty.ERROR_HANDLER, this);
Validator validator = xv.schema.createValidator(propertyMapBuilder.toPropertyMap());
ErrorHandler eh = xv.isNormative() ? this : new UsageErrorHandler(context);
propertyMapBuilder.put(ValidateProperty.ERROR_HANDLER, eh);
Validator validator = xv.getSchema().createValidator(propertyMapBuilder.toPropertyMap());
ContentHandler contentHandler = validator.getContentHandler();
if (contentHandler != null)
{
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/com/adobe/epubcheck/xml/XMLValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
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 @@ -55,18 +54,16 @@

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.trans.XPathException;


public class XMLValidator
{

Schema schema;
private final Schema schema;
private final boolean isNormative;

/**
* Basic Resolver from Jing modified to add support for resolving zip and
Expand Down Expand Up @@ -223,9 +220,10 @@ public void warning(SAXParseException exception) throws
}

}

public XMLValidator(String schemaName)
public XMLValidator(String schemaName, boolean isNormative)
{
this.isNormative = isNormative;
try
{
String resourcePath = ResourceUtil.getResourcePath(schemaName);
Expand Down Expand Up @@ -271,4 +269,12 @@ public XMLValidator(String schemaName)
throw new Error("Internal error: " + e + " " + schemaName);
}
}

public Schema getSchema() {
return schema;
}

public boolean isNormative() {
return isNormative;
}
}
8 changes: 7 additions & 1 deletion src/main/java/com/adobe/epubcheck/xml/XMLValidators.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public enum XMLValidators
SVG_20_NVDL("schema/20/rng/ops20-svg.nvdl"),
SVG_30_RNC("schema/30/epub-svg-30.rnc"),
SVG_30_NVDL("schema/30/epub-svg-30.nvdl"),
SVG_INFORMATIVE_30_NVDL("schema/30/epub-svg-informative.nvdl", false),
SVG_30_SCH("schema/30/epub-svg-30.sch"),
XHTML_20_NVDL("schema/20/rng/ops20.nvdl"),
XHTML_20_SCH("schema/20/sch/xhtml.sch"),
Expand All @@ -55,7 +56,12 @@ public enum XMLValidators

private XMLValidators(String schemaName)
{
val = new XMLValidator(schemaName);
this(schemaName, true);
}

private XMLValidators(String schemaName, boolean isNormative)
{
this.val = new XMLValidator(schemaName, isNormative);
}

public XMLValidator get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ RSC_020="%1$s" is not a valid URI.
RSC_021=A Search Key Map Document must point to Content Documents ("%1$s" was not found in the spine).
RSC_022=Cannot check image details (requires Java version 7 or higher).
RSC_023=Couldn’t parse host of URL "%1$s" (probably due to disallowed characters or missing slashes after the protocol)
RSC_024=Informative parsing warning: %1$s
RSC_025=Informative parsing error: %1$s

#Scripting
SCP_001=Use of Javascript eval() function in EPUB scripts is a security risk.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<namespace ns="http://www.w3.org/2000/svg">
<validate schema="epub-svg-30.rnc" schemaType="application/relax-ng-compact-syntax"
useMode="allowForeignNS">
<context path="foreignObject" useMode="allowHTMLOnly"/>
<context path="foreignObject" useMode="attachAnyNS"/>
</validate>
<validate schema="epub-svg-30.sch" useMode="attachAnyNS"/>
</namespace>
Expand All @@ -29,23 +29,6 @@
<allow/>
</anyNamespace>
</mode>
<mode name="allowHTMLOnly">
<namespace ns="http://www.w3.org/1999/xhtml">
<attach/>
</namespace>
<namespace ns="http://www.idpf.org/2007/ops" match="attributes">
<attach/>
</namespace>
<namespace ns="http://www.w3.org/1999/xlink" match="attributes">
<attach/>
</namespace>
<namespace ns="http://www.w3.org/XML/1998/namespace" match="attributes">
<attach/>
</namespace>
<namespace ns="" match="attributes">
<attach/>
</namespace>
</mode>
<mode name="attachAnyNS">
<anyNamespace>
<attach/>
Expand Down
18 changes: 17 additions & 1 deletion src/main/resources/com/adobe/epubcheck/schema/30/epub-svg-30.rnc
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
include "./mod/epub-svg11.rnc"
namespace svg = "http://www.w3.org/2000/svg"

include "./mod/epub-xhtml.rnc" {
start = svg
}
include "./mod/epub-mathml3.rnc"
include "./mod/epub-svg-inc.rnc"

svg.title.content |= common.elem.phrasing

svg.foreignObject.content |=
( body.elem
| common.inner.flow
| math
)

mtext.content |= common.elem.phrasing
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://purl.oclc.org/dsdl/schematron">

<ns uri="http://www.w3.org/2000/svg" prefix="svg" />

<pattern></pattern>
<include href="./mod/id-unique.sch"/>
<include href="./mod/epub-svg11-re.sch"/>

</schema>

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<rules xmlns="http://purl.oclc.org/dsdl/nvdl/ns/structure/1.0">
<namespace ns="http://www.w3.org/2000/svg">
<validate schema="epub-svg-informative.rnc" schemaType="application/relax-ng-compact-syntax"/>
</namespace>
<namespace ns="" match="attributes">
<attach/>
</namespace>
<anyNamespace match="elements attributes">
<allow/>
</anyNamespace>
</rules>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace svg = "http://www.w3.org/2000/svg"

include "./mod/epub-xhtml.rnc" {
# This schema applies both to XHTML and SVG documents
start = svg | html.elem
}
include "./mod/svg11/svg11-inc.rnc" {
# ID datatype is set here to the lower HTML requirements,
# proper checking is done by the normative schema
SVG.id.attrib = attribute id { datatype.html5.token }?
}
include "./mod/svg11/inkscape.rnc"

## EPUB-specific additions:

SVG.Core.attrib &= aria.global?
SVG.Core.extra.attrib &= epub.type.attr?

## Do not check restricted elements, they are checked in normative schemas
SVG.foreignObject.content |= any.content
SVG.title.content |= any.content

## Anything

## Any attribute from any namespace, other than the special cases
any.attr = attribute * { text }*
## Any element from any namespace, other than the special cases
any.elem = element * { any.content & any.attr }
## Any content from any namespace
any.content = text & any.elem*
18 changes: 14 additions & 4 deletions src/main/resources/com/adobe/epubcheck/schema/30/epub-xhtml-30.rnc
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
## XHTML5
namespace svg = "http://www.w3.org/2000/svg"

include "./mod/epub-xhtml.rnc"
include "./mod/epub-mathml3.rnc"
include "./mod/epub-svg-inc.rnc" {
svg.attr.id = attribute id { datatype.html5.token }?
}

common.elem.phrasing |= svg
common.elem.phrasing |= math

## SVG and MathML
svg.title.content |= common.elem.phrasing

include "./mod/epub-xhtml-mathml3.rnc"
include "./mod/epub-xhtml-svg11.rnc"
svg.foreignObject.content |=
( common.inner.flow
| math
)
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,4 @@
</rule>
</pattern>

<include href="./mod/epub-svg11-re.sch"/>

</schema>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
<system systemId="http://www.idpf.org/epub/30/schema/epub-svg-30.sch"
uri="epub-svg-30.sch"/>

<system systemId="http://www.idpf.org/epub/30/schema/epub-svg-informative.nvdl"
uri="epub-svg-informative.nvdl"/>

<system systemId="http://www.idpf.org/epub/30/schema/epub-svg-informative.rnc"
uri="epub-svg-informative.rnc"/>

<system systemId="http://www.idpf.org/epub/30/schema/epub-xhtml-30.nvdl"
uri="epub-xhtml-30.nvdl"/>

Expand Down Expand Up @@ -52,13 +58,9 @@
uri="mod/epub-ssml-attrs.rnc"/>
<system systemId="epub-ssml-attrs.rnc" uri="mod/epub-ssml-attrs.rnc"/>

<system systemId="http://www.idpf.org/epub/30/schema/mod/epub-svg11.rnc"
uri="mod/epub-svg11.rnc"/>
<system systemId="epub-svg11.rnc" uri="mod/epub-svg11.rnc"/>

<system systemId="http://www.idpf.org/epub/30/schema/mod/epub-svg11-re.sch"
uri="mod/epub-svg11-re.sch"/>
<system systemId="epub-svg11-re.sch" uri="mod/epub-svg11-re.sch"/>
<system systemId="http://www.idpf.org/epub/30/schema/mod/epub-svg-inc.rnc"
uri="mod/epub-svg-inc.rnc"/>
<system systemId="epub-svg-inc.rnc" uri="mod/epub-svg-inc.rnc"/>

<system systemId="http://www.idpf.org/epub/30/schema/mod/epub-switch.rnc"
uri="mod/epub-switch.rnc"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
default namespace = "http://www.w3.org/2000/svg"

# #####################################################################
## Anything-goes schema for svg #
# #####################################################################

## SVG root
svg = svg.elem
svg.elem =
element svg { any.content & any.attr }

## Special cases
specific.elems =
( svg.foreignObject
| svg.title
)
specific.attrs = svg.attr.id

### SVG ID datatype
svg.attr.id = attribute id { xsd:ID }?

### SVG foreignObject element restrictions
svg.foreignObject =
element foreignObject { any.attr & svg.foreignObject.content }
svg.foreignObject.content = notAllowed

### SVG title element restrictions
svg.title =
element title { any.attr & svg.title.content }
svg.title.content = text

## Anything

## Any attribute from any namespace, other than the special cases
any.attr = attribute * -id { text }* & svg.attr.id
## Any element from any namespace, other than the special cases
any.elem = element * - (foreignObject|title) { any.content & any.attr }
## Any content from any namespace
any.content = text & any.elem* & specific.elems*
Loading