XslCompiledTransform.cs 7.6 KB

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