2004-03-01 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl.Operations / XslForEach.cs
1 //
2 // XslForEach.cs
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //      Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
7 //      
8 // (C) 2003 Ben Maurer
9 // (C) 2003 Atsushi Enomoto
10 //
11
12 using System;
13 using System.Collections;
14 using System.Xml;
15 using System.Xml.XPath;
16 using System.Xml.Xsl;
17
18 namespace Mono.Xml.Xsl.Operations {
19         public class XslForEach : XslCompiledElement {
20                 XPathExpression select;
21                 XslOperation children;
22                 
23                 public XslForEach (Compiler c) : base (c) {}
24                 
25                 protected override void Compile (Compiler c)
26                 {
27                         c.AssertAttribute ("select");
28                         select = c.CompileExpression (c.GetAttribute ("select"));
29                         
30                         if (c.Input.MoveToFirstChild ()) {
31                                 bool alldone = true;
32                                 do {
33                                         if (c.Input.NodeType == XPathNodeType.Text)
34                                                 { alldone = false; break; }
35                                         
36                                         if (c.Input.NodeType != XPathNodeType.Element)
37                                                 continue;
38                                         if (c.Input.NamespaceURI != Compiler.XsltNamespace)
39                                                 { alldone = false; break; }
40                                         if (c.Input.LocalName != "sort")
41                                                 { alldone = false; break; }
42                                                 
43                                         c.AddSort (select, new Sort (c));
44                                         
45                                 } while (c.Input.MoveToNext ());
46                                 if (!alldone)
47                                         children = c.CompileTemplateContent ();
48                                 c.Input.MoveToParent ();
49                         }
50                 }
51                 
52                 public override void Evaluate (XslTransformProcessor p)
53                 {
54                         p.PushNodeset (p.Select (select));
55                         p.PushForEachContext ();
56                         
57                         while (p.NodesetMoveNext ())
58                                 children.Evaluate (p);
59                         p.PopForEachContext();
60                         p.PopNodeset ();
61                 }
62         }
63 }