Skip to content

Commit

Permalink
Manually apply PR #185, better URI-to-File conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
ndw committed Mar 4, 2018
1 parent 1679da3 commit 048e39e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/xmlcalabash/extensions/fileutils/Info.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.xmlcalabash.util.MessageFormatter;
import com.xmlcalabash.util.TreeWriter;
import com.xmlcalabash.util.S9apiUtils;
import com.xmlcalabash.util.URIUtils;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XdmNode;
Expand Down Expand Up @@ -97,7 +98,7 @@ public void run() throws SaxonApiException {
tree.startDocument(step.getNode().getBaseURI());

if ("file".equals(uri.getScheme())) {
File file = new File(uri.getPath());
File file = URIUtils.toFile(uri);

if (!file.exists()) {
if (failOnError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.xmlcalabash.runtime.XAtomicStep;
import com.xmlcalabash.model.RuntimeValue;
import com.xmlcalabash.util.TreeWriter;
import com.xmlcalabash.util.URIUtils;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.SaxonApiException;

Expand Down Expand Up @@ -79,7 +80,7 @@ public void run() throws SaxonApiException {
if (!"file".equals(uri.getScheme())) {
throw new XProcException(step.getNode(), "Only file: scheme URIs are supported by the tempfile step.");
} else {
file = new File(uri.getPath());
file = URIUtils.toFile(uri);
}

if (!file.isDirectory()) {
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/com/xmlcalabash/io/FileDataStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.xmlcalabash.io;

import com.xmlcalabash.core.XProcException;
import com.xmlcalabash.util.URIUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -67,7 +68,7 @@ public URI writeEntry(String href, String base, String media,
URI baseURI = URI.create(base);
URI uri = baseURI.resolve(href);
if ("file".equalsIgnoreCase(uri.getScheme())) {
File file = new File(uri);
File file = URIUtils.toFile(uri);
String suffix = getFileSuffixFromType(media);
if (file.isDirectory() || uri.getPath().endsWith("/")) {
if (!file.isDirectory() && !file.mkdirs()) {
Expand Down Expand Up @@ -114,7 +115,7 @@ public void readEntry(String href, String base, String accept,
URI baseURI = URI.create(base);
URI uri = baseURI.resolve(href);
if ("file".equalsIgnoreCase(uri.getScheme())) {
File file = new File(uri);
File file = URIUtils.toFile(uri);
String type = getContentTypeFromName(file.getName());
if (overrideContentType != null) {
type = overrideContentType;
Expand All @@ -136,7 +137,7 @@ public void infoEntry(String href, String base, String accept,
URI baseURI = URI.create(base);
URI uri = baseURI.resolve(href);
if ("file".equalsIgnoreCase(uri.getScheme())) {
File file = new File(uri);
File file = URIUtils.toFile(uri);
String type;
if (file.isFile()) {
type = getContentTypeFromName(file.getName());
Expand All @@ -157,7 +158,7 @@ public void listEachEntry(String href, String base, String accept,
URI baseURI = URI.create(base);
URI uri = baseURI.resolve(href);
if ("file".equalsIgnoreCase(uri.getScheme())) {
File file = new File(uri);
File file = URIUtils.toFile(uri);

if (!file.canRead()) {
throw XProcException.stepError(12);
Expand Down Expand Up @@ -186,7 +187,7 @@ public URI createList(String href, String base)
URI baseURI = URI.create(base);
URI uri = baseURI.resolve(href);
if ("file".equalsIgnoreCase(uri.getScheme())) {
File file = new File(uri);
File file = URIUtils.toFile(uri);
if (file.isDirectory()) {
return file.toURI();
} else if (file.exists()) {
Expand All @@ -209,7 +210,7 @@ public void deleteEntry(String href, String base)
URI baseURI = URI.create(base);
URI uri = baseURI.resolve(href);
if ("file".equalsIgnoreCase(uri.getScheme())) {
File file = new File(uri);
File file = URIUtils.toFile(uri);
if (!file.exists()) {
throw new FileNotFoundException(file.toURI().toASCIIString());
} else if (!file.delete()) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/xmlcalabash/util/URIUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,16 @@ public static URI makeAbsolute(String localFn) {
URI cwd = cwdAsURI();
return cwd.resolve(encode(localFn));
}

public static File toFile(URI uri) {
if (!"file".equalsIgnoreCase(uri.getScheme())) {
throw new IllegalStateException("Expecting a file URI: " + uri.toASCIIString());
}

if (uri.getAuthority() != null && uri.getAuthority().length() > 0) {
return new File("//"+uri.getAuthority()+uri.getPath());
} else {
return new File(uri.getPath());
}
}
}

0 comments on commit 048e39e

Please sign in to comment.