2004-11-17 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl.Operations / XslTemplateContent.cs
1 //
2 // XslTemplateContent.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 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.Collections;
35 using System.Xml;
36 using System.Xml.XPath;
37 using System.Xml.Xsl;
38
39 namespace Mono.Xml.Xsl.Operations {
40         internal class XslTemplateContent : XslCompiledElement {
41                 ArrayList content = new ArrayList ();
42                 
43                 bool hasStack;
44                 int stackSize;
45                 
46                 public XslTemplateContent (Compiler c, XPathNodeType parentType)
47                         : base (c, parentType) 
48                 {
49                 }
50
51                 private void ThrowIfNotElement (Compiler c)
52                 {
53                         switch (ParentType) {
54                         case XPathNodeType.All:
55                         case XPathNodeType.Element:
56                                         break;
57                         default:
58                                         throw new XsltCompileException ("Cannot contain attribute from this parent node " + ParentType, null, c.Input);
59                         }
60                 }
61
62                 protected override void Compile (Compiler c)
63                 {
64                         hasStack = (c.CurrentVariableScope == null);
65                         c.PushScope ();
66                         do {    
67                                 Debug.EnterNavigator (c);
68                                 XPathNavigator n = c.Input;                     
69                                 switch (n.NodeType) {
70                                 case XPathNodeType.Element:
71                                         switch (n.NamespaceURI) {
72                                         case XsltNamespace:
73                                                 
74                                                 switch (n.LocalName) {
75                                                 case "apply-imports":
76                                                         content.Add (new XslApplyImports (c));
77                                                         break;
78                                                 case "apply-templates":
79                                                         content.Add (new XslApplyTemplates (c));
80                                                         break;
81                                                 case "attribute":
82                                                         ThrowIfNotElement (c);
83                                                         content.Add (new XslAttribute (c));
84                                                         break;
85                                                 case "call-template":
86                                                         content.Add (new XslCallTemplate (c));
87                                                         break;
88                                                 case "choose":
89                                                         content.Add (new XslChoose (c));
90                                                         break;
91                                                 case "comment":
92                                                         ThrowIfNotElement (c);
93                                                         content.Add (new XslComment (c));
94                                                         break;
95                                                 case "copy":
96                                                         content.Add (new XslCopy (c));
97                                                         break;
98                                                 case "copy-of":
99                                                         content.Add (new XslCopyOf (c));
100                                                         break;
101                                                 case "element":
102                                                         ThrowIfNotElement (c);
103                                                         content.Add (new XslElement (c));
104                                                         break;
105                                                 case "fallback":
106                                                         content.Add (new XslFallback (c));
107                                                         break;
108                                                 case "for-each":
109                                                         content.Add (new XslForEach (c));
110                                                         break;
111                                                 case "if":
112                                                         content.Add (new XslIf (c));
113                                                         break;
114                                                 case "message":
115                                                         content.Add (new XslMessage(c));
116                                                         break;
117                                                 case "number":
118                                                         content.Add (new XslNumber(c));
119                                                         break;
120                                                 case "processing-instruction":
121                                                         ThrowIfNotElement (c);
122                                                         content.Add (new XslProcessingInstruction(c));
123                                                         break;
124                                                 case "text":
125                                                         content.Add (new XslText(c, false));
126                                                         break;
127                                                 case "value-of":
128                                                         content.Add (new XslValueOf(c));
129                                                         break;
130                                                 case "variable":
131                                                         content.Add (new XslLocalVariable (c));
132                                                         break;
133                                                 default:
134                                                         // TODO: handle fallback, like we should
135                                                         throw new XsltCompileException ("Did not recognize element " + n.Name, null, n);
136                                                 }
137                                                 break;
138                                         default:
139                                                 if (!c.IsExtensionNamespace (n.NamespaceURI))
140                                                         content.Add (new XslLiteralElement(c));
141                                                 else {
142                                                         if (n.MoveToFirstChild ()) {
143                                                                 do {
144                                                                         if (n.NamespaceURI == XsltNamespace && n.LocalName == "fallback")
145                                                                                 content.Add (new XslFallback (c));
146                                                                 } while (n.MoveToNext ());
147                                                                 n.MoveToParent ();
148                                                         }
149                                                 }
150                                                 break;
151                                         }
152                                         break;
153
154                                 case XPathNodeType.SignificantWhitespace:
155                                         content.Add (new XslText(c, true));
156                                         break;
157                                 case XPathNodeType.Text:
158                                         content.Add (new XslText(c, false));
159                                         break;
160                                 default:
161                                         break;
162                                 }
163
164                                 Debug.ExitNavigator (c);
165                                 
166                         } while (c.Input.MoveToNext ());
167                         
168                         
169                         if (hasStack) {
170                                 stackSize = c.PopScope ().VariableHighTide;
171                                 hasStack = stackSize > 0;
172                         } else 
173                                 c.PopScope ();
174                 }
175                 
176                 public override void Evaluate (XslTransformProcessor p)
177                 {
178                         if (hasStack)
179                                 p.PushStack (stackSize);
180                         
181                         int len = content.Count;
182                         for (int i = 0; i < len; i++)
183                                 ((XslOperation) content [i]).Evaluate (p);
184                         
185                         if (hasStack)
186                                 p.PopStack ();
187                 }
188         }
189 }