XslCompiledTransform.cs 6.5 KB

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