2003-12-20 Atsushi Enomoto <atsushi@ximian.com>
[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;
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                                                                 if (withParams == null) withParams = new ArrayList ();
47                                                                 withParams.Add (new XslVariableInformation (c));
48                                                                 break;
49                                                                 
50                                                         case "sort":
51                                                                 if (select == null)
52                                                                         select = c.CompileExpression ("*");
53                                                                 c.AddSort (select, new Sort (c));
54                                                                 break;
55                                                         default:
56                                                                 throw new Exception ("unexptected element"); // todo forwards compat
57                                                 }
58                                                 break;
59                                         default:
60                                                 throw new Exception ("unexpected node type " + c.Input.NodeType);       // todo forwards compat
61                                         }
62                                 } while (c.Input.MoveToNext ());
63                                 c.Input.MoveToParent ();
64                         }
65                 }
66                 
67                 public override void Evaluate (XslTransformProcessor p)
68                 {               
69                         if (select == null)     
70                                 p.ApplyTemplates (p.CurrentNode.SelectChildren (XPathNodeType.All), mode, withParams);
71                         else
72                                 p.ApplyTemplates (p.Select (select), mode, withParams);
73                 }
74         }
75 }