2008-03-30 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl.Operations / XslAttribute.cs
1 //
2 // XslAttribute.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 using System.IO;
39
40 using QName = System.Xml.XmlQualifiedName;
41
42 namespace Mono.Xml.Xsl.Operations {
43         internal class XslAttribute : XslCompiledElement {
44                 XslAvt name, ns;
45                 string calcName, calcNs, calcPrefix;
46 //              XmlNamespaceManager nsm;
47                 Hashtable nsDecls;
48                 XslOperation value;
49
50                 public XslAttribute (Compiler c) : base (c) {}
51                 
52                 protected override void Compile (Compiler c)
53                 {
54                         if (c.Debugger != null)
55                                 c.Debugger.DebugCompile (c.Input);
56
57                         XPathNavigator nav = c.Input.Clone ();
58                         nsDecls = c.GetNamespacesToCopy ();
59
60                         c.CheckExtraAttributes ("attribute", "name", "namespace");
61
62                         name = c.ParseAvtAttribute ("name");
63                         if (name == null)
64                                 throw new XsltCompileException ("Attribute \"name\" is required on XSLT attribute element", null, c.Input);
65                         ns = c.ParseAvtAttribute ("namespace");
66
67                         calcName = XslAvt.AttemptPreCalc (ref name);
68                         calcPrefix = String.Empty;
69
70                         if (calcName != null) {
71                                 int colonAt = calcName.IndexOf (':');
72                                 calcPrefix = colonAt < 0 ? String.Empty : calcName.Substring (0, colonAt);
73                                 calcName = colonAt < 0 ? calcName : calcName.Substring (colonAt + 1, calcName.Length - colonAt - 1);
74
75                                 try {
76                                         XmlConvert.VerifyNCName (calcName);
77                                         if (calcPrefix != String.Empty)
78                                                 XmlConvert.VerifyNCName (calcPrefix);
79                                 } catch (XmlException ex) {
80                                         throw new XsltCompileException ("Invalid attribute name", ex, c.Input);
81                                 }
82                         }
83
84                         if (calcPrefix != String.Empty) {
85                                 calcPrefix = c.CurrentStylesheet.GetActualPrefix (calcPrefix);
86                                 if (calcPrefix == null)
87                                         calcPrefix = String.Empty;
88                         }
89
90                         if (calcPrefix != String.Empty && ns == null)
91                                 calcNs = nav.GetNamespace (calcPrefix);
92                         else if (ns != null)
93                                 calcNs = XslAvt.AttemptPreCalc (ref ns);
94
95                         if (c.Input.MoveToFirstChild ()) {
96                                 value = c.CompileTemplateContent (XPathNodeType.Attribute);
97                                 c.Input.MoveToParent ();
98                         }
99                 }
100
101                 public override void Evaluate (XslTransformProcessor p)
102                 {
103                         if (p.Debugger != null)
104                                 p.Debugger.DebugExecute (p, this.DebugInput);
105
106                         string nm, nmsp, prefix;
107                         
108                         nm = calcName != null ? calcName : name.Evaluate (p);
109                         nmsp = calcNs != null ? calcNs : ns != null ? ns.Evaluate (p) : String.Empty;
110                         prefix = calcPrefix != null ? calcPrefix : String.Empty;
111
112                         if (nm == "xmlns")
113                                 // It is an error. We must recover by not emmiting any attributes 
114                                 // (unless we throw an exception).
115                                 return;
116
117                         int colonAt = nm.IndexOf (':');
118                         // local attribute
119                         if (colonAt > 0) {
120                                 prefix = nm.Substring (0, colonAt);
121                                 nm = nm.Substring (colonAt + 1);
122
123                                 // global attribute
124                                 if (nmsp == String.Empty &&
125                                         prefix == XmlNamespaceManager.PrefixXml)
126                                         nmsp = XmlNamespaceManager.XmlnsXml;
127                                 else if (nmsp == String.Empty) {
128                                         nmsp = (string) nsDecls [prefix];
129                                         if (nmsp == null)
130                                                 nmsp = String.Empty;
131                                 }
132                         }
133
134                         if (prefix == "xmlns")
135                                 prefix = String.Empty;  // Should not be allowed.
136
137                         XmlConvert.VerifyName (nm);
138
139                         p.Out.WriteAttributeString (prefix, nm, nmsp,
140                                 value == null ? "" : value.EvaluateAsString (p));
141                 }
142         }
143 }