XslCompiledTransform.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #if NET_2_0
  29. using System;
  30. using System.CodeDom.Compiler;
  31. using System.Collections;
  32. using System.IO;
  33. using System.Text;
  34. using System.Runtime.InteropServices;
  35. using System.Security;
  36. using System.Security.Policy;
  37. using System.Xml.XPath;
  38. using Mono.Xml.Xsl;
  39. namespace System.Xml.Xsl
  40. {
  41. [MonoTODO]
  42. // FIXME: Of cource this is just a stub for now.
  43. public sealed class XslCompiledTransform
  44. {
  45. bool enable_debug;
  46. CompiledStylesheet s;
  47. #if !TARGET_JVM
  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. }
  59. [MonoTODO]
  60. public XmlWriterSettings OutputSettings {
  61. get { return output_settings; }
  62. }
  63. #if !TARGET_JVM
  64. [MonoTODO]
  65. public TempFileCollection TemporaryFiles {
  66. get { return temporary_files; }
  67. }
  68. #endif
  69. #region Transform
  70. public void Transform (string inputfile, string outputfile)
  71. {
  72. using (XmlReader reader = XmlReader.Create (inputfile)) {
  73. using (XmlWriter writer = XmlWriter.Create (outputfile, output_settings)) {
  74. Transform (reader, writer);
  75. }
  76. }
  77. }
  78. public void Transform (string inputfile, XmlWriter output)
  79. {
  80. Transform (inputfile, null, output);
  81. }
  82. public void Transform (string inputfile, XsltArgumentList args, Stream output)
  83. {
  84. Transform (new XPathDocument (inputfile), args, output);
  85. }
  86. public void Transform (string inputfile, XsltArgumentList args, TextWriter output)
  87. {
  88. Transform (new XPathDocument (inputfile), args, output);
  89. }
  90. public void Transform (string inputfile, XsltArgumentList args, XmlWriter output)
  91. {
  92. Transform (new XPathDocument (inputfile), args, output);
  93. }
  94. public void Transform (XmlReader reader, XmlWriter output)
  95. {
  96. Transform (reader, null, output);
  97. }
  98. public void Transform (XmlReader reader, XsltArgumentList args, Stream output)
  99. {
  100. Transform (new XPathDocument (reader), args, output);
  101. }
  102. public void Transform (XmlReader reader, XsltArgumentList args, TextWriter output)
  103. {
  104. Transform (new XPathDocument (reader), args, output);
  105. }
  106. public void Transform (XmlReader reader, XsltArgumentList args, XmlWriter output)
  107. {
  108. Transform (reader, args, output, null);
  109. }
  110. public void Transform (IXPathNavigable input, XsltArgumentList args, TextWriter output)
  111. {
  112. Transform (input.CreateNavigator (), args, output);
  113. }
  114. public void Transform (IXPathNavigable input, XsltArgumentList args, Stream output)
  115. {
  116. Transform (input.CreateNavigator (), args, output);
  117. }
  118. public void Transform (IXPathNavigable input, XmlWriter output)
  119. {
  120. Transform (input, null, output);
  121. }
  122. public void Transform (IXPathNavigable input, XsltArgumentList args, XmlWriter output)
  123. {
  124. Transform (input.CreateNavigator (), args, output, null);
  125. }
  126. public void Transform (XmlReader input, XsltArgumentList args, XmlWriter output, XmlResolver resolver)
  127. {
  128. Transform (new XPathDocument (input).CreateNavigator (), args, output, resolver);
  129. }
  130. void Transform (XPathNavigator input, XsltArgumentList args, XmlWriter output, XmlResolver resolver)
  131. {
  132. if (s == null)
  133. throw new XsltException ("No stylesheet was loaded.", null);
  134. Outputter outputter = new GenericOutputter (output, s.Outputs, null);
  135. new XslTransformProcessor (s, null).Process (input, outputter, args, resolver);
  136. output.Flush ();
  137. }
  138. void Transform (XPathNavigator input, XsltArgumentList args, Stream output)
  139. {
  140. XslOutput xslOutput = (XslOutput)s.Outputs[String.Empty];
  141. Transform (input, args, new StreamWriter (output, xslOutput.Encoding));
  142. }
  143. void Transform (XPathNavigator input, XsltArgumentList args, TextWriter output)
  144. {
  145. if (s == null)
  146. throw new XsltException ("No stylesheet was loaded.", null);
  147. Outputter outputter = new GenericOutputter(output, s.Outputs, output.Encoding);
  148. new XslTransformProcessor (s, null).Process (input, outputter, args, null);
  149. outputter.Done ();
  150. output.Flush ();
  151. }
  152. #endregion
  153. #region Load
  154. private XmlReader GetXmlReader (string url)
  155. {
  156. XmlResolver res = new XmlUrlResolver ();
  157. Uri uri = res.ResolveUri (null, url);
  158. Stream s = res.GetEntity (uri, null, typeof (Stream)) as Stream;
  159. XmlTextReader xtr = new XmlTextReader (uri.ToString (), s);
  160. xtr.XmlResolver = res;
  161. XmlValidatingReader xvr = new XmlValidatingReader (xtr);
  162. xvr.XmlResolver = res;
  163. xvr.ValidationType = ValidationType.None;
  164. return xvr;
  165. }
  166. public void Load (string url)
  167. {
  168. using (XmlReader r = GetXmlReader (url)) {
  169. Load (r);
  170. }
  171. }
  172. public void Load (XmlReader stylesheet)
  173. {
  174. Load (stylesheet, null, null);
  175. }
  176. public void Load (IXPathNavigable stylesheet)
  177. {
  178. Load (stylesheet.CreateNavigator(), null, null);
  179. }
  180. public void Load (IXPathNavigable stylesheet, XsltSettings settings, XmlResolver resolver)
  181. {
  182. Load (stylesheet.CreateNavigator(), settings, resolver);
  183. }
  184. public void Load (XmlReader stylesheet, XsltSettings settings, XmlResolver resolver)
  185. {
  186. Load (new XPathDocument (stylesheet).CreateNavigator (), settings, resolver);
  187. }
  188. public void Load (string stylesheet, XsltSettings settings, XmlResolver resolver)
  189. {
  190. Load (new XPathDocument (stylesheet).CreateNavigator (), settings, resolver);
  191. }
  192. private void Load (XPathNavigator stylesheet,
  193. XsltSettings settings, XmlResolver resolver)
  194. {
  195. s = new Compiler (null).Compile (stylesheet, resolver, null);
  196. }
  197. #endregion
  198. }
  199. }
  200. #endif