Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SparqlResultSetCsvCodec.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
10
12{
18 {
23 {
24 }
25
29 public string[] ContentTypes => new string[0];
30
34 public string[] FileExtensions => new string[0];
35
43 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
44 {
45 if (Object is SparqlResultSet &&
47 {
48 Grade = Grade.Excellent;
49 return true;
50 }
51 else if (Object is ObjectMatrix M && M.HasColumnNames &&
53 {
54 Grade = Grade.Ok;
55 return true;
56 }
57 else if (Object is bool &&
59 {
60 Grade = Grade.Barely;
61 return true;
62 }
63 else
64 {
65 Grade = Grade.NotAtAll;
66 return false;
67 }
68 }
69
77 public Task<KeyValuePair<byte[], string>> EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
78 {
79 string Text;
80
81 if (Encoding is null)
82 Encoding = Encoding.UTF8;
83
84 if (Object is SparqlResultSet Result)
85 {
86 if (Result.BooleanResult.HasValue)
87 {
88 string[][] Records = new string[1][];
89 Records[0] = new string[] { CommonTypes.Encode(Result.BooleanResult.Value) };
90
91 Text = CSV.Encode(Records);
92 }
93 else
94 Text = CSV.Encode(Result.ToMatrix());
95 }
96 else if (Object is ObjectMatrix M)
97 Text = CSV.Encode(M, ElementToString);
98 else if (Object is bool b)
99 {
100 string[][] Records = new string[1][];
101 Records[0] = new string[] { CommonTypes.Encode(b) };
102
103 Text = CSV.Encode(Records);
104 }
105 else
106 throw new ArgumentException("Unable to encode object.", nameof(Object));
107
108 byte[] Bin = Encoding.GetBytes(Text);
109 string ContentType = CsvCodec.CsvContentTypes[0] + "; charset=" + Encoding.WebName;
110
111 return Task.FromResult(new KeyValuePair<byte[], string>(Bin, ContentType));
112 }
113
114 private static string ElementToString(IElement E)
115 {
116 object Obj = E.AssociatedObjectValue;
117
118 if (Obj is string s)
119 return s;
120 else if (Obj is SemanticLiteral Literal)
121 return Literal.StringValue;
122 else if (Obj is SemanticTriple Triple)
123 {
124 StringBuilder sb = new StringBuilder();
125
126 sb.Append("<<");
127 sb.Append(ElementToString(Triple.Subject));
128 sb.Append(' ');
129 sb.Append(ElementToString(Triple.Predicate));
130 sb.Append(' ');
131 sb.Append(ElementToString(Triple.Object));
132 sb.Append(">>");
133
134 return sb.ToString();
135 }
136 else
137 return Obj?.ToString() ?? string.Empty;
138 }
139
146 public bool TryGetContentType(string FileExtension, out string ContentType)
147 {
148 ContentType = null;
149 return false;
150 }
151
158 public bool TryGetFileExtension(string ContentType, out string FileExtension)
159 {
160 FileExtension = null;
161 return false;
162 }
163 }
164}
Helps with common CSV-related tasks. (CSV=Comma Separated Values)
Definition: CSV.cs:21
static string Encode(string[][] Records)
Encodes records as a Comma-separated values string.
Definition: CSV.cs:209
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static string Encode(bool x)
Encodes a Boolean for use in XML and other formats.
Definition: CommonTypes.cs:594
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.
Abstract base class for semantic literal values.
Encoder and Decoder of semantic information from SPARQL queries using CSV. https://www....
string[] ContentTypes
Supported Internet Content Types.
string[] FileExtensions
Supported file extensions.
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a specific object.
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of content of a given file extension.
Task< KeyValuePair< byte[], string > > EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
Encodes an object
SparqlResultSetCsvCodec()
Encoder and Decoder of semantic information from SPARQL queries using CSV.
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of content of a given content type.
Contains the results of a SPARQL query. https://www.w3.org/TR/2023/WD-sparql12-results-xml-20230516/ ...
static readonly string[] CsvContentTypes
CSV content types.
Definition: CsvCodec.cs:30
bool HasColumnNames
If the matrix has column names defined.
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
Grade
Grade enumeration
Definition: Grade.cs:7