4using System.Threading.Tasks;
33 private static readonly
string[] RdfContentTypes =
new string[]
43 private static readonly
string[] RdfFileExtensions =
new string[]
66 if (Array.IndexOf(RdfContentTypes, ContentType) >= 0)
88 public Task<ContentResponse>
DecodeAsync(
string ContentType,
byte[] Data, Encoding Encoding,
89 KeyValuePair<string, string>[] Fields, Uri BaseUri,
ICodecProgress Progress)
137 public Task<ContentResponse>
EncodeAsync(
object Object, Encoding Encoding,
ICodecProgress Progress, params
string[] AcceptedContentTypes)
139 if (Encoding is
null)
140 Encoding = Encoding.UTF8;
148 StringBuilder sb =
new StringBuilder();
149 sb.Append(
"<?xml version=\"1.0\" encoding=\"");
150 sb.Append(Encoding.WebName);
151 sb.AppendLine(
"\"?>");
153 XmlWriterSettings Settings =
new XmlWriterSettings()
155 ConformanceLevel = ConformanceLevel.Document,
158 NamespaceHandling = NamespaceHandling.OmitDuplicates,
159 NewLineHandling = NewLineHandling.None,
160 NewLineOnAttributes =
false,
161 OmitXmlDeclaration =
true,
162 WriteEndDocumentOnClose =
true
165 using (XmlWriter w = XmlWriter.Create(sb, Settings))
167 Dictionary<string, string> Prefixes =
new Dictionary<string, string>();
168 Dictionary<string, ChunkedList<ISemanticTriple>> PerSubject =
new Dictionary<string, ChunkedList<ISemanticTriple>>();
173 CheckPrefix(Triple.
Subject, Prefixes);
175 CheckPrefix(Triple.
Object, Prefixes);
185 PerSubject[s] = List;
193 foreach (KeyValuePair<string, string> P
in Prefixes)
194 w.WriteAttributeString(
"xmlns", P.Value,
string.Empty, P.Key);
198 w.WriteStartElement(
"rdf",
"Description",
Rdf.
Namespace);
200 if (Subject.Key.StartsWith(
"_:"))
201 w.WriteAttributeString(
"rdf",
"nodeID",
Rdf.
Namespace, Subject.Key.Substring(2));
203 w.WriteAttributeString(
"rdf",
"about",
Rdf.
Namespace, Subject.Key);
209 string Uri = Predicate.UriString;
210 string Namespace = GetNamespace(Uri);
212 if (!(Namespace is
null) &&
213 Prefixes.TryGetValue(Namespace, out
string Prefix))
215 string LocalName = Uri.Substring(Namespace.Length);
216 w.WriteStartElement(Prefix, LocalName, Namespace);
219 w.WriteStartElement(Uri);
223 if (
string.IsNullOrEmpty(Literal.StringType))
229 w.WriteValue(Literal.StringValue);
232 w.WriteValue(Literal.StringValue);
236 w.WriteAttributeString(
"rdf",
"datatype",
Rdf.
Namespace, Literal.StringType);
237 w.WriteValue(Literal.StringValue);
245 w.WriteValue(Triple.
Object.ToString());
250 return Task.FromResult(
new ContentResponse(
new Exception(
"Unable to encode semantic model as RDF document. RDF documents require predicates to be URIs.")));
259 Text = sb.ToString();
263 return Task.FromResult(
new ContentResponse(
new ArgumentException(
"Unable to encode object.", nameof(Object))));
265 byte[] Bin = Encoding.GetBytes(Text);
266 string ContentType = RdfContentTypes[0] +
"; charset=" + Encoding.WebName;
271 private static void CheckPrefix(
ISemanticElement Element, Dictionary<string, string> Prefixes)
276 if (!
string.IsNullOrEmpty(Namespace) && !Prefixes.ContainsKey(Namespace))
278 string Prefix =
"p" + (Prefixes.Count + 1).
ToString();
279 Prefixes[Namespace] = Prefix;
284 private static string GetNamespace(
string Uri)
286 int i = Uri.LastIndexOf(
'#');
288 return Uri.Substring(0, i + 1);
290 i = Uri.LastIndexOf(
'/');
292 return Uri.Substring(0, i + 1);
294 i = Uri.LastIndexOf(
':');
296 return Uri.Substring(0, i + 1);
309 if (
string.Compare(FileExtension, RdfFileExtensions[0],
true) == 0)
311 ContentType = RdfContentTypes[0];
329 if (Array.IndexOf(RdfContentTypes, ContentType) >= 0)
331 FileExtension = RdfFileExtensions[0];
336 FileExtension =
null;
Contains information about a response to a content request.
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.
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.
Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object
const string DefaultContentType
application/rdf+xml
string[] ContentTypes
Supported Internet Content Types.
Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri, ICodecProgress Progress)
Decodes an object
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.
static readonly string[] XmlContentTypes
XML content types.
A chunked list is a linked list of chunks of objects of type T .
Static class managing binary representations of strings.
static string GetString(byte[] Data, int Offset, int Count, Encoding DefaultEncoding)
Gets a string from its binary representation, taking any Byte Order Mark (BOM) into account.
Interface for reporting progress about an encoding or decoding.
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.