Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Multimedia.cs
1using System;
2using System.Reflection;
4using System.Threading.Tasks;
6using Waher.Events;
9
11{
16 {
17 private static readonly Dictionary<Type, IMultimediaRenderer[]> renderersPerType = new Dictionary<Type, IMultimediaRenderer[]>();
18
19 private IMultimediaContent handler = null;
20 private Type handlerType = null;
21 private readonly MultimediaItem[] items;
22 private readonly bool aloneInParagraph;
23
32 : base(Document, ChildElements)
33 {
34 this.items = Items;
35 this.aloneInParagraph = AloneInParagraph;
36 }
37
41 public MultimediaItem[] Items => this.items;
42
46 public bool AloneInParagraph => this.aloneInParagraph;
47
52 public override Task Render(IRenderer Output) => Output.Render(this);
53
58 where T : IMultimediaRenderer
59 {
60 if (this.handlerType is null || this.handlerType != typeof(T))
61 {
62 this.handler = GetMultimediaHandler<T>(this.items);
63 this.handlerType = typeof(T);
64 }
65
66 return (T)this.handler;
67 }
68
74 public static IMultimediaContent GetMultimediaHandler<T>(params string[] URLs)
75 where T : IMultimediaRenderer
76 {
77 int i, c = URLs.Length;
79
80 for (i = 0; i < c; i++)
81 Items[i] = new MultimediaItem(null, URLs[i], string.Empty, null, null);
82
84 }
85
92 where T : IMultimediaRenderer
93 {
94 T Best = default;
95 Grade BestGrade = Grade.NotAtAll;
96 Grade CurrentGrade;
97 bool HasBest = false;
98
99 foreach (MultimediaItem Item in Items)
100 {
101 foreach (IMultimediaContent Handler in GetRenderers<T>())
102 {
103 CurrentGrade = Handler.Supports(Item);
104 if (CurrentGrade > BestGrade && Handler is T TypedHandler)
105 {
106 Best = TypedHandler;
107 BestGrade = CurrentGrade;
108 HasBest = true;
109 }
110 }
111
112 if (HasBest)
113 break;
114 }
115
116 return Best;
117 }
118
123 where T : IMultimediaRenderer
124 {
125 lock (renderersPerType)
126 {
127 if (!renderersPerType.TryGetValue(typeof(T), out IMultimediaRenderer[] Renderers))
128 {
130 IMultimediaRenderer Handler;
131
132 foreach (Type Type in Types.GetTypesImplementingInterface(typeof(T)))
133 {
134 ConstructorInfo CI = Types.GetDefaultConstructor(Type);
135 if (CI is null)
136 continue;
137
138 try
139 {
140 Handler = (IMultimediaRenderer)CI.Invoke(Types.NoParameters);
141 }
142 catch (Exception ex)
143 {
144 Log.Exception(ex);
145 continue;
146 }
147
148 Handlers.Add(Handler);
149 }
150
151 Renderers = Handlers.ToArray();
152 renderersPerType[typeof(T)] = Renderers;
153 }
154
155 return Renderers;
156 }
157 }
158
159 static Multimedia()
160 {
161 Types.OnInvalidated += Types_OnInvalidated;
162 }
163
164 private static void Types_OnInvalidated(object Sender, EventArgs e)
165 {
166 lock (renderersPerType)
167 {
168 renderersPerType.Clear();
169 }
170 }
171
175 public override bool OutsideParagraph => true;
176
180 public override bool InlineSpanElement => true;
181
190 {
191 return new Multimedia(Document, Children, this.aloneInParagraph, this.items);
192 }
193
200 public override bool SameMetaData(MarkdownElement E)
201 {
202 return E is Multimedia x &&
203 x.aloneInParagraph == this.aloneInParagraph &&
204 AreEqual(x.items, this.items) &&
205 base.SameMetaData(E);
206 }
207
213 public override bool Equals(object obj)
214 {
215 return obj is Multimedia x &&
216 this.aloneInParagraph == x.aloneInParagraph &&
217 AreEqual(x.items, this.items) &&
218 base.Equals(obj);
219 }
220
225 public override int GetHashCode()
226 {
227 int h1 = base.GetHashCode();
228 int h2 = this.aloneInParagraph.GetHashCode();
229
230 h1 = ((h1 << 5) + h1) ^ h2;
231 h2 = GetHashCode(this.items);
232
233 h1 = ((h1 << 5) + h1) ^ h2;
234
235 return h1;
236 }
237
242 public override void IncrementStatistics(MarkdownStatistics Statistics)
243 {
244 IncrementStatistics(Statistics, this.items);
245 }
246
247 internal static void IncrementStatistics(MarkdownStatistics Statistics, MultimediaItem[] Items)
248 {
249 if (!(Items is null))
250 {
251 Statistics.NrMultimedia++;
252
253 if (Items.Length > 1)
254 Statistics.NrMultiformatMultimedia++;
255
256 foreach (MultimediaItem Item in Items)
257 {
258 if (Statistics.IntMultimediaPerContentType is null)
259 {
260 Statistics.IntMultimediaPerContentType = new Dictionary<string, ChunkedList<string>>();
261 Statistics.IntMultimediaPerContentCategory = new Dictionary<string, ChunkedList<string>>();
262 Statistics.IntMultimediaPerExtension = new Dictionary<string, ChunkedList<string>>();
263 }
264
265 IncItem(Item.ContentType, Item.Url, Statistics.IntMultimediaPerContentType);
266 IncItem(Item.Extension, Item.Url, Statistics.IntMultimediaPerExtension);
267
268 string s = Item.ContentType;
269 int i = s.IndexOf('/');
270 if (i > 0)
271 s = s.Substring(0, i);
272
273 IncItem(s, Item.Url, Statistics.IntMultimediaPerContentCategory);
274 }
275 }
276 }
277
278 private static void IncItem(string Key, string Url, Dictionary<string, ChunkedList<string>> Dictionary)
279 {
280 if (!Dictionary.TryGetValue(Key, out ChunkedList<string> List))
281 {
282 List = new ChunkedList<string>();
283 Dictionary[Key] = List;
284 }
285
286 List.Add(Url);
287 }
288
289 }
290}
Contains a markdown document. This markdown document class supports original markdown,...
Contains some basic statistical information about a Markdown document.
int NrMultimedia
Number of multimedia (total).
int NrMultiformatMultimedia
Number of multi-formatted multimedia.
Abstract base class for all markdown elements with a variable number of child elements.
override ChunkedList< MarkdownElement > Children
Any children of the element.
Abstract base class for all markdown elements.
MarkdownDocument Document
Markdown document.
static bool AreEqual(Array Items1, Array Items2)
Checks if two typed arrays are equal
static IMultimediaContent GetMultimediaHandler< T >(params string[] URLs)
Gets the best multimedia handler for a set of URLs or file names.
Definition: Multimedia.cs:74
MultimediaItem[] Items
Multimedia items.
Definition: Multimedia.cs:41
override bool OutsideParagraph
If element, parsed as a span element, can stand outside of a paragraph if alone in it.
Definition: Multimedia.cs:175
override bool SameMetaData(MarkdownElement E)
If the current object has same meta-data as E (but not necessarily same content).
Definition: Multimedia.cs:200
override bool InlineSpanElement
If the element is an inline span element.
Definition: Multimedia.cs:180
Multimedia(MarkdownDocument Document, ChunkedList< MarkdownElement > ChildElements, bool AloneInParagraph, params MultimediaItem[] Items)
Multimedia
Definition: Multimedia.cs:31
bool AloneInParagraph
If the element is alone in a paragraph.
Definition: Multimedia.cs:46
static IMultimediaRenderer[] GetRenderers< T >()
Multimedia handlers.
Definition: Multimedia.cs:122
override Task Render(IRenderer Output)
Renders the element.
override MarkdownElementChildren Create(ChunkedList< MarkdownElement > Children, MarkdownDocument Document)
Creates an object of the same type, and meta-data, as the current object, but with content defined by...
Definition: Multimedia.cs:189
override bool Equals(object obj)
Determines whether the specified object is equal to the current object.
Definition: Multimedia.cs:213
override int GetHashCode()
Serves as the default hash function.
Definition: Multimedia.cs:225
override void IncrementStatistics(MarkdownStatistics Statistics)
Increments the property or properties in Statistics corresponding to the element.
Definition: Multimedia.cs:242
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1657
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:15
static object[] NoParameters
Contains an empty array of parameter values.
Definition: Types.cs:572
static Type[] GetTypesImplementingInterface(string InterfaceFullName)
Gets all types implementing a given interface.
Definition: Types.cs:85
static ConstructorInfo GetDefaultConstructor(Type Type)
Gets the default constructor of a type, if one exists.
Definition: Types.cs:1742
Interface for all markdown handlers of multimedia content.
Grade Supports(MultimediaItem Item)
Checks how well the handler supports multimedia content of a given type.
Interface for multimedia content renderers.
Interface for Markdown renderers.
Definition: IRenderer.cs:12
Grade
Grade enumeration
Definition: Grade.cs:7