Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ReportFile.cs
1using System;
3using System.IO;
4using System.Threading.Tasks;
5using System.Xml;
6using System.Xml.Schema;
14using Waher.Script;
15using Waher.Things;
16
18{
22 public class ReportFile
23 {
24 private const string ReportFileLocalName = "Report";
25 private const string ReportFileNamespace = "http://waher.se/Schema/ReportFile.xsd";
26
27 private static readonly XmlSchema schema = XSL.LoadSchema(typeof(ReportFileNode).Namespace + ".Schema.ReportFile.xsd");
28
29 private readonly string fileName;
30 private readonly string title;
31 private readonly string @namespace;
32 private readonly string origin;
33 private readonly string[] privileges;
34 private readonly ReportParameter[] parameters;
35 private readonly ReportAction[] content;
36
41 public ReportFile(string FileName)
42 {
43 this.fileName = FileName;
44
45 XmlDocument Doc = XML.LoadFromFile(FileName, true);
46
47 XSL.Validate(Path.GetFileName(FileName), Doc, ReportFileLocalName, ReportFileNamespace, schema);
48
49 List<ReportParameter> Parameters = new List<ReportParameter>();
50 List<string> Privileges = new List<string>();
51 List<ReportAction> Content = new List<ReportAction>();
52 this.title = string.Empty;
53 this.@namespace = XML.Attribute(Doc.DocumentElement, "namespace");
54 this.origin = XML.Attribute(Doc.DocumentElement, "originVariable", "origin");
55
56 foreach (XmlNode N in Doc.DocumentElement.ChildNodes)
57 {
58 if (!(N is XmlElement E))
59 continue;
60
61 switch (E.LocalName)
62 {
63 case "Title":
64 this.title = E.InnerText;
65 break;
66
67 case "Privilege":
68 Privileges.Add(E.InnerText);
69 break;
70
71 case "Parameters":
72 foreach (XmlNode N2 in E.ChildNodes)
73 {
74 if (!(N2 is XmlElement E2))
75 continue;
76
77 switch (E2.LocalName)
78 {
79 case "Boolean":
80 Parameters.Add(new BooleanParameter(E2));
81 break;
82
83 case "Color":
84 Parameters.Add(new ColorParameter(E2));
85 break;
86
87 case "Date":
88 Parameters.Add(new DateParameter(E2));
89 break;
90
91 case "DateTime":
92 Parameters.Add(new DateTimeParameter(E2));
93 break;
94
95 case "Double":
96 Parameters.Add(new DoubleParameter(E2));
97 break;
98
99 case "Fixed":
100 Parameters.Add(new FixedParameter(E2));
101 break;
102
103 case "Int8":
104 Parameters.Add(new Int8Parameter(E2));
105 break;
106
107 case "Int16":
108 Parameters.Add(new Int16Parameter(E2));
109 break;
110
111 case "Int32":
112 Parameters.Add(new Int32Parameter(E2));
113 break;
114
115 case "Int64":
116 Parameters.Add(new Int64Parameter(E2));
117 break;
118
119 case "Jid":
120 Parameters.Add(new JidParameter(E2));
121 break;
122
123 case "Jids":
124 Parameters.Add(new JidsParameter(E2));
125 break;
126
127 case "Media":
128 Parameters.Add(new MediaParameter(E2));
129 break;
130
131 case "Password":
132 Parameters.Add(new PasswordParameter(E2));
133 break;
134
135 case "String":
136 Parameters.Add(new StringParameter(E2));
137 break;
138
139 case "Text":
140 Parameters.Add(new TextParameter(E2));
141 break;
142
143 case "Time":
144 Parameters.Add(new TimeParameter(E2));
145 break;
146
147 case "Uri":
148 Parameters.Add(new UriParameter(E2));
149 break;
150
151 default:
152 throw new NotSupportedException("Unrecognized parameter type: " + N2.LocalName);
153 }
154 }
155 break;
156
157 case "Content":
158 this.ParseActions(Content, N);
159 break;
160
161 default:
162 throw new NotSupportedException("Unrecognized report element: " + E.LocalName);
163 }
164 }
165
166 this.privileges = Privileges.ToArray();
167 this.parameters = Parameters.ToArray();
168 this.content = Content.ToArray();
169 }
170
171 internal ReportAction[] ParseActions(XmlNode Container)
172 {
173 List<ReportAction> Actions = new List<ReportAction>();
174 this.ParseActions(Actions, Container, out _, out _);
175 return Actions.ToArray();
176 }
177
178 internal ReportAction[] ParseActions(XmlNode Container,
179 out ReportAction[] Catch, out ReportAction[] Finally)
180 {
181 List<ReportAction> Actions = new List<ReportAction>();
182 this.ParseActions(Actions, Container, out Catch, out Finally);
183 return Actions.ToArray();
184 }
185
186 private void ParseActions(List<ReportAction> Actions, XmlNode Container)
187 {
188 this.ParseActions(Actions, Container, out _, out _);
189 }
190
191 private void ParseActions(List<ReportAction> Actions, XmlNode Container,
192 out ReportAction[] Catch, out ReportAction[] Finally)
193 {
194 List<ReportAction> Catch2 = null;
195 List<ReportAction> Finally2 = null;
196
197 foreach (XmlNode N in Container.ChildNodes)
198 {
199 if (!(N is XmlElement E))
200 continue;
201
202 switch (E.LocalName)
203 {
204 case "CreateTable":
205 Actions.Add(new CreateTable(E, this));
206 break;
207
208 case "TableRecords":
209 Actions.Add(new TableRecords(E, this));
210 break;
211
212 case "TableComplete":
213 Actions.Add(new TableComplete(E, this));
214 break;
215
216 case "Script":
217 Actions.Add(new ReportScript(E, this));
218 break;
219
220 case "Object":
221 Actions.Add(new ReportObject(E, this));
222 break;
223
224 case "Message":
225 Actions.Add(new Message(E, this));
226 break;
227
228 case "Status":
229 Actions.Add(new Status(E, this));
230 break;
231
232 case "Section":
233 Actions.Add(new Section(E, this));
234 break;
235
236 case "Conditional":
237 Actions.Add(new Conditional(E, this));
238 break;
239
240 case "While":
241 Actions.Add(new While(E, this));
242 break;
243
244 case "Do":
245 Actions.Add(new Do(E, this));
246 break;
247
248 case "ForEach":
249 Actions.Add(new ForEach(E, this));
250 break;
251
252 case "Try":
253 Actions.Add(new Try(E, this));
254 break;
255
256 case "Catch":
257 Catch2 ??= new List<ReportAction>();
258 this.ParseActions(Catch2, N);
259 break;
260
261 case "Finally":
262 Finally2 ??= new List<ReportAction>();
263 this.ParseActions(Finally2, N);
264 break;
265
266 default:
267 throw new NotSupportedException("Unrecognized report action: " + E.LocalName);
268 }
269 }
270
271 Catch = Catch2?.ToArray();
272 Finally = Finally2?.ToArray();
273 }
274
278 public string FileName => this.fileName;
279
283 public string Title => this.title;
284
288 public string[] Privileges => this.privileges;
289
293 public ReportParameter[] Parameters => this.parameters;
294
298 public ReportAction[] Content => this.content;
299
309 {
310 if (!string.IsNullOrEmpty(this.origin))
311 Variables[this.origin] = Origin;
312
313 foreach (ReportParameter Parameter in this.parameters)
314 await Parameter.PopulateForm(Parameters, Language, Variables);
315 }
316
326 public async Task<SetEditableFormResult> SetParameters(DataForm Parameters, Language Language,
327 bool OnlySetChanged, Variables Variables, IRequestOrigin Origin)
328 {
330
331 if (!string.IsNullOrEmpty(this.origin))
332 Variables[this.origin] = Origin;
333
334 foreach (ReportParameter Parameter in this.parameters)
335 await Parameter.SetParameter(Parameters, Language, OnlySetChanged, Variables, Result);
336
337 return Result;
338 }
339
345 public async Task<bool> Execute(ReportState State)
346 {
347 if (!string.IsNullOrEmpty(this.@namespace))
348 State.Namespace = await State.Language.GetNamespaceAsync(this.@namespace);
349
350 if (!string.IsNullOrEmpty(this.origin))
351 State.Variables[this.origin] = State.Origin;
352
353 if (!string.IsNullOrEmpty(this.title))
354 await State.Query.SetTitle(this.title);
355
356 foreach (ReportAction Action in this.content)
357 await Action.Execute(State);
358
359 return true;
360 }
361
362 }
363}
Helps with common XML-related tasks.
Definition: XML.cs:21
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:1062
static XmlDocument LoadFromFile(string FileName)
Loads an XML document from a file.
Definition: XML.cs:1808
Static class managing loading of XSL resources stored as embedded resources or in content files.
Definition: XSL.cs:16
static XmlSchema LoadSchema(string ResourceName)
Loads an XML schema from an embedded resource.
Definition: XSL.cs:24
static void Validate(string ObjectID, XmlDocument Xml, params XmlSchema[] Schemas)
Validates an XML document given a set of XML schemas.
Definition: XSL.cs:134
Static class managing editable parameters in objects. Editable parameters are defined by using the at...
Definition: Parameters.cs:26
Implements support for data forms. Data Forms are defined in the following XEPs:
Definition: DataForm.cs:42
Defines conditional execution in a report.
Definition: Conditional.cs:11
Defines a do-loop in a report.
Definition: Do.cs:13
Defines a for each-loop in a report.
Definition: ForEach.cs:14
Includes an object into the report.
Definition: ReportObject.cs:11
Executes script when report is being executed.
Definition: ReportScript.cs:11
Defines a section in the report.
Definition: Section.cs:11
Reports a status message.
Definition: Status.cs:11
Defines a TRY...CATCH...FINALLY statement in a report.
Definition: Try.cs:13
Defines a while-loop in a report.
Definition: While.cs:13
Represents a Boolean-valued parameter.
Represents a color-valued parameter.
Represents a Date-valued parameter.
Represents a Double-valued parameter.
Represents a fixed-valued parameter.
Represents a Int16-valued parameter.
Represents a Int32-valued parameter.
Represents a Int64-valued parameter.
Represents a Int8-valued parameter.
Represents a Jid-valued parameter.
Definition: JidParameter.cs:21
Represents a multiple JIDs-valued parameter.
Represents a media-valued parameter.
Represents a String-valued parameter.
Represents a text-valued parameter.
Represents a Time-valued parameter.
Represents a Uri-valued parameter.
Definition: UriParameter.cs:20
Abstract base class for report actions.
Definition: ReportAction.cs:9
Contains a parsed report, as defined in a report file.
Definition: ReportFile.cs:23
ReportFile(string FileName)
Contains a parsed report, as defined in a report file.
Definition: ReportFile.cs:41
async Task< SetEditableFormResult > SetParameters(DataForm Parameters, Language Language, bool OnlySetChanged, Variables Variables, IRequestOrigin Origin)
Sets the parameters of the object, based on contents in the data form.
Definition: ReportFile.cs:326
string FileName
File name of report.
Definition: ReportFile.cs:278
string[] Privileges
Privileges required to execute the report.
Definition: ReportFile.cs:288
async Task PopulateForm(DataForm Parameters, Language Language, Variables Variables, IRequestOrigin Origin)
Populates a data form with parameters for the object.
Definition: ReportFile.cs:307
ReportAction[] Content
Content of report.
Definition: ReportFile.cs:298
async Task< bool > Execute(ReportState State)
Executes the report action.
Definition: ReportFile.cs:345
string Title
Title of report.
Definition: ReportFile.cs:283
Abstract base class for report parameters.
abstract Task SetParameter(DataForm Parameters, Language Language, bool OnlySetChanged, Variables Variables, SetEditableFormResult Result)
Sets the parameters of the object, based on contents in the data form.
abstract Task PopulateForm(DataForm Parameters, Language Language, Variables Variables)
Populates a data form with parameters for the object.
Class containing the state of a report.
Definition: ReportState.cs:12
Variables Variables
Current set of variables.
Definition: ReportState.cs:55
Query Query
Current query object reference.
Definition: ReportState.cs:35
RequestOrigin Origin
Origin of request to generate report.
Definition: ReportState.cs:60
Language Language
Preferred language.
Definition: ReportState.cs:40
Report node based on the contents of a report file.
Contains information about a language.
Definition: Language.cs:17
async Task< Namespace > GetNamespaceAsync(string Name)
Gets the namespace object, given its name, if available.
Definition: Language.cs:99
Contains information about a namespace in a language.
Definition: Namespace.cs:17
Collection of variables.
Definition: Variables.cs:25
Interface for requestors that can act as an origin for distributed requests.
Definition: ImplTypes.g.cs:58