Kaynağa Gözat

initial pattern work

svn path=/trunk/mcs/; revision=17786
Ben Maurer 22 yıl önce
ebeveyn
işleme
a7578be9cf

+ 33 - 0
mcs/class/System.XML/Mono.Xml.XPath/IdPattern.cs

@@ -0,0 +1,33 @@
+//
+// Mono.Xml.XPath.IdPattern
+//
+// Author:
+//	Ben Maurer ([email protected])
+//
+// (C) 2003 Ben Maurer
+//
+
+using System;
+using System.Collections;
+using System.IO;
+using System.Xml;
+using System.Xml.Schema;
+using System.Xml.XPath;
+using System.Xml.Xsl;
+
+namespace Mono.Xml.XPath {
+	internal class IdPattern : Pattern {
+
+		string arg0;
+		
+		public IdPattern (string arg0)
+		{
+			this.arg0 = arg0;
+		}
+		
+		public override bool Matches (XPathNavigator node, XsltContext ctx)
+		{
+			throw new NotImplementedException ();
+		}
+	}
+}

+ 34 - 0
mcs/class/System.XML/Mono.Xml.XPath/KeyPattern.cs

@@ -0,0 +1,34 @@
+//
+// Mono.Xml.XPath.KeyPattern
+//
+// Author:
+//	Ben Maurer ([email protected])
+//
+// (C) 2003 Ben Maurer
+//
+
+using System;
+using System.Collections;
+using System.IO;
+using System.Xml;
+using System.Xml.Schema;
+using System.Xml.XPath;
+using System.Xml.Xsl;
+
+namespace Mono.Xml.XPath {
+	internal class KeyPattern : Pattern {
+
+		string arg0, arg1;
+		
+		public KeyPattern (string arg0, string arg1)
+		{
+			this.arg0 = arg0;
+			this.arg1 = arg1;
+		}
+		
+		public override bool Matches (XPathNavigator node, XsltContext ctx)
+		{
+			throw new NotImplementedException ();
+		}
+	}
+}

+ 95 - 0
mcs/class/System.XML/Mono.Xml.XPath/LocationPathPattern.cs

@@ -0,0 +1,95 @@
+//
+// Mono.Xml.XPath.LocationPathPattern
+//
+// Author:
+//	Ben Maurer ([email protected])
+//
+// (C) 2003 Ben Maurer
+//
+
+using System;
+using System.Collections;
+using System.IO;
+using System.Xml;
+using System.Xml.Schema;
+using System.Xml.XPath;
+using System.Xml.Xsl;
+
+namespace Mono.Xml.XPath {
+	internal class LocationPathPattern : Pattern {
+		
+		internal Pattern patternPrevious;
+		internal bool isAncestor;
+		internal NodeTest nodeTest;
+		ExprFilter filter;
+		
+		public LocationPathPattern (NodeTest nodeTest)
+		{
+			this.nodeTest = nodeTest;
+		}
+		
+		public LocationPathPattern (ExprFilter filter) : this ((NodeTest)filter.expr)
+		{
+			this.filter = filter;
+		}
+
+		internal void SetPreviousPattern (Pattern prev, bool isAncestor)
+		{
+			this.patternPrevious = prev;
+		}
+		
+		public override double DefaultPriority { 
+			get { 
+				if (patternPrevious == null && filter == null) {
+					NodeNameTest t = nodeTest as NodeNameTest;
+					if (t != null) {
+						if (t.Name.Name == "*")
+							return -.25;
+						return 0;
+					}
+
+					return -.5;
+				}
+				return .5;
+			}
+		}
+		
+		public override bool Matches (XPathNavigator node, XsltContext ctx)
+		{
+			if (! nodeTest.Match (ctx, node))
+				return false;
+			
+			if (filter == null && patternPrevious == null)
+				return true;
+			
+			if (isAncestor) {
+				XPathNavigator parent = node.Clone ();
+				if (!patternPrevious.Matches (parent, ctx))
+					return false;
+			} else {
+				XPathNavigator anc = node.Clone ();
+				while (true) {
+					if (!anc.MoveToParent ())
+						return false;
+					
+					if (patternPrevious.Matches (anc, ctx))
+						break;
+				}
+			}
+
+						
+			if (filter == null)
+				return true;
+			
+			BaseIterator parentItr = new ParentIterator (node, ctx);
+			BaseIterator matches = filter.EvaluateNodeSet (parentItr);
+			
+			while (matches.MoveNext ()) {
+				if (node.IsSamePosition (matches.Current))
+					return true;
+			}
+			
+			return false;
+		}
+	}
+}

+ 85 - 0
mcs/class/System.XML/Mono.Xml.XPath/Pattern.cs

@@ -0,0 +1,85 @@
+//
+// Mono.Xml.XPath.Pattern
+//
+// Author:
+//	Ben Maurer ([email protected])
+//
+// (C) 2003 Ben Maurer
+//
+
+using System;
+using System.Collections;
+using System.IO;
+using System.Xml;
+using System.Xml.Schema;
+using System.Xml.XPath;
+using System.Xml.Xsl;
+
+namespace Mono.Xml.XPath {
+	internal abstract class Pattern {
+		
+		public static Pattern Compile (string s)
+		{
+			Tokenizer tokenizer = new Tokenizer (s);
+			XPathParser parser = new XPathParser ();
+			Expression expr = (Expression) parser.yyparseSafe (tokenizer);
+			
+			return Compile (expr);
+		}
+		
+		public static Pattern Compile (Expression e)
+		{		
+			if (e is ExprUNION)
+				return new UnionPattern (
+					Compile (((ExprUNION)e).left),
+					Compile (((ExprUNION)e).right)
+				);
+			
+			if (e is ExprRoot)
+				return new LocationPathPattern (
+					new NodeTypeTest (Axes.Self, XPathNodeType.Root)
+				);
+			
+			if (e is NodeTest)
+				return new LocationPathPattern (
+					(NodeTest)e
+				);
+			
+			if (e is ExprFilter)
+				return new LocationPathPattern (
+					(ExprFilter)e
+				);
+			
+			if (e is ExprSLASH)
+			{
+				Pattern p0 = Compile (((ExprSLASH)e).right);
+				LocationPathPattern p1
+					= (LocationPathPattern)Compile (((ExprSLASH)e).right);
+				
+				p1.SetPreviousPattern (p0, false);
+				return p1;
+			}
+			
+			if (e is ExprSLASH2)
+			{
+				if (((ExprSLASH2)e).right is ExprRoot)
+					return Compile (((ExprSLASH2)e).right);
+				
+				Pattern p0 = Compile (((ExprSLASH2)e).right);
+				LocationPathPattern p1
+					= (LocationPathPattern)Compile (((ExprSLASH2)e).right);
+				
+				p1.SetPreviousPattern (p0, true);
+				return p1;
+			}
+			
+			// TODO: Handle ID/KEY
+			
+			throw new Exception ("Invalid Pattern");
+		}
+		
+		public virtual double DefaultPriority { get { return 0.5; }}
+		
+		public abstract bool Matches (XPathNavigator node, XsltContext ctx);
+	}
+}

+ 34 - 0
mcs/class/System.XML/Mono.Xml.XPath/UnionPattern.cs

@@ -0,0 +1,34 @@
+//
+// Mono.Xml.XPath.UnionPattern
+//
+// Author:
+//	Ben Maurer ([email protected])
+//
+// (C) 2003 Ben Maurer
+//
+
+using System;
+using System.Collections;
+using System.IO;
+using System.Xml;
+using System.Xml.Schema;
+using System.Xml.XPath;
+using System.Xml.Xsl;
+
+namespace Mono.Xml.XPath {
+	internal class UnionPattern : Pattern {
+		
+		Pattern p0, p1;
+		
+		public UnionPattern (Pattern p0, Pattern p1)
+		{
+			this.p0 = p0;
+			this.p1 = p1;
+		}
+		
+		public override bool Matches (XPathNavigator node, XsltContext ctx)
+		{
+			return p0.Matches (node, ctx) || p1.Matches (node, ctx);
+		}
+	}
+}

+ 44 - 21
mcs/class/System.XML/System.Xml.XPath/Expression.cs

@@ -801,35 +801,55 @@ namespace System.Xml.XPath
 	}
 	internal class ExprUNION : NodeSet
 	{
-		protected Expression _left, _right;
+		public readonly Expression left, right;
 		public ExprUNION (Expression left, Expression right)
 		{
-			_left = left;
-			_right = right;
+			this.left = left;
+			this.right = right;
 		}
-		public override String ToString () { return _left.ToString ()+ " | " + _right.ToString (); }
+		public override String ToString () { return left.ToString ()+ " | " + right.ToString (); }
 		public override object Evaluate (BaseIterator iter)
 		{
-			BaseIterator iterLeft = _left.EvaluateNodeSet (iter);
-			BaseIterator iterRight = _right.EvaluateNodeSet (iter);
+			BaseIterator iterLeft = left.EvaluateNodeSet (iter);
+			BaseIterator iterRight = right.EvaluateNodeSet (iter);
 			return new UnionIterator (iter, iterLeft, iterRight);
 		}
 	}
 
 	internal class ExprSLASH : NodeSet
 	{
-		protected Expression _left;
-		protected NodeSet _right;
+		public readonly Expression left;
+		public readonly NodeSet right;
 		public ExprSLASH (Expression left, NodeSet right)
 		{
-			_left = left;
-			_right = right;
+			this.left = left;
+			this.right = right;
+		}
+		public override String ToString () { return left.ToString ()+ "/" + right.ToString (); }
+		public override object Evaluate (BaseIterator iter)
+		{
+			BaseIterator iterLeft = left.EvaluateNodeSet (iter);
+			return new SlashIterator (iterLeft, right);
+		}
+	}
+	
+	internal class ExprSLASH2 : NodeSet {
+		public readonly Expression left;
+		public readonly NodeSet right;
+
+		public ExprSLASH2 (Expression left, NodeSet right)
+		{
+			this.left = left;
+			this.right = right;
 		}
-		public override String ToString () { return _left.ToString ()+ "/" + _right.ToString (); }
+		public override String ToString () { return left.ToString ()+ "//" + right.ToString (); }
 		public override object Evaluate (BaseIterator iter)
 		{
-			BaseIterator iterLeft = _left.EvaluateNodeSet (iter);
-			return new SlashIterator (iterLeft, _right);
+			BaseIterator iterLeft = new DescendantOrSelfIterator(
+				left.EvaluateNodeSet (iter)
+			);
+			
+			return new SlashIterator (iterLeft, right);
 		}
 	}
 
@@ -1048,6 +1068,8 @@ namespace System.Xml.XPath
 			_name = name;
 		}
 		public override String ToString () { return _axis.ToString () + "::" + _name.ToString (); }
+		
+		public XmlQualifiedName Name { get { return _name; } }
 		[MonoTODO]
 		public override bool Match (XmlNamespaceManager nsm, XPathNavigator nav)
 		{
@@ -1113,19 +1135,20 @@ namespace System.Xml.XPath
 
 	internal class ExprFilter : NodeSet
 	{
-		protected Expression _expr;
-		protected Expression _pred;
+		public readonly Expression expr, pred;
+		
 		public ExprFilter (Expression expr, Expression pred)
 		{
-			_expr = expr;
-			_pred = pred;
+			this.expr = expr;
+			this.pred = pred;
 		}
-		internal Expression LeftHandSide {get{return _expr;}}
-		public override String ToString () { return "(" + _expr.ToString () + ")[" + _pred.ToString () + "]"; }
+		
+		internal Expression LeftHandSide {get{return expr;}}
+		public override String ToString () { return "(" + expr.ToString () + ")[" + pred.ToString () + "]"; }
 		public override object Evaluate (BaseIterator iter)
 		{
-			BaseIterator iterExpr = _expr.EvaluateNodeSet (iter);
-			return new PredicateIterator (iterExpr, _pred);
+			BaseIterator iterExpr = expr.EvaluateNodeSet (iter);
+			return new PredicateIterator (iterExpr, pred);
 		}
 	}
 

+ 1 - 0
mcs/class/System.XML/System.Xml.XPath/Iterator.cs

@@ -148,6 +148,7 @@ namespace System.Xml.XPath
 	{
 		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 ()
 		{

+ 3 - 6
mcs/class/System.XML/System.Xml.XPath/Parser.jay

@@ -228,8 +228,7 @@ PathExpr
 	}
 	| FilterExpr SLASH2 RelativeLocationPath
 	{
-		NodeTest test = new NodeTypeTest (Axes.DescendantOrSelf, XPathNodeType.All);
-		$$ = new ExprSLASH (new ExprSLASH ((Expression) $1, test), (NodeSet) $3);
+		$$ = new ExprSLASH2 ((Expression) $1, (NodeSet) $3);
 	}
 	;
 
@@ -249,8 +248,7 @@ AbsoluteLocationPath
 	}
 	| SLASH2 RelativeLocationPath
 	{
-		NodeTest test = new NodeTypeTest (Axes.DescendantOrSelf, XPathNodeType.All);
-		$$ = new ExprSLASH (new ExprSLASH (new ExprRoot (), test), (NodeSet) $2);
+		$$ = new ExprSLASH2 (new ExprRoot (), (NodeSet) $3);
 	}
 	;
 
@@ -262,8 +260,7 @@ RelativeLocationPath
 	}
 	| RelativeLocationPath SLASH2 Step 
 	{
-		NodeTest test = new NodeTypeTest (Axes.DescendantOrSelf, XPathNodeType.All);
-		$$ = new ExprSLASH (new ExprSLASH ((NodeSet) $1, test), (NodeSet) $3);
+		$$ = new ExprSLASH2 ((NodeSet) $1, (NodeSet) $3);
 	}
 	;
 

+ 5 - 0
mcs/class/System.XML/System.Xml.dll.sources

@@ -5,6 +5,11 @@ Mono.Xml.Schema/XsdValidatingReader.cs
 Mono.Xml.XPath/DTMXPathDocument.cs
 Mono.Xml.XPath/DTMXPathDocumentBuilder.cs
 Mono.Xml.XPath/DTMXPathNavigator.cs
+Mono.Xml.XPath/IdPattern.cs
+Mono.Xml.XPath/KeyPattern.cs
+Mono.Xml.XPath/LocationPathPattern.cs
+Mono.Xml.XPath/Pattern.cs
+Mono.Xml.XPath/UnionPattern.cs
 Mono.Xml.Xsl.Operations/XslApplyImports.cs
 Mono.Xml.Xsl.Operations/XslApplyTemplates.cs
 Mono.Xml.Xsl.Operations/XslAttribute.cs