Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SparqlResultSetCsvEncoder.cs
1using System;
2using System.Text;
3using System.Threading.Tasks;
9
11{
17 {
22 {
23 }
24
28 public string[] ContentTypes => Array.Empty<string>();
29
33 public string[] FileExtensions => Array.Empty<string>();
34
42 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
43 {
44 if (Object is SparqlResultSet &&
46 {
47 Grade = Grade.Excellent;
48 return true;
49 }
50 else if (Object is ObjectMatrix M && M.HasColumnNames &&
52 {
53 Grade = Grade.Ok;
54 return true;
55 }
56 else if (Object is bool &&
58 {
59 Grade = Grade.Barely;
60 return true;
61 }
62 else
63 {
64 Grade = Grade.NotAtAll;
65 return false;
66 }
67 }
68
77 public Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, 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(false));
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 return Task.FromResult(new ContentResponse(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 ContentResponse(ContentType, Object, Bin));
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:15
static string Encode(bool x)
Encodes a Boolean for use in XML and other formats.
Definition: CommonTypes.cs:596
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.
Abstract base class for semantic literal values.
Encoder of semantic information from SPARQL queries using CSV. https://www.w3.org/TR/sparql12-results...
string[] ContentTypes
Supported Internet Content Types.
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of content of a given file extension.
SparqlResultSetCsvEncoder()
Encoder and Decoder of semantic information from SPARQL queries using CSV.
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a specific object.
Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object
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:31
bool HasColumnNames
If the matrix has column names defined.
Interface for reporting progress about an encoding or decoding.
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:21
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:34
Grade
Grade enumeration
Definition: Grade.cs:7