Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SelectXml.cs
1using System.Threading.Tasks;
2using System.Xml;
8
10{
15 {
25 : base(Xml, Name, Start, Length, Expression)
26 {
27 }
28
32 public override string FunctionName => nameof(SelectXml);
33
37 public override string[] DefaultArgumentNames => new string[] { "XML", "XPath" };
38
42 public override bool UpgradeScalarsToSameSet => false;
43
52 {
53 object Obj = Argument1.AssociatedObjectValue;
54 XmlNodeList Result;
55
56 if (Obj is null)
57 return ObjectValue.Null;
58 else if (Obj is XmlNode N)
59 Result = Evaluate(N, ToString(Argument2) ?? string.Empty);
60 else
61 throw new ScriptRuntimeException("XPath expression only operate on XML.", this);
62
63 int i, c = Result.Count;
64
65 switch (c)
66 {
67 case 0:
68 return ObjectValue.Null;
69
70 case 1:
71 return ToElement(Result[0]);
72
73 default:
74 IElement[] Items = new IElement[c];
75
76 for (i = 0; i < c; i++)
77 Items[i] = ToElement(Result[i]);
78
79 return Operators.Vectors.VectorDefinition.Encapsulate(Items, false, this);
80 }
81 }
82
91 {
92 return Task.FromResult(this.EvaluateScalar(Argument1, Argument2, Variables));
93 }
94
95 internal static XmlNodeList Evaluate(XmlNode N, string XPath)
96 {
97 XmlNamespaceManager NamespaceManager;
98 XmlNodeList Result;
99 XmlElement Root;
100
101 if (N is XmlDocument Doc)
102 {
103 NamespaceManager = new XmlNamespaceManager(Doc.NameTable);
104 Root = Doc.DocumentElement;
105 }
106 else if (N is XmlElement E && !(N.OwnerDocument is null))
107 {
108 NamespaceManager = new XmlNamespaceManager(N.OwnerDocument.NameTable);
109 Root = E;
110 }
111 else
112 {
113 NamespaceManager = null;
114 Root = null;
115 }
116
117 if (NamespaceManager is null)
118 Result = N.SelectNodes(XPath);
119 else
120 {
121 if (XPath.Contains("default:"))
122 {
123 string Namespace = null;
124
125 if (string.IsNullOrEmpty(Root.Prefix) && !string.IsNullOrEmpty(Root.NamespaceURI))
126 Namespace = Root.NamespaceURI;
127 else
128 {
130
131 foreach (XmlNode N2 in Root.ChildNodes)
132 {
133 if (N2 is XmlElement E)
134 ToProcess.Add(E);
135 }
136
137 while (ToProcess.HasFirstItem)
138 {
139 Root = ToProcess.RemoveFirst();
140
141 if (string.IsNullOrEmpty(Root.Prefix) && !string.IsNullOrEmpty(Root.NamespaceURI))
142 {
143 Namespace = Root.NamespaceURI;
144 break;
145 }
146
147 foreach (XmlNode N2 in Root.ChildNodes)
148 {
149 if (N2 is XmlElement E)
150 ToProcess.Add(E);
151 }
152 }
153 }
154
155 if (!string.IsNullOrEmpty(Namespace) && !NamespaceManager.HasNamespace(Namespace))
156 NamespaceManager.AddNamespace("default", Namespace);
157 }
158
159 Result = N.SelectNodes(XPath, NamespaceManager);
160 }
161
162 return Result;
163 }
164
170 public static IElement ToElement(XmlNode Node)
171 {
172 if (Node is XmlText Text)
173 return ToElement(Text.Value);
174 else if (Node is XmlCDataSection CData)
175 return ToElement(CData.Value);
176 else if (Node is XmlAttribute Attr)
177 return ToElement(Attr.Value);
178 else
179 {
180 if (Node.HasChildNodes && Node.FirstChild == Node.LastChild && Node.FirstChild is XmlText Text2)
181 return ToElement(Text2.Value);
182 else
183 return new ObjectValue(Node);
184 }
185 }
186
187 private static IElement ToElement(string s)
188 {
189 if (Expression.TryParse(s, out double d))
190 return new DoubleNumber(d);
191 else
192 return new StringValue(s);
193 }
194
195 }
196}
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
bool HasFirstItem
If there is a first item in the collection
Definition: ChunkedList.cs:778
T RemoveFirst()
Removes the first item in the collection.
Definition: ChunkedList.cs:876
void Add(T Item)
Adds an item to the collection.
Definition: ChunkedList.cs:272
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
Base class for funcions of two scalar variables.
ScriptNode Argument2
Function argument 2.
ScriptNode Argument1
Function argument 1.
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
override string ToString()
Definition: ScriptNode.cs:359
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:88
Collection of variables.
Definition: Variables.cs:25
Performs an XML slection using XPATH.
Definition: SelectXml.cs:15
override string FunctionName
Name of the function
Definition: SelectXml.cs:32
override bool UpgradeScalarsToSameSet
If scalars should be upgraded to the same set before evaluation.
Definition: SelectXml.cs:42
static IElement ToElement(XmlNode Node)
Encapsulates an XML Node for use in script.
Definition: SelectXml.cs:170
SelectXml(ScriptNode Xml, ScriptNode Name, int Start, int Length, Expression Expression)
Performs an XML slection using XPATH.
Definition: SelectXml.cs:24
override IElement EvaluateScalar(IElement Argument1, IElement Argument2, Variables Variables)
Evaluates the function on two scalar arguments.
Definition: SelectXml.cs:51
override string[] DefaultArgumentNames
Default Argument names
Definition: SelectXml.cs:37
override Task< IElement > EvaluateScalarAsync(IElement Argument1, IElement Argument2, Variables Variables)
Evaluates the function on two scalar arguments.
Definition: SelectXml.cs:90
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.