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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
|
//------------------------------------------------------------------------------
// <copyright file="ContentIterators.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------
using System;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Schema;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
namespace System.Xml.Xsl.Runtime {
/// <summary>
/// Iterate over all child content nodes (this is different from the QIL Content operator, which iterates over content + attributes).
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public struct ContentIterator {
private XPathNavigator navCurrent;
private bool needFirst;
/// <summary>
/// Initialize the ContentIterator.
/// </summary>
public void Create(XPathNavigator context) {
this.navCurrent = XmlQueryRuntime.SyncToNavigator(this.navCurrent, context);
this.needFirst = true;
}
/// <summary>
/// Position the iterator on the next child content node. Return true if such a child exists and
/// set Current property. Otherwise, return false (Current property is undefined).
/// </summary>
public bool MoveNext() {
if (this.needFirst) {
this.needFirst = !this.navCurrent.MoveToFirstChild();
return !this.needFirst;
}
return this.navCurrent.MoveToNext();
}
/// <summary>
/// Return the current result navigator. This is only defined after MoveNext() has returned true.
/// </summary>
public XPathNavigator Current {
get { return this.navCurrent; }
}
}
/// <summary>
/// Iterate over all child elements with a matching name.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public struct ElementContentIterator {
private string localName, ns;
private XPathNavigator navCurrent;
private bool needFirst;
/// <summary>
/// Initialize the ElementContentIterator.
/// </summary>
public void Create(XPathNavigator context, string localName, string ns) {
this.navCurrent = XmlQueryRuntime.SyncToNavigator(this.navCurrent, context);
this.localName = localName;
this.ns = ns;
this.needFirst = true;
}
/// <summary>
/// Position the iterator on the next child element with a matching name. Return true if such a child exists and
/// set Current property. Otherwise, return false (Current property is undefined).
/// </summary>
public bool MoveNext() {
if (this.needFirst) {
this.needFirst = !this.navCurrent.MoveToChild(this.localName, this.ns);
return !this.needFirst;
}
return this.navCurrent.MoveToNext(this.localName, this.ns);
}
/// <summary>
/// Return the current result navigator. This is only defined after MoveNext() has returned true.
/// </summary>
public XPathNavigator Current {
get { return this.navCurrent; }
}
}
/// <summary>
/// Iterate over all child content nodes with a matching node kind.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public struct NodeKindContentIterator {
private XPathNodeType nodeType;
private XPathNavigator navCurrent;
private bool needFirst;
/// <summary>
/// Initialize the NodeKindContentIterator.
/// </summary>
public void Create(XPathNavigator context, XPathNodeType nodeType) {
Debug.Assert(nodeType != XPathNodeType.Attribute && nodeType != XPathNodeType.Namespace);
this.navCurrent = XmlQueryRuntime.SyncToNavigator(this.navCurrent, context);
this.nodeType = nodeType;
this.needFirst = true;
}
/// <summary>
/// Position the iterator on the next child content node with a matching node kind. Return true if such a child
/// exists and set Current property. Otherwise, return false (Current property is undefined).
/// </summary>
public bool MoveNext() {
if (this.needFirst) {
this.needFirst = !this.navCurrent.MoveToChild(this.nodeType);
return !this.needFirst;
}
return this.navCurrent.MoveToNext(this.nodeType);
}
/// <summary>
/// Return the current result navigator. This is only defined after MoveNext() has returned true.
/// </summary>
public XPathNavigator Current {
get { return this.navCurrent; }
}
}
/// <summary>
/// Iterate over all attributes.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public struct AttributeIterator {
private XPathNavigator navCurrent;
private bool needFirst;
/// <summary>
/// Initialize the AttributeIterator.
/// </summary>
public void Create(XPathNavigator context) {
this.navCurrent = XmlQueryRuntime.SyncToNavigator(this.navCurrent, context);
this.needFirst = true;
}
/// <summary>
/// Position the iterator on the attribute. Return true if such a child exists and set Current
/// property. Otherwise, return false (Current property is undefined).
/// </summary>
public bool MoveNext() {
if (this.needFirst) {
this.needFirst = !this.navCurrent.MoveToFirstAttribute();
return !this.needFirst;
}
return this.navCurrent.MoveToNextAttribute();
}
/// <summary>
/// Return the current result navigator. This is only defined after MoveNext() has returned true.
/// </summary>
public XPathNavigator Current {
get { return this.navCurrent; }
}
}
/// <summary>
/// Iterate over all namespace nodes.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public struct NamespaceIterator {
private XPathNavigator navCurrent;
private XmlNavigatorStack navStack;
/// <summary>
/// Initialize the NamespaceIterator.
/// </summary>
public void Create(XPathNavigator context) {
// Push all of context's in-scope namespaces onto a stack in order to return them in document order
// (MoveToXXXNamespace methods return namespaces in reverse document order)
this.navStack.Reset();
if (context.MoveToFirstNamespace(XPathNamespaceScope.All)) {
do {
// Don't return the default namespace undeclaration
if (context.LocalName.Length != 0 || context.Value.Length != 0)
this.navStack.Push(context.Clone());
}
while (context.MoveToNextNamespace(XPathNamespaceScope.All));
context.MoveToParent();
}
}
/// <summary>
/// Pop the top namespace from the stack and save it as navCurrent. If there are no more namespaces, return false.
/// </summary>
public bool MoveNext() {
if (this.navStack.IsEmpty)
return false;
this.navCurrent = this.navStack.Pop();
return true;
}
/// <summary>
/// Return the current result navigator. This is only defined after MoveNext() has returned true.
/// </summary>
public XPathNavigator Current {
get { return this.navCurrent; }
}
}
/// <summary>
/// Iterate over all attribute and child content nodes.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public struct AttributeContentIterator {
private XPathNavigator navCurrent;
private bool needFirst;
/// <summary>
/// Initialize the AttributeContentIterator.
/// </summary>
public void Create(XPathNavigator context) {
this.navCurrent = XmlQueryRuntime.SyncToNavigator(this.navCurrent, context);
this.needFirst = true;
}
/// <summary>
/// Position the iterator on the next child content node with a matching node kind. Return true if such a child
/// exists and set Current property. Otherwise, return false (Current property is undefined).
/// </summary>
public bool MoveNext() {
if (this.needFirst) {
this.needFirst = !XmlNavNeverFilter.MoveToFirstAttributeContent(this.navCurrent);
return !this.needFirst;
}
return XmlNavNeverFilter.MoveToNextAttributeContent(this.navCurrent);
}
/// <summary>
/// Return the current result navigator. This is only defined after MoveNext() has returned true.
/// </summary>
public XPathNavigator Current {
get { return this.navCurrent; }
}
}
/// <summary>
/// Iterate over child content nodes or following-sibling nodes. Maintain document order by using a stack. Input
/// nodes are assumed to be in document order, but can contain one another (ContentIterator doesn't allow this).
/// </summary>
/// <remarks>
/// 1. Assume that the list I of input nodes is in document order, with no duplicates. There are N nodes in list I.
/// 2. For each node in list I, derive a list of nodes consisting of matching children or following-sibling nodes.
/// Call these lists S(1)...S(N).
/// 3. Let F be the first node in any list S(X), where X >= 1 and X < N
/// 4. There exists exactly one contiguous sequence of lists S(Y)...S(Z), where Y > X and Z <= N, such that the lists
/// S(X+1)...S(N) can be partitioned into these three groups:
/// a. 1st group (S(X+1)...S(Y-1)) -- All nodes in these lists precede F in document order
/// b. 2nd group (S(Y)...S(Z)) -- All nodes in these lists are duplicates of nodes in list S(X)
/// c. 3rd group (> S(Z)) -- All nodes in these lists succeed F in document order
/// 5. Given #4, node F can be returned once all nodes in the 1st group have been returned. Lists S(Y)...S(Z) can be
/// discarded. And only a single node in the 3rd group need be generated in order to guarantee that all nodes in
/// the 1st and 2nd groups have already been generated.
/// </remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
public struct ContentMergeIterator {
private XmlNavigatorFilter filter;
private XPathNavigator navCurrent, navNext;
private XmlNavigatorStack navStack;
private IteratorState state;
private enum IteratorState {
NeedCurrent = 0,
HaveCurrentNeedNext,
HaveCurrentNoNext,
HaveCurrentHaveNext,
};
/// <summary>
/// Initialize the ContentMergeIterator (merge multiple sets of content nodes in document order and remove duplicates).
/// </summary>
public void Create(XmlNavigatorFilter filter) {
this.filter = filter;
this.navStack.Reset();
this.state = IteratorState.NeedCurrent;
}
/// <summary>
/// Position this iterator to the next content or sibling node. Return IteratorResult.NoMoreNodes if there are
/// no more content or sibling nodes. Return IteratorResult.NeedInputNode if the next input node needs to be
/// fetched first. Return IteratorResult.HaveCurrent if the Current property is set to the next node in the
/// iteration.
/// </summary>
public IteratorResult MoveNext(XPathNavigator input) {
return MoveNext(input, true);
}
/// <summary>
/// Position this iterator to the next content or sibling node. Return IteratorResult.NoMoreNodes if there are
/// no more content or sibling nodes. Return IteratorResult.NeedInputNode if the next input node needs to be
/// fetched first. Return IteratorResult.HaveCurrent if the Current property is set to the next node in the
/// iteration.
/// </summary>
internal IteratorResult MoveNext(XPathNavigator input, bool isContent) {
switch (this.state) {
case IteratorState.NeedCurrent:
// If there are no more input nodes, then iteration is complete
if (input == null)
return IteratorResult.NoMoreNodes;
// Save the input node as the current node
this.navCurrent = XmlQueryRuntime.SyncToNavigator(this.navCurrent, input);
// If matching child or sibling is found, then we have a current node
if (isContent ? this.filter.MoveToContent(this.navCurrent) :
this.filter.MoveToFollowingSibling(this.navCurrent))
this.state = IteratorState.HaveCurrentNeedNext;
return IteratorResult.NeedInputNode;
case IteratorState.HaveCurrentNeedNext:
if (input == null) {
// There are no more input nodes, so enter HaveCurrentNoNext state and return Current
this.state = IteratorState.HaveCurrentNoNext;
return IteratorResult.HaveCurrentNode;
}
// Save the input node as the next node
this.navNext = XmlQueryRuntime.SyncToNavigator(this.navNext, input);
// If matching child or sibling is found,
if (isContent ? this.filter.MoveToContent(this.navNext) :
this.filter.MoveToFollowingSibling(this.navNext)) {
// Then compare position of current and next nodes
this.state = IteratorState.HaveCurrentHaveNext;
return DocOrderMerge();
}
// Input node does not result in matching child or sibling, so get next input node
return IteratorResult.NeedInputNode;
case IteratorState.HaveCurrentNoNext:
case IteratorState.HaveCurrentHaveNext:
// If the current node has no more matching siblings,
if (isContent ? !this.filter.MoveToNextContent(this.navCurrent) :
!this.filter.MoveToFollowingSibling(this.navCurrent)) {
if (this.navStack.IsEmpty) {
if (this.state == IteratorState.HaveCurrentNoNext) {
// No more input nodes, so iteration is complete
return IteratorResult.NoMoreNodes;
}
// Make navNext the new current node and fetch a new navNext
this.navCurrent = XmlQueryRuntime.SyncToNavigator(this.navCurrent, this.navNext);
this.state = IteratorState.HaveCurrentNeedNext;
return IteratorResult.NeedInputNode;
}
// Pop new current node from the stack
this.navCurrent = this.navStack.Pop();
}
// If there is no next node, then no need to call DocOrderMerge; just return the current node
if (this.state == IteratorState.HaveCurrentNoNext)
return IteratorResult.HaveCurrentNode;
// Compare positions of current and next nodes
return DocOrderMerge();
}
Debug.Assert(false, "Invalid IteratorState " + this.state);
return IteratorResult.NoMoreNodes;
}
/// <summary>
/// Return the current result navigator. This is only defined after MoveNext() has returned IteratorResult.HaveCurrentNode.
/// </summary>
public XPathNavigator Current {
get { return this.navCurrent; }
}
/// <summary>
/// If the context node-set returns a node that is contained in the subtree of the previous node,
/// then returning children of each node in "natural" order may not correspond to document order.
/// Therefore, in order to guarantee document order, keep a stack in order to push the sibling of
/// ancestor nodes. These siblings will not be returned until all of the descendants' children are
/// returned first.
/// </summary>
private IteratorResult DocOrderMerge() {
XmlNodeOrder cmp;
Debug.Assert(this.state == IteratorState.HaveCurrentHaveNext);
// Compare location of navCurrent with navNext
cmp = this.navCurrent.ComparePosition(this.navNext);
// If navCurrent is before navNext in document order,
// If cmp = XmlNodeOrder.Unknown, then navCurrent is before navNext (since input is is doc order)
if (cmp == XmlNodeOrder.Before || cmp == XmlNodeOrder.Unknown) {
// Then navCurrent can be returned (it is guaranteed to be first in document order)
return IteratorResult.HaveCurrentNode;
}
// If navCurrent is after navNext in document order, then delay returning navCurrent
// Otherwise, discard navNext since it is positioned to the same node as navCurrent
if (cmp == XmlNodeOrder.After) {
this.navStack.Push(this.navCurrent);
this.navCurrent = this.navNext;
this.navNext = null;
}
// Need next input node
this.state = IteratorState.HaveCurrentNeedNext;
return IteratorResult.NeedInputNode;
}
}
}
|