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