-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
188 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using IS4.SFI.Services; | ||
using IS4.SFI.Vocabulary; | ||
using System; | ||
using System.Net.Mime; | ||
using System.Threading.Tasks; | ||
|
||
namespace IS4.SFI.Analyzers | ||
{ | ||
/// <summary> | ||
/// An analyzer of media types, as instances of <see cref="ContentType"/>. | ||
/// </summary> | ||
public sealed class MediaTypeAnalyzer : EntityAnalyzer<ContentType> | ||
{ | ||
/// <summary> | ||
/// Whether to add class information to the created node. | ||
/// </summary> | ||
public bool AddClasses { get; set; } = true; | ||
|
||
/// <inheritdoc cref="EntityAnalyzer.EntityAnalyzer"/> | ||
public MediaTypeAnalyzer() | ||
{ | ||
|
||
} | ||
|
||
/// <inheritdoc/> | ||
public async override ValueTask<AnalysisResult> Analyze(ContentType entity, AnalysisContext context, IEntityAnalyzers analyzers) | ||
{ | ||
var type = entity.ToString(); | ||
var node = context.NodeFactory.Create(Vocabularies.Urim, Uri.EscapeUriString(type)); | ||
if(AddClasses) | ||
{ | ||
node.SetClass(Classes.MediaType); | ||
if(type.StartsWith(TextTools.ImpliedMediaTypePrefix, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
node.SetClass(Classes.MediaTypeImplied); | ||
} | ||
int semicolon = type.IndexOf(';'); | ||
if(semicolon != -1) | ||
{ | ||
node.SetClass(Classes.MediaTypeParametrized); | ||
} | ||
int plus = type.IndexOf('+'); | ||
if(plus != -1 && (semicolon == -1 || plus < semicolon)) | ||
{ | ||
node.SetClass(Classes.MediaTypeStructured); | ||
} | ||
} | ||
return new(node, type); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using IS4.SFI.Services; | ||
using System; | ||
using System.Net.Mime; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace IS4.SFI.Tools | ||
{ | ||
/// <summary> | ||
/// Provides an implementation of <see cref="ContentType"/> that preserves the value | ||
/// it was constructed from and prevents changes to it from taking effects. | ||
/// </summary> | ||
/// <remarks> | ||
/// The <see cref="ToString"/> method returns the value passed to | ||
/// <see cref="ImmutableContentType(string)"/>, but if the object is changed, | ||
/// the method throws an exception, signifying changes to the object. | ||
/// </remarks> | ||
public class ImmutableContentType : ContentType, ICloneable, IEquatable<ContentType>, IPersistentKey | ||
{ | ||
static readonly ConditionalWeakTable<string, ImmutableContentType> cache = new(); | ||
|
||
/// <summary> | ||
/// Obtains an instance of <see cref="ImmutableContentType"/> constructed | ||
/// from a particular string, potentially reusing an earlier constructed object. | ||
/// </summary> | ||
/// <param name="contentType"></param> | ||
/// <returns></returns> | ||
public static ImmutableContentType GetCached(string contentType) | ||
{ | ||
return cache.GetValue(contentType, ct => new ImmutableContentType(ct)); | ||
} | ||
|
||
readonly string value; | ||
readonly string reparsedValue; | ||
|
||
bool Changed => reparsedValue != base.ToString(); | ||
|
||
object? IPersistentKey.ReferenceKey => null; | ||
|
||
object? IPersistentKey.DataKey => ToString(); | ||
|
||
/// <inheritdoc/> | ||
public ImmutableContentType(string contentType) : base(contentType) | ||
{ | ||
value = contentType; | ||
reparsedValue = base.ToString(); | ||
} | ||
|
||
/// <inheritdoc cref="ICloneable.Clone"/> | ||
public virtual ImmutableContentType Clone() | ||
{ | ||
return new ImmutableContentType(value); | ||
} | ||
|
||
object ICloneable.Clone() | ||
{ | ||
return Clone(); | ||
} | ||
|
||
/// <inheritdoc cref="Equals(object)"/> | ||
public virtual bool Equals(ContentType other) | ||
{ | ||
return ToString() == other.ToString(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override bool Equals(object rparam) | ||
{ | ||
return rparam is ContentType other && Equals(other); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override int GetHashCode() | ||
{ | ||
return ToString().GetHashCode(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override string ToString() | ||
{ | ||
if(Changed) | ||
{ | ||
throw new InvalidOperationException("The instance was modified during its lifetime."); | ||
} | ||
return value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters