Skip to content

Commit

Permalink
fix: support UNC paths (fix URL/File conversions)
Browse files Browse the repository at this point in the history
EPUBCheck used to work well with UNC paths in 4.x, but this was broken when implementing the new URL and container parsing code.

This commit improves the URL-to-File conversion:
- relies on `java.nio.file.Paths`
- utility methods are introduced in `URLUtils`

This was tested manually on Windows, but not in the test suite since this is largely OS-dependent.

Fix #1485
  • Loading branch information
rdeltour committed Apr 22, 2023
1 parent 1b425fd commit 6e5b791
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
12 changes: 3 additions & 9 deletions src/main/java/com/adobe/epubcheck/ocf/OCFZipResources.java
Expand Up @@ -3,7 +3,6 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Enumeration;
Expand All @@ -13,6 +12,8 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.w3c.epubcheck.util.url.URLUtils;

import com.adobe.epubcheck.util.FeatureEnum;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
Expand All @@ -25,14 +26,7 @@ public class OCFZipResources implements Iterable<OCFResource>

public OCFZipResources(URL url) throws IOException
{
File file = null;
try
{
file = new File(url.toJavaURI());
} catch (URISyntaxException e)
{
new IllegalArgumentException("Not a file URL: " + url);
}
File file = URLUtils.toFile(url);
this.zip = new ZipFile(file, StandardCharsets.UTF_8);
}

Expand Down
10 changes: 1 addition & 9 deletions src/main/java/com/adobe/epubcheck/opf/ValidationContext.java
@@ -1,7 +1,5 @@
package com.adobe.epubcheck.opf;

import java.io.File;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.Locale;
Expand Down Expand Up @@ -163,13 +161,7 @@ private String computePath()
}
else if ("file".equals(url.scheme()))
{
try
{
return new File(url.toJavaURI()).getAbsolutePath();
} catch (URISyntaxException e)
{
return url.toHumanString();
}
return URLUtils.toFilePath(url);
}
else
{
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/org/w3c/epubcheck/util/url/URLUtils.java
Expand Up @@ -6,6 +6,8 @@
import static io.mola.galimatias.URLUtils.percentEncode;

import java.io.File;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;

Expand All @@ -24,6 +26,26 @@ public static URL toURL(File file)
Preconditions.checkArgument(file != null, "file must not be null");
return URL.fromJavaURI(file.toURI());
}

public static File toFile(URL url) {
Preconditions.checkArgument(url != null, "file must not be null");
Preconditions.checkArgument("file".equals(url.scheme()));
try {
return Paths.get(url.toJavaURI()).toFile();
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
}

public static String toFilePath(URL url) {
Preconditions.checkArgument(url != null, "file must not be null");
Preconditions.checkArgument("file".equals(url.scheme()));
try {
return Paths.get(url.toJavaURI()).toString();
} catch (Exception e) {
return decode(url.path());
}
}

public static URL docURL(URL url)
{
Expand Down

0 comments on commit 6e5b791

Please sign in to comment.