From 7bef4dd04233c30d02c04bd2a40c60436853841c Mon Sep 17 00:00:00 2001 From: spaceflint <> Date: Mon, 7 Dec 2020 18:50:43 +0200 Subject: [PATCH] URI support for BNA --- Baselib/src/System/Text/DecoderNLS.cs | 11 ++ Baselib/src/System/Uri.cs | 138 ++++++++++++++++++++++++++ Baselib/system.filter | 2 + 3 files changed, 151 insertions(+) create mode 100644 Baselib/src/System/Text/DecoderNLS.cs create mode 100644 Baselib/src/System/Uri.cs diff --git a/Baselib/src/System/Text/DecoderNLS.cs b/Baselib/src/System/Text/DecoderNLS.cs new file mode 100644 index 0000000..bad8bc0 --- /dev/null +++ b/Baselib/src/System/Text/DecoderNLS.cs @@ -0,0 +1,11 @@ + +namespace system.text +{ + + // this class is referenced by System.IO.BinaryReader + + public class DecoderNLS + { + } + +} diff --git a/Baselib/src/System/Uri.cs b/Baselib/src/System/Uri.cs new file mode 100644 index 0000000..0c77333 --- /dev/null +++ b/Baselib/src/System/Uri.cs @@ -0,0 +1,138 @@ + +using System; +using System.Runtime.Serialization; + +namespace system +{ + + public class Uri : ISerializable + { + + [java.attr.RetainType] private java.net.URI JavaURI; + + // + // Constructor + // + + public Uri(string uriString, bool dontEscape, UriKind uriKind) + { + ThrowHelper.ThrowIfNull(uriString); + if (uriString == "") + throw new UriFormatException("empty URI"); + + if (! dontEscape) + { + if (uriKind != UriKind.Relative) + uriString = java.net.URLEncoder.encode(uriString, "UTF-8"); + } + + try + { + JavaURI = new java.net.URI(uriString); + } + catch (java.net.URISyntaxException e) + { + throw new UriFormatException(e.getMessage(), e); + } + + if (uriKind == UriKind.Absolute) + { + if (! JavaURI.isAbsolute()) + throw new UriFormatException("relative URI for " + uriKind); + } + else if (uriKind == UriKind.Relative) + { + if (JavaURI.isAbsolute()) + throw new UriFormatException("absolute URI for " + uriKind); + } + else if (uriKind != UriKind.RelativeOrAbsolute) + { + throw new ArgumentException(); + } + } + + // + // Constructors + // + + public Uri(string uriString) + : this(uriString, false, UriKind.Absolute) { } + + public Uri(string uriString, bool dontEscape) + : this(uriString, dontEscape, UriKind.Absolute) { } + + public Uri(string uriString, UriKind uriKind) + : this(uriString, false, uriKind) { } + + // + // Constructor + // + + public Uri(Uri baseUri, Uri relativeUri) + { + ThrowHelper.ThrowIfNull(baseUri); + if (! baseUri.IsAbsoluteUri) + throw new ArgumentOutOfRangeException(); + + try + { + JavaURI = baseUri.JavaURI.resolve(relativeUri.JavaURI); + } + catch (java.net.URISyntaxException e) + { + throw new UriFormatException(e.getMessage(), e); + } + } + + public Uri(Uri baseUri, string relativeUri, bool dontEscape) + : this(baseUri, new Uri(relativeUri, dontEscape)) { } + + // + // Methods + // + + public override string ToString() => JavaURI.ToString(); + public override int GetHashCode() => JavaURI.GetHashCode(); + public override bool Equals(object other) + { + var otherAsUri = other as Uri; + return (((object) otherAsUri) != null && JavaURI.Equals(otherAsUri.JavaURI)); + } + public static bool operator ==(Uri uri1, Uri uri2) + => (uri1 == null) ? (uri2 == null) : uri1.Equals(uri2); + public static bool operator !=(Uri uri1, Uri uri2) => ! (uri1 == uri2); + + + // + // Properties + // + + public bool IsAbsoluteUri => JavaURI.isAbsolute(); + + public string AbsolutePath => AbsoluteUriOrThrow.getPath(); + public string AbsoluteUri => AbsoluteUriOrThrow.ToString(); + public string LocalPath => AbsoluteUriOrThrow.getPath(); + public string Authority => AbsoluteUriOrThrow.getAuthority(); + public string Host => AbsoluteUriOrThrow.getHost(); + public string Query => AbsoluteUriOrThrow.getQuery(); + public string Fragment => AbsoluteUriOrThrow.getFragment(); + public string Scheme => AbsoluteUriOrThrow.getScheme(); + public string UserInfo => AbsoluteUriOrThrow.getUserInfo(); + public int Port => AbsoluteUriOrThrow.getPort(); + public bool IsDefaultPort => Port == -1; + public bool IsFile => Scheme == "file"; + public bool IsUnc => AbsoluteUriOrThrow.getPath()?.StartsWith("\\") ?? false; + + private java.net.URI AbsoluteUriOrThrow + => JavaURI.isAbsolute() ? JavaURI + : throw new InvalidOperationException("not absolute URI"); + + // + // ISerializable + // + + public void GetObjectData(SerializationInfo info, StreamingContext context) + => throw new PlatformNotSupportedException(); + + } +} diff --git a/Baselib/system.filter b/Baselib/system.filter index d076170..0ae5f37 100644 --- a/Baselib/system.filter +++ b/Baselib/system.filter @@ -7,3 +7,5 @@ System.ComponentModel.CategoryAttribute System.ComponentModel.DescriptionAttribute System.Text.RegularExpressions.RegexOptions + +System.UriKind