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;
3using System.Collections.Generic;
4using System.Threading.Tasks;
6using Waher.Events;
8
10{
15 {
16 private static readonly Dictionary<Type, IMultimediaRenderer[]> renderersPerType = new Dictionary<Type, IMultimediaRenderer[]>();
17
18 private IMultimediaContent handler = null;
19 private Type handlerType = null;
20 private readonly MultimediaItem[] items;
21 private readonly bool aloneInParagraph;
22
30 public Multimedia(MarkdownDocument Document, IEnumerable<MarkdownElement> ChildElements, bool AloneInParagraph, params MultimediaItem[] Items)
31 : base(Document, ChildElements)
32 {
33 this.items = Items;
34 this.aloneInParagraph = AloneInParagraph;
35 }
36
40 public MultimediaItem[] Items => this.items;
41
45 public bool AloneInParagraph => this.aloneInParagraph;
46
51 public override Task Render(IRenderer Output) => Output.Render(this);
52
57 where T : IMultimediaRenderer
58 {
59 if (this.handlerType is null || this.handlerType != typeof(T))
60 {
61 this.handler = GetMultimediaHandler<T>(this.items);
62 this.handlerType = typeof(T);
63 }
64
65 return (T)this.handler;
66 }
67
73 public static IMultimediaContent GetMultimediaHandler<T>(params string[] URLs)
74 where T : IMultimediaRenderer
75 {
76 int i, c = URLs.Length;
78
79 for (i = 0; i < c; i++)
80 Items[i] = new MultimediaItem(null, URLs[i], string.Empty, null, null);
81
83 }
84
91 where T : IMultimediaRenderer
92 {
93 T Best = default;
94 Grade BestGrade = Grade.NotAtAll;
95 Grade CurrentGrade;
96 bool HasBest = false;
97
98 foreach (MultimediaItem Item in Items)
99 {
100 foreach (IMultimediaContent Handler in GetRenderers<T>())
101 {
102 CurrentGrade = Handler.Supports(Item);
103 if (CurrentGrade > BestGrade && Handler is T TypedHandler)
104 {
105 Best = TypedHandler;
106 BestGrade = CurrentGrade;
107 HasBest = true;
108 }
109 }
110
111 if (HasBest)
112 break;
113 }
114
115 return Best;
116 }
117
122 where T : IMultimediaRenderer
123 {
124 lock (renderersPerType)
125 {
126 if (!renderersPerType.TryGetValue(typeof(T), out IMultimediaRenderer[] Renderers))
127 {
128 List<IMultimediaRenderer> Handlers = new List<IMultimediaRenderer>();
129 IMultimediaRenderer Handler;
130
131 foreach (Type Type in Types.GetTypesImplementingInterface(typeof(T)))
132 {
133 ConstructorInfo CI = Types.GetDefaultConstructor(Type);
134 if (CI is null)
135 continue;
136
137 try
138 {
139 Handler = (IMultimediaRenderer)CI.Invoke(Types.NoParameters);
140 }
141 catch (Exception ex)
142 {
143 Log.Exception(ex);
144 continue;
145 }
146
147 Handlers.Add(Handler);
148 }
149
150 Renderers = Handlers.ToArray();
151 renderersPerType[typeof(T)] = Renderers;
152 }
153
154 return Renderers;
155 }
156 }
157
158 static Multimedia()
159 {
160 Types.OnInvalidated += Types_OnInvalidated;
161 }
162
163 private static void Types_OnInvalidated(object Sender, EventArgs e)
164 {
165 lock (renderersPerType)
166 {
167 renderersPerType.Clear();
168 }
169 }
170
174 public override bool OutsideParagraph => true;
175
179 public override bool InlineSpanElement => true;
180
188 public override MarkdownElementChildren Create(IEnumerable<MarkdownElement> Children, MarkdownDocument Document)
189 {
190 return new Multimedia(Document, Children, this.aloneInParagraph, this.items);
191 }
192
199 public override bool SameMetaData(MarkdownElement E)
200 {
201 return E is Multimedia x &&
202 x.aloneInParagraph == this.aloneInParagraph &&
203 AreEqual(x.items, this.items) &&
204 base.SameMetaData(E);
205 }
206
212 public override bool Equals(object obj)
213 {
214 return obj is Multimedia x &&
215 this.aloneInParagraph == x.aloneInParagraph &&
216 AreEqual(x.items, this.items) &&
217 base.Equals(obj);
218 }
219
224 public override int GetHashCode()
225 {
226 int h1 = base.GetHashCode();
227 int h2 = this.aloneInParagraph.GetHashCode();
228
229 h1 = ((h1 << 5) + h1) ^ h2;
230 h2 = GetHashCode(this.items);
231
232 h1 = ((h1 << 5) + h1) ^ h2;
233
234 return h1;
235 }
236
241 public override void IncrementStatistics(MarkdownStatistics Statistics)
242 {
243 IncrementStatistics(Statistics, this.items);
244 }
245
246 internal static void IncrementStatistics(MarkdownStatistics Statistics, MultimediaItem[] Items)
247 {
248 if (!(Items is null))
249 {
250 Statistics.NrMultimedia++;
251
252 if (Items.Length > 1)
253 Statistics.NrMultiformatMultimedia++;
254
255 foreach (MultimediaItem Item in Items)
256 {
257 if (Statistics.IntMultimediaPerContentType is null)
258 {
259 Statistics.IntMultimediaPerContentType = new Dictionary<string, List<string>>();
260 Statistics.IntMultimediaPerContentCategory = new Dictionary<string, List<string>>();
261 Statistics.IntMultimediaPerExtension = new Dictionary<string, List<string>>();
262 }
263
264 IncItem(Item.ContentType, Item.Url, Statistics.IntMultimediaPerContentType);
265 IncItem(Item.Extension, Item.Url, Statistics.IntMultimediaPerExtension);
266
267 string s = Item.ContentType;
268 int i = s.IndexOf('/');
269 if (i > 0)
270 s = s.Substring(0, i);
271
272 IncItem(s, Item.Url, Statistics.IntMultimediaPerContentCategory);
273 }
274 }
275 }
276
277 private static void IncItem(string Key, string Url, Dictionary<string, List<string>> Dictionary)
278 {
279 if (!Dictionary.TryGetValue(Key, out List<string> List))
280 {
281 List = new List<string>();
282 Dictionary[Key] = List;
283 }
284
285 List.Add(Url);
286 }
287
288 }
289}
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 IEnumerable< 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:73
MultimediaItem[] Items
Multimedia items.
Definition: Multimedia.cs:40
override bool OutsideParagraph
If element, parsed as a span element, can stand outside of a paragraph if alone in it.
Definition: Multimedia.cs:174
override bool SameMetaData(MarkdownElement E)
If the current object has same meta-data as E (but not necessarily same content).
Definition: Multimedia.cs:199
override bool InlineSpanElement
If the element is an inline span element.
Definition: Multimedia.cs:179
bool AloneInParagraph
If the element is alone in a paragraph.
Definition: Multimedia.cs:45
Multimedia(MarkdownDocument Document, IEnumerable< MarkdownElement > ChildElements, bool AloneInParagraph, params MultimediaItem[] Items)
Multimedia
Definition: Multimedia.cs:30
static IMultimediaRenderer[] GetRenderers< T >()
Multimedia handlers.
Definition: Multimedia.cs:121
override MarkdownElementChildren Create(IEnumerable< 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:188
override Task Render(IRenderer Output)
Renders the element.
override bool Equals(object obj)
Determines whether the specified object is equal to the current object.
Definition: Multimedia.cs:212
override int GetHashCode()
Serves as the default hash function.
Definition: Multimedia.cs:224
override void IncrementStatistics(MarkdownStatistics Statistics)
Increments the property or properties in Statistics corresponding to the element.
Definition: Multimedia.cs:241
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
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:1647
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:14
static object[] NoParameters
Contains an empty array of parameter values.
Definition: Types.cs:548
static Type[] GetTypesImplementingInterface(string InterfaceFullName)
Gets all types implementing a given interface.
Definition: Types.cs:84
static ConstructorInfo GetDefaultConstructor(Type Type)
Gets the default constructor of a type, if one exists.
Definition: Types.cs:1630
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