System.Drawing: added email to icon and test file headers
[mono.git] / mcs / class / System.XML / System.Xml.Xsl / XslCompiledTransform.cs
1 // XslCompiledTransform.cs
2 //
3 // Author:
4 //      Atsushi Enomoto  <atsushi@ximian.com>
5 //
6 // Copyright (C) 2005 Novell Inc. http://www.novell.com
7 //
8
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 #if NET_2_0
31
32 using System;
33 using System.CodeDom.Compiler;
34 using System.Collections;
35 using System.IO;
36 using System.Text;
37 using System.Runtime.InteropServices;
38 using System.Security;
39 using System.Security.Policy;
40 using System.Xml.XPath;
41 using Mono.Xml.Xsl;
42
43 namespace System.Xml.Xsl
44 {
45         [MonoTODO]
46         // FIXME: Of cource this is just a stub for now.
47         public sealed class XslCompiledTransform
48         {
49                 bool enable_debug;
50                 object debugger;
51                 CompiledStylesheet s;
52 #if !TARGET_JVM && !MOBILE
53 //              TempFileCollection temporary_files;
54 #endif
55                 XmlWriterSettings output_settings = new XmlWriterSettings ();
56
57                 public XslCompiledTransform ()
58                         : this (false)
59                 {
60                 }
61
62                 public XslCompiledTransform (bool enableDebug)
63                 {
64                         enable_debug = enableDebug;
65                         if (enable_debug)
66                                 debugger = new NoOperationDebugger ();
67                         output_settings.ConformanceLevel = ConformanceLevel.Fragment;
68                 }
69
70                 [MonoTODO]
71                 public XmlWriterSettings OutputSettings {
72                         get { return output_settings; }
73                 }
74
75 #if !TARGET_JVM && !MOBILE
76                 [MonoTODO]
77                 public TempFileCollection TemporaryFiles {
78                         get { return null; /*temporary_files;*/ }
79                 }
80 #endif
81
82                 #region Transform
83
84                 public void Transform (string inputfile, string outputfile)
85                 {
86                         using (Stream outStream = File.Create (outputfile)) {
87                                 Transform (new XPathDocument (inputfile, XmlSpace.Preserve), null, outStream);
88                         }
89                 }
90
91                 public void Transform (string inputfile, XmlWriter output)
92                 {
93                         Transform (inputfile, null, output);
94                 }
95
96                 public void Transform (string inputfile, XsltArgumentList args, Stream output)
97                 {
98                         Transform (new XPathDocument (inputfile, XmlSpace.Preserve), args, output);
99                 }
100
101                 public void Transform (string inputfile, XsltArgumentList args, TextWriter output)
102                 {
103                         Transform (new XPathDocument (inputfile, XmlSpace.Preserve), args, output);
104                 }
105
106                 public void Transform (string inputfile, XsltArgumentList args, XmlWriter output)
107                 {
108                         Transform (new XPathDocument (inputfile, XmlSpace.Preserve), args, output);
109                 }
110
111                 public void Transform (XmlReader reader, XmlWriter output)
112                 {
113                         Transform (reader, null, output);
114                 }
115
116                 public void Transform (XmlReader reader, XsltArgumentList args, Stream output)
117                 {
118                         Transform (new XPathDocument (reader, XmlSpace.Preserve), args, output);
119                 }
120
121                 public void Transform (XmlReader reader, XsltArgumentList args, TextWriter output)
122                 {
123                         Transform (new XPathDocument (reader, XmlSpace.Preserve), args, output);
124                 }
125
126                 public void Transform (XmlReader reader, XsltArgumentList args, XmlWriter output)
127                 {
128                         Transform (reader, args, output, null);
129                 }
130
131                 public void Transform (IXPathNavigable input, XsltArgumentList args, TextWriter output)
132                 {
133                         Transform (input.CreateNavigator (), args, output);
134                 }
135
136                 public void Transform (IXPathNavigable input, XsltArgumentList args, Stream output)
137                 {
138                         Transform (input.CreateNavigator (), args, output);
139                 }
140
141                 public void Transform (IXPathNavigable input, XmlWriter output)
142                 {
143                         Transform (input, null, output);
144                 }
145
146                 public void Transform (IXPathNavigable input, XsltArgumentList args, XmlWriter output)
147                 {
148                         Transform (input.CreateNavigator (), args, output, null);
149                 }
150
151                 public void Transform (XmlReader input, XsltArgumentList args, XmlWriter output, XmlResolver resolver)
152                 {
153                         Transform (new XPathDocument (input, XmlSpace.Preserve).CreateNavigator (), args, output, resolver);
154                 }
155
156                 void Transform (XPathNavigator input, XsltArgumentList args, XmlWriter output, XmlResolver resolver)
157                 {
158                         if (s == null)
159                                 throw new XsltException ("No stylesheet was loaded.", null);
160
161                         Outputter outputter = new GenericOutputter (output, s.Outputs, null);
162                         new XslTransformProcessor (s, debugger).Process (input, outputter, args, resolver);
163                         output.Flush ();
164                 }
165
166                 void Transform (XPathNavigator input, XsltArgumentList args, Stream output)
167                 {
168                         XslOutput xslOutput = (XslOutput)s.Outputs[String.Empty];
169                         Transform (input, args, new StreamWriter (output, xslOutput.Encoding));
170                 }
171
172                 void Transform (XPathNavigator input, XsltArgumentList args, TextWriter output)
173                 {
174                         if (s == null)
175                                 throw new XsltException ("No stylesheet was loaded.", null);
176
177                         Outputter outputter = new GenericOutputter(output, s.Outputs, output.Encoding);
178                         new XslTransformProcessor (s, debugger).Process (input, outputter, args, null);
179                         outputter.Done ();
180                         output.Flush ();
181                 }
182
183                 #endregion
184
185                 #region Load
186
187                 private XmlReader GetXmlReader (string url)
188                 {
189                         XmlResolver res = new XmlUrlResolver ();
190                         Uri uri = res.ResolveUri (null, url);
191                         Stream s = res.GetEntity (uri, null, typeof (Stream)) as Stream;
192                         XmlTextReader xtr = new XmlTextReader (uri.ToString (), s);
193                         xtr.XmlResolver = res;
194                         XmlValidatingReader xvr = new XmlValidatingReader (xtr);
195                         xvr.XmlResolver = res;
196                         xvr.ValidationType = ValidationType.None;
197                         return xvr;
198                 }
199
200                 public void Load (string url)
201                 {
202                         using (XmlReader r = GetXmlReader (url)) {
203                                 Load (r);
204                         }
205                 }
206
207                 public void Load (XmlReader stylesheet)
208                 {
209                         Load (stylesheet, null, null);
210                 }
211
212                 public void Load (IXPathNavigable stylesheet)
213                 {
214                         Load (stylesheet.CreateNavigator(), null, null);
215                 }
216
217                 public void Load (IXPathNavigable stylesheet, XsltSettings settings, XmlResolver resolver)
218                 {
219                         Load (stylesheet.CreateNavigator(), settings, resolver);
220                 }
221
222                 public void Load (XmlReader stylesheet, XsltSettings settings, XmlResolver resolver)
223                 {
224                         Load (new XPathDocument (stylesheet, XmlSpace.Preserve).CreateNavigator (), settings, resolver);
225                 }
226
227                 public void Load (string stylesheet, XsltSettings settings, XmlResolver resolver)
228                 {
229                         Load (new XPathDocument (stylesheet, XmlSpace.Preserve).CreateNavigator (), settings, resolver);
230                 }
231
232                 private void Load (XPathNavigator stylesheet,
233                         XsltSettings settings, XmlResolver resolver)
234                 {
235                         s = new Compiler (debugger).Compile (stylesheet, resolver, null);
236                 }
237
238                 #endregion
239         }
240
241                 class NoOperationDebugger
242                 {
243                         protected void OnCompile (XPathNavigator input)
244                         {
245                         }
246
247                         protected void OnExecute (XPathNodeIterator currentNodeset, XPathNavigator style, XsltContext context)
248                         {
249                                 //ShowLocationInTrace (style);
250                         }
251 /*
252                         string ShowLocationInTrace (XPathNavigator style)
253                         {
254                                 IXmlLineInfo li = style as IXmlLineInfo;
255                                 return li != null ? String.Format ("at {0} ({1},{2})", style.BaseURI, li.LineNumber, li.LinePosition) : "(no debug info available)";
256                         }
257 */
258                 }
259 }
260 #endif