Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
TurtleCodec.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
6
8{
13 {
17 public TurtleCodec()
18 {
19 }
20
24 public string[] ContentTypes => TurtleContentTypes;
25
26 private static readonly string[] TurtleContentTypes = new string[]
27 {
29 "application/x-turtle"
30 };
31
35 public string[] FileExtensions => TurtleFileExtensions;
36
37 private static readonly string[] TurtleFileExtensions = new string[]
38 {
40 };
41
45 public const string DefaultContentType = "text/turtle";
46
50 public const string DefaultExtension = "ttl";
51
58 public bool Decodes(string ContentType, out Grade Grade)
59 {
60 if (Array.IndexOf(TurtleContentTypes, ContentType) >= 0)
61 {
62 Grade = Grade.Ok;
63 return true;
64 }
65 else
66 {
67 Grade = Grade.NotAtAll;
68 return false;
69 }
70 }
71
81 public Task<object> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair<string, string>[] Fields, Uri BaseUri)
82 {
83 string s = CommonTypes.GetString(Data, Encoding ?? Encoding.UTF8);
84 TurtleDocument Parsed = new TurtleDocument(s, BaseUri, "n", BlankNodeIdMode.Guid);
85 return Task.FromResult<object>(Parsed);
86 }
87
95 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
96 {
97 if (Object is TurtleDocument &&
98 InternetContent.IsAccepted(TurtleContentTypes, AcceptedContentTypes))
99 {
100 Grade = Grade.Excellent;
101 return true;
102 }
103 else if (Object is ISemanticModel &&
104 InternetContent.IsAccepted(TurtleContentTypes, AcceptedContentTypes))
105 {
106 Grade = Grade.Barely;
107 return true;
108 }
109
110 Grade = Grade.NotAtAll;
111 return false;
112 }
113
121 public Task<KeyValuePair<byte[], string>> EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
122 {
123 if (Encoding is null)
124 Encoding = Encoding.UTF8;
125
126 string Text;
127
128 if (Object is TurtleDocument Doc)
129 Text = Doc.Text;
130 else
131 {
132 if (!(Object is ISemanticModel Model))
133 throw new ArgumentException("Unable to encode object.", nameof(Object));
134
135 StringBuilder sb = new StringBuilder();
136
137 foreach (ISemanticTriple Triple in Model)
138 {
139 sb.Append(Triple.Subject);
140 sb.Append('\t');
141 sb.Append(Triple.Predicate);
142 sb.Append('\t');
143 sb.Append(Triple.Object);
144 sb.Append('\t');
145 sb.AppendLine(".");
146 }
147
148 Text = sb.ToString();
149 }
150
151 byte[] Bin = Encoding.GetBytes(Text);
152 string ContentType = TurtleContentTypes[0] + "; charset=" + Encoding.WebName;
153
154 return Task.FromResult(new KeyValuePair<byte[], string>(Bin, ContentType));
155 }
156
163 public bool TryGetContentType(string FileExtension, out string ContentType)
164 {
165 if (string.Compare(FileExtension, TurtleFileExtensions[0], true) == 0)
166 {
167 ContentType = TurtleContentTypes[0];
168 return true;
169 }
170 else
171 {
172 ContentType = null;
173 return false;
174 }
175 }
176
183 public bool TryGetFileExtension(string ContentType, out string FileExtension)
184 {
185 if (Array.IndexOf(TurtleContentTypes, ContentType) >= 0)
186 {
187 FileExtension = TurtleFileExtensions[0];
188 return true;
189 }
190 else
191 {
192 FileExtension = null;
193 return false;
194 }
195 }
196 }
197}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static string GetString(byte[] Data, Encoding DefaultEncoding)
Gets a string from its binary representation, taking any Byte Order Mark (BOM) into account.
Static class managing encoding and decoding of internet content.
static bool IsAccepted(string ContentType, params string[] AcceptedContentTypes)
Checks if a given content type is acceptable.
Encoder and Decoder of semantic information in Turtle Documents.
Definition: TurtleCodec.cs:13
Task< KeyValuePair< byte[], string > > EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
Encodes an object
Definition: TurtleCodec.cs:121
const string DefaultContentType
text/turtle
Definition: TurtleCodec.cs:45
string[] FileExtensions
Supported file extensions.
Definition: TurtleCodec.cs:35
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a specific object.
Definition: TurtleCodec.cs:95
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of content of a given file extension.
Definition: TurtleCodec.cs:163
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of content of a given content type.
Definition: TurtleCodec.cs:183
TurtleCodec()
Encoder and Decoder of semantic information in Turtle Documents.
Definition: TurtleCodec.cs:17
Task< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object
Definition: TurtleCodec.cs:81
string[] ContentTypes
Supported Internet Content Types.
Definition: TurtleCodec.cs:24
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes content of a given Internet Content Type.
Definition: TurtleCodec.cs:58
Contains semantic information stored in a turtle document. https://www.w3.org/TR/rdf12-turtle/ https:...
Basic interface for Internet Content decoders. A class implementing this interface and having a defau...
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...
Interface for semantic models.
Interface for semantic triples.
ISemanticElement Object
Object element
ISemanticElement Predicate
Predicate element
ISemanticElement Subject
Subject element
BlankNodeIdMode
How blank node IDs are generated
Grade
Grade enumeration
Definition: Grade.cs:7