2004-02-06 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl.Operations / XslProcessingInstruction.cs
1 //
2 // XslProcessingInstruction.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 using System.IO;
18
19 namespace Mono.Xml.Xsl.Operations {
20         public class XslProcessingInstruction : XslCompiledElement {
21                 XslAvt name;
22                 XslOperation value;
23                 
24                 public XslProcessingInstruction (Compiler c) : base (c) {}
25                 
26                 protected override void Compile (Compiler c)
27                 {
28                         name = c.ParseAvtAttribute ("name");
29
30                         if (c.Input.MoveToFirstAttribute ()) {
31                                 do {
32                                         if (c.Input.NamespaceURI == String.Empty && c.Input.LocalName != "name")
33                                                 throw new XsltCompileException ("Invalid attribute \"" + c.Input.Name + "\"", null, c.Input);
34                                 } while (c.Input.MoveToNextAttribute ());
35                                 c.Input.MoveToParent ();
36                         }
37
38                         if (!c.Input.MoveToFirstChild ()) return;
39                         
40                         value = c.CompileTemplateContent (XPathNodeType.ProcessingInstruction);
41                         c.Input.MoveToParent ();
42                 }
43
44                 public override void Evaluate (XslTransformProcessor p)
45                 {
46                         StringWriter s = new StringWriter ();
47                         Outputter outputter = new TextOutputter(s, true);
48                         p.PushOutput (outputter);
49                         value.Evaluate (p);
50                         p.PopOutput ();
51                         
52                         string actualName = name.Evaluate (p);
53                         if (actualName.ToLower () == "xml")
54                                 throw new XsltException ("Processing instruction name was evaluated to \"xml\"", null, p.CurrentNode);
55                         p.Out.WriteProcessingInstruction (actualName, s.ToString ());
56                 }
57         }
58 }