-
Notifications
You must be signed in to change notification settings - Fork 659
Expand file tree
/
Copy pathUtils.cs
More file actions
471 lines (394 loc) · 16.4 KB
/
Utils.cs
File metadata and controls
471 lines (394 loc) · 16.4 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Xsl;
using Newtonsoft.Json.Linq;
using NuGet.Packaging;
using NuGet.Versioning;
#if NETFRAMEWORK
using JsonLD.Core;
using Newtonsoft.Json;
using NuGet.Services.Metadata.Catalog.JsonLDIntegration;
using NuGet.Services.Metadata.Catalog.Helpers;
using VDS.RDF;
using VDS.RDF.Parsing;
#endif
namespace NuGet.Services.Metadata.Catalog
{
public static class Utils
{
#if NETFRAMEWORK
private const string XslTransformNuSpec = "xslt.nuspec.xslt";
private const string XslTransformNormalizeNuSpecNamespace = "xslt.normalizeNuspecNamespace.xslt";
private static readonly Lazy<XslCompiledTransform> XslTransformNuSpecCache = new Lazy<XslCompiledTransform>(() => SafeLoadXslTransform(XslTransformNuSpec));
private static readonly Lazy<XslCompiledTransform> XslTransformNormalizeNuSpecNamespaceCache = new Lazy<XslCompiledTransform>(() => SafeLoadXslTransform(XslTransformNormalizeNuSpecNamespace));
#endif
private static readonly char[] TagTrimChars = { ',', ' ', '\t', '|', ';' };
public static string[] SplitTags(string original)
{
var fields = original
.Split(TagTrimChars)
.Select(w => w.Trim(TagTrimChars))
.Where(w => w.Length > 0)
.ToArray();
return fields;
}
public static void AssertValidPackageId(string packageId)
{
if (packageId is null || !PackageIdValidator.IsValidPackageId(packageId))
{
throw new InvalidOperationException($"The package ID {(packageId is null ? "<null>" : $"'{packageId}'")} is not valid.");
}
}
public static void AssertValidPackageVersion(string packageVersion)
{
if (packageVersion is null || !NuGetVersion.TryParse(packageVersion, out _))
{
throw new InvalidOperationException($"The package version {(packageVersion is null ? "<null>" : $"'{packageVersion}'")} is not valid.");
}
}
public static Stream GetResourceStream(string resourceName)
{
if (string.IsNullOrEmpty(resourceName))
{
throw new ArgumentException(Strings.ArgumentMustNotBeNullOrEmpty, nameof(resourceName));
}
Assembly assembly = typeof(Utils).Assembly;
string name = assembly.GetName().Name;
return assembly.GetManifestResourceStream($"{name}.{resourceName}");
}
private static void NormalizeXml(XmlNode xmlNode)
{
if (xmlNode.Attributes != null)
{
foreach (XmlAttribute attribute in xmlNode.Attributes)
{
attribute.Value = attribute.Value.Normalize(NormalizationForm.FormC);
}
}
if (xmlNode.Value != null)
{
xmlNode.Value = xmlNode.Value.Normalize(NormalizationForm.FormC);
return;
}
foreach (XmlNode childNode in xmlNode.ChildNodes)
{
NormalizeXml(childNode);
}
}
internal static XmlDocument SafeCreateXmlDocument(XmlReader reader = null)
{
// CodeAnalysis / XmlDocument: set the resolver to null or instance
var xmlDoc = new XmlDocument();
xmlDoc.XmlResolver = null;
if (reader != null)
{
xmlDoc.Load(reader);
}
return xmlDoc;
}
private static XslCompiledTransform SafeLoadXslTransform(string resourceName)
{
var transform = new XslCompiledTransform();
// CodeAnalysis / XmlReader.Create: provide settings instance and set resolver property to null or instance
var settings = new XmlReaderSettings();
settings.XmlResolver = null;
var reader = XmlReader.Create(new StreamReader(GetResourceStream(resourceName)), settings);
// CodeAnalysis / XslCompiledTransform.Load: specify default settings or set resolver property to null or instance
transform.Load(reader, XsltSettings.Default, stylesheetResolver: null);
return transform;
}
public static XDocument GetNuspecXDocument(PackageArchiveReader packageArchiveReader)
{
using var nuspecStream = packageArchiveReader.GetNuspec();
return XDocument.Load(nuspecStream);
}
public static Uri Expand(JToken context, string term)
{
if (term.StartsWith("http:", StringComparison.OrdinalIgnoreCase))
{
return new Uri(term);
}
int indexOf = term.IndexOf(':');
if (indexOf > 0)
{
string ns = term.Substring(0, indexOf);
return new Uri(context[ns].ToString() + term.Substring(indexOf + 1));
}
return new Uri(context["@vocab"] + term);
}
public static string GenerateHash(Stream stream)
{
stream.Seek(0, SeekOrigin.Begin);
using (var hashAlgorithm = HashAlgorithm.Create(Constants.Sha512))
{
return Convert.ToBase64String(hashAlgorithm.ComputeHash(stream));
}
}
public static IEnumerable<PackageEntry> GetEntries(ZipArchive package)
{
IList<PackageEntry> result = new List<PackageEntry>();
HashSet<string> seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (ZipArchiveEntry entry in package.Entries)
{
if (entry.FullName.EndsWith("/.rels", StringComparison.OrdinalIgnoreCase))
{
continue;
}
if (entry.FullName.EndsWith("[Content_Types].xml", StringComparison.OrdinalIgnoreCase))
{
continue;
}
if (entry.FullName.EndsWith(".psmdcp", StringComparison.OrdinalIgnoreCase))
{
continue;
}
string normalizedFulName = entry.FullName.Replace('\\', '/');
if (seen.Add(normalizedFulName))
{
result.Add(new PackageEntry(entry));
}
}
return result;
}
public static NupkgMetadata GetNupkgMetadata(Stream stream, string packageHash)
{
if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}
var packageSize = stream.Length;
packageHash = packageHash ?? GenerateHash(stream);
stream.Seek(0, SeekOrigin.Begin);
using (var package = new ZipArchive(stream, ZipArchiveMode.Read, leaveOpen: true))
using (var packageArchiveReader = new PackageArchiveReader(package))
{
var identity = packageArchiveReader.GetIdentity();
AssertValidPackageId(identity.Id);
if (identity.Version is null)
{
throw new InvalidOperationException("The version from the package identity must not be null.");
}
var nuspec = GetNuspecXDocument(packageArchiveReader);
var entries = GetEntries(package);
return new NupkgMetadata(nuspec, entries, packageSize, packageHash);
}
}
public static void TraceException(Exception e)
{
if (e is AggregateException)
{
foreach (Exception ex in ((AggregateException)e).InnerExceptions)
{
TraceException(ex);
}
}
else
{
Trace.TraceError("{0} {1}", e.GetType().Name, e.Message);
Trace.TraceError("{0}", e.StackTrace);
if (e.InnerException != null)
{
TraceException(e.InnerException);
}
}
}
internal static T Deserialize<T>(JObject jObject, string propertyName)
{
if (jObject == null)
{
throw new ArgumentNullException(nameof(jObject));
}
if (string.IsNullOrEmpty(propertyName))
{
throw new ArgumentException(Strings.ArgumentMustNotBeNullOrEmpty, nameof(propertyName));
}
if (!jObject.TryGetValue(propertyName, out var value) || value == null)
{
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Strings.PropertyRequired, propertyName));
}
return value.ToObject<T>();
}
#if NETFRAMEWORK
public static IGraph CreateNuspecGraph(XDocument nuspec, string baseAddress, bool normalizeXml = false)
{
XsltArgumentList arguments = new XsltArgumentList();
arguments.AddParam("base", "", baseAddress);
arguments.AddParam("extension", "", ".json");
arguments.AddExtensionObject("urn:helper", new XsltHelper());
nuspec = SafeXmlTransform(nuspec.CreateReader(), XslTransformNormalizeNuSpecNamespaceCache.Value);
var rdfxml = SafeXmlTransform(nuspec.CreateReader(), XslTransformNuSpecCache.Value, arguments);
var doc = SafeCreateXmlDocument(rdfxml.CreateReader());
if (normalizeXml)
{
NormalizeXml(doc);
}
RdfXmlParser rdfXmlParser = new RdfXmlParser();
IGraph graph = new Graph();
rdfXmlParser.Load(graph, doc);
return graph;
}
public static JToken CreateJson(IGraph graph, JToken frame = null)
{
System.IO.StringWriter writer = new System.IO.StringWriter();
IRdfWriter rdfWriter = new JsonLdWriter();
rdfWriter.Save(graph, writer);
writer.Flush();
if (frame == null)
{
return JToken.Parse(writer.ToString());
}
else
{
JToken flattened = JToken.Parse(writer.ToString());
JObject framed = JsonLdProcessor.Frame(flattened, frame, new JsonLdOptions());
JObject compacted = JsonLdProcessor.Compact(framed, frame["@context"], new JsonLdOptions());
return JsonSort.OrderJson(compacted);
}
}
public static string CreateArrangedJson(IGraph graph, JToken frame = null)
{
System.IO.StringWriter writer = new System.IO.StringWriter();
IRdfWriter rdfWriter = new JsonLdWriter();
rdfWriter.Save(graph, writer);
writer.Flush();
if (frame == null)
{
return writer.ToString();
}
else
{
JToken flattened = JToken.Parse(writer.ToString());
JObject framed = JsonLdProcessor.Frame(flattened, frame, new JsonLdOptions());
JObject compacted = JsonLdProcessor.Compact(framed, frame["@context"], new JsonLdOptions());
var arranged = JsonSort.OrderJson(compacted);
return arranged.ToString();
}
}
public static IGraph CreateGraph(Uri resourceUri, string json)
{
if (json == null)
{
return null;
}
try
{
JToken compacted = JToken.Parse(json);
return CreateGraph(compacted, readOnly: false);
}
catch (JsonException e)
{
Trace.TraceError("Exception: failed to parse {0} {1}", resourceUri, e);
throw;
}
}
public static IGraph CreateGraph(JToken compacted, bool readOnly)
{
JToken flattened = JsonLdProcessor.Flatten(compacted, new JsonLdOptions());
IRdfReader rdfReader = new JsonLdReader();
IGraph graph = new Graph();
rdfReader.Load(graph, new StringReader(flattened.ToString(Newtonsoft.Json.Formatting.None, new Newtonsoft.Json.JsonConverter[0])));
if (readOnly)
{
graph = new ReadOnlyGraph(graph);
}
return graph;
}
public static bool IsCatalogNode(INode sourceNode, IGraph source)
{
Triple rootTriple = source.GetTriplesWithSubjectObject(sourceNode, source.CreateUriNode(Schema.DataTypes.CatalogRoot)).FirstOrDefault();
Triple pageTriple = source.GetTriplesWithSubjectObject(sourceNode, source.CreateUriNode(Schema.DataTypes.CatalogPage)).FirstOrDefault();
return (rootTriple != null || pageTriple != null);
}
public static void CopyCatalogContentGraph(INode sourceNode, IGraph source, IGraph target)
{
if (IsCatalogNode(sourceNode, source))
{
return;
}
foreach (Triple triple in source.GetTriplesWithSubject(sourceNode))
{
if (target.Assert(triple.CopyTriple(target)) && triple.Object is IUriNode)
{
CopyCatalogContentGraph(triple.Object, source, target);
}
}
}
// where the property exists on the graph being merged in remove it from the existing graph
public static void RemoveExistingProperties(IGraph existingGraph, IGraph graphToMerge, Uri[] properties)
{
foreach (Uri property in properties)
{
foreach (Triple t1 in graphToMerge.GetTriplesWithPredicate(graphToMerge.CreateUriNode(property)))
{
INode subject = t1.Subject.CopyNode(existingGraph);
INode predicate = t1.Predicate.CopyNode(existingGraph);
IList<Triple> retractList = new List<Triple>(existingGraph.GetTriplesWithSubjectPredicate(subject, predicate));
foreach (Triple t2 in retractList)
{
existingGraph.Retract(t2);
}
}
}
}
public static PackageCatalogItem CreateCatalogItem(
string origin,
Stream stream,
DateTime createdDate,
DateTime? lastEditedDate = null,
DateTime? publishedDate = null,
string licenseNames = null,
string licenseReportUrl = null,
string packageHash = null,
PackageDeprecationItem deprecationItem = null,
IList<PackageVulnerabilityItem> vulnerabilities = null)
{
try
{
NupkgMetadata nupkgMetadata = GetNupkgMetadata(stream, packageHash);
return new PackageCatalogItem(
nupkgMetadata,
createdDate,
lastEditedDate,
publishedDate,
deprecation: deprecationItem,
vulnerabilities: vulnerabilities);
}
catch (InvalidDataException e)
{
Trace.TraceError("Exception: {0} {1} {2}", origin, e.GetType().Name, e);
return null;
}
catch (Exception e)
{
throw new Exception(string.Format("Exception processsing {0}", origin), e);
}
}
private static XDocument SafeXmlTransform(XmlReader reader, XslCompiledTransform transform, XsltArgumentList arguments = null)
{
XDocument result = new XDocument();
using (XmlWriter writer = result.CreateWriter())
{
if (arguments == null)
{
arguments = new XsltArgumentList();
}
// CodeAnalysis / XslCompiledTransform.Transform: set resolver property to null or instance
transform.Transform(reader, arguments, writer, documentResolver: null);
}
return result;
}
#endif
}
}