Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
XPath.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
9
11{
16 {
17 private readonly string xpath;
18 private bool extractValue;
19
28 : base(XPath, Start, Length, Expression)
29 {
30 this.xpath = null;
31 this.extractValue = false;
32 }
33
42 public XPath(string XPath, bool ExtractValue, int Start, int Length, Expression Expression)
44 {
45 this.xpath = XPath;
46 this.extractValue = ExtractValue;
47 }
48
52 public override string FunctionName => nameof(xpath);
53
57 public string XPathExpression => this.xpath;
58
62 public bool ExtractValue
63 {
64 get => this.extractValue;
65 internal set => this.extractValue = value;
66 }
67
74 {
75 if (string.IsNullOrEmpty(this.xpath))
76 return base.Evaluate(Variables);
77 else
78 return this.EvaluateScalar(this.xpath, Variables);
79 }
80
86 public override Task<IElement> EvaluateAsync(Variables Variables)
87 {
88 return Task.FromResult<IElement>(this.Evaluate(Variables));
89 }
90
98 {
99 if (!Variables.TryGetVariable("this", out Variable v))
100 throw new ScriptRuntimeException("'this' variable not defined.", this);
101
102 object Obj = v.ValueObject;
103 XmlNodeList Result;
104
105 if (Obj is null)
106 return ObjectValue.Null;
107 else if (Obj is XmlNode N)
108 {
109 XmlNamespaceManager NamespaceManager;
110 XmlElement Root;
111
112 if (Obj is XmlDocument Doc)
113 {
114 NamespaceManager = new XmlNamespaceManager(Doc.NameTable);
115 Root = Doc.DocumentElement;
116 }
117 else if (N is XmlElement E && !(N.OwnerDocument is null))
118 {
119 NamespaceManager = new XmlNamespaceManager(N.OwnerDocument.NameTable);
120 Root = E;
121 }
122 else
123 {
124 NamespaceManager = null;
125 Root = null;
126 }
127
128 if (NamespaceManager is null)
129 Result = N.SelectNodes(Argument);
130 else
131 {
132 if (Argument.Contains("default:"))
133 {
134 string Namespace = null;
135
136 if (string.IsNullOrEmpty(Root.Prefix) && !string.IsNullOrEmpty(Root.NamespaceURI))
137 Namespace = Root.NamespaceURI;
138 else
139 {
140 LinkedList<XmlElement> ToProcess = new LinkedList<XmlElement>();
141
142 foreach (XmlNode N2 in Root.ChildNodes)
143 {
144 if (N2 is XmlElement E)
145 ToProcess.AddLast(E);
146 }
147
148 while (!(ToProcess.First is null))
149 {
150 Root = ToProcess.First.Value;
151 ToProcess.RemoveFirst();
152
153 if (string.IsNullOrEmpty(Root.Prefix) && !string.IsNullOrEmpty(Root.NamespaceURI))
154 {
155 Namespace = Root.NamespaceURI;
156 break;
157 }
158
159 foreach (XmlNode N2 in Root.ChildNodes)
160 {
161 if (N2 is XmlElement E)
162 ToProcess.AddLast(E);
163 }
164 }
165 }
166
167 if (!string.IsNullOrEmpty(Namespace) && !NamespaceManager.HasNamespace(Namespace))
168 NamespaceManager.AddNamespace("default", Namespace);
169 }
170
171 Result = N.SelectNodes(Argument, NamespaceManager);
172 }
173 }
174 else
175 throw new ScriptRuntimeException("XPath expression only operate on XML.", this);
176
177 int i, c = Result.Count;
178
179 switch (c)
180 {
181 case 0:
182 return ObjectValue.Null;
183
184 case 1:
185 return ToElement(Result[0], this.extractValue);
186
187 default:
188 IElement[] Items = new IElement[c];
189
190 for (i = 0; i < c; i++)
191 Items[i] = ToElement(Result[i], this.extractValue);
192
193 return Operators.Vectors.VectorDefinition.Encapsulate(Items, false, this);
194 }
195 }
196
203 public static IElement ToElement(XmlNode Node, bool ExtractValue)
204 {
205 if (Node is XmlText Text)
206 return new StringValue(Text.Value);
207 else if (Node is XmlCDataSection CData)
208 return new StringValue(CData.Value);
209 else if (Node is XmlAttribute Attr)
210 return ToElement(Attr.Value);
211 else
212 {
213 if (ExtractValue && Node.HasChildNodes && Node.FirstChild == Node.LastChild && Node.FirstChild is XmlText Text2)
214 return ToElement(Text2.Value);
215 else
216 return new ObjectValue(Node);
217 }
218 }
219
220 private static IElement ToElement(string s)
221 {
222 if (Expression.TryParse(s, out double d))
223 return new DoubleNumber(d);
224 else
225 return new StringValue(s);
226 }
227
228 }
229}
Class managing a script expression.
Definition: Expression.cs:39
static bool TryParse(string s, out double Value)
Tries to parse a double-precision floating-point value.
Definition: Expression.cs:4517
Represents a constant element value.
Base class for funcions of one scalar variable.
ScriptNode Argument
Function argument.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:86
Node repesenting an XPath expression
Definition: XPath.cs:16
bool ExtractValue
If the value should be extracted.
Definition: XPath.cs:63
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: XPath.cs:73
XPath(ScriptNode XPath, int Start, int Length, Expression Expression)
Node repesenting an XPath expression
Definition: XPath.cs:27
XPath(string XPath, bool ExtractValue, int Start, int Length, Expression Expression)
Node repesenting an XPath expression
Definition: XPath.cs:42
string XPathExpression
XPATH expression
Definition: XPath.cs:57
static IElement ToElement(XmlNode Node, bool ExtractValue)
Encapsulates an XML Node for use in script.
Definition: XPath.cs:203
override string FunctionName
Name of the function
Definition: XPath.cs:52
override IElement EvaluateScalar(string Argument, Variables Variables)
Evaluates the function on a scalar argument.
Definition: XPath.cs:97
override Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: XPath.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
Basic interface for all types of elements.
Definition: IElement.cs:20
IElement Encapsulate(ICollection< IElement > Elements, ScriptNode Node)
Encapsulates a set of elements into a similar structure as that provided by the current element.