Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
XmlScriptAttributeScript.cs
1using System.Collections.Generic;
2using System.Threading.Tasks;
3using System.Xml;
7
9{
14 {
15 private ScriptNode node;
16 private bool isAsync;
17 private string variableReference;
18
28 : base(Name, Start, Length, Expression)
29 {
30 this.node = Node;
31 this.node?.SetParent(this);
32
33 this.isAsync = Node?.IsAsynchronous ?? false;
34
35 if (Node is VariableReference Ref)
36 this.variableReference = Ref.VariableName;
37 else
38 this.variableReference = null;
39 }
40
45 public override bool IsAsynchronous => this.isAsync;
46
54 public override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
55 {
56 if (Order == SearchMethod.DepthFirst)
57 {
58 if (!this.node.ForAllChildNodes(Callback, State, Order))
59 return false;
60 }
61
62 bool b = !Callback(this.node, out ScriptNode NewNode, State);
63 if (!(NewNode is null))
64 {
65 this.node = NewNode;
66 this.node.SetParent(this);
67
68 this.isAsync = NewNode.IsAsynchronous;
69
70 if (NewNode is VariableReference Ref)
71 this.variableReference = Ref.VariableName;
72 else
73 this.variableReference = null;
74 }
75
76 if (b || (Order == SearchMethod.TreeOrder && !this.node.ForAllChildNodes(Callback, State, Order)))
77 return false;
78
79 if (Order == SearchMethod.BreadthFirst)
80 {
81 if (!this.node.ForAllChildNodes(Callback, State, Order))
82 return false;
83 }
84
85 return true;
86 }
87
94 internal override void Build(XmlDocument Document, XmlElement Parent, Variables Variables)
95 {
96 if (string.IsNullOrEmpty(this.variableReference))
97 {
98 string s = EvaluateString(this.node, Variables);
99 if (!(s is null))
100 Parent.SetAttribute(this.Name, s);
101 }
102 else
103 {
104 if (Variables.TryGetVariable(this.variableReference, out Variable v))
105 Parent.SetAttribute(this.Name, EvaluateString(v.ValueElement));
106 else if (Expression.TryGetConstant(this.variableReference, Variables, out IElement ValueElement))
107 Parent.SetAttribute(this.Name, EvaluateString(ValueElement));
108 }
109 }
110
117 internal override async Task BuildAsync(XmlDocument Document, XmlElement Parent, Variables Variables)
118 {
119 if (string.IsNullOrEmpty(this.variableReference))
120 {
121 string s = await EvaluateStringAsync(this.node, Variables);
122 if (!(s is null))
123 Parent.SetAttribute(this.Name, s);
124 }
125 else
126 {
127 if (Variables.TryGetVariable(this.variableReference, out Variable v))
128 Parent.SetAttribute(this.Name, EvaluateString(v.ValueElement));
129 else if (Expression.TryGetConstant(this.variableReference, Variables, out IElement ValueElement))
130 Parent.SetAttribute(this.Name, EvaluateString(ValueElement));
131 }
132 }
133
138 internal override string GetValue(Variables Variables)
139 {
140 return EvaluateString(this.node, Variables);
141 }
142
147 internal override Task<string> GetValueAsync(Variables Variables)
148 {
149 return EvaluateStringAsync(this.node, Variables);
150 }
151
158 public override PatternMatchResult PatternMatch(XmlNode CheckAgainst, Dictionary<string, IElement> AlreadyFound)
159 {
160 if (CheckAgainst is XmlAttribute)
161 return this.node.PatternMatch(new StringValue(CheckAgainst.Value), AlreadyFound);
162 else if (CheckAgainst is null)
163 return this.node.PatternMatch(ObjectValue.Null, AlreadyFound);
164 else
165 return PatternMatchResult.NoMatch;
166 }
167
174 public override PatternMatchResult PatternMatch(string CheckAgainst, Dictionary<string, IElement> AlreadyFound)
175 {
176 if (CheckAgainst is null)
177 return this.node.PatternMatch(ObjectValue.Null, AlreadyFound);
178 else
179 return this.node.PatternMatch(new StringValue(CheckAgainst), AlreadyFound);
180 }
181
187 public override bool IsApplicable(string CheckAgainst)
188 {
189 return true;
190 }
191 }
192}
Class managing a script expression.
Definition: Expression.cs:39
static bool TryGetConstant(string Name, Variables Variables, out IElement ValueElement)
Tries to get a constant value, given its name.
Definition: Expression.cs:3307
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, bool DepthFirst)
Calls the callback method for all child nodes.
Definition: ScriptNode.cs:243
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
virtual PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary< string, IElement > AlreadyFound)
Performs a pattern match operation.
Definition: ScriptNode.cs:169
ScriptNode Parent
Parent node.
Definition: ScriptNode.cs:126
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
virtual bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: ScriptNode.cs:142
void SetParent(ScriptNode Parent)
Sets the parent node. Can only be used when expression is being parsed.
Definition: ScriptNode.cs:132
Represents a variable reference.
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:86
Contains information about a variable.
Definition: Variable.cs:10
Collection of variables.
Definition: Variables.cs:25
virtual bool TryGetVariable(string Name, out Variable Variable)
Tries to get a variable object, given its name.
Definition: Variables.cs:52
Abstract base class for XML Script attribute nodes.
XML Script attribute node, whose value is defined by script.
override PatternMatchResult PatternMatch(XmlNode CheckAgainst, Dictionary< string, IElement > AlreadyFound)
Performs a pattern match operation.
override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
override bool IsApplicable(string CheckAgainst)
If the node is applicable in pattern matching against CheckAgainst .
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
XmlScriptAttributeScript(string Name, ScriptNode Node, int Start, int Length, Expression Expression)
XML Script attribute node, whose value is defined by script.
override PatternMatchResult PatternMatch(string CheckAgainst, Dictionary< string, IElement > AlreadyFound)
Performs a pattern match operation.
static string EvaluateString(ScriptNode Node, Variables Variables)
Evaluates a script node to a string.
static async Task< string > EvaluateStringAsync(ScriptNode Node, Variables Variables)
Evaluates a script node to a string.
Basic interface for all types of elements.
Definition: IElement.cs:20
delegate bool ScriptNodeEventHandler(ScriptNode Node, out ScriptNode NewNode, object State)
Delegate for ScriptNode callback methods.
PatternMatchResult
Status result of a pattern matching operation.
Definition: ScriptNode.cs:17
SearchMethod
Method to traverse the expression structure
Definition: ScriptNode.cs:38