Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
LocalContentSource.cs
1using System;
2using System.IO;
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 || !IsLocal(Source))
38 return Grade.NotAtAll;
39 else
40 return Grade.Barely;
41 }
42
48 public static bool IsLocal(Uri Source)
49 {
50 if (!InternetContent.IsLocalDomain(Source.Host, true))
51 return false;
52
53 if (!string.IsNullOrEmpty(Source.Query))
54 return false;
55
56 if (!Types.TryGetModuleParameter("HTTP", out IResourceMap ResourceMap))
57 return false;
58
59 return
60 ResourceMap.TryGetFileName(Source.AbsolutePath, true, out string _) ||
61 ResourceMap.TryGetFileName("/" + Source.Host + Source.AbsolutePath, true, out string _);
62 }
63
72 public async Task<ISemanticCube> LoadGraph(Uri Source, ScriptNode Node, bool NullIfNotFound,
73 RequestOrigin Caller)
74 {
75 object Result = null;
76 string XmlString = null;
77
78 try
79 {
80 // TODO: Include caller credentials in request, if available.
81
82 if (!Types.TryGetModuleParameter("HTTP", out IResourceMap ResourceMap))
83 throw new Exception("No appropriate resource map registered.");
84
85 if (!ResourceMap.TryGetFileName(Source.AbsolutePath, true, out string FileName))
86 throw new Exception("Local resource file not found.");
87
88 string ContentType = InternetContent.GetContentType(Path.GetExtension(FileName));
89 byte[] Data = await Files.ReadAllBytesAsync(FileName);
90
91 ContentResponse Response = await InternetContent.DecodeAsync(ContentType, Data, Source);
92 Response.AssertOk();
93
94 Result = Response.Decoded;
95
96 if (Result is ISemanticCube Cube)
97 return Cube;
98
99 if (Result is ISemanticModel Model)
100 return await InMemorySemanticCube.Create(Model);
101
102 if (Result is XmlDocument Xml)
103 return new RdfDocument(Xml);
104
105 if (Result is string s)
106 {
107 if (XML.IsValidXml(s))
108 {
109 Xml = XML.ParseXml(XmlString = s, true);
110 return new RdfDocument(Xml);
111 }
112 else
113 return new TurtleDocument(s);
114 }
115
116 }
117 catch (XmlException ex)
118 {
119 if (NullIfNotFound)
120 return null;
121 else
122 throw XML.AnnotateException(ex, XmlString);
123 }
124 catch (Exception ex)
125 {
126 if (NullIfNotFound)
127 return null;
128 else
129 ExceptionDispatchInfo.Capture(ex).Throw();
130 }
131
132 if (NullIfNotFound)
133 return null;
134 else
135 {
136 StringBuilder sb = new StringBuilder();
137
138 sb.Append("Graph not a semantic cube or semantic model: ");
139 sb.Append(Source.ToString());
140 sb.Append(" Type of content returned: ");
141 sb.Append(Result?.GetType().FullName);
142
143 throw new ScriptRuntimeException(sb.ToString(), Node);
144 }
145 }
146 }
147}
Contains information about a response to a content request.
object Decoded
Decoded object.
void AssertOk()
Asserts response is OK.
Static class managing encoding and decoding of internet content.
static string GetContentType(string FileExtension)
Gets the content type of an item, given its file extension. It uses the TryGetContentType to see if a...
static bool IsLocalDomain(string DomainOrHost, bool IncludeAlternativeDomains)
Checks if a domain or nost name is local.
static Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
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:23
Contains semantic information stored in a turtle document. https://www.w3.org/TR/rdf12-turtle/ https:...
Helps with common XML-related tasks.
Definition: XML.cs:21
static XmlException AnnotateException(XmlException ex)
Creates a new XML Exception object, with reference to the source XML file, for information.
Definition: XML.cs:1762
static XmlDocument ParseXml(string Xml)
Parses an XML Document from its string representation.
Definition: XML.cs:713
static bool IsValidXml(string Xml)
Checks if a string is valid XML
Definition: XML.cs:1397
Contains static methods
Definition: Files.cs:14
static async Task< byte[]> ReadAllBytesAsync(string FileName)
Reads a binary file asynchronously.
Definition: Files.cs:20
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:15
static bool TryGetModuleParameter(string Name, out object Value)
Tries to get a module parameter value.
Definition: Types.cs:607
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
static bool IsLocal(Uri Source)
Checks if an URI corresponds to a local file name resource.
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
Basic interface for Resource maps.
Definition: IResourceMap.cs:7
Interface for semantic cubes.
Interface for semantic models.
Definition: ImplTypes.g.cs:58
Grade
Grade enumeration
Definition: Grade.cs:7