From bfd30b5e4bef0339a999ca999afa87815dd1ece3 Mon Sep 17 00:00:00 2001 From: Romain Deltour Date: Fri, 28 Oct 2022 23:40:41 +0200 Subject: [PATCH] refactor: minor internal changes to OCFChecker --- .../com/adobe/epubcheck/ocf/OCFChecker.java | 42 +++++++++++-------- .../com/adobe/epubcheck/ocf/OCFContainer.java | 1 - .../com/adobe/epubcheck/ocf/OCFMetaFile.java | 1 - 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/adobe/epubcheck/ocf/OCFChecker.java b/src/main/java/com/adobe/epubcheck/ocf/OCFChecker.java index 7e03d3edb..181a9a949 100755 --- a/src/main/java/com/adobe/epubcheck/ocf/OCFChecker.java +++ b/src/main/java/com/adobe/epubcheck/ocf/OCFChecker.java @@ -26,11 +26,12 @@ import java.io.IOException; import java.io.InputStream; import java.text.Normalizer; -import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Set; import org.w3c.epubcheck.constants.MIMEType; import org.w3c.epubcheck.core.AbstractChecker; @@ -281,10 +282,11 @@ private boolean checkContainerStructure(OCFCheckerState state) // FIXME 2022 build resourcesProvider depending on MIME type // Get a container Iterable resourcesProvider = new OCFZipResources(context.url); - // Map to store the container resource files - Map resources = new HashMap<>(); - // List to store the container resource directories - List directories = new LinkedList<>(); + // Set to store the normalized paths for duplicate checks + final Set normalizedPaths = new HashSet<>(); + // Lists to store the container entries for later empty directory check + final List filePaths = new LinkedList<>(); + final List directoryPaths = new LinkedList<>(); // Loop through the entries for (OCFResource resource : resourcesProvider) @@ -295,12 +297,12 @@ private boolean checkContainerStructure(OCFCheckerState state) // FIXME 2022 report symbolic links and continue // Check duplicate entries - if (resources.containsKey(resource.getPath().toLowerCase(Locale.ROOT))) + if (normalizedPaths.contains(resource.getPath().toLowerCase(Locale.ROOT))) { context.report.message(MessageId.OPF_060, EPUBLocation.of(context), resource.getPath()); } // Check duplicate entries after NFC normalization - else if (resources.containsKey( + else if (normalizedPaths.contains( Normalizer.normalize(resource.getPath().toLowerCase(Locale.ROOT), Normalizer.Form.NFC))) { context.report.message(MessageId.OPF_061, EPUBLocation.of(context), resource.getPath()); @@ -310,30 +312,34 @@ else if (resources.containsKey( if (resource.isDirectory()) { // the container resource is a directory, - // store it for later checking of empty directories - directories.add(resource.getPath()); + // store its path for later checking of empty directories + directoryPaths.add(resource.getPath()); } else { + // The container resource is a file, + // sStore its path for later checking of empty directories + filePaths.add(resource.getPath()); + normalizedPaths.add(resource.getPath().toLowerCase(Locale.ROOT)); + // Check file name requirements - new OCFFilenameChecker(resource.getPath(), state.context().build()).check();; + new OCFFilenameChecker(resource.getPath(), state.context().build()).check(); - // report entry metadata + // Report entry metadata reportFeatures(resource.getProperties()); - // the container resource is a file, - // add the resource to the container model - resources.put(resource.getPath().toLowerCase(Locale.ROOT), resource); + + // Add the resource to the container model state.addResource(resource); } } // Report empty directories - for (String directory : directories) + for (String directoryPath : directoryPaths) { boolean hasContents = false; - for (OCFResource resource : resources.values()) + for (String filePath : filePaths) { - if (resource.getPath().startsWith(directory)) + if (filePath.startsWith(directoryPath)) { hasContents = true; break; @@ -341,7 +347,7 @@ else if (resources.containsKey( } if (!hasContents) { - report.message(MessageId.PKG_014, EPUBLocation.of(context), directory); + report.message(MessageId.PKG_014, EPUBLocation.of(context), directoryPath); } } return true; diff --git a/src/main/java/com/adobe/epubcheck/ocf/OCFContainer.java b/src/main/java/com/adobe/epubcheck/ocf/OCFContainer.java index e412173d1..372ba8406 100644 --- a/src/main/java/com/adobe/epubcheck/ocf/OCFContainer.java +++ b/src/main/java/com/adobe/epubcheck/ocf/OCFContainer.java @@ -3,7 +3,6 @@ import java.io.IOException; import java.io.InputStream; import java.util.LinkedHashMap; -import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.UUID; diff --git a/src/main/java/com/adobe/epubcheck/ocf/OCFMetaFile.java b/src/main/java/com/adobe/epubcheck/ocf/OCFMetaFile.java index 73fca5f68..097c2ff16 100644 --- a/src/main/java/com/adobe/epubcheck/ocf/OCFMetaFile.java +++ b/src/main/java/com/adobe/epubcheck/ocf/OCFMetaFile.java @@ -1,7 +1,6 @@ package com.adobe.epubcheck.ocf; import com.adobe.epubcheck.api.EPUBLocation; -import com.adobe.epubcheck.opf.ValidationContext; import com.google.common.base.Preconditions; import io.mola.galimatias.GalimatiasParseException;