2005-11-24 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl / XslTemplate.cs
1 //
2 // XslTemplate.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.Globalization;
36 using System.Xml;
37 using System.Xml.XPath;
38 using System.Xml.Xsl;
39 using Mono.Xml.Xsl.Operations;
40 using Mono.Xml.XPath;
41
42 using QName = System.Xml.XmlQualifiedName;
43
44 namespace Mono.Xml.Xsl {
45         internal class XslModedTemplateTable {
46                 
47                 class TemplateWithPriority : IComparable {
48                         public readonly double Priority;
49                         public readonly XslTemplate Template;
50                         public readonly Pattern Pattern;
51                         public readonly int TemplateID;
52                         
53                         public TemplateWithPriority (XslTemplate t, Pattern p)
54                         {
55                                 Template = t;
56                                 Pattern = p;
57                                 Priority = p.DefaultPriority;
58                                 TemplateID = t.Id;
59                         }
60                         
61                         public TemplateWithPriority (XslTemplate t, double p)
62                         {
63                                 Template = t;
64                                 Pattern = t.Match;
65                                 Priority = p;
66                                 TemplateID = t.Id;
67                         }
68                         
69                         public int CompareTo (object o)
70                         {
71                                 TemplateWithPriority a = this,
72                                         b = (TemplateWithPriority)o;
73                                 
74                                 //Debug.WriteLine (a.Pattern.ToString () + " ? " + b.Pattern.ToString ());
75                                 //Debug.WriteLine (a.Priority + "   " + b.Priority);
76                                 
77                                 int r0 = a.Priority.CompareTo (b.Priority);
78                                 //Debug.WriteLine (r0);
79                                 if (r0 != 0) return r0;
80                                 
81                                 int r1 = a.TemplateID.CompareTo (b.TemplateID);
82                                 //Debug.WriteLine (r1);
83                                 return r1;
84                         }
85                         
86                         public bool Matches (XPathNavigator n, XslTransformProcessor p)
87                         {
88                                 //Debug.WriteLine (Pattern.ToString ());
89                                 return p.Matches (Pattern, n);
90                         }
91                 }
92                 
93                 // [QName name]=>XslTemplate
94                 
95                 ArrayList unnamedTemplates = new ArrayList ();
96                 
97                 XmlQualifiedName mode;
98
99                 public XslModedTemplateTable (XmlQualifiedName mode)
100                 {
101                         if (mode == null)
102                                 throw new InvalidOperationException ();
103                         this.mode = mode;
104                 }
105
106                 public XmlQualifiedName Mode {
107                         get { return mode; }
108                 }
109
110                 public void Add (XslTemplate t)
111                 {
112                         if (!double.IsNaN (t.Priority))
113                                 unnamedTemplates.Add (new TemplateWithPriority (t, t.Priority));
114                         else
115                                 Add (t, t.Match);
116                 }
117                 
118                 public void Add (XslTemplate t, Pattern p)
119                 {
120                         if (p is UnionPattern) {
121                                 Add (t, ((UnionPattern)p).p0);
122                                 Add (t, ((UnionPattern)p).p1);
123                                 return;
124                         }
125                         
126                         unnamedTemplates.Add (new TemplateWithPriority (t, p));
127                 }
128                 
129                 bool sorted = false;
130                 
131                 public XslTemplate FindMatch (XPathNavigator node, XslTransformProcessor p)
132                 {
133                         //Debug.WriteLine ("...");
134                         if (!sorted) {
135                                 unnamedTemplates.Sort ();
136                                 unnamedTemplates.Reverse ();
137                                 
138                                 sorted = true;
139                         }
140                         
141                         for (int i = 0; i < unnamedTemplates.Count; i++) {
142                                 TemplateWithPriority t = (TemplateWithPriority) unnamedTemplates [i];
143                                 if (t.Matches (node, p))
144                                         return t.Template;
145                         }
146
147                         return null;
148                 }
149         }
150
151         internal class XslTemplateTable {
152                 // [QName mode]=>XslTemplateTable
153                 Hashtable templateTables = new Hashtable ();
154                 Hashtable namedTemplates = new Hashtable ();
155                 XslStylesheet parent;
156                 
157                 public XslTemplateTable (XslStylesheet parent)
158                 {
159                         this.parent = parent;
160                 }
161                 
162                 public Hashtable TemplateTables {
163                         get { return templateTables; }
164                 }
165
166                 public XslModedTemplateTable this [XmlQualifiedName mode] {
167                         get {
168                                 return templateTables [mode] as XslModedTemplateTable;
169                         }
170                 }
171
172                 public void Add (XslTemplate template)
173                 {
174                         if (template.Name != XmlQualifiedName.Empty) {
175                                 if (namedTemplates [template.Name] != null)
176                                         throw new InvalidOperationException ("Named template " + template.Name + " is already registered.");
177                                 
178                                 namedTemplates [template.Name] = template;
179                         }
180                         
181                         if (template.Match == null) return;
182                         
183                         XslModedTemplateTable tbl = this [template.Mode];
184                         if (tbl == null) {
185                                 tbl = new XslModedTemplateTable (template.Mode);
186                                 Add (tbl);
187                         }
188
189                         tbl.Add (template);
190                 }
191
192                 public void Add (XslModedTemplateTable table)
193                 {
194                         if (this [table.Mode] != null)
195                                 throw new InvalidOperationException ("Mode " + table.Mode + " is already registered.");
196                         templateTables.Add (table.Mode, table);
197                 }
198                 
199                 public XslTemplate FindMatch (XPathNavigator node, XmlQualifiedName mode, XslTransformProcessor p)
200                 {       
201                         XslTemplate ret;
202                         
203                         if (this [mode] != null)
204                         {
205                                 ret =  this [mode].FindMatch (node, p);
206                                 if (ret != null) return ret;
207                         }
208                         
209                         for (int i = parent.Imports.Count - 1; i >= 0; i--)
210                         {
211                                 XslStylesheet s = (XslStylesheet)parent.Imports [i];
212                                 ret = s.Templates.FindMatch (node, mode, p);
213                                 if (ret != null)
214                                         return ret;
215                         }
216                         
217                         return null;
218                 }
219                 
220                 public XslTemplate FindTemplate (XmlQualifiedName name)
221                 {
222                         XslTemplate ret = (XslTemplate)namedTemplates [name];
223                         
224                         if (ret != null) return ret;
225                                 
226                         for (int i = parent.Imports.Count - 1; i >= 0; i--) {
227                                 XslStylesheet s = (XslStylesheet)parent.Imports [i];
228                                 ret = s.Templates.FindTemplate (name);
229                                 if (ret != null)
230                                         return ret;
231                         }
232                         
233                         return null;
234                 }
235         }
236
237         internal class XslTemplate
238         {
239                 XmlQualifiedName name;
240                 Pattern match;
241                 XmlQualifiedName mode;
242                 double priority = double.NaN;
243                 ArrayList parameters;
244                 XslOperation content;
245                 
246                 static int nextId = 0;
247                 public readonly int Id = nextId ++;
248
249                 XslStylesheet style;
250                 int stackSize;
251                 
252                 
253                 public XslTemplate (Compiler c)
254                 {
255                         if (c == null) return; // built in template
256                         this.style = c.CurrentStylesheet;
257                         
258                         c.PushScope ();
259
260                         if (c.Input.MoveToAttribute ("mode", String.Empty)) {
261                                 c.Input.MoveToParent ();
262                                 if (!c.Input.MoveToAttribute ("match", String.Empty))
263                                         throw new XsltCompileException ("XSLT 'template' element must not have 'mode' attribute when it does not have 'match' attribute.", null, c.Input);
264                                 c.Input.MoveToParent ();
265                         }
266
267                         if (c.Input.NamespaceURI != Compiler.XsltNamespace) {
268                                 this.name = QName.Empty;
269                                 this.match = c.CompilePattern ("/", c.Input);
270                                 this.mode = QName.Empty;
271                         } else {
272                                 this.name = c.ParseQNameAttribute ("name");
273                                 this.match = c.CompilePattern (c.GetAttribute ("match"), c.Input);
274                                 this.mode = c.ParseQNameAttribute ("mode");
275                                 
276                                 string pri = c.GetAttribute ("priority");
277                                 if (pri != null) {
278                                         try {
279                                                 this.priority = double.Parse (pri, CultureInfo.InvariantCulture);
280                                         } catch (FormatException ex) {
281                                                 throw new XsltException ("Invalid priority number format.", ex, c.Input);
282                                         }
283                                 }
284                         }
285                         Parse (c);
286                         
287                         stackSize = c.PopScope ().VariableHighTide;
288                         
289                 }
290
291                 public XmlQualifiedName Name {
292                         get { return name; }
293                 }
294
295                 public Pattern Match {
296                         get {
297                                 return match;
298                         }
299                 }
300
301                 public XmlQualifiedName Mode {
302                         get { return mode; }
303                 }
304
305                 public double Priority {
306                         get { return priority; }
307                 }
308                 
309                 public XslStylesheet Parent {
310                         get { return style; }
311                 }
312
313                 private void Parse (Compiler c) {
314                         if (c.Input.NamespaceURI != Compiler.XsltNamespace) {
315                                 content = c.CompileTemplateContent ();
316                                 return;
317                         }
318
319                         if (c.Input.MoveToFirstChild ()) {
320                                 bool alldone = true;
321                                 XPathNavigator contentStart = c.Input.Clone ();
322                                 bool shouldMove = false;
323                                 do {
324                                         if (shouldMove) {
325                                                 shouldMove = false;
326                                                 contentStart.MoveTo (c.Input);
327                                         }
328                                         if (c.Input.NodeType == XPathNodeType.Text)
329                                                 { alldone = false; break; }
330                                         
331                                         if (c.Input.NodeType != XPathNodeType.Element)
332                                                 continue;
333                                         if (c.Input.NamespaceURI != Compiler.XsltNamespace)
334                                                 { alldone = false; break; }
335                                         if (c.Input.LocalName != "param")
336                                                 { alldone = false; break; }
337                                         
338                                         if (this.parameters == null)
339                                                 this.parameters = new ArrayList ();
340                                         
341                                         parameters.Add (new XslLocalParam (c));
342                                         shouldMove = true;
343                                 } while (c.Input.MoveToNext ());
344                                 if (!alldone) {
345                                         c.Input.MoveTo (contentStart);
346                                         content = c.CompileTemplateContent ();
347                                 }
348                                 c.Input.MoveToParent ();
349                         }
350                 }
351                 
352                 public virtual void Evaluate (XslTransformProcessor p, Hashtable withParams)
353                 {
354                         p.PushStack (stackSize);
355
356                         if (parameters != null) {
357                                 if (withParams == null) {
358                                         int len = parameters.Count;
359                                         for (int i = 0; i < len; i++) {
360                                                 XslLocalParam param = (XslLocalParam)parameters [i];
361                                                 param.Evaluate (p);
362                                         }
363                                 } else {
364                                         int len = parameters.Count;
365                                         for (int i = 0; i < len; i++) {
366                                                 XslLocalParam param = (XslLocalParam)parameters [i];
367                                                 object o = withParams [param.Name];
368                                                 if (o != null)
369                                                         param.Override (p, o);
370                                                 else
371                                                         param.Evaluate (p);
372                                         }
373                                 }
374                         }
375                         
376                         if (content != null)
377                                 content.Evaluate (p);
378
379                         p.PopStack ();
380                 }
381                 public void Evaluate (XslTransformProcessor p)
382                 {
383                         Evaluate (p, null);
384                 }
385         }
386         
387         internal class XslDefaultNodeTemplate : XslTemplate {
388                 QName mode;
389                 
390                 static XslDefaultNodeTemplate instance = new XslDefaultNodeTemplate (QName.Empty);
391                 public XslDefaultNodeTemplate (QName mode) : base (null)
392                 {
393                         this.mode = mode;
394                 }
395                         
396                 public static XslTemplate Instance {
397                         get { return instance; }
398                 }
399                 
400                 public override void Evaluate (XslTransformProcessor p, Hashtable withParams)
401                 {
402                         p.ApplyTemplates (p.CurrentNode.SelectChildren (XPathNodeType.All), mode, null);
403                 }
404         }
405         
406         internal class XslEmptyTemplate : XslTemplate {
407
408                 static XslEmptyTemplate instance = new XslEmptyTemplate ();
409                 XslEmptyTemplate () : base (null) {}
410                         
411                 public static XslTemplate Instance {
412                         get { return instance; }
413                 }
414                 
415                 public override void Evaluate (XslTransformProcessor p, Hashtable withParams)
416                 {
417                 }
418         }
419         
420         internal class XslDefaultTextTemplate: XslTemplate {
421
422                 static XslDefaultTextTemplate instance = new XslDefaultTextTemplate ();
423                 XslDefaultTextTemplate () : base (null) {}
424                         
425                 public static XslTemplate Instance {
426                         get { return instance; }
427                 }
428                 
429                 public override void Evaluate (XslTransformProcessor p, Hashtable withParams)
430                 {
431                         if (p.CurrentNode.NodeType == XPathNodeType.Whitespace) {
432                                 if (p.PreserveWhitespace ())
433                                         p.Out.WriteWhitespace (p.CurrentNode.Value);
434                         }
435                         else
436                                 p.Out.WriteString (p.CurrentNode.Value);
437                 }
438         }
439 }