Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ScriptStringParameterNode.cs
1using System.Text.RegularExpressions;
2using System.Threading.Tasks;
10using Waher.Script;
12
14{
19 {
20 private string pattern = string.Empty;
21 private Regex parsed = null;
22
27 : base()
28 {
29 }
30
34 [Page(2, "Script", 100)]
35 [Header(29, "Default value:")]
36 [ToolTip(30, "Default value presented to user.")]
37 public string DefaultValue { get; set; }
38
42 [Page(2, "Script", 100)]
43 [Header(31, "Pattern:")]
44 [ToolTip(32, "Regular expression used for validating user input.")]
45 public string Pattern
46 {
47 get => this.pattern;
48 set
49 {
50 if (string.IsNullOrEmpty(value))
51 {
52 this.pattern = null;
53 this.parsed = null;
54 }
55 else
56 {
57 this.parsed = new Regex(value);
58 this.pattern = value;
59 }
60 }
61 }
62
68 public override Task<string> GetTypeNameAsync(Language Language)
69 {
70 return Language.GetStringAsync(typeof(ScriptNode), 35, "String-valued parameter");
71 }
72
79 public override async Task PopulateForm(DataForm Parameters, Language Language, object Value)
80 {
81 ValidationMethod Validation;
83
84 if (string.IsNullOrEmpty(this.Pattern))
85 Validation = new OpenValidation();
86 else
87 Validation = new RegexValidation(this.Pattern);
88
89 if (this.RestrictToOptions)
90 {
91 Field = new ListSingleField(Parameters, this.ParameterName, this.Label, this.Required,
92 new string[] { this.DefaultValue }, await this.GetOptions(), this.Description, StringDataType.Instance,
93 Validation, string.Empty, false, false, false);
94 }
95 else
96 {
98 new string[] { this.DefaultValue }, await this.GetOptions(), this.Description, StringDataType.Instance,
99 Validation, string.Empty, false, false, false);
100 }
101
102 Parameters.Add(Field);
103
104 Page Page = Parameters.GetPage(this.Page);
105 Page.Add(Field);
106 }
107
117 public override async Task SetParameter(DataForm Parameters, Language Language, bool OnlySetChanged, Variables Values,
119 {
121 if (Field is null)
122 {
123 if (this.Required)
124 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 42, "Required parameter."));
125
126 Values[this.ParameterName] = null;
127 }
128 else
129 {
130 string s = Field.ValueString;
131
132 if (string.IsNullOrEmpty(s))
133 {
134 if (this.Required)
135 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 42, "Required parameter."));
136
137 Values[this.ParameterName] = null;
138 }
139 else
140 {
141 Values[this.ParameterName] = s;
142
143 if (!(this.parsed is null))
144 {
145 Match M = this.parsed.Match(s);
146
147 if (!M.Success || M.Index > 0 || M.Length != s.Length)
148 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 43, "Value does not match expected pattern."));
149 }
150 }
151 }
152 }
153
154 }
155}
Static class managing editable parameters in objects. Editable parameters are defined by using the at...
Definition: Parameters.cs:25
void AddError(string Key, string Value)
Adds an error to the list of errors.
Implements support for data forms. Data Forms are defined in the following XEPs:
Definition: DataForm.cs:42
static readonly StringDataType Instance
Public instance of data type.
Base class for form fields
Definition: Field.cs:16
string ValueString
Value as a single string. If field contains multiple values, they will be concatenated into a single ...
Definition: Field.cs:101
Class managing a page in a data form layout.
Definition: Page.cs:11
void Add(LayoutElement Element)
Adds a layout element.
Definition: Section.cs:135
Contains information about a language.
Definition: Language.cs:17
Task< string > GetStringAsync(Type Type, int Id, string Default)
Gets the string value of a string ID. If no such string exists, a string is created with the default ...
Definition: Language.cs:209
Collection of variables.
Definition: Variables.cs:25
Represents a parameter with possible options on a command.
async Task< KeyValuePair< string, string >[]> GetOptions()
Gets available options, if any are defined.
override async Task SetParameter(DataForm Parameters, Language Language, bool OnlySetChanged, Variables Values, SetEditableFormResult Result)
Sets the parameters of the object, based on contents in the data form.
override Task< string > GetTypeNameAsync(Language Language)
Gets the type name of the node.
override async Task PopulateForm(DataForm Parameters, Language Language, object Value)
Populates a data form with parameters for the object.
string Pattern
Optional regular expression used for validating user input.
ScriptStringParameterNode()
Represents a string-valued script parameter.
Node defined by script.
Definition: ScriptNode.cs:19
bool Required
If parameter is required.
string Description
Parameter description.