2003-08-14 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl.Operations / XslApplyTemplates.cs
1 //
2 // XslApplyTemplates.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
20         public class XslApplyTemplates : XslCompiledElement {
21                 XPathExpression select;
22                 XmlQualifiedName mode;
23                 ArrayList withParams = new ArrayList ();
24                 
25                 public XslApplyTemplates (Compiler c) : base (c) {}
26                 
27                 protected override void Compile (Compiler c)
28                 {
29                         select = c.CompileExpression (c.GetAttribute ("select"));
30                         mode = c.ParseQNameAttribute ("mode");
31                         
32                         if (c.Input.MoveToFirstChild ()) {
33                                 do {
34                                         switch (c.Input.NodeType) {
35                                         case XPathNodeType.Comment:
36                                         case XPathNodeType.ProcessingInstruction:
37                                         case XPathNodeType.Whitespace:
38                                                 continue;
39                                         case XPathNodeType.Element:
40                                                 if (c.Input.NamespaceURI != XsltNamespace)
41                                                         throw new Exception ("unexptected element"); // TODO: fwd compat
42                                                 
43                                                 switch (c.Input.LocalName)
44                                                 {
45                                                         case "with-param":
46                                                                 withParams.Add (new XslVariableInformation (c));
47                                                                 break;
48                                                                 
49                                                         case "sort":
50                                                                 if (select == null)
51                                                                         select = c.CompileExpression ("*");
52                                                                 c.AddSort (select, new Sort (c));
53                                                                 break;
54                                                         default:
55                                                                 throw new Exception ("unexptected element"); // todo forwards compat
56                                                 }
57                                                 break;
58                                         default:
59                                                 throw new Exception ("unexpected node type " + c.Input.NodeType);       // todo forwards compat
60                                         }
61                                 } while (c.Input.MoveToNext ());
62                                 c.Input.MoveToParent ();
63                         }
64                 }
65                 
66                 public override void Evaluate (XslTransformProcessor p)
67                 {
68                         Hashtable passedParams = null;
69                         
70                         if (withParams.Count > 0) {
71                                 passedParams = new Hashtable ();
72                                 foreach (XslVariableInformation param in withParams)
73                                         passedParams [param.Name] = param.Evaluate (p);
74                         }
75                         
76                         if (select == null)     
77                                 p.ApplyTemplates (p.CurrentNode.SelectChildren (XPathNodeType.All), mode, passedParams);
78                         else
79                                 p.ApplyTemplates (p.Select (select), mode, passedParams);
80                 }
81         }
82 }