New test.
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl.Operations / XslApplyTemplates.cs
index c74965d6a0d79151ec12f53d2fb8b4f96597ced6..8e4bae85425e84eeabc96cdb5483985418945bbd 100644 (file)
@@ -42,14 +42,15 @@ namespace Mono.Xml.Xsl.Operations {
                XPathExpression select;
                XmlQualifiedName mode;
                ArrayList withParams;
-               
+               XslSortEvaluator sortEvaluator;
+
                public XslApplyTemplates (Compiler c) : base (c) {}
                
                protected override void Compile (Compiler c)
                {
                        select = c.CompileExpression (c.GetAttribute ("select"));
                        mode = c.ParseQNameAttribute ("mode");
-                       
+                       ArrayList sorterList = null;
                        if (c.Input.MoveToFirstChild ()) {
                                do {
                                        switch (c.Input.NodeType) {
@@ -60,7 +61,7 @@ namespace Mono.Xml.Xsl.Operations {
                                                continue;
                                        case XPathNodeType.Element:
                                                if (c.Input.NamespaceURI != XsltNamespace)
-                                                       throw new XsltCompileException ("unexptected element", null, c.Input); // TODO: fwd compat
+                                                       throw new XsltCompileException ("Unexpected element", null, c.Input); // TODO: fwd compat
                                                
                                                switch (c.Input.LocalName)
                                                {
@@ -71,28 +72,38 @@ namespace Mono.Xml.Xsl.Operations {
                                                                break;
                                                                
                                                        case "sort":
+                                                               if (sorterList == null)
+                                                                       sorterList = new ArrayList ();
                                                                if (select == null)
                                                                        select = c.CompileExpression ("*");
-                                                               c.AddSort (select, new Sort (c));
+                                                               sorterList.Add (new Sort (c));
+                                                               //c.AddSort (select, new Sort (c));
                                                                break;
                                                        default:
-                                                               throw new XsltCompileException ("unexptected element", null, c.Input); // todo forwards compat
+                                                               throw new XsltCompileException ("Unexpected element", null, c.Input); // todo forwards compat
                                                }
                                                break;
                                        default:
-                                               throw new XsltCompileException ("unexpected node type " + c.Input.NodeType, null, c.Input);     // todo forwards compat
+                                               throw new XsltCompileException ("Unexpected node type " + c.Input.NodeType, null, c.Input);     // todo forwards compat
                                        }
                                } while (c.Input.MoveToNext ());
                                c.Input.MoveToParent ();
                        }
+                       if (sorterList != null)
+                               sortEvaluator = new XslSortEvaluator (select,
+                                       (Sort []) sorterList.ToArray (typeof (Sort)));
                }
                
                public override void Evaluate (XslTransformProcessor p)
                {               
                        if (select == null)     
                                p.ApplyTemplates (p.CurrentNode.SelectChildren (XPathNodeType.All), mode, withParams);
-                       else
-                               p.ApplyTemplates (p.Select (select), mode, withParams);
+                       else {
+                               XPathNodeIterator iter = sortEvaluator != null ?
+                                       sortEvaluator.SortedSelect (p) :
+                                       p.Select (select);
+                               p.ApplyTemplates (iter, mode, withParams);
+                       }
                }
        }
 }