From 789ac98fa258692a25eaf5f380088479caa389ac Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 26 Apr 2016 23:25:42 +0100 Subject: Add runtime classes so that PathRelativeFileReader works on .NET --- dotnet/Mammoth/Couscous/java/net/URI.cs | 42 ++++++++++++++++++++++ .../Couscous/java/net/URISyntaxException.cs | 6 ++++ dotnet/Mammoth/Couscous/java/net/URL.cs | 26 ++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 dotnet/Mammoth/Couscous/java/net/URI.cs create mode 100644 dotnet/Mammoth/Couscous/java/net/URISyntaxException.cs create mode 100644 dotnet/Mammoth/Couscous/java/net/URL.cs (limited to 'dotnet/Mammoth/Couscous/java/net') diff --git a/dotnet/Mammoth/Couscous/java/net/URI.cs b/dotnet/Mammoth/Couscous/java/net/URI.cs new file mode 100644 index 0000000..818d694 --- /dev/null +++ b/dotnet/Mammoth/Couscous/java/net/URI.cs @@ -0,0 +1,42 @@ +using System; + +namespace Mammoth.Couscous.java.net { + internal class URI { + private readonly Uri _uri; + + internal URI(string uri) { + try { + _uri = new Uri(uri, UriKind.RelativeOrAbsolute); + } catch (UriFormatException exception) { + throw new URISyntaxException(exception.Message); + } + } + + internal URI(Uri uri) { + _uri = uri; + } + + internal bool isAbsolute() { + return _uri.IsAbsoluteUri; + } + + internal URL toURL() { + return new URL(_uri.ToString()); + } + + internal URI resolve(string relativeUri) { + if (new URI(relativeUri).isAbsolute()) { + return new URI(relativeUri); + } else if (_uri.IsAbsoluteUri) { + return new URI(new Uri(_uri, relativeUri)); + } else { + var path = _uri.ToString(); + var lastSlashIndex = path.LastIndexOf("/"); + var basePath = lastSlashIndex == -1 + ? "." + : path.Substring(0, lastSlashIndex + 1); + return new URI(basePath + relativeUri); + } + } + } +} diff --git a/dotnet/Mammoth/Couscous/java/net/URISyntaxException.cs b/dotnet/Mammoth/Couscous/java/net/URISyntaxException.cs new file mode 100644 index 0000000..7380ff6 --- /dev/null +++ b/dotnet/Mammoth/Couscous/java/net/URISyntaxException.cs @@ -0,0 +1,6 @@ +namespace Mammoth.Couscous.java.net { + internal class URISyntaxException : System.Exception { + internal URISyntaxException(string message) : base(message) { + } + } +} diff --git a/dotnet/Mammoth/Couscous/java/net/URL.cs b/dotnet/Mammoth/Couscous/java/net/URL.cs new file mode 100644 index 0000000..af6e021 --- /dev/null +++ b/dotnet/Mammoth/Couscous/java/net/URL.cs @@ -0,0 +1,26 @@ +using System.Net; +using Mammoth.Couscous.java.io; + +namespace Mammoth.Couscous.java.net { + internal class URL { + private readonly string _url; + + internal URL(string url) { + _url = url; + } + + internal InputStream openStream() { + try { + var response = WebRequest.Create(_url).GetResponse(); + try { + return ToJava.StreamToInputStream(response.GetResponseStream()); + } catch { + response.Close(); + throw; + } + } catch (System.UriFormatException) { + return ToJava.StreamToInputStream(System.IO.File.OpenRead(_url)); + } + } + } +} -- cgit v1.2.3