2004-04-24 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl.Operations / XslLiteralElement.cs
1 //
2 // XslLiteralElement.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         internal class XslLiteralElement : XslCompiledElement {
20                 XslOperation children;
21                 string localname, prefix, nsUri;
22                 bool isEmptyElement;
23                 ArrayList attrs;
24                 XmlQualifiedName [] useAttributeSets;
25                 Hashtable nsDecls;
26                 string excludeResultPrefixes;
27                 string extensionElementPrefixes;
28                 ArrayList excludedPrefixes;
29                 bool requireNameFix;
30                 XPathNavigator stylesheetNode;
31                 XslStylesheet stylesheet;
32
33                 public XslLiteralElement (Compiler c) : base (c) {}
34                         
35                 class XslLiteralAttribute {
36                         string localname, prefix, nsUri;
37                         XslAvt val;
38                         
39                         public XslLiteralAttribute (Compiler c)
40                         {
41                                 this.prefix = c.Input.Prefix;
42                                 this.nsUri = c.Input.NamespaceURI;
43                                 this.localname = c.Input.LocalName;
44                                 this.val = new XslAvt (c.Input.Value, c);
45                         }
46                         
47                         public void Evaluate (XslTransformProcessor p)
48                         {
49                                 p.Out.WriteAttributeString (prefix, localname, nsUri, val.Evaluate (p));
50                         }
51                 }
52                 
53                 protected override void Compile (Compiler c)
54                 {
55                         requireNameFix = true;
56                         stylesheetNode = c.Input.Clone ();
57                         stylesheet = c.CurrentStylesheet;
58
59                         this.localname = c.Input.LocalName;
60                         this.useAttributeSets = c.ParseQNameListAttribute ("use-attribute-sets", XsltNamespace);
61                         this.nsDecls = c.GetNamespacesToCopy ();
62                         if (nsDecls.Count == 0) nsDecls = null;
63                         this.isEmptyElement = c.Input.IsEmptyElement;
64                         this.excludeResultPrefixes = c.Input.GetAttribute ("exclude-result-prefixes", XsltNamespace);
65                         this.extensionElementPrefixes = c.Input.GetAttribute ("extension-element-prefixes", XsltNamespace);
66                         excludedPrefixes = new ArrayList (excludeResultPrefixes.Split (XmlChar.WhitespaceChars));
67                         excludedPrefixes.AddRange (extensionElementPrefixes.Split (XmlChar.WhitespaceChars));
68
69                         if (c.Input.MoveToFirstAttribute ())
70                         {
71                                 attrs = new ArrayList ();
72                                 do {
73                                         if (c.Input.NamespaceURI == XsltNamespace)
74                                                 continue; //already handled
75                                         attrs.Add (new XslLiteralAttribute (c));
76                                 } while (c.Input.MoveToNextAttribute());
77                                 c.Input.MoveToParent ();
78                         }
79                         
80                         if (!c.Input.MoveToFirstChild ()) return;
81                         children = c.CompileTemplateContent ();
82                         c.Input.MoveToParent ();
83                 }
84
85                 private void GetCorrectNames ()
86                 {
87                         requireNameFix = false;
88                         prefix = stylesheetNode.Prefix;
89                         string alias = stylesheet.PrefixInEffect (prefix, null);
90                         if (alias != null && alias != stylesheetNode.Prefix) {
91                                 nsUri = stylesheetNode.GetNamespace (alias);
92                                 if (alias != null)
93                                         prefix = alias;
94                         }
95                         else
96                                 nsUri = stylesheetNode.NamespaceURI;
97
98                 }
99                 
100                 public override void Evaluate (XslTransformProcessor p)
101                 {
102                         // Since namespace-alias might be determined after compilation
103                         // of import-ing stylesheets, this must be determined later.
104                         if (requireNameFix)
105                                 GetCorrectNames ();
106
107                         bool isCData = p.InsideCDataElement;
108                         p.PushElementState (localname, nsUri, true);
109                         p.Out.WriteStartElement (prefix, localname, nsUri);
110
111                         if (useAttributeSets != null)
112                                 foreach (XmlQualifiedName s in useAttributeSets)
113                                         p.ResolveAttributeSet (s).Evaluate (p);
114                                                 
115                         if (attrs != null) {
116                                 int len = attrs.Count;
117                                 for (int i = 0; i < len; i++)
118                                         ((XslLiteralAttribute)attrs [i]).Evaluate (p);
119                         }
120
121                         p.TryStylesheetNamespaceOutput (excludedPrefixes);
122                         if (nsDecls != null) {
123                                 foreach (DictionaryEntry de in nsDecls) {
124                                         string actualPrefix = p.CompiledStyle.Style.PrefixInEffect (de.Key as String, excludedPrefixes);
125                                         if (actualPrefix != null)
126                                                 p.Out.WriteNamespaceDecl (actualPrefix, (string)de.Value);
127                                 }
128                         }
129                         
130                         if (children != null) children.Evaluate (p);
131
132                         if (isEmptyElement)
133                                 p.Out.WriteEndElement ();
134                         else
135                                 p.Out.WriteFullEndElement ();
136
137                         p.PopCDataState (isCData);
138                 }
139         }
140 }