[System.Net] Add support for .pac proxy config scripts on mac
[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 {
41         internal class XslTemplateContent : XslCompiledElementBase
42         {
43                 ArrayList content = new ArrayList ();
44                 
45                 bool hasStack;
46                 int stackSize;
47                 XPathNodeType parentType;
48                 bool xslForEach;
49                 
50                 public XslTemplateContent (Compiler c,
51                         XPathNodeType parentType, bool xslForEach)
52                         : base (c) 
53                 {
54                         this.parentType = parentType;
55                         this.xslForEach = xslForEach;
56                         Compile (c);
57                 }
58
59                 public XPathNodeType ParentType {
60                         get { return parentType; }
61                 }
62
63                 protected override void Compile (Compiler c)
64                 {
65                         if (c.Debugger != null)
66                                 c.Debugger.DebugCompile (this.DebugInput);
67
68                         hasStack = (c.CurrentVariableScope == null);
69                         c.PushScope ();
70                         do {    
71                                 Debug.EnterNavigator (c);
72                                 XPathNavigator n = c.Input;                     
73                                 switch (n.NodeType) {
74                                 case XPathNodeType.Element:
75                                         switch (n.NamespaceURI) {
76                                         case XsltNamespace:
77                                                 
78                                                 switch (n.LocalName) {
79                                                 case "apply-imports":
80                                                         content.Add (new XslApplyImports (c));
81                                                         break;
82                                                 case "apply-templates":
83                                                         content.Add (new XslApplyTemplates (c));
84                                                         break;
85                                                 case "attribute":
86                                                         if (ParentType == XPathNodeType.All
87                                                                 || ParentType == XPathNodeType.Element)
88                                                                 content.Add (new XslAttribute (c));
89                                                         break;
90                                                 case "call-template":
91                                                         content.Add (new XslCallTemplate (c));
92                                                         break;
93                                                 case "choose":
94                                                         content.Add (new XslChoose (c));
95                                                         break;
96                                                 case "comment":
97                                                         if (ParentType == XPathNodeType.All
98                                                                 || ParentType == XPathNodeType.Element)
99                                                         content.Add (new XslComment (c));
100                                                         break;
101                                                 case "copy":
102                                                         content.Add (new XslCopy (c));
103                                                         break;
104                                                 case "copy-of":
105                                                         content.Add (new XslCopyOf (c));
106                                                         break;
107                                                 case "element":
108                                                         if (ParentType == XPathNodeType.All
109                                                                 || ParentType == XPathNodeType.Element)
110                                                                 content.Add (new XslElement (c));
111                                                         break;
112                                                 case "fallback":
113                                                         break;
114                                                 case "for-each":
115                                                         content.Add (new XslForEach (c));
116                                                         break;
117                                                 case "if":
118                                                         content.Add (new XslIf (c));
119                                                         break;
120                                                 case "message":
121                                                         content.Add (new XslMessage(c));
122                                                         break;
123                                                 case "number":
124                                                         content.Add (new XslNumber(c));
125                                                         break;
126                                                 case "processing-instruction":
127                                                         if (ParentType == XPathNodeType.All
128                                                                 || ParentType == XPathNodeType.Element)
129                                                                 content.Add (new XslProcessingInstruction(c));
130                                                         break;
131                                                 case "text":
132                                                         content.Add (new XslText(c, false));
133                                                         break;
134                                                 case "value-of":
135                                                         content.Add (new XslValueOf(c));
136                                                         break;
137                                                 case "variable":
138                                                         content.Add (new XslLocalVariable (c));
139                                                         break;
140                                                 case "sort":
141                                                         if (xslForEach)
142                                                                 break;
143                                                         throw new XsltCompileException ("'sort' element is not allowed here as a templete content", null, n);
144                                                 default:
145                                                         // TODO: handle fallback, like we should
146 //                                                      throw new XsltCompileException ("Did not recognize element " + n.Name, null, n);
147                                                         content.Add (new XslNotSupportedOperation (c));
148                                                         break;
149                                                 }
150                                                 break;
151                                         default:
152                                                 if (!c.IsExtensionNamespace (n.NamespaceURI))
153                                                         content.Add (new XslLiteralElement(c));
154                                                 else {
155                                                         if (n.MoveToFirstChild ()) {
156                                                                 do {
157                                                                         if (n.NamespaceURI == XsltNamespace && n.LocalName == "fallback")
158                                                                                 content.Add (new XslFallback (c));
159                                                                 } while (n.MoveToNext ());
160                                                                 n.MoveToParent ();
161                                                         }
162                                                 }
163                                                 break;
164                                         }
165                                         break;
166
167                                 case XPathNodeType.SignificantWhitespace:
168                                         content.Add (new XslText(c, true));
169                                         break;
170                                 case XPathNodeType.Text:
171                                         content.Add (new XslText(c, false));
172                                         break;
173                                 default:
174                                         break;
175                                 }
176
177                                 Debug.ExitNavigator (c);
178                                 
179                         } while (c.Input.MoveToNext ());
180                         
181                         
182                         if (hasStack) {
183                                 stackSize = c.PopScope ().VariableHighTide;
184                                 hasStack = stackSize > 0;
185                         } else 
186                                 c.PopScope ();
187                 }
188                 
189                 public override void Evaluate (XslTransformProcessor p)
190                 {
191                         if (p.Debugger != null)
192                                 p.Debugger.DebugExecute (p, this.DebugInput);
193
194                         if (hasStack)
195                                 p.PushStack (stackSize);
196                         
197                         int len = content.Count;
198                         for (int i = 0; i < len; i++)
199                                 ((XslOperation) content [i]).Evaluate (p);
200                         
201                         if (hasStack)
202                                 p.PopStack ();
203                 }
204         }
205 }