Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
XmlScriptValue.cs
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Numerics;
5using System.Threading.Tasks;
6using System.Xml;
7using Waher.Content;
15
17{
22 {
23 private ScriptNode node;
24 private bool isAsync;
25
34 : base(Start, Length, Expression)
35 {
36 this.node = Node;
37 this.node?.SetParent(this);
38
39 this.isAsync = Node?.IsAsynchronous ?? false;
40 }
41
46 public override bool IsAsynchronous => this.isAsync;
47
55 public override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
56 {
57 if (Order == SearchMethod.DepthFirst)
58 {
59 if (!this.node.ForAllChildNodes(Callback, State, Order))
60 return false;
61 }
62
63 bool b = !Callback(this.node, out ScriptNode NewNode, State);
64 if (!(NewNode is null))
65 {
66 this.node = NewNode;
67 this.node.SetParent(this);
68
69 this.isAsync = NewNode.IsAsynchronous;
70 }
71
72 if (b)
73 return false;
74
75 if (Order != SearchMethod.DepthFirst)
76 {
77 if (!this.node.ForAllChildNodes(Callback, State, Order))
78 return false;
79 }
80
81 return true;
82 }
83
90 internal override void Build(XmlDocument Document, XmlElement Parent, Variables Variables)
91 {
92 IElement Bak = Variables.TryGetVariable(ParentNamespaceVariableName, out Variable v) ? v.ValueElement : null;
93 Variables[ParentNamespaceVariableName] = Parent.NamespaceURI;
94
95 try
96 {
98 this.AppendChild(Document, Parent, Element.AssociatedObjectValue);
99 }
100 finally
101 {
102 if (Bak is null)
103 Variables.Remove(ParentNamespaceVariableName);
104 else
105 Variables[ParentNamespaceVariableName] = Bak;
106 }
107 }
108
109 internal const string ParentNamespaceVariableName = " Parent NS ";
110
117 internal override async Task BuildAsync(XmlDocument Document, XmlElement Parent, Variables Variables)
118 {
119 IElement Bak = Variables.TryGetVariable(ParentNamespaceVariableName, out Variable v) ? v.ValueElement : null;
120 Variables[ParentNamespaceVariableName] = Parent.NamespaceURI;
121
122 try
123 {
124 IElement Element = await this.node.EvaluateAsync(Variables);
125 this.AppendChild(Document, Parent, Element.AssociatedObjectValue);
126 }
127 finally
128 {
129 if (Bak is null)
130 Variables.Remove(ParentNamespaceVariableName);
131 else
132 Variables[ParentNamespaceVariableName] = Bak;
133 }
134 }
135
136 private void AppendChild(XmlDocument Document, XmlElement Parent, object Value)
137 {
138 if (Value is null)
139 return;
140
141 if (Value is string s)
142 Parent.AppendChild(Document.CreateTextNode(s));
143 else if (Value is bool b)
144 Parent.AppendChild(Document.CreateTextNode(CommonTypes.Encode(b)));
145 else if (Value is double d)
146 Parent.AppendChild(Document.CreateTextNode(CommonTypes.Encode(d)));
147 else if (Value is DateTime TP)
148 Parent.AppendChild(Document.CreateTextNode(XML.Encode(TP, TP.TimeOfDay == TimeSpan.Zero)));
149 else if (Value is CaseInsensitiveString cis)
150 Parent.AppendChild(Document.CreateTextNode(cis.Value));
151 else if (Value is BigInteger I)
152 Parent.AppendChild(Document.CreateTextNode(I.ToString()));
153 else if (Value is XmlDocument Doc)
154 Parent.AppendChild(Document.ImportNode(Doc.DocumentElement, true));
155 else if (Value is XmlElement E)
156 Parent.AppendChild(Document.ImportNode(E, true));
157 else if (Value is IEnumerable A)
158 {
159 IEnumerator e = A.GetEnumerator();
160
161 while (e.MoveNext())
162 this.AppendChild(Document, Parent, e.Current);
163 }
164 else
165 Parent.AppendChild(Document.CreateTextNode(Expression.ToString(Value)));
166 }
167
174 public override PatternMatchResult PatternMatch(XmlNode CheckAgainst, Dictionary<string, IElement> AlreadyFound)
175 {
176 if (CheckAgainst is XmlText ||
177 CheckAgainst is XmlWhitespace ||
178 CheckAgainst is XmlSignificantWhitespace ||
179 CheckAgainst is XmlCDataSection)
180 {
181 return this.node.PatternMatch(new StringValue(CheckAgainst.InnerText), AlreadyFound);
182 }
183 else if (CheckAgainst is XmlElement)
184 return this.node.PatternMatch(new ObjectValue(CheckAgainst), AlreadyFound);
185 else if (CheckAgainst is null)
186 {
187 if (this.node is ToVector)
188 return this.node.PatternMatch(new ObjectVector(Array.Empty<object>()), AlreadyFound);
189 else
190 return this.node.PatternMatch(ObjectValue.Null, AlreadyFound);
191 }
192 else
193 return PatternMatchResult.NoMatch;
194 }
195
202 public override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary<string, IElement> AlreadyFound)
203 {
204 return this.node.PatternMatch(CheckAgainst, AlreadyFound);
205 }
206
212 public override bool IsApplicable(XmlNode CheckAgainst)
213 {
214 return (CheckAgainst is XmlText ||
215 CheckAgainst is XmlWhitespace ||
216 CheckAgainst is XmlSignificantWhitespace ||
217 CheckAgainst is XmlCDataSection ||
218 CheckAgainst is XmlElement);
219 }
220
224 public override bool IsVector
225 {
226 get => this.node is ToVector;
227 }
228
229 }
230}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static string Encode(bool x)
Encodes a Boolean for use in XML and other formats.
Definition: CommonTypes.cs:594
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Encode(string s)
Encodes a string for use in XML.
Definition: XML.cs:27
Represents a case-insensitive string.
Base class for all types of elements.
Definition: Element.cs:13
abstract object AssociatedObjectValue
Associated object value.
Definition: Element.cs:46
Class managing a script expression.
Definition: Expression.cs:39
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
Definition: Expression.cs:4496
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
abstract IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection. This method should be ...
virtual Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection. This method should be ...
Definition: ScriptNode.cs:158
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
virtual bool Remove(string VariableName)
Removes a varaiable from the collection.
Definition: Variables.cs:147
Base class for all XML Script nodes in a parsed script tree.
override PatternMatchResult PatternMatch(XmlNode CheckAgainst, Dictionary< string, IElement > AlreadyFound)
Performs a pattern match operation.
override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary< string, IElement > AlreadyFound)
Performs a pattern match operation.
XmlScriptValue(ScriptNode Node, int Start, int Length, Expression Expression)
XML Script value node.
override bool IsVector
If the node represents a vector of nodes.
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
override bool IsApplicable(XmlNode CheckAgainst)
If the node is applicable in pattern matching against CheckAgainst .
override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
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