Permalink
Browse files

Manually apply PR #185, better URI-to-File conversion

  • Loading branch information...
ndw committed Mar 4, 2018
1 parent 9daa087 commit 88e91234d464d5e293146823882a79eb511b85c8
@@ -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;
@@ -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) {
@@ -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;
@@ -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()) {
@@ -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;
@@ -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()) {
@@ -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;
@@ -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());
@@ -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);
@@ -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()) {
@@ -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()) {
@@ -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 88e9123

Please sign in to comment.