2 // Mono.Xml.XPath.LocationPathPattern
5 // Ben Maurer (bmaurer@users.sourceforge.net)
11 using System.Collections;
14 using System.Xml.Schema;
15 using System.Xml.XPath;
18 namespace Mono.Xml.XPath {
19 internal class LocationPathPattern : Pattern {
21 internal LocationPathPattern patternPrevious;
22 internal bool isAncestor;
23 internal NodeTest nodeTest;
26 public LocationPathPattern (NodeTest nodeTest)
28 this.nodeTest = nodeTest;
31 public LocationPathPattern (ExprFilter filter)
35 while (! (filter.expr is NodeTest))
36 filter = (ExprFilter)filter.expr;
38 this.nodeTest = (NodeTest)filter.expr;
41 internal void SetPreviousPattern (Pattern prev, bool isAncestor)
43 LocationPathPattern toSet = LastPathPattern;
44 toSet.patternPrevious = (LocationPathPattern)prev;
45 toSet.isAncestor = isAncestor;
48 public override double DefaultPriority {
50 if (patternPrevious == null && filter == null) {
51 NodeNameTest t = nodeTest as NodeNameTest;
53 if (t.Name.Name == "*")
64 public override bool Matches (XPathNavigator node, XsltContext ctx)
66 if (! nodeTest.Match (ctx, node))
69 if (nodeTest is NodeTypeTest) {
70 // node () is different in xslt patterns
71 if (((NodeTypeTest)nodeTest).type == XPathNodeType.All &&
72 (node.NodeType == XPathNodeType.Root ||
73 node.NodeType == XPathNodeType.Attribute)
78 if (filter == null && patternPrevious == null)
81 if (patternPrevious != null) {
83 XPathNavigator parent = node.Clone ();
84 parent.MoveToParent ();
85 if (!patternPrevious.Matches (parent, ctx))
88 XPathNavigator anc = node.Clone ();
90 if (!anc.MoveToParent ())
93 if (patternPrevious.Matches (anc, ctx))
103 XPathNavigator p = node.Clone ();
105 BaseIterator matches = filter.EvaluateNodeSet (new NullIterator (p, ctx));
107 while (matches.MoveNext ()) {
108 if (node.IsSamePosition (matches.Current))
115 public override string ToString ()
118 if (patternPrevious != null) ret = patternPrevious.ToString () + (isAncestor ? "//" : "/");
119 if (filter != null) ret += filter.ToString ();
120 else ret += nodeTest.ToString ();
125 public LocationPathPattern LastPathPattern {
127 LocationPathPattern ret = this;
129 while (ret.patternPrevious != null)
130 ret = ret.patternPrevious;