Here comes managed XSLT! WOOOOOHOOOOOO!
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl.Operations / XslChoose.cs
1 //
2 // XslChoose.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 XslChoose : XslCompiledElement {
20                 XslOperation defaultChoice = null;
21                 ArrayList conditions = new ArrayList ();
22                 
23                 public XslChoose (Compiler c) : base (c) {}
24                 
25                 protected override void Compile (Compiler c)
26                 {
27                         if (!c.Input.MoveToFirstChild ()) throw new Exception ("Expecting non-empty element");
28                         
29                         do {
30                                 if (c.Input.NodeType != XPathNodeType.Element) continue;
31                                 if (c.Input.NamespaceURI != XsltNamespace) continue;
32                                 
33                                 if (defaultChoice != null)
34                                         throw new Exception ("otherwise attribute must be last");
35
36                                 switch (c.Input.LocalName) {
37                                 case "when":
38                                         conditions.Add (new XslIf (c));
39                                         break;
40                                         
41                                 case "otherwise":
42                                         if (c.Input.MoveToFirstChild ()) {
43                                                 defaultChoice = c.CompileTemplateContent ();
44                                                 c.Input.MoveToParent ();
45                                         }
46                                         break;
47
48                                 default:
49                                         break; // TODO: forwards compat
50                                 }
51                         } while (c.Input.MoveToNext ());
52                         
53                         c.Input.MoveToParent ();
54                         
55                         if (conditions.Count == 0)
56                                 throw new Exception ("Choose must have 1 or ore when elements");
57                 }
58
59                 public override void Evaluate (XslTransformProcessor p)
60                 {
61                         foreach (XslIf test in conditions) {
62                                 if (test.EvaluateIfTrue (p))
63                                         return;
64                         }
65                         if (defaultChoice != null)      
66                                 defaultChoice.Evaluate (p);
67                 }
68
69         }
70 }