Add MIT license to System.XML.DLL
[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                 
48                 XslOperation value;
49                 XPathNavigator nav;
50
51                 public XslAttribute (Compiler c) : base (c) {}
52                 
53                 protected override void Compile (Compiler c)
54                 {
55                         nav = c.Input.Clone ();
56                         
57                         name = c.ParseAvtAttribute ("name");
58                         if (name == null)
59                                 throw new XsltCompileException ("attribute \"name\" is required on XSLT attribute element.", null, c.Input);
60                         ns = c.ParseAvtAttribute ("namespace");
61
62                         calcName = XslAvt.AttemptPreCalc (ref name);
63                         calcPrefix = String.Empty;
64
65                         if (calcName != null) {
66                                 int colonAt = calcName.IndexOf (':');
67                                 calcPrefix = colonAt < 0 ? String.Empty : calcName.Substring (0, colonAt);
68                                 calcName = colonAt < 0 ? calcName : calcName.Substring (colonAt + 1, calcName.Length - colonAt - 1);
69
70                                 try {
71                                         XmlConvert.VerifyNCName (calcName);
72                                         if (calcPrefix != String.Empty)
73                                                 XmlConvert.VerifyNCName (calcPrefix);
74                                 } catch (XmlException ex) {
75                                         throw new XsltCompileException ("Invalid attribute name.", ex, c.Input);
76                                 }
77                         }
78                         if (calcPrefix != String.Empty && ns == null)
79                                 calcNs = nav.GetNamespace (calcPrefix);
80                         else if (ns != null)
81                                 calcNs = XslAvt.AttemptPreCalc (ref ns);
82
83                         if (calcNs ==null && calcPrefix != String.Empty) {
84                                 string test = c.CurrentStylesheet.PrefixInEffect (calcPrefix, null);
85                                 if (test != null) {
86                                         string alias = c.CurrentStylesheet.NamespaceAliases [calcPrefix] as string;
87                                         if (alias != null)
88                                                 calcNs = c.Input.GetNamespace (alias);
89                                         else
90                                                 calcNs = c.Input.NamespaceURI;
91                                 }
92                         }
93
94                         if (ns == null && calcNs == null)
95                                 nsm = c.GetNsm ();
96                                 
97                         if (c.Input.MoveToFirstChild ()) {
98                                 value = c.CompileTemplateContent (XPathNodeType.Attribute);
99                                 c.Input.MoveToParent ();
100                         }
101
102                 }
103
104                 public override void Evaluate (XslTransformProcessor p)
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, nm.Length - colonAt - 1);
122
123                                 // global attribute
124                                 if (nmsp == null) {
125                                         QName q = XslNameUtil.FromString (nm, nsm);
126                                         nm = q.Name;
127                                         nmsp = q.Namespace;
128                                 } else
129                                         nm = XslNameUtil.LocalNameOf (nm);
130                         }
131
132                         if (nmsp != String.Empty && prefix == String.Empty) {
133                                 if (nav.MoveToFirstNamespace (XPathNamespaceScope.ExcludeXml)) {
134                                         do {
135                                                 if (nav.Value == nmsp) {
136                                                         prefix = nav.Name;
137                                                         break;
138                                                 }
139                                         } while (nav.MoveToNextNamespace (XPathNamespaceScope.ExcludeXml));
140                                         nav.MoveToParent ();
141                                 }
142                         }
143
144                         if (prefix == "xmlns")
145                                 prefix = String.Empty;  // Should not be allowed.
146
147                         XmlConvert.VerifyName (nm);
148
149                         if (value == null)
150                                 p.Out.WriteAttributeString(prefix, nm, nmsp, "");
151                         else {
152                                 StringWriter sw = new StringWriter ();
153                                 Outputter outputter = new TextOutputter (sw, true);
154                                 p.PushOutput (outputter);
155                                 value.Evaluate (p);                         
156                                 p.PopOutput ();
157                                 outputter.Done ();                              
158                                 p.Out.WriteAttributeString (prefix, nm, nmsp, sw.ToString ());                                                                  
159                         }                                               
160                 }
161         }
162 }