Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
InternetContentSource.cs
1using System;
2using System.Collections.Generic;
3using System.Runtime.ExceptionServices;
4using System.Text;
5using System.Threading.Tasks;
6using System.Xml;
7using Waher.Content;
14using Waher.Things;
15
17{
22 {
27 {
28 }
29
35 public Grade Supports(Uri Source)
36 {
37 if (!Source.IsAbsoluteUri ||
38 !InternetContent.CanGet(Source, out Grade _, out _))
39 {
40 return Grade.NotAtAll;
41 }
42 else
43 return Grade.Barely;
44 }
45
54 public async Task<ISemanticCube> LoadGraph(Uri Source, ScriptNode Node, bool NullIfNotFound,
55 RequestOrigin Caller)
56 {
57 object Result = null;
58 string XmlString = null;
59
60 try
61 {
62 // TODO: Include credentials in request, if available.
63
64 Result = await InternetContent.GetAsync(Source,
65 new KeyValuePair<string, string>("Accept", "text/turtle, application/x-turtle, application/rdf+xml;q=0.9, application/ld+json;q=0.8, text/xml;q=0.2, " + PlainTextCodec.DefaultContentType + ";q=0.1"));
66
67 if (Result is ISemanticCube Cube)
68 return Cube;
69
70 if (Result is ISemanticModel Model)
71 return await InMemorySemanticCube.Create(Model);
72
73 if (Result is XmlDocument Xml)
74 return new RdfDocument(Xml);
75
76 if (Result is string s)
77 {
78 if (XML.IsValidXml(s))
79 {
80 Xml = new XmlDocument()
81 {
82 PreserveWhitespace = true
83 };
84
85 Xml.LoadXml(XmlString = s);
86
87 return new RdfDocument(Xml);
88 }
89 else
90 return new TurtleDocument(s);
91 }
92
93 }
94 catch (XmlException ex)
95 {
96 if (NullIfNotFound)
97 return null;
98 else
99 throw XML.AnnotateException(ex, XmlString);
100 }
101 catch (Exception ex)
102 {
103 if (NullIfNotFound)
104 return null;
105 else
106 ExceptionDispatchInfo.Capture(ex).Throw();
107 }
108
109 if (NullIfNotFound)
110 return null;
111 else
112 {
113 StringBuilder sb = new StringBuilder();
114
115 sb.Append("Graph not a semantic cube or semantic model: ");
116 sb.Append(Source.ToString());
117 sb.Append(" Type of content returned: ");
118 sb.Append(Result?.GetType().FullName);
119
120 throw new ScriptRuntimeException(sb.ToString(), Node);
121 }
122 }
123 }
124}
Static class managing encoding and decoding of internet content.
static Task< object > GetAsync(Uri Uri, params KeyValuePair< string, string >[] Headers)
Gets a resource, given its URI.
static bool CanGet(Uri Uri, out Grade Grade, out IContentGetter Getter)
If a resource can be gotten, given its URI.
static async Task< InMemorySemanticCube > Create(ISemanticModel Model)
Creates an in-memory semantic cube from a semantic model.
Contains semantic information stored in an RDF document.
Definition: RdfDocument.cs:21
Contains semantic information stored in a turtle document. https://www.w3.org/TR/rdf12-turtle/ https:...
Plain text encoder/decoder.
const string DefaultContentType
text/plain
Helps with common XML-related tasks.
Definition: XML.cs:19
static XmlException AnnotateException(XmlException ex)
Creates a new XML Exception object, with reference to the source XML file, for information.
Definition: XML.cs:1588
static bool IsValidXml(string Xml)
Checks if a string is valid XML
Definition: XML.cs:1223
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
async Task< ISemanticCube > LoadGraph(Uri Source, ScriptNode Node, bool NullIfNotFound, RequestOrigin Caller)
Loads the graph
Grade Supports(Uri Source)
How well a source with a given URI can be loaded by the class.
Tokens available in request.
Definition: RequestOrigin.cs:9
Interface for semantic cubes.
Interface for semantic models.
Grade
Grade enumeration
Definition: Grade.cs:7