| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181 |
- //
- // System.Xml.XPath.BaseIterator
- //
- // Author:
- // Piers Haken ([email protected])
- // Atsushi Enomoto ([email protected])
- //
- // (C) 2002 Piers Haken
- // (C) 2003 Atsushi Enomoto
- //
- using System;
- using System.Collections;
- using System.Xml;
- using System.Xml.XPath;
- using System.Xml.Xsl;
- namespace System.Xml.XPath
- {
- internal abstract class BaseIterator : XPathNodeIterator
- {
- private XmlNamespaceManager _nsm;
- internal BaseIterator (BaseIterator other)
- {
- _nsm = other._nsm;
- }
- internal BaseIterator (XmlNamespaceManager nsm)
- {
- _nsm = nsm;
- }
- public XmlNamespaceManager NamespaceManager
- {
- get { return _nsm; }
- set { _nsm = value; }
- }
-
- public virtual bool ReverseAxis {
- get { return false; }
- }
- public abstract bool RequireSorting { get; }
- public virtual int ComparablePosition {
- get {
- if (ReverseAxis) {
- int diff = Count - CurrentPosition + 1;
- return diff < 1 ? 1 : diff;
- }
- else
- return CurrentPosition;
- }
- }
- public override string ToString ()
- {
- if (Current != null)
- return Current.NodeType.ToString () + "[" + CurrentPosition + "] : " + Current.Name + " = " + Current.Value;
- else
- return this.GetType().ToString () + "[" + CurrentPosition + "]";
- }
- }
- internal class MergedIterator : BaseIterator
- {
- protected ArrayList _iters = new ArrayList ();
- protected int _pos;
- protected int _index;
- public MergedIterator (BaseIterator iter ) : base (iter) {}
- protected MergedIterator (MergedIterator other) : base (other)
- {
- foreach (XPathNodeIterator iter in other._iters)
- _iters.Add (iter.Clone ());
- _pos = other._pos;
- _index = other._index;
- }
- public override XPathNodeIterator Clone () { return new MergedIterator (this); }
- public void Add (BaseIterator iter)
- {
- _iters.Add (iter);
- }
- public override bool MoveNext ()
- {
- while (_index < _iters.Count)
- {
- BaseIterator iter = (BaseIterator) _iters [_index];
- if (iter.MoveNext ())
- {
- _pos ++;
- return true;
- }
- _index ++;
- }
- return false;
- }
- public override XPathNavigator Current
- {
- get
- {
- if (_index >= _iters.Count)
- return null;
- BaseIterator iter = (BaseIterator) _iters [_index];
- return iter.Current;
- }
- }
- public override int CurrentPosition { get { return _pos; }}
- public override bool RequireSorting { get { return true; } }
- }
- internal abstract class SimpleIterator : BaseIterator
- {
- protected readonly BaseIterator _iter;
- protected readonly XPathNavigator _nav;
- protected XPathNavigator _current;
- protected int _pos;
- public SimpleIterator (BaseIterator iter) : base (iter)
- {
- _iter = iter;
- _nav = iter.Current.Clone ();
- _current = _nav.Clone ();
- }
- protected SimpleIterator (SimpleIterator other) : base (other)
- {
- if (other._nav == null)
- _iter = (BaseIterator) other._iter.Clone ();
- else
- _nav = other._nav.Clone ();
- _pos = other._pos;
- _current = other._current.Clone ();
- }
- public SimpleIterator (XPathNavigator nav, XmlNamespaceManager nsm) : base (nsm)
- {
- _nav = nav.Clone ();
- _current = nav.Clone ();
- }
- public override XPathNavigator Current { get { return _current; }}
- public override int CurrentPosition { get { return _pos; }}
- }
- internal class SelfIterator : SimpleIterator
- {
- public SelfIterator (BaseIterator iter) : base (iter) {}
- public SelfIterator (XPathNavigator nav, XmlNamespaceManager nsm) : base (nav, nsm) {}
- protected SelfIterator (SelfIterator other) : base (other) {}
- public override XPathNodeIterator Clone () { return new SelfIterator (this); }
- public override bool MoveNext ()
- {
- if (_pos == 0)
- {
- _pos = 1;
- _current = _nav.Clone ();
- return true;
- }
- return false;
- }
- public override bool RequireSorting { get { return false; } }
- }
- internal class NullIterator : SelfIterator
- {
- public NullIterator (BaseIterator iter) : base (iter) {}
- public NullIterator (XPathNavigator nav) : this (nav, null) {}
- public NullIterator (XPathNavigator nav, XmlNamespaceManager nsm) : base (nav, nsm) {}
- protected NullIterator (NullIterator other) : base (other) {}
- public override XPathNodeIterator Clone () { return new NullIterator (this); }
- public override bool MoveNext ()
- {
- return false;
- }
- }
- internal class ParensIterator : BaseIterator
- {
- BaseIterator _iter;
- public ParensIterator (BaseIterator iter) : base (iter)
- {
- _iter = iter;
- }
- protected ParensIterator (ParensIterator other) : base (other)
- {
- _iter = (BaseIterator) other._iter.Clone ();
- }
- public override XPathNodeIterator Clone () { return new ParensIterator (this); }
- public override bool MoveNext ()
- {
- return _iter.MoveNext ();
- }
- public override XPathNavigator Current { get { return _iter.Current; }}
- public override int CurrentPosition { get { return _iter.CurrentPosition; } }
- public override bool RequireSorting { get { return _iter.RequireSorting; } }
- public override int Count { get { return _iter.Count; } }
- }
- internal class ParentIterator : SimpleIterator
- {
- public ParentIterator (BaseIterator iter) : base (iter) {}
- protected ParentIterator (ParentIterator other) : base (other) {}
- public ParentIterator (XPathNavigator nav, XmlNamespaceManager nsm) : base (nav, nsm) {}
- public override XPathNodeIterator Clone () { return new ParentIterator (this); }
- public override bool MoveNext ()
- {
- if (_pos == 0 && _nav.MoveToParent ())
- {
- _pos = 1;
- _current = _nav.Clone ();
- return true;
- }
- return false;
- }
- public override bool RequireSorting { get { return true; } }
- }
- internal class ChildIterator : SimpleIterator
- {
- public ChildIterator (BaseIterator iter) : base (iter) {}
- protected ChildIterator (ChildIterator other) : base (other) {}
- public override XPathNodeIterator Clone () { return new ChildIterator (this); }
- public override bool MoveNext ()
- {
- bool fSuccess = (_pos == 0) ? _nav.MoveToFirstChild () : _nav.MoveToNext ();
- if (fSuccess) {
- _pos ++;
- _current = _nav.Clone ();
- }
- return fSuccess;
- }
- public override bool RequireSorting { get { return false; } }
- }
- internal class FollowingSiblingIterator : SimpleIterator
- {
- public FollowingSiblingIterator (BaseIterator iter) : base (iter) {}
- protected FollowingSiblingIterator (FollowingSiblingIterator other) : base (other) {}
- public override XPathNodeIterator Clone () { return new FollowingSiblingIterator (this); }
- public override bool MoveNext ()
- {
- switch (_nav.NodeType) {
- case XPathNodeType.Attribute:
- case XPathNodeType.Namespace:
- // They have no siblings.
- return false;
- }
- if (_nav.MoveToNext ())
- {
- _pos ++;
- _current = _nav.Clone ();
- return true;
- }
- return false;
- }
- public override bool RequireSorting { get { return false; } }
- }
- internal class PrecedingSiblingIterator : SimpleIterator
- {
- bool finished;
- bool started;
- XPathNavigator startPosition;
- public PrecedingSiblingIterator (BaseIterator iter) : base (iter)
- {
- startPosition = iter.Current.Clone ();
- _current = startPosition.Clone ();
- }
- protected PrecedingSiblingIterator (PrecedingSiblingIterator other) : base (other)
- {
- startPosition = other.startPosition;
- started = other.started;
- finished = other.finished;
- _current = other._current.Clone ();
- }
- public override XPathNodeIterator Clone () { return new PrecedingSiblingIterator (this); }
- public override bool MoveNext ()
- {
- if (finished)
- return false;
- if (!started) {
- started = true;
- switch (_nav.NodeType) {
- case XPathNodeType.Attribute:
- case XPathNodeType.Namespace:
- // They have no siblings.
- finished = true;
- return false;
- }
- _nav.MoveToFirst ();
- if (_nav.ComparePosition (startPosition) == XmlNodeOrder.Same) {
- _pos++;
- _current = _nav.Clone ();
- return true;
- }
- } else {
- if (!_nav.MoveToNext ()) {
- finished = true;
- return false;
- }
- }
- if (_nav.ComparePosition (startPosition) != XmlNodeOrder.Before) {
- // Note that if _nav contains only 1 node, it won't be Same.
- finished = true;
- return false;
- } else {
- _pos ++;
- _current = _nav.Clone ();
- return true;
- }
- }
- public override bool ReverseAxis {
- get { return true; }
- }
- public override bool RequireSorting { get { return true; } }
- }
- internal class AncestorIterator : SimpleIterator
- {
- bool finished;
- bool started;
- ArrayList positions = new ArrayList ();
- XPathNavigator startPosition;
- int nextDepth;
- public AncestorIterator (BaseIterator iter) : base (iter)
- {
- startPosition = iter.Current.Clone ();
- _current = startPosition.Clone ();
- }
- protected AncestorIterator (AncestorIterator other) : base (other)
- {
- startPosition = other.startPosition;
- started = other.started;
- finished = other.finished;
- positions = (ArrayList) other.positions.Clone ();
- nextDepth = other.nextDepth;
- _current = other._current.Clone ();
- }
- public override XPathNodeIterator Clone () { return new AncestorIterator (this); }
- public override bool MoveNext ()
- {
- if (finished)
- return false;
- if (!started) {
- started = true;
- XPathNavigator ancestors = startPosition.Clone ();
- ancestors.MoveToParent ();
- _nav.MoveToParent ();
- while (ancestors.NodeType != XPathNodeType.Root) {
- int i = 0;
- _nav.MoveToFirst ();
- while (_nav.ComparePosition (ancestors) == XmlNodeOrder.Before) {
- _nav.MoveToNext ();
- i++;
- }
- positions.Add (i);
- ancestors.MoveToParent ();
- _nav.MoveToParent ();
- }
- positions.Reverse ();
- if (startPosition.NodeType != XPathNodeType.Root) {
- // First time it returns Root
- _pos++;
- _current = _nav.Clone ();
- return true;
- }
- }
- // Don't worry about node type of start position, like AncestorOrSelf.
- // It should be Element or Root.
- if (nextDepth < positions.Count) {
- int thisTimePos = (int) positions [nextDepth];
- _nav.MoveToFirstChild ();
- for (int i = 0; i < thisTimePos; i++)
- _nav.MoveToNext ();
- nextDepth++;
- _pos++;
- _current = _nav.Clone ();
- return true;
- }
- finished = true;
- return false;
- }
- public override bool ReverseAxis {
- get { return true; }
- }
- public override bool RequireSorting { get { return true; } }
- }
- internal class AncestorOrSelfIterator : SimpleIterator
- {
- bool finished;
- bool started;
- ArrayList positions = new ArrayList ();
- XPathNavigator startPosition;
- int nextDepth;
- public AncestorOrSelfIterator (BaseIterator iter) : base (iter)
- {
- startPosition = iter.Current.Clone ();
- _current = startPosition.Clone ();
- }
- protected AncestorOrSelfIterator (AncestorOrSelfIterator other) : base (other)
- {
- startPosition = other.startPosition;
- started = other.started;
- finished = other.finished;
- positions = (ArrayList) other.positions.Clone ();
- nextDepth = other.nextDepth;
- _current = other._current.Clone ();
- }
- public override XPathNodeIterator Clone () { return new AncestorOrSelfIterator (this); }
- public override bool MoveNext ()
- {
- bool initialIteration = false;
- if (finished)
- return false;
- if (!started) {
- initialIteration = true;
- started = true;
- XPathNavigator ancestors = startPosition.Clone ();
- do {
- int i = 0;
- _nav.MoveToFirst ();
- while (_nav.ComparePosition (ancestors) == XmlNodeOrder.Before) {
- _nav.MoveToNext ();
- i++;
- }
- positions.Add (i);
- ancestors.MoveToParent ();
- _nav.MoveToParent ();
- } while (ancestors.NodeType != XPathNodeType.Root);
- positions.Reverse ();
- }
- if (initialIteration && startPosition.NodeType != XPathNodeType.Root) {
- _current = _nav.Clone ();
- return true;
- } else if (nextDepth + 1 == positions.Count) {
- nextDepth++;
- _pos++;
- _nav.MoveTo (startPosition);
- _current = _nav.Clone ();
- return true;
- }
- else if (nextDepth < positions.Count) {
- int thisTimePos = (int) positions [nextDepth];
- _nav.MoveToFirstChild ();
- for (int i = 0; i < thisTimePos; i++)
- _nav.MoveToNext ();
- nextDepth++;
- _pos++;
- _current = _nav.Clone ();
- return true;
- }
- finished = true;
- return false;
- }
- public override bool ReverseAxis {
- get { return true; }
- }
- public override bool RequireSorting { get { return true; } }
- }
- internal class DescendantIterator : SimpleIterator
- {
- protected int _depth;
- private bool _finished;
- public DescendantIterator (BaseIterator iter) : base (iter) {}
- protected DescendantIterator (DescendantIterator other) : base (other)
- {
- _depth = other._depth;
- _current = other._current.Clone ();
- }
- public override XPathNodeIterator Clone () { return new DescendantIterator (this); }
- public override bool MoveNext ()
- {
- if (_finished)
- return false;
- if (_nav.MoveToFirstChild ())
- {
- _depth ++;
- _pos ++;
- _current = _nav.Clone ();
- return true;
- }
- while (_depth != 0)
- {
- if (_nav.MoveToNext ())
- {
- _pos ++;
- _current = _nav.Clone ();
- return true;
- }
- if (!_nav.MoveToParent ()) // should NEVER fail!
- throw new XPathException ("There seems some bugs on the XPathNavigator implementation class.");
- _depth --;
- }
- _finished = true;
- return false;
- }
- public override bool RequireSorting { get { return false; } }
- }
- internal class DescendantOrSelfIterator : SimpleIterator
- {
- protected int _depth;
- private bool _finished;
- public DescendantOrSelfIterator (BaseIterator iter) : base (iter) {}
- protected DescendantOrSelfIterator (DescendantOrSelfIterator other) : base (other)
- {
- _depth = other._depth;
- _current = other._current.Clone ();
- }
- public override XPathNodeIterator Clone () { return new DescendantOrSelfIterator (this); }
- public override bool MoveNext ()
- {
- if (_finished)
- return false;
- if (_pos == 0)
- {
- // self
- _pos ++;
- _current = _nav.Clone ();
- return true;
- }
- if (_nav.MoveToFirstChild ())
- {
- _depth ++;
- _pos ++;
- _current = _nav.Clone ();
- return true;
- }
- while (_depth != 0)
- {
- if (_nav.MoveToNext ())
- {
- _pos ++;
- _current = _nav.Clone ();
- return true;
- }
- if (!_nav.MoveToParent ()) // should NEVER fail!
- throw new XPathException ("There seems some bugs on the XPathNavigator implementation class.");
- _depth --;
- }
- _finished = true;
- return false;
- }
- public override bool RequireSorting { get { return false; } }
- }
- internal class FollowingIterator : SimpleIterator
- {
- private bool _finished = false;
- public FollowingIterator (BaseIterator iter) : base (iter) {}
- protected FollowingIterator (FollowingIterator other) : base (other) {}
- public override XPathNodeIterator Clone () { return new FollowingIterator (this); }
- public override bool MoveNext ()
- {
- if (_finished)
- return false;
- if (_pos == 0)
- {
- if (_nav.MoveToNext ())
- {
- _pos ++;
- _current = _nav.Clone ();
- return true;
- } else {
- while (_nav.MoveToParent ()) {
- if (_nav.MoveToNext ()) {
- _pos ++;
- _current = _nav.Clone ();
- return true;
- }
- }
- }
- }
- else
- {
- if (_nav.MoveToFirstChild ())
- {
- _pos ++;
- _current = _nav.Clone ();
- return true;
- }
- do
- {
- if (_nav.MoveToNext ())
- {
- _pos ++;
- _current = _nav.Clone ();
- return true;
- }
- }
- while (_nav.MoveToParent ());
- }
- _finished = true;
- return false;
- }
- public override bool RequireSorting { get { return false; } }
- }
- internal class PrecedingIterator : SimpleIterator
- {
- bool finished;
- bool started;
- XPathNavigator startPosition;
- public PrecedingIterator (BaseIterator iter) : base (iter)
- {
- startPosition = iter.Current.Clone ();
- _current = startPosition.Clone ();
- }
- protected PrecedingIterator (PrecedingIterator other) : base (other)
- {
- startPosition = other.startPosition;
- started = other.started;
- finished = other.finished;
- _current = other._current.Clone ();
- }
- public override XPathNodeIterator Clone () { return new PrecedingIterator (this); }
- public override bool MoveNext ()
- {
- if (finished)
- return false;
- if (!started) {
- started = true;
- _nav.MoveToRoot ();
- }
- bool loop = true;
- while (loop) {
- while (!_nav.MoveToFirstChild ()) {
- while (!_nav.MoveToNext ()) {
- if (!_nav.MoveToParent ()) { // Should not finish, at least before startPosition.
- finished = true;
- return false;
- }
- }
- break;
- }
- if (_nav.IsDescendant (startPosition))
- continue;
- loop = false;
- break;
- }
- if (_nav.ComparePosition (startPosition) != XmlNodeOrder.Before) {
- // Note that if _nav contains only 1 node, it won't be Same.
- finished = true;
- return false;
- } else {
- _pos ++;
- _current = _nav.Clone ();
- return true;
- }
- }
- public override bool ReverseAxis {
- get { return true; }
- }
- public override bool RequireSorting { get { return true; } }
- }
- internal class NamespaceIterator : SimpleIterator
- {
- public NamespaceIterator (BaseIterator iter) : base (iter) {}
- protected NamespaceIterator (NamespaceIterator other) : base (other) {}
- public override XPathNodeIterator Clone () { return new NamespaceIterator (this); }
- public override bool MoveNext ()
- {
- if (_pos == 0)
- {
- if (_nav.MoveToFirstNamespace ())
- {
- _pos ++;
- _current = _nav.Clone ();
- return true;
- }
- }
- else if (_nav.MoveToNextNamespace ())
- {
- _pos ++;
- _current = _nav.Clone ();
- return true;
- }
- return false;
- }
- public override bool ReverseAxis { get { return true; } }
- public override bool RequireSorting { get { return false; } }
- }
- internal class AttributeIterator : SimpleIterator
- {
- public AttributeIterator (BaseIterator iter) : base (iter) {}
- protected AttributeIterator (AttributeIterator other) : base (other) {}
- public override XPathNodeIterator Clone () { return new AttributeIterator (this); }
- public override bool MoveNext ()
- {
- if (_pos == 0)
- {
- if (_nav.MoveToFirstAttribute ())
- {
- _pos += 1;
- _current = _nav.Clone ();
- return true;
- }
- }
- else if (_nav.MoveToNextAttribute ())
- {
- _pos ++;
- _current = _nav.Clone ();
- return true;
- }
- return false;
- }
- public override bool RequireSorting { get { return false; } }
- }
- internal class AxisIterator : BaseIterator
- {
- protected SimpleIterator _iter;
- protected NodeTest _test;
- protected int _pos;
-
- string name, ns;
- XPathNodeType matchType;
- public AxisIterator (SimpleIterator iter, NodeTest test) : base (iter)
- {
- _iter = iter;
- _test = test;
- test.GetInfo (out name, out ns, out matchType, NamespaceManager);
- if (name != null)
- name = Current.NameTable.Add (name);
- if (ns != null)
- ns = Current.NameTable.Add (ns);
- }
- protected AxisIterator (AxisIterator other) : base (other)
- {
- _iter = (SimpleIterator) other._iter.Clone ();
- _test = other._test;
- _pos = other._pos;
- name = other.name;
- ns = other.ns;
- matchType = other.matchType;
- }
- public override XPathNodeIterator Clone () { return new AxisIterator (this); }
- public override bool MoveNext ()
- {
- while (_iter.MoveNext ())
- {
- if (_test.Match (NamespaceManager, Current))
- {
- _pos ++;
- return true;
- }
- }
- return false;
- }
- public override XPathNavigator Current { get { return _iter.Current; }}
- public override int CurrentPosition { get { return _pos; }}
-
- bool Match ()
- {
- if (Current.NodeType != matchType && matchType != XPathNodeType.All)
- return false;
-
- if (ns == null)
- return name == null || (object)name == (object)Current.LocalName;
- else
- return (object)ns == (object)Current.NamespaceURI &&
- (name == null || (object)name == (object)Current.LocalName);
- }
- public override bool ReverseAxis {
- get { return _iter.ReverseAxis; }
- }
- public override bool RequireSorting { get { return _iter.RequireSorting; } }
- }
- internal class SlashIterator : BaseIterator
- {
- protected BaseIterator _iterLeft;
- protected BaseIterator _iterRight;
- protected NodeSet _expr;
- protected int _pos;
- ArrayList _navStore;
- SortedList _iterList;
- bool _finished;
- BaseIterator _nextIterRight;
- public SlashIterator (BaseIterator iter, NodeSet expr) : base (iter)
- {
- _iterLeft = iter;
- _expr = expr;
- }
- protected SlashIterator (SlashIterator other) : base (other)
- {
- _iterLeft = (BaseIterator) other._iterLeft.Clone ();
- if (other._iterRight != null)
- _iterRight = (BaseIterator) other._iterRight.Clone ();
- _expr = other._expr;
- _pos = other._pos;
- if (other._iterList != null)
- _iterList = (SortedList) other._iterList.Clone ();
- if (other._navStore != null)
- _navStore = (ArrayList) other._navStore.Clone ();
- _finished = other._finished;
- if (other._nextIterRight != null)
- _nextIterRight = (BaseIterator) other._nextIterRight.Clone ();
- }
- public override XPathNodeIterator Clone () { return new SlashIterator (this); }
- public override bool MoveNext ()
- {
- if (_finished)
- return false;
- if (RequireSorting) {
- if (_pos <= 0) {
- CollectResults ();
- if (_navStore.Count == 0) {
- _finished = true;
- return false;
- }
- }
- _pos++;
- if (_navStore.Count < _pos) {
- _finished = true;
- _pos--;
- return false;
- }
- while (_navStore.Count > _pos) {
- if (((XPathNavigator) _navStore [_pos]).ComparePosition (
- (XPathNavigator) _navStore [_pos - 1]) == XmlNodeOrder.Same)
- _navStore.RemoveAt (_pos);
- else
- break;
- }
- return true;
- } else {
- if (_iterRight == null) { // First time
- if (!_iterLeft.MoveNext ())
- return false;
- _iterRight = _expr.EvaluateNodeSet (_iterLeft);
- _iterList = new SortedList (XPathIteratorComparer.Instance);
- }
- while (true) {
- while (!_iterRight.MoveNext ()) {
- if (_iterList.Count > 0) {
- int last = _iterList.Count - 1;
- BaseIterator tmpIter = (BaseIterator) _iterList.GetByIndex (last);
- _iterList.RemoveAt (last);
- switch (tmpIter.Current.ComparePosition (_iterRight.Current)) {
- case XmlNodeOrder.Same:
- case XmlNodeOrder.Before:
- _iterRight = tmpIter;
- continue;
- default:
- _iterRight = tmpIter;
- break;
- }
- break;
- } else if (_nextIterRight != null) {
- _iterRight = _nextIterRight;
- _nextIterRight = null;
- break;
- } else if (!_iterLeft.MoveNext ()) {
- _finished = true;
- return false;
- }
- else
- _iterRight = _expr.EvaluateNodeSet (_iterLeft);
- }
- bool loop = true;
- while (loop) {
- loop = false;
- if (_nextIterRight == null) {
- bool noMoreNext = false;
- while (_nextIterRight == null || !_nextIterRight.MoveNext ()) {
- if(_iterLeft.MoveNext ())
- _nextIterRight = _expr.EvaluateNodeSet (_iterLeft);
- else {
- noMoreNext = true;
- break;
- }
- }
- if (noMoreNext)
- _nextIterRight = null; // FIXME: More efficient code. Maybe making noMoreNext class scope would be better.
- }
- if (_nextIterRight != null) {
- switch (_iterRight.Current.ComparePosition (_nextIterRight.Current)) {
- case XmlNodeOrder.After:
- _iterList.Add (_iterList.Count, _iterRight);
- _iterRight = _nextIterRight;
- _nextIterRight = null;
- loop = true;
- break;
- case XmlNodeOrder.Same:
- if (!_nextIterRight.MoveNext ())
- _nextIterRight = null;
- else {
- int last = _iterList.Count;
- if (last > 0) {
- _iterList.Add (last, _nextIterRight);
- _nextIterRight = (BaseIterator) _iterList.GetByIndex (last);
- _iterList.RemoveAt (last);
- }
- }
- loop = true;
- break;
- }
- }
- }
- _pos ++;
- return true;
- }
- }
- }
- private void CollectResults ()
- {
- if (_navStore != null)
- return;
- _navStore = new ArrayList ();
- while (true) {
- while (_iterRight == null || !_iterRight.MoveNext ()) {
- if (!_iterLeft.MoveNext ()) {
- _navStore.Sort (XPathNavigatorComparer.Instance);
- return;
- }
- _iterRight = _expr.EvaluateNodeSet (_iterLeft);
- }
- XPathNavigator nav = _iterRight.Current.Clone ();
- _navStore.Add (nav);
- }
- }
- public override XPathNavigator Current {
- get {
- if (_pos <= 0) return null;
- if (RequireSorting) {
- return (XPathNavigator) _navStore [_pos - 1];
- } else {
- return _iterRight.Current;
- }
- }
- }
- public override int CurrentPosition { get { return _pos; }}
- public override bool RequireSorting {
- get {
- return _iterLeft.RequireSorting || _expr.RequireSorting;
- }
- }
- }
- internal class PredicateIterator : BaseIterator
- {
- protected BaseIterator _iter;
- protected Expression _pred;
- protected int _pos;
- protected XPathResultType resType;
- public PredicateIterator (BaseIterator iter, Expression pred) : base (iter)
- {
- _iter = iter;
- _pred = pred;
- resType = pred.GetReturnType (iter);
- }
- protected PredicateIterator (PredicateIterator other) : base (other)
- {
- _iter = (BaseIterator) other._iter.Clone ();
- _pred = other._pred;
- _pos = other._pos;
- resType = other.resType;
- }
- public override XPathNodeIterator Clone () { return new PredicateIterator (this); }
- public override bool MoveNext ()
- {
- while (_iter.MoveNext ())
- {
- switch (resType) {
- case XPathResultType.Number:
- if (_pred.EvaluateNumber (_iter) != _iter.ComparablePosition)
- continue;
- break;
- case XPathResultType.Any: {
- object result = _pred.Evaluate (_iter);
- if (result is double)
- {
- if ((double) result != _iter.ComparablePosition)
- continue;
- }
- else if (!XPathFunctions.ToBoolean (result))
- continue;
- }
- break;
- default:
- if (!_pred.EvaluateBoolean (_iter))
- continue;
- break;
- }
- _pos ++;
- return true;
- }
- return false;
- }
- public override XPathNavigator Current { get { return _iter.Current; }}
- public override int CurrentPosition { get { return _pos; }}
- public override bool ReverseAxis {
- get { return _iter.ReverseAxis; }
- }
- public override bool RequireSorting { get { return true; } }
- }
- // WARNING: Before using be sure about IEnumerator argument, because
- internal class EnumeratorIterator : BaseIterator
- {
- protected IEnumerator _enum;
- protected int _pos;
- public EnumeratorIterator (BaseIterator iter, IEnumerator enumerator) : base (iter)
- {
- if (!(enumerator is ICloneable))
- throw new ArgumentException ("Target enumerator must be cloneable.");
- _enum = enumerator;
- }
-
- public EnumeratorIterator (IEnumerator enumerator, XmlNamespaceManager nsm) : base (nsm)
- {
- if (!(enumerator is ICloneable))
- throw new ArgumentException ("Target enumerator must be cloneable.");
- _enum = enumerator;
- }
- protected EnumeratorIterator (EnumeratorIterator other) : base (other)
- {
- ICloneable enumClone = other._enum as ICloneable;
- _enum = (IEnumerator) enumClone.Clone ();
- _pos = other._pos;
- }
- public override XPathNodeIterator Clone () { return new EnumeratorIterator (this); }
- public override bool MoveNext ()
- {
- if (!_enum.MoveNext ())
- return false;
- _pos++;
- return true;
- }
- public override XPathNavigator Current { get { return (XPathNavigator) _enum.Current; }}
- public override int CurrentPosition { get { return _pos; }}
- public override bool RequireSorting { get { return true; } }
- }
- internal class UnionIterator : BaseIterator
- {
- protected BaseIterator _left, _right;
- private int _pos;
- private bool keepLeft;
- private bool keepRight;
- private bool useRight;
- public UnionIterator (BaseIterator iter, BaseIterator left, BaseIterator right) : base (iter)
- {
- _left = left;
- _right = right;
- }
- protected UnionIterator (UnionIterator other) : base (other)
- {
- _left = (BaseIterator) other._left.Clone ();
- _right = (BaseIterator) other._right.Clone ();
- _pos = other._pos;
- keepLeft = other.keepLeft;
- keepRight = other.keepRight;
- useRight = other.useRight;
- }
- public override XPathNodeIterator Clone () { return new UnionIterator (this); }
- public override bool MoveNext ()
- {
- if (!keepLeft)
- keepLeft = _left.MoveNext ();
- if (!keepRight)
- keepRight = _right.MoveNext ();
- if (!keepLeft && !keepRight)
- return false;
- _pos ++;
- if (!keepRight) {
- keepLeft = useRight = false;
- return true;
- } else if (!keepLeft) {
- keepRight = false;
- useRight = true;
- return true;
- }
- switch (_left.Current.ComparePosition (_right.Current)) {
- case XmlNodeOrder.Same:
- // consume both. i.e. don't output duplicate result.
- keepLeft = keepRight = false;
- useRight = true;
- return true;
- case XmlNodeOrder.Before:
- case XmlNodeOrder.Unknown: // Maybe happen because of "document(a) | document(b)"
- keepLeft = useRight = false;
- return true;
- case XmlNodeOrder.After:
- keepRight = false;
- useRight = true;
- return true;
- default:
- throw new InvalidOperationException ("Should not happen.");
- }
- }
- public override XPathNavigator Current
- {
- get
- {
- if (_pos == 0)
- return null;
- if (useRight)
- return _right.Current;
- else
- return _left.Current;
- }
- }
- public override int CurrentPosition { get { return _pos; }}
- public override bool RequireSorting { get { return _left.RequireSorting || _right.RequireSorting; } }
- }
- }
|