From 09244a493f70aa4bee626a577aa5869585941786 Mon Sep 17 00:00:00 2001 From: Romain Deltour Date: Thu, 15 Dec 2022 23:02:30 +0100 Subject: [PATCH] feat: report as a usage when no reference were found to manifest items This PR introduces a new usage message (`OPF-097`) reported when the manifest includes a resource for which no reference was found in content. This is legit for instance for exempt resources (like data files included in the container). But it is likely an author error otherwise, so reporting a usage message can help to detect that. We do not report an error since: - references can be added via scripting, so the check can only be informative for scripted content - EPUBCheck historically required all container resources to be listed in the manifest (not doing so was reported as a warning, `OPF-003`), which is conflicting with a requirement to **not** list unsused resources Close #1452 --- .../epubcheck/messages/DefaultSeverities.java | 1 + .../adobe/epubcheck/messages/MessageId.java | 1 + .../com/adobe/epubcheck/opf/OPFChecker30.java | 23 +++++++++++++++++++ .../epubcheck/core/references/Reference.java | 21 ++++++++++++++++- .../messages/MessageBundle.properties | 3 ++- .../{content_001.xhtml => content 001.xhtml} | 0 .../tools/20-severity-tester/OPS/toc.ncx | 2 +- .../{content_001.xhtml => content 001.xhtml} | 0 .../20-warning-tester/OPS/content_002.xhtml | 11 --------- .../tools/20-warning-tester/OPS/package.opf | 2 +- .../tools/20-warning-tester/OPS/toc.ncx | 2 +- .../epubcheck/tools/severity_override.txt | 1 + .../EPUB/content_001.xhtml | 1 + 13 files changed, 52 insertions(+), 16 deletions(-) rename src/test/resources/com/adobe/epubcheck/tools/20-severity-tester/OPS/{content_001.xhtml => content 001.xhtml} (100%) rename src/test/resources/com/adobe/epubcheck/tools/20-warning-tester/OPS/{content_001.xhtml => content 001.xhtml} (100%) delete mode 100644 src/test/resources/com/adobe/epubcheck/tools/20-warning-tester/OPS/content_002.xhtml diff --git a/src/main/java/com/adobe/epubcheck/messages/DefaultSeverities.java b/src/main/java/com/adobe/epubcheck/messages/DefaultSeverities.java index b6ad4ace9..bdffe6cbe 100644 --- a/src/main/java/com/adobe/epubcheck/messages/DefaultSeverities.java +++ b/src/main/java/com/adobe/epubcheck/messages/DefaultSeverities.java @@ -291,6 +291,7 @@ private void initialize() severities.put(MessageId.OPF_095, Severity.ERROR); severities.put(MessageId.OPF_096, Severity.ERROR); severities.put(MessageId.OPF_096b, Severity.USAGE); + severities.put(MessageId.OPF_097, Severity.USAGE); // PKG severities.put(MessageId.PKG_001, Severity.WARNING); diff --git a/src/main/java/com/adobe/epubcheck/messages/MessageId.java b/src/main/java/com/adobe/epubcheck/messages/MessageId.java index c0a835807..748996355 100644 --- a/src/main/java/com/adobe/epubcheck/messages/MessageId.java +++ b/src/main/java/com/adobe/epubcheck/messages/MessageId.java @@ -285,6 +285,7 @@ public enum MessageId implements Comparable OPF_095("OPF-095"), OPF_096("OPF-096"), OPF_096b("OPF-096b"), + OPF_097("OPF-097"), // Messages relating to the entire package PKG_001("PKG-001"), diff --git a/src/main/java/com/adobe/epubcheck/opf/OPFChecker30.java b/src/main/java/com/adobe/epubcheck/opf/OPFChecker30.java index 011993962..77e104c66 100644 --- a/src/main/java/com/adobe/epubcheck/opf/OPFChecker30.java +++ b/src/main/java/com/adobe/epubcheck/opf/OPFChecker30.java @@ -122,6 +122,17 @@ protected void checkItem(OPFItem item, OPFHandler opfHandler) { report.message(MessageId.OPF_091, item.getLocation()); } + + // Register media overlay usage + if (context.referenceRegistry.isPresent() && !Strings.isNullOrEmpty(item.getMediaOverlay())) + { + Optional overlay = opfHandler.getItemById(item.getMediaOverlay()); + if (overlay.isPresent()) + { + context.referenceRegistry.get().registerReference(overlay.get().getURL(), + Reference.Type.MEDIA_OVERLAY, item.getLocation()); + } + } } @Override @@ -156,6 +167,18 @@ else if (!context.referenceRegistry.get().hasReferencesTo(item.getURL())) } } + // check that resources in the manifest are referenced (usage report) + // - search the reference registry + // - report if no reference (of a publication-resource type) is found + if (!(item.isInSpine() || item.isNav() || item.isNcx()) + && context.referenceRegistry.isPresent() + && context.referenceRegistry.get().asList().stream() + .noneMatch(ref -> ref.targetResource.equals(item.getURL()) + && ref.type.isPublicationResourceReference())) + { + report.message(MessageId.OPF_097, item.getLocation(), item.getPath()); + } + if (isBlessedItemType(mediatype, version)) { // check whether media-overlay attribute needs to be specified diff --git a/src/main/java/org/w3c/epubcheck/core/references/Reference.java b/src/main/java/org/w3c/epubcheck/core/references/Reference.java index aa30babce..7ac9c5893 100644 --- a/src/main/java/org/w3c/epubcheck/core/references/Reference.java +++ b/src/main/java/org/w3c/epubcheck/core/references/Reference.java @@ -15,6 +15,7 @@ public static enum Type // Publication resources GENERIC, STYLESHEET, + MEDIA_OVERLAY, HYPERLINK, FONT, IMAGE, @@ -30,7 +31,25 @@ public static enum Type SEARCH_KEY, NAV_TOC_LINK, NAV_PAGELIST_LINK, - OVERLAY_TEXT_LINK, + OVERLAY_TEXT_LINK; + + public boolean isPublicationResourceReference() + { + switch (this) + { + case GENERIC: + case STYLESHEET: + case FONT: + case IMAGE: + case AUDIO: + case VIDEO: + case TRACK: + case MEDIA_OVERLAY: + return true; + default: + return false; + } + } } public final URL url; diff --git a/src/main/resources/com/adobe/epubcheck/messages/MessageBundle.properties b/src/main/resources/com/adobe/epubcheck/messages/MessageBundle.properties index 4affd8cad..24e140527 100644 --- a/src/main/resources/com/adobe/epubcheck/messages/MessageBundle.properties +++ b/src/main/resources/com/adobe/epubcheck/messages/MessageBundle.properties @@ -212,7 +212,8 @@ OPF_093=The "media-type" attribute is required for linked resources located in t OPF_094=The "media-type" attribute is required for "%1$s" links. OPF_095=The "media-type" attribute of "voicing" links must be an audio MIME type, but found "%1$s". OPF_096=Non-linear content must be reachable, but found no hyperlink to "%1$s". -OPF_096b=No hyperlink was found to non-linear document "%1$s", please check that it can be reached from scripted content. +OPF_096b=No hyperlink was found to non-linear document "%1$s", please check that it can be reached from scripted content. +OPF_097=Resource "%1$s" is listed in the manifest, but no reference to it was found in content documents. #Package PKG_001=Validating the EPUB against version %1$s but detected version %2$s. diff --git a/src/test/resources/com/adobe/epubcheck/tools/20-severity-tester/OPS/content_001.xhtml b/src/test/resources/com/adobe/epubcheck/tools/20-severity-tester/OPS/content 001.xhtml similarity index 100% rename from src/test/resources/com/adobe/epubcheck/tools/20-severity-tester/OPS/content_001.xhtml rename to src/test/resources/com/adobe/epubcheck/tools/20-severity-tester/OPS/content 001.xhtml diff --git a/src/test/resources/com/adobe/epubcheck/tools/20-severity-tester/OPS/toc.ncx b/src/test/resources/com/adobe/epubcheck/tools/20-severity-tester/OPS/toc.ncx index b4a29aba3..734dee474 100644 --- a/src/test/resources/com/adobe/epubcheck/tools/20-severity-tester/OPS/toc.ncx +++ b/src/test/resources/com/adobe/epubcheck/tools/20-severity-tester/OPS/toc.ncx @@ -12,7 +12,7 @@ Loomings - + diff --git a/src/test/resources/com/adobe/epubcheck/tools/20-warning-tester/OPS/content_001.xhtml b/src/test/resources/com/adobe/epubcheck/tools/20-warning-tester/OPS/content 001.xhtml similarity index 100% rename from src/test/resources/com/adobe/epubcheck/tools/20-warning-tester/OPS/content_001.xhtml rename to src/test/resources/com/adobe/epubcheck/tools/20-warning-tester/OPS/content 001.xhtml diff --git a/src/test/resources/com/adobe/epubcheck/tools/20-warning-tester/OPS/content_002.xhtml b/src/test/resources/com/adobe/epubcheck/tools/20-warning-tester/OPS/content_002.xhtml deleted file mode 100644 index a42aa4e84..000000000 --- a/src/test/resources/com/adobe/epubcheck/tools/20-warning-tester/OPS/content_002.xhtml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - Minimal EPUB - - -

Loomings

-

Call me Ishmael.

- - diff --git a/src/test/resources/com/adobe/epubcheck/tools/20-warning-tester/OPS/package.opf b/src/test/resources/com/adobe/epubcheck/tools/20-warning-tester/OPS/package.opf index 737953278..ab799ae03 100644 --- a/src/test/resources/com/adobe/epubcheck/tools/20-warning-tester/OPS/package.opf +++ b/src/test/resources/com/adobe/epubcheck/tools/20-warning-tester/OPS/package.opf @@ -6,7 +6,7 @@ NOID - + diff --git a/src/test/resources/com/adobe/epubcheck/tools/20-warning-tester/OPS/toc.ncx b/src/test/resources/com/adobe/epubcheck/tools/20-warning-tester/OPS/toc.ncx index 618e220d9..6c03ea08f 100644 --- a/src/test/resources/com/adobe/epubcheck/tools/20-warning-tester/OPS/toc.ncx +++ b/src/test/resources/com/adobe/epubcheck/tools/20-warning-tester/OPS/toc.ncx @@ -11,7 +11,7 @@ Loomings - + diff --git a/src/test/resources/com/adobe/epubcheck/tools/severity_override.txt b/src/test/resources/com/adobe/epubcheck/tools/severity_override.txt index 1024ead97..676281ba2 100644 --- a/src/test/resources/com/adobe/epubcheck/tools/severity_override.txt +++ b/src/test/resources/com/adobe/epubcheck/tools/severity_override.txt @@ -1,4 +1,5 @@ ID Severity Message Suggestion RSC-008 ERROR This is an overridden message This is an overridden suggestion. +PKG-010 ERROR OPF-003 ERROR NCX-004 SUPPRESSED diff --git a/src/test/resources/epub3/09-media-overlays/files/mediaoverlays-active-class-stylesheet-undeclared-valid/EPUB/content_001.xhtml b/src/test/resources/epub3/09-media-overlays/files/mediaoverlays-active-class-stylesheet-undeclared-valid/EPUB/content_001.xhtml index 28757800c..ec6bef886 100644 --- a/src/test/resources/epub3/09-media-overlays/files/mediaoverlays-active-class-stylesheet-undeclared-valid/EPUB/content_001.xhtml +++ b/src/test/resources/epub3/09-media-overlays/files/mediaoverlays-active-class-stylesheet-undeclared-valid/EPUB/content_001.xhtml @@ -3,6 +3,7 @@ Minimal EPUB +

Loomings