Emit profiler enter/leave instrumentation calls as icalls.
[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 using System;
31 using System.CodeDom.Compiler;
32 using System.Collections;
33 using System.IO;
34 using System.Text;
35 using System.Runtime.InteropServices;
36 using System.Security;
37 using System.Security.Policy;
38 using System.Xml.XPath;
39 using Mono.Xml.Xsl;
40
41 namespace System.Xml.Xsl
42 {
43         [MonoTODO]
44         // FIXME: Of cource this is just a stub for now.
45         public sealed class XslCompiledTransform
46         {
47                 bool enable_debug;
48                 object debugger;
49                 CompiledStylesheet s;
50 #if !TARGET_JVM && !MOBILE
51 //              TempFileCollection temporary_files;
52 #endif
53                 XmlWriterSettings output_settings = new XmlWriterSettings ();
54
55                 public XslCompiledTransform ()
56                         : this (false)
57                 {
58                 }
59
60                 public XslCompiledTransform (bool enableDebug)
61                 {
62                         enable_debug = enableDebug;
63                         if (enable_debug)
64                                 debugger = new NoOperationDebugger ();
65                         output_settings.ConformanceLevel = ConformanceLevel.Fragment;
66                 }
67
68                 [MonoTODO]
69                 public XmlWriterSettings OutputSettings {
70                         get { return output_settings; }
71                 }
72
73 #if !TARGET_JVM && !MOBILE
74                 [MonoTODO]
75                 public TempFileCollection TemporaryFiles {
76                         get { return null; /*temporary_files;*/ }
77                 }
78 #endif
79
80                 #region Transform
81
82                 public void Transform (string inputUri, string resultsFile)
83                 {
84                         using (Stream outStream = File.Create (resultsFile)) {
85                                 Transform (new XPathDocument (inputUri, XmlSpace.Preserve), null, outStream);
86                         }
87                 }
88
89                 public void Transform (string inputUri, XmlWriter results)
90                 {
91                         Transform (inputUri, null, results);
92                 }
93
94                 public void Transform (string inputUri, XsltArgumentList arguments, Stream results)
95                 {
96                         Transform (new XPathDocument (inputUri, XmlSpace.Preserve), arguments, results);
97                 }
98
99                 public void Transform (string inputUri, XsltArgumentList arguments, TextWriter results)
100                 {
101                         Transform (new XPathDocument (inputUri, XmlSpace.Preserve), arguments, results);
102                 }
103
104                 public void Transform (string inputUri, XsltArgumentList arguments, XmlWriter results)
105                 {
106                         Transform (new XPathDocument (inputUri, XmlSpace.Preserve), arguments, results);
107                 }
108
109                 public void Transform (XmlReader input, XmlWriter results)
110                 {
111                         Transform (input, null, results);
112                 }
113
114                 public void Transform (XmlReader input, XsltArgumentList arguments, Stream results)
115                 {
116                         Transform (new XPathDocument (input, XmlSpace.Preserve), arguments, results);
117                 }
118
119                 public void Transform (XmlReader input, XsltArgumentList arguments, TextWriter results)
120                 {
121                         Transform (new XPathDocument (input, XmlSpace.Preserve), arguments, results);
122                 }
123
124                 public void Transform (XmlReader input, XsltArgumentList arguments, XmlWriter results)
125                 {
126                         Transform (input, arguments, results, null);
127                 }
128
129                 public void Transform (IXPathNavigable input, XsltArgumentList arguments, TextWriter results)
130                 {
131                         Transform (input.CreateNavigator (), arguments, results);
132                 }
133
134                 public void Transform (IXPathNavigable input, XsltArgumentList arguments, Stream results)
135                 {
136                         Transform (input.CreateNavigator (), arguments, results);
137                 }
138
139                 public void Transform (IXPathNavigable input, XmlWriter results)
140                 {
141                         Transform (input, null, results);
142                 }
143
144                 public void Transform (IXPathNavigable input, XsltArgumentList arguments, XmlWriter results)
145                 {
146                         Transform (input.CreateNavigator (), arguments, results, null);
147                 }
148
149                 public void Transform (XmlReader input, XsltArgumentList arguments, XmlWriter results, XmlResolver documentResolver)
150                 {
151                         Transform (new XPathDocument (input, XmlSpace.Preserve).CreateNavigator (), arguments, results, documentResolver);
152                 }
153
154                 void Transform (XPathNavigator input, XsltArgumentList args, XmlWriter output, XmlResolver resolver)
155                 {
156                         if (s == null)
157                                 throw new XsltException ("No stylesheet was loaded.", null);
158
159                         Outputter outputter = new GenericOutputter (output, s.Outputs, null);
160                         new XslTransformProcessor (s, debugger).Process (input, outputter, args, resolver);
161                         output.Flush ();
162                 }
163
164                 void Transform (XPathNavigator input, XsltArgumentList args, Stream output)
165                 {
166                         XslOutput xslOutput = (XslOutput)s.Outputs[String.Empty];
167                         Transform (input, args, new StreamWriter (output, xslOutput.Encoding));
168                 }
169
170                 void Transform (XPathNavigator input, XsltArgumentList args, TextWriter output)
171                 {
172                         if (s == null)
173                                 throw new XsltException ("No stylesheet was loaded.", null);
174
175                         Outputter outputter = new GenericOutputter(output, s.Outputs, output.Encoding);
176                         new XslTransformProcessor (s, debugger).Process (input, outputter, args, null);
177                         outputter.Done ();
178                         output.Flush ();
179                 }
180
181                 #endregion
182
183                 #region Load
184
185                 private XmlReader GetXmlReader (string url)
186                 {
187                         XmlResolver res = new XmlUrlResolver ();
188                         Uri uri = res.ResolveUri (null, url);
189                         Stream s = res.GetEntity (uri, null, typeof (Stream)) as Stream;
190                         XmlTextReader xtr = new XmlTextReader (uri.ToString (), s);
191                         xtr.XmlResolver = res;
192                         XmlValidatingReader xvr = new XmlValidatingReader (xtr);
193                         xvr.XmlResolver = res;
194                         xvr.ValidationType = ValidationType.None;
195                         return xvr;
196                 }
197
198                 public void Load (string stylesheetUri)
199                 {
200                         using (XmlReader r = GetXmlReader (stylesheetUri)) {
201                                 Load (r);
202                         }
203                 }
204
205                 public void Load (XmlReader stylesheet)
206                 {
207                         Load (stylesheet, null, null);
208                 }
209
210                 public void Load (IXPathNavigable stylesheet)
211                 {
212                         Load (stylesheet.CreateNavigator(), null, null);
213                 }
214
215                 public void Load (IXPathNavigable stylesheet, XsltSettings settings, XmlResolver stylesheetResolver)
216                 {
217                         Load (stylesheet.CreateNavigator(), settings, stylesheetResolver);
218                 }
219
220                 public void Load (XmlReader stylesheet, XsltSettings settings, XmlResolver stylesheetResolver)
221                 {
222                         Load (new XPathDocument (stylesheet, XmlSpace.Preserve).CreateNavigator (), settings, stylesheetResolver);
223                 }
224
225                 public void Load (string stylesheetUri, XsltSettings settings, XmlResolver stylesheetResolver)
226                 {
227                         Load (new XPathDocument (stylesheetUri, XmlSpace.Preserve).CreateNavigator (), settings, stylesheetResolver);
228                 }
229
230                 private void Load (XPathNavigator stylesheet,
231                         XsltSettings settings, XmlResolver stylesheetResolver)
232                 {
233                         s = new Compiler (debugger).Compile (stylesheet, stylesheetResolver, null);
234                 }
235
236                 #endregion
237         }
238
239                 class NoOperationDebugger
240                 {
241                         protected void OnCompile (XPathNavigator input)
242                         {
243                         }
244
245                         protected void OnExecute (XPathNodeIterator currentNodeset, XPathNavigator style, XsltContext context)
246                         {
247                                 //ShowLocationInTrace (style);
248                         }
249 /*
250                         string ShowLocationInTrace (XPathNavigator style)
251                         {
252                                 IXmlLineInfo li = style as IXmlLineInfo;
253                                 return li != null ? String.Format ("at {0} ({1},{2})", style.BaseURI, li.LineNumber, li.LinePosition) : "(no debug info available)";
254                         }
255 */
256                 }
257 }