/
PathExpression.cs
184 lines (160 loc) · 7.71 KB
/
PathExpression.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using FontoXPathCSharp.DomFacade;
using FontoXPathCSharp.Expressions.DataTypes;
using FontoXPathCSharp.Sequences;
using FontoXPathCSharp.Value;
using FontoXPathCSharp.Value.Types;
using ValueType = FontoXPathCSharp.Value.Types.ValueType;
namespace FontoXPathCSharp.Expressions;
public class PathExpression<TNode> : AbstractExpression<TNode> where TNode : notnull
{
private readonly bool _requireSortedResults;
private readonly AbstractExpression<TNode>[] _stepExpressions;
public PathExpression(AbstractExpression<TNode>[] stepExpressions, bool requireSortedResults) : base(
stepExpressions.Aggregate(
new Specificity(),
(specificity, selector) => specificity.Add(selector.Specificity)
),
stepExpressions,
new OptimizationOptions(
false,
stepExpressions.All(selector => selector.Peer),
requireSortedResults ? ResultOrdering.Sorted : ResultOrdering.Unsorted,
stepExpressions.All(selector => selector.Subtree))
)
{
_stepExpressions = stepExpressions;
_requireSortedResults = requireSortedResults;
}
public override string ToString()
{
return $"PathExpr[ {string.Join(", ", _stepExpressions.Select(x => x.ToString()))} ]";
}
private static AbstractValue[] SortResults(DomFacade<TNode> domFacade, AbstractValue[] result)
{
var resultContainsNodes = false;
var resultContainsNonNodes = false;
foreach (var resultValue in result)
if (resultValue.GetValueType().IsSubtypeOf(ValueType.Node)) resultContainsNodes = true;
else resultContainsNonNodes = true;
if (resultContainsNonNodes && resultContainsNodes)
throw new XPathException(
"XPTY0018",
"The path operator should either return nodes or non-nodes. Mixed sequences are not allowed."
);
if (resultContainsNodes)
{
var nodesList = result.Cast<NodeValue<TNode>>().ToList();
return DocumentOrderUtils<TNode>
.SortNodeValues(domFacade, nodesList)
.Cast<AbstractValue>()
.ToArray();
}
return result;
}
public override ISequence Evaluate(DynamicContext? dynamicContext, ExecutionParameters<TNode>? executionParameters)
{
var sequenceHasPeerProperty = true;
var result = _stepExpressions.Reduce(
(ISequence?)null,
(intermediateResultNodesSequence, selector, index) =>
{
var childContextIterator = intermediateResultNodesSequence == null
? IteratorUtils.SingleValueIterator(dynamicContext!)
: dynamicContext?.CreateSequenceIterator(intermediateResultNodesSequence);
Iterator<ISequence> resultValuesInOrderOfEvaluation = hint =>
{
var childContext = childContextIterator!(hint);
if (childContext.IsDone) return IteratorResult<ISequence>.Done();
if (
childContext.Value?.ContextItem != null &&
!childContext.Value.ContextItem.GetValueType().IsSubtypeOf(ValueType.Node)
)
if (index > 0)
// The result comes from the first expression: that's not allowed:
// XPTY0019.
// If the result comes from the zero-th expression, it is coming
// from outside. In that case, the axis step is supposed to error
// with XPTY0020
throw new XPathException(
"XPTY0019",
"The result of E1 in a path expression E1/E2 should not evaluate to a sequence of nodes."
);
return IteratorResult<ISequence>.Ready(
selector.EvaluateMaybeStatically(
childContext.Value,
executionParameters
)
);
};
ISequence? sortedResultSequence = null;
if (!_requireSortedResults)
{
sortedResultSequence =
SortedSequenceUtils<TNode>.ConcatSortedSequences(resultValuesInOrderOfEvaluation);
}
else {
switch (selector.ExpectedResultOrder)
{
case ResultOrdering.ReverseSorted:
var resultValuesInReverseOrder = resultValuesInOrderOfEvaluation;
resultValuesInOrderOfEvaluation = hint =>
{
var res = resultValuesInReverseOrder(hint);
if (res.IsDone) return res;
return IteratorResult<ISequence>.Ready(
res.Value!.MapAll(items =>
SequenceFactory.CreateFromArray(items.Reverse().ToArray())
)
);
};
// Can't do fallthrough in C#, so it is copied.
if (selector.Subtree && sequenceHasPeerProperty)
{
sortedResultSequence = SortedSequenceUtils<TNode>.ConcatSortedSequences(
resultValuesInOrderOfEvaluation
);
break;
}
// Only locally sorted
sortedResultSequence = SortedSequenceUtils<TNode>.MergeSortedSequences(
executionParameters!.DomFacade,
resultValuesInOrderOfEvaluation
);
break;
case ResultOrdering.Sorted:
if (selector.Subtree && sequenceHasPeerProperty)
{
sortedResultSequence = SortedSequenceUtils<TNode>.ConcatSortedSequences(
resultValuesInOrderOfEvaluation
);
break;
}
// Only locally sorted
sortedResultSequence = SortedSequenceUtils<TNode>.MergeSortedSequences(
executionParameters!.DomFacade,
resultValuesInOrderOfEvaluation
);
break;
case ResultOrdering.Unsorted:
// The result should be sorted before we can continue
var concattedSequence = SortedSequenceUtils<TNode>.ConcatSortedSequences(
resultValuesInOrderOfEvaluation
);
return concattedSequence.MapAll(allValues =>
SequenceFactory.CreateFromArray(
SortResults(executionParameters!.DomFacade, allValues)
)
);
}
}
sequenceHasPeerProperty = sequenceHasPeerProperty && selector.Peer;
return sortedResultSequence!;
}
)!;
return result;
}
public override string? GetBucket()
{
return _stepExpressions.First().GetBucket();
}
}