Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
XmlCodec.cs
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Text;
5using System.Threading.Tasks;
6using System.Xml;
9
11{
16 {
20 public XmlCodec()
21 {
22 }
23
27 public const string DefaultContentType = "text/xml";
28
32 public const string SchemaContentType = "application/xml";
33
37 public static readonly string[] XmlContentTypes = new string[]
38 {
41 };
42
46 public static readonly string[] XmlFileExtensions = new string[]
47 {
48 "xml",
49 "xsd"
50 };
51
55 public string[] ContentTypes => XmlContentTypes;
56
61
68 public bool Decodes(string ContentType, out Grade Grade)
69 {
70 if (Array.IndexOf(XmlContentTypes, ContentType) >= 0)
71 {
72 Grade = Grade.Excellent;
73 return true;
74 }
75 else if (ContentType.StartsWith("application/") && ContentType.EndsWith("+xml"))
76 {
77 Grade = Grade.Barely;
78 return true;
79 }
80 else
81 {
82 Grade = Grade.NotAtAll;
83 return false;
84 }
85 }
86
97 public Task<object> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair<string, string>[] Fields, Uri BaseUri)
98 {
99 XmlDocument Doc = new XmlDocument()
100 {
101 PreserveWhitespace = true
102 };
103
104 if (Encoding is null)
105 {
106 using (MemoryStream ms = new MemoryStream(Data))
107 {
108 Doc.Load(ms);
109 }
110 }
111 else
112 {
113 string s = CommonTypes.GetString(Data, Encoding);
114 Doc.LoadXml(s);
115 }
116
117 return Task.FromResult<object>(Doc);
118 }
119
126 public bool TryGetContentType(string FileExtension, out string ContentType)
127 {
128 switch (FileExtension.ToLower())
129 {
130 case "xml":
131 ContentType = DefaultContentType;
132 return true;
133
134 case "xsd":
135 ContentType = SchemaContentType;
136 return true;
137
138 default:
139 ContentType = string.Empty;
140 return false;
141 }
142 }
143
150 public bool TryGetFileExtension(string ContentType, out string FileExtension)
151 {
152 ContentType = ContentType.ToLower();
153
154 switch (ContentType.ToLower())
155 {
157 FileExtension = "xml";
158 return true;
159
161 FileExtension = "xsd";
162 return true;
163
164 default:
165 if (ContentType.StartsWith("application/") && ContentType.EndsWith("+xml"))
166 {
167 FileExtension = ContentType.Substring(12, ContentType.Length - 4 - 12);
168 return true;
169 }
170 else
171 {
172 FileExtension = string.Empty;
173 return false;
174 }
175 }
176 }
177
185 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
186 {
187 if ((Object is XmlDocument || Object is XmlElement || Object is NamedDictionary<string, object>) &&
188 InternetContent.IsAccepted(XmlContentTypes, AcceptedContentTypes))
189 {
190 Grade = Grade.Ok;
191 return true;
192 }
193
194 if ((Object is NamedDictionary<string, IElement>) &&
195 InternetContent.IsAccepted(XmlContentTypes, AcceptedContentTypes))
196 {
197 Grade = Grade.Barely;
198 return true;
199 }
200
201 Grade = Grade.NotAtAll;
202 return false;
203 }
204
213 public Task<KeyValuePair<byte[], string>> EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
214 {
215 if (InternetContent.IsAccepted(XmlContentTypes, out string ContentType, AcceptedContentTypes))
216 {
217 if (Object is XmlDocument Doc)
218 return EncodeXmlAsync(Doc, Encoding, ContentType);
219 else if (Object is XmlElement E)
220 {
221 Doc = new XmlDocument();
222 Doc.AppendChild(Doc.ImportNode(E, true));
223 return EncodeXmlAsync(Doc, Encoding, ContentType);
224 }
225 else if (Object is NamedDictionary<string, object> Obj)
226 {
227 string Xml = XML.Encode(Obj);
228 return EncodeXmlAsync(Xml, Encoding, ContentType);
229 }
230 else if (Object is NamedDictionary<string, IElement> Obj2)
231 {
232 string Xml = XML.Encode(NamedDictionary<string, object>.ToNamedDictionary(Obj2));
233 return EncodeXmlAsync(Xml, Encoding, ContentType);
234 }
235 }
236
237 throw new ArgumentException("Unable to encode object, or content type not accepted.", nameof(Object));
238 }
239
247 public static Task<KeyValuePair<byte[], string>> EncodeXmlAsync(XmlDocument Xml, Encoding Encoding, string ContentType)
248 {
249 MemoryStream ms = null;
250 XmlWriterSettings Settings;
251 XmlWriter w = null;
252 byte[] Result;
253
254 try
255 {
256 ms = new MemoryStream();
257 Settings = XML.WriterSettings(false, false);
258
259 if (Encoding is null)
260 {
261 Settings.Encoding = Encoding.UTF8;
262 ContentType += "; charset=utf-8";
263 }
264 else
265 {
266 Settings.Encoding = Encoding;
267 ContentType += "; charset=" + Encoding.WebName;
268 }
269
270 w = XmlWriter.Create(ms, Settings);
271
272 Xml.Save(w);
273 w.Flush();
274
275 Result = ms.ToArray();
276 }
277 finally
278 {
279 w?.Dispose();
280 ms?.Dispose();
281 }
282
283 return Task.FromResult(new KeyValuePair<byte[], string>(Result, ContentType));
284 }
285
293 public static Task<KeyValuePair<byte[], string>> EncodeXmlAsync(string Xml, Encoding Encoding, string ContentType)
294 {
295 byte[] Bin;
296
297 if (Encoding is null)
298 {
299 ContentType += "; charset=utf-8";
300 Bin = Encoding.UTF8.GetBytes(Xml);
301 }
302 else
303 {
304 ContentType += "; charset=" + Encoding.WebName;
305 Bin = Encoding.GetBytes(Xml);
306 }
307
308 return Task.FromResult(new KeyValuePair<byte[], string>(Bin, ContentType));
309 }
310 }
311}
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.
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
XML encoder/decoder.
Definition: XmlCodec.cs:16
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Definition: XmlCodec.cs:150
Task< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
Definition: XmlCodec.cs:97
static Task< KeyValuePair< byte[], string > > EncodeXmlAsync(XmlDocument Xml, Encoding Encoding, string ContentType)
Encodes an XML Document.
Definition: XmlCodec.cs:247
string[] ContentTypes
Supported content types.
Definition: XmlCodec.cs:55
static readonly string[] XmlContentTypes
XML content types.
Definition: XmlCodec.cs:37
Task< KeyValuePair< byte[], string > > EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
Encodes an object.
Definition: XmlCodec.cs:213
static readonly string[] XmlFileExtensions
XML file extensions.
Definition: XmlCodec.cs:46
const string DefaultContentType
Default content type for XML documents.
Definition: XmlCodec.cs:27
string[] FileExtensions
Supported file extensions.
Definition: XmlCodec.cs:60
static Task< KeyValuePair< byte[], string > > EncodeXmlAsync(string Xml, Encoding Encoding, string ContentType)
Encodes an XML Document.
Definition: XmlCodec.cs:293
const string SchemaContentType
Default content type for XML schema documents.
Definition: XmlCodec.cs:32
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
Definition: XmlCodec.cs:68
XmlCodec()
XML encoder/decoder.
Definition: XmlCodec.cs:20
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
Definition: XmlCodec.cs:185
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
Definition: XmlCodec.cs:126
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Encode(string s)
Encodes a string for use in XML.
Definition: XML.cs:27
static XmlWriterSettings WriterSettings(bool Indent, bool OmitXmlDeclaration)
Gets an XML writer settings object.
Definition: XML.cs:1177
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...
Grade
Grade enumeration
Definition: Grade.cs:7