Skip to content

Commit 545b7f7

Browse files
committed
feat: improve fallback detection check
This commit updates fallback-related checks and refactors the code used for checking references to publication resources. The following checks are introduced or updated: - `RSC-032` (new): reports foreign resources with no fallback used in content documents. Replaces `MED-001`, `MED-002`, and `CSS-010` - `MED-003` now reports when an `img` element child of a `picture` element is not a core media type. - `MED-001` is suppressed. It was used to report a video `poster` attribute did not reference a core image media type. It is now reported as `RSC-032`. - `MED-002` is suppressed. It was used to report HTML elements referencing foreign resources without fallback. It is now reported as `RSC-032`. - `CSS-010` is suppressed. It was used to report references to foreign stylesheets with no fallback. It is now reported as `RSC-032`. - `OPF_040` is now reported when the `fallback` attribute of a package document `item` element does not points to an existing ID. This was previously implemented as a Schematron check (`RSC-005`) in EPUB 3.x. - `OPF-013` is now a warning. It is reported when a MIME type declared inline in content (for instance with an HTML `type` attribute) does not match the MIME type declared in the package document. The code is refactored as follows: - the reference/resource registry functionality of the `XRefChecker` class is extracted to new top-level classes in the (new) package `org.w3c.epubcheck.references`. - `Resource` represents a publication resource - `ResourceRegistry` is a registry of `Resource` instances - `Reference` represents a reference (URL) used anywhere in content - `ReferenceRegistry` is a registry of `Reference` instances - `XRefChecker` is renamed to `ResourceReferencesChecker`. - `ValidationContext` contains optional references to `ResourceRegistry` and `ReferenceRegistry`. - fallback chain resolution is now done in a new `FallbackChainResolver` class, when building `OPFItem` instances from the builders created when parsing the package document. - `XMLHandler` has convenience methods used to register references to the `ReferenceRegistry`. Fix #1304, Fix #1298.
1 parent fdf9b22 commit 545b7f7

File tree

351 files changed

+3065
-2960
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

351 files changed

+3065
-2960
lines changed

OCFCheckerCopy.java

Lines changed: 0 additions & 566 deletions
This file was deleted.

XRefChecker_copy.java

Lines changed: 0 additions & 608 deletions
This file was deleted.

src/main/java/com/adobe/epubcheck/api/EpubCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
import org.w3c.epubcheck.constants.MIMEType;
3434
import org.w3c.epubcheck.core.Checker;
35-
import org.w3c.epubcheck.url.URLUtils;
35+
import org.w3c.epubcheck.util.url.URLUtils;
3636

3737
import com.adobe.epubcheck.messages.MessageId;
3838
import com.adobe.epubcheck.ocf.OCFChecker;

src/main/java/com/adobe/epubcheck/css/CSSHandler.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@
1919
import org.idpf.epubcheck.util.css.CssGrammar.CssSelector;
2020
import org.idpf.epubcheck.util.css.CssGrammar.CssURI;
2121
import org.idpf.epubcheck.util.css.CssLocation;
22-
import org.w3c.epubcheck.url.URLChecker;
22+
import org.w3c.epubcheck.core.references.URLChecker;
23+
import org.w3c.epubcheck.core.references.Reference;
2324

2425
import com.adobe.epubcheck.api.EPUBLocation;
2526
import com.adobe.epubcheck.api.Report;
2627
import com.adobe.epubcheck.messages.MessageId;
2728
import com.adobe.epubcheck.opf.OPFChecker;
2829
import com.adobe.epubcheck.opf.OPFChecker30;
2930
import com.adobe.epubcheck.opf.ValidationContext;
30-
import com.adobe.epubcheck.opf.XRefChecker;
31-
import com.adobe.epubcheck.opf.XRefChecker.Type;
3231
import com.adobe.epubcheck.util.EPUBVersion;
3332
import com.adobe.epubcheck.util.FeatureEnum;
3433
import com.adobe.epubcheck.vocab.PackageVocabs;
@@ -42,7 +41,6 @@
4241
public class CSSHandler implements CssContentHandler, CssErrorHandler
4342
{
4443
final ValidationContext context;
45-
final XRefChecker xrefChecker;
4644
final Report report;
4745
final EPUBVersion version;
4846
int startingLineNumber = 0; // append to line info from css parser
@@ -69,7 +67,6 @@ public class CSSHandler implements CssContentHandler, CssErrorHandler
6967
public CSSHandler(ValidationContext context)
7068
{
7169
this.context = context;
72-
this.xrefChecker = context.xrefChecker.orNull();
7370
this.report = context.report;
7471
this.version = context.version;
7572
this.urlChecker = new URLChecker(context);
@@ -159,7 +156,7 @@ else if (uriOrString.getType() == CssConstruct.Type.STRING)
159156
}
160157
if (uri != null)
161158
{
162-
resolveAndRegister(uri, line, col, atRule.toCssString(), Type.GENERIC);
159+
resolveAndRegister(uri, line, col, atRule.toCssString(), Reference.Type.GENERIC);
163160
}
164161
}
165162
}
@@ -322,10 +319,10 @@ else if (propertyName.equals("src"))
322319
if (construct.getType() == CssConstruct.Type.URI)
323320
{
324321
URL fontURL = parsedURLs.get(((CssURI) construct).toUriString());
325-
if (fontURL != null)
322+
if (fontURL != null && context.resourceRegistry.isPresent())
326323
{
327324
// check font mimetypes
328-
String fontMimeType = xrefChecker.getMimeType(fontURL);
325+
String fontMimeType = context.getMimeType(fontURL);
329326
if (fontMimeType != null)
330327
{
331328
boolean blessed = true;
@@ -367,12 +364,13 @@ private void registerURIs(List<CssConstruct> constructs, int line, int col)
367364
if (construct.getType() == CssConstruct.Type.URI)
368365
{
369366
resolveAndRegister(((CssURI) construct).toUriString(), line, col, construct.toCssString(),
370-
inFontFace ? Type.FONT : Type.GENERIC);
367+
inFontFace ? Reference.Type.FONT : Reference.Type.GENERIC);
371368
}
372369
}
373370
}
374371

375-
private void resolveAndRegister(String uriString, int line, int col, String cssContext, Type type)
372+
private void resolveAndRegister(String uriString, int line, int col, String cssContext,
373+
Reference.Type type)
376374
{
377375
if (uriString != null && uriString.trim().length() > 0)
378376
{
@@ -386,9 +384,9 @@ private void resolveAndRegister(String uriString, int line, int col, String cssC
386384
URL url = urlChecker.checkURL(uriString, getCorrectedEPUBLocation(line, col, cssContext));
387385
parsedURLs.put(uriString, url);
388386

389-
if (url != null)
387+
if (url != null && context.referenceRegistry.isPresent())
390388
{
391-
xrefChecker.registerReference(url, type, getCorrectedEPUBLocation(line, col, cssContext));
389+
context.referenceRegistry.get().registerReference(url, type, getCorrectedEPUBLocation(line, col, cssContext));
392390
if (context.isRemote(url))
393391
{
394392
detectedProperties.add(ITEM_PROPERTIES.REMOTE_RESOURCES);

src/main/java/com/adobe/epubcheck/dict/SearchKeyMapHandler.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.adobe.epubcheck.dict;
22

3+
import org.w3c.epubcheck.core.references.Reference;
4+
35
import com.adobe.epubcheck.opf.ValidationContext;
4-
import com.adobe.epubcheck.opf.XRefChecker.Type;
56
import com.adobe.epubcheck.xml.handlers.XMLHandler;
67
import com.adobe.epubcheck.xml.model.XMLElement;
78

@@ -39,10 +40,7 @@ else if ("match".equals(name))
3940
private void processRef()
4041
{
4142
URL ref = checkURL(currentElement().getAttribute("href"));
42-
if (ref != null && context.xrefChecker.isPresent())
43-
{
44-
context.xrefChecker.get().registerReference(ref, Type.SEARCH_KEY, location());
45-
}
43+
registerReference(ref, Reference.Type.SEARCH_KEY);
4644
}
4745

4846
}

src/main/java/com/adobe/epubcheck/dtbook/DTBookHandler.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222

2323
package com.adobe.epubcheck.dtbook;
2424

25+
import org.w3c.epubcheck.core.references.Reference;
26+
2527
import com.adobe.epubcheck.messages.MessageId;
2628
import com.adobe.epubcheck.opf.ValidationContext;
27-
import com.adobe.epubcheck.opf.XRefChecker;
2829
import com.adobe.epubcheck.util.FeatureEnum;
2930
import com.adobe.epubcheck.util.URISchemes;
3031
import com.adobe.epubcheck.xml.handlers.XMLHandler;
@@ -34,12 +35,10 @@
3435

3536
public class DTBookHandler extends XMLHandler
3637
{
37-
private final XRefChecker xrefChecker;
3838

3939
public DTBookHandler(ValidationContext context)
4040
{
4141
super(context);
42-
this.xrefChecker = context.xrefChecker.get();
4342
}
4443

4544
@Override
@@ -51,11 +50,15 @@ public void startElement()
5150
if (ns.equals("http://www.daisy.org/z3986/2005/dtbook/"))
5251
{
5352
// Register IDs
54-
xrefChecker.registerID(e.getAttribute("id"), XRefChecker.Type.GENERIC, location());
53+
if (context.resourceRegistry.isPresent())
54+
{
55+
context.resourceRegistry.get().registerID(e.getAttribute("id"), Reference.Type.GENERIC,
56+
location().url);
57+
}
5558

5659
// Check cross-references (link@href | a@href | img@src)
5760
URL url = null;
58-
XRefChecker.Type type = XRefChecker.Type.GENERIC;
61+
Reference.Type type = Reference.Type.GENERIC;
5962
/*
6063
* This section checks to see if the references used are registered
6164
* schema-types and whether they point to external resources. The
@@ -68,8 +71,9 @@ public void startElement()
6871

6972
if (url != null && "true".equals(e.getAttribute("external")))
7073
{
71-
//FIXME 2022 check that external attribute is set for remote URLs
72-
if (context.isRemote(url)) {
74+
// FIXME 2022 check that external attribute is set for remote URLs
75+
if (context.isRemote(url))
76+
{
7377
report.info(path, FeatureEnum.REFERENCE, url.toString());
7478
if (!URISchemes.contains(url.scheme()))
7579
{
@@ -86,12 +90,12 @@ else if (name.equals("link"))
8690
else if (name.equals("img"))
8791
{
8892
url = checkURL(e.getAttribute("src"));
89-
type = XRefChecker.Type.IMAGE;
93+
type = Reference.Type.IMAGE;
9094
}
9195

9296
if (url != null)
9397
{
94-
xrefChecker.registerReference(url, type, location());
98+
registerReference(url, type);
9599
if (context.isRemote(url))
96100
{
97101
report.info(path, FeatureEnum.REFERENCE, url.toString());

src/main/java/com/adobe/epubcheck/messages/DefaultSeverities.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ private void initialize()
144144
severities.put(MessageId.HTM_058, Severity.ERROR);
145145

146146
// Media
147-
severities.put(MessageId.MED_001, Severity.ERROR);
148-
severities.put(MessageId.MED_002, Severity.ERROR);
147+
severities.put(MessageId.MED_001, Severity.SUPPRESSED);
148+
severities.put(MessageId.MED_002, Severity.SUPPRESSED);
149149
severities.put(MessageId.MED_003, Severity.ERROR);
150150
severities.put(MessageId.MED_004, Severity.ERROR);
151151
severities.put(MessageId.MED_005, Severity.ERROR);
@@ -205,7 +205,7 @@ private void initialize()
205205
severities.put(MessageId.OPF_010, Severity.ERROR);
206206
severities.put(MessageId.OPF_011, Severity.ERROR);
207207
severities.put(MessageId.OPF_012, Severity.ERROR);
208-
severities.put(MessageId.OPF_013, Severity.ERROR);
208+
severities.put(MessageId.OPF_013, Severity.WARNING);
209209
severities.put(MessageId.OPF_014, Severity.ERROR);
210210
severities.put(MessageId.OPF_015, Severity.ERROR);
211211
severities.put(MessageId.OPF_016, Severity.ERROR);
@@ -348,6 +348,7 @@ private void initialize()
348348
severities.put(MessageId.RSC_029, Severity.ERROR);
349349
severities.put(MessageId.RSC_030, Severity.ERROR);
350350
severities.put(MessageId.RSC_031, Severity.WARNING);
351+
severities.put(MessageId.RSC_032, Severity.ERROR);
351352

352353
// Scripting
353354
severities.put(MessageId.SCP_001, Severity.SUPPRESSED); // checking scripts is out of scope

src/main/java/com/adobe/epubcheck/messages/MessageId.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ public enum MessageId implements Comparable<MessageId>
342342
RSC_029("RSC-029"),
343343
RSC_030("RSC-030"),
344344
RSC_031("RSC-031"),
345+
RSC_032("RSC-032"),
345346

346347
// Messages relating to scripting
347348
SCP_001("SCP-001"),

src/main/java/com/adobe/epubcheck/nav/NavHandler.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import java.util.EnumSet;
44
import java.util.Set;
55

6+
import org.w3c.epubcheck.core.references.Reference;
7+
68
import com.adobe.epubcheck.messages.MessageId;
79
import com.adobe.epubcheck.opf.ValidationContext;
8-
import com.adobe.epubcheck.opf.XRefChecker;
910
import com.adobe.epubcheck.ops.OPSHandler30;
1011
import com.adobe.epubcheck.util.EpubConstants;
1112
import com.adobe.epubcheck.util.FeatureEnum;
@@ -78,12 +79,11 @@ public void startElement()
7879
// cross-reference checker, to be able to check that they are in reading
7980
// order
8081
// after all the Content Documents have been parsed
81-
else if ((NavType.TOC__PAGE_LIST.contains(currentNavType)) && xrefChecker.isPresent())
82+
else if (NavType.TOC__PAGE_LIST.contains(currentNavType))
8283
{
83-
xrefChecker.get().registerReference(url,
84-
(currentNavType == NavType.TOC) ? XRefChecker.Type.NAV_TOC_LINK
85-
: XRefChecker.Type.NAV_PAGELIST_LINK,
86-
location());
84+
registerReference(url,
85+
(currentNavType == NavType.TOC) ? Reference.Type.NAV_TOC_LINK
86+
: Reference.Type.NAV_PAGELIST_LINK);
8787
}
8888
}
8989
}

src/main/java/com/adobe/epubcheck/ncx/NCXHandler.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222

2323
package com.adobe.epubcheck.ncx;
2424

25+
import org.w3c.epubcheck.core.references.Reference;
26+
2527
import com.adobe.epubcheck.messages.MessageId;
2628
import com.adobe.epubcheck.opf.ValidationContext;
27-
import com.adobe.epubcheck.opf.XRefChecker;
2829
import com.adobe.epubcheck.util.FeatureEnum;
2930
import com.adobe.epubcheck.xml.handlers.XMLHandler;
3031
import com.adobe.epubcheck.xml.model.XMLElement;
@@ -33,14 +34,12 @@
3334

3435
public class NCXHandler extends XMLHandler
3536
{
36-
private final XRefChecker xrefChecker;
3737
private static final String TEXT = "text";
3838
String uid;
3939

4040
public NCXHandler(ValidationContext context)
4141
{
4242
super(context);
43-
this.xrefChecker = context.xrefChecker.get();
4443
}
4544

4645
@Override
@@ -76,7 +75,7 @@ public void startElement()
7675
{
7776
report.info(path, FeatureEnum.REFERENCE, srcURL.toString());
7877
}
79-
xrefChecker.registerReference(srcURL, XRefChecker.Type.HYPERLINK, location());
78+
registerReference(srcURL, Reference.Type.HYPERLINK);
8079
}
8180
}
8281
else if ("meta".equals(name))

0 commit comments

Comments
 (0)