Skip to content

Commit

Permalink
refactor: minor internal changes to OCFChecker
Browse files Browse the repository at this point in the history
  • Loading branch information
rdeltour committed Nov 17, 2022
1 parent b6ac8ea commit bfd30b5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
42 changes: 24 additions & 18 deletions src/main/java/com/adobe/epubcheck/ocf/OCFChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -281,10 +282,11 @@ private boolean checkContainerStructure(OCFCheckerState state)
// FIXME 2022 build resourcesProvider depending on MIME type
// Get a container
Iterable<OCFResource> resourcesProvider = new OCFZipResources(context.url);
// Map to store the container resource files
Map<String, OCFResource> resources = new HashMap<>();
// List to store the container resource directories
List<String> directories = new LinkedList<>();
// Set to store the normalized paths for duplicate checks
final Set<String> normalizedPaths = new HashSet<>();
// Lists to store the container entries for later empty directory check
final List<String> filePaths = new LinkedList<>();
final List<String> directoryPaths = new LinkedList<>();

// Loop through the entries
for (OCFResource resource : resourcesProvider)
Expand All @@ -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());
Expand All @@ -310,38 +312,42 @@ 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;
}
}
if (!hasContents)
{
report.message(MessageId.PKG_014, EPUBLocation.of(context), directory);
report.message(MessageId.PKG_014, EPUBLocation.of(context), directoryPath);
}
}
return true;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/adobe/epubcheck/ocf/OCFContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/adobe/epubcheck/ocf/OCFMetaFile.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit bfd30b5

Please sign in to comment.