2using System.Collections.Generic;
3using System.Security.Cryptography.X509Certificates;
5using System.Threading.Tasks;
31 private static readonly
string[] RdfContentTypes =
new string[]
41 private static readonly
string[] RdfFileExtensions =
new string[]
64 if (Array.IndexOf(RdfContentTypes, ContentType) >= 0)
85 public Task<object>
DecodeAsync(
string ContentType,
byte[] Data, Encoding Encoding, KeyValuePair<string, string>[] Fields, Uri BaseUri)
89 return Task.FromResult<
object>(Parsed);
127 public Task<KeyValuePair<byte[], string>>
EncodeAsync(
object Object, Encoding Encoding, params
string[] AcceptedContentTypes)
129 if (Encoding is
null)
130 Encoding = Encoding.UTF8;
138 StringBuilder sb =
new StringBuilder();
139 sb.Append(
"<?xml version=\"1.0\" encoding=\"");
140 sb.Append(Encoding.WebName);
141 sb.AppendLine(
"\"?>");
143 XmlWriterSettings Settings =
new XmlWriterSettings()
145 ConformanceLevel = ConformanceLevel.Document,
148 NamespaceHandling = NamespaceHandling.OmitDuplicates,
149 NewLineHandling = NewLineHandling.None,
150 NewLineOnAttributes =
false,
151 OmitXmlDeclaration =
true,
152 WriteEndDocumentOnClose =
true
155 using (XmlWriter w = XmlWriter.Create(sb, Settings))
157 Dictionary<string, string> Prefixes =
new Dictionary<string, string>();
158 Dictionary<string, LinkedList<ISemanticTriple>> PerSubject =
new Dictionary<string, LinkedList<ISemanticTriple>>();
163 CheckPrefix(Triple.
Subject, Prefixes);
165 CheckPrefix(Triple.
Object, Prefixes);
169 if (!PerSubject.TryGetValue(s, out LinkedList<ISemanticTriple> List))
171 List =
new LinkedList<ISemanticTriple>();
172 PerSubject[s] = List;
175 List.AddLast(Triple);
180 foreach (KeyValuePair<string, string> P
in Prefixes)
181 w.WriteAttributeString(
"xmlns", P.Key,
string.Empty, P.Value);
183 foreach (KeyValuePair<
string, LinkedList<ISemanticTriple>> Subject
in PerSubject)
185 w.WriteStartElement(
"rdf",
"Description",
Rdf.
Namespace);
187 if (Subject.Value.First.Value is
BlankNode SubjectBlankNode)
188 w.WriteAttributeString(
"rdf",
"nodeID",
string.Empty, SubjectBlankNode.NodeId);
190 w.WriteAttributeString(
"rdf",
"about",
string.Empty, Subject.Key);
196 string Uri = Predicate.UriString;
197 string Namespace = GetNamespace(Uri);
199 if (Prefixes.TryGetValue(Namespace, out
string Prefix))
201 string LocalName = Uri.Substring(Namespace.Length);
202 w.WriteStartElement(Prefix, LocalName, Namespace);
205 w.WriteStartElement(Uri);
209 if (
string.IsNullOrEmpty(Literal.StringType))
215 w.WriteValue(Literal.StringValue);
218 w.WriteValue(Literal.StringValue);
222 w.WriteAttributeString(
"rdf",
"datatype",
string.Empty, Literal.StringType);
223 w.WriteValue(Literal.StringValue);
227 w.WriteAttributeString(
"rdf",
"nodeID",
string.Empty,
BlankNode.
NodeId);
231 w.WriteValue(Triple.
Object.ToString());
236 throw new Exception(
"Unable to encode semantic model as RDF document. RDF documents require predicates to be URIs.");
245 Text = sb.ToString();
249 throw new ArgumentException(
"Unable to encode object.", nameof(Object));
251 byte[] Bin = Encoding.GetBytes(Text);
252 string ContentType = RdfContentTypes[0] +
"; charset=" + Encoding.WebName;
254 return Task.FromResult(
new KeyValuePair<
byte[],
string>(Bin, ContentType));
257 private static void CheckPrefix(
ISemanticElement Element, Dictionary<string, string> Prefixes)
262 if (!
string.IsNullOrEmpty(Namespace) && !Prefixes.ContainsKey(Namespace))
264 string Prefix =
"p" + (Prefixes.Count + 1).
ToString();
265 Prefixes[Namespace] = Prefix;
270 private static string GetNamespace(
string Uri)
272 int i = Uri.LastIndexOf(
'#');
274 return Uri.Substring(0, i + 1);
276 i = Uri.LastIndexOf(
'/');
278 return Uri.Substring(0, i + 1);
291 if (
string.Compare(FileExtension, RdfFileExtensions[0],
true) == 0)
293 ContentType = RdfContentTypes[0];
311 if (Array.IndexOf(RdfContentTypes, ContentType) >= 0)
313 FileExtension = RdfFileExtensions[0];
318 FileExtension =
null;
Helps with parsing of commong data types.
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.
string NodeId
Blank node Node ID.
Represents a string literal.
string Language
Language of string.
Abstract base class for semantic literal values.
string UriString
URI string
const string Namespace
http://www.w3.org/1999/02/22-rdf-syntax-ns#
Encoder and Decoder of semantic information in RDF Documents.
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a specific object.
Task< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object
const string DefaultExtension
rdf
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes content of a given Internet Content Type.
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of content of a given content type.
RdfCodec()
Encoder and Decoder of semantic information in RDF Documents.
const string DefaultContentType
application/rdf+xml
Task< KeyValuePair< byte[], string > > EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
Encodes an object
string[] ContentTypes
Supported Internet Content Types.
string[] FileExtensions
Supported file extensions.
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of content of a given file extension.
Contains semantic information stored in an RDF document.
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 nodes.
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
delegate string ToString(IElement Element)
Delegate for callback methods that convert an element value to a string.