1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
namespace ${project_name};
public class LoadingOptions
{
public IFetcher fetcher;
public string? fileUri;
public Dictionary<string, string>? namespaces;
public bool? noLinkCheck;
public string? container;
public List<string>? schemas;
public Dictionary<string, object> idx;
public Dictionary<string, string> vocab;
public Dictionary<string, string> rvocab;
public LoadingOptions(
in IFetcher? fetcher = null,
in string? fileUri = null,
in Dictionary<string, string>? namespaces = null,
in bool? noLinkCheck = null,
in string? container = null,
in List<string>? schemas = null,
in Dictionary<string, object>? idx = null,
LoadingOptions? copyFrom = null)
{
this.fileUri = fileUri;
this.namespaces = namespaces;
this.noLinkCheck = noLinkCheck;
this.container = container;
this.schemas = schemas;
this.idx = idx ?? new Dictionary<string, object>();
if (copyFrom != null)
{
this.idx = copyFrom.idx;
if (fetcher == null)
{
this.fetcher = copyFrom.fetcher;
}
if (fileUri == null)
{
this.fileUri = copyFrom.fileUri;
}
if (namespaces == null)
{
this.namespaces = copyFrom.namespaces;
}
if (noLinkCheck == null)
{
this.noLinkCheck = copyFrom.noLinkCheck;
}
if (container == null)
{
this.container = copyFrom.container;
}
if (schemas == null)
{
this.schemas = copyFrom.schemas;
}
}
if (fetcher != null)
{
this.fetcher = fetcher;
}
else
{
this.fetcher = new DefaultFetcher();
}
this.vocab = Vocabs.Vocab;
this.rvocab = Vocabs.Rvocab;
if (this.namespaces != null)
{
this.vocab = new Dictionary<string, string>(Vocabs.Vocab);
this.rvocab = new Dictionary<string, string>(Vocabs.Rvocab);
foreach (KeyValuePair<string, string> namespaceEntry in this.namespaces)
{
this.vocab.Add(namespaceEntry.Key, namespaceEntry.Value);
this.rvocab.Add(namespaceEntry.Value, namespaceEntry.Key);
}
}
}
public string ExpandUrl(in string url_, string baseUrl, bool scopeId, bool vocabTerm, int? scopedRef)
{
string url = url_;
if (url.Equals("@id") || url.Equals("@type"))
{
return url;
}
if (vocabTerm && this.vocab.ContainsKey(url))
{
return url;
}
if (vocab.Count > 0 && url.Contains(':'))
{
string prefix = url.Split(":")[0];
if (vocab.ContainsKey(prefix))
{
url = string.Concat(vocab[prefix], url.AsSpan(prefix.Length + 1));
}
}
UriBuilder split = Utilities.Split(url);
bool hasFragment = split.Fragment != "";
if (split.Scheme.Equals("http") || split.Scheme.Equals("https") || split.Scheme.Equals("file")
|| url.StartsWith("$(")
|| url.StartsWith("${"))
{
// Do nothing
}
else if (scopeId && !hasFragment)
{
UriBuilder splitbase = Utilities.Split(baseUrl);
string frg;
if (splitbase.Fragment.Length > 0)
{
frg = splitbase.FragmentWithoutFragmentation() + split.Path;
}
else
{
frg = split.Path.Substring(1);
}
UriBuilder builder = new()
{
Scheme = splitbase.Scheme,
Host = splitbase.Host,
Path = splitbase.Path,
Fragment = frg
};
url = builder.ToString();
}
else if (scopedRef != null && !hasFragment)
{
UriBuilder splitbase = Utilities.Split(baseUrl);
List<string> sp = new(splitbase.FragmentWithoutFragmentation().Split("/").ToList());
int? n = scopedRef;
while (n > 0 && sp.Count > 0)
{
sp.RemoveAt(sp.Count - 1);
n--;
}
sp.Add(url);
string fragment = string.Join("/", sp);
UriBuilder builder = new()
{
Scheme = splitbase.Scheme,
Host = splitbase.Host,
Path = splitbase.Path,
Query = splitbase.Query,
Fragment = fragment
};
url = builder.ToString();
}
else
{
url = fetcher.Urljoin(baseUrl, url);
}
if (vocabTerm)
{
split = Utilities.Split(url);
if (split.Scheme.Length > 0)
{
if (rvocab.ContainsKey(url))
{
return rvocab[url];
}
}
else
{
throw new ValidationException($"Term '{url}' not in vocabulary");
}
}
return url;
}
public string PrefixUrl(in string url)
{
foreach (KeyValuePair<string, string> k in vocab)
{
if (url.StartsWith(k.Value))
{
return k + ":" + url.Substring(0, k.Value.Length);
}
}
return url;
}
}
|