XslCompiledTransform.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 (new XPathDocument (reader), args, output);
  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);
  120. }
  121. void Transform (XPathNavigator input, XsltArgumentList args, XmlWriter output)
  122. {
  123. if (s == null)
  124. throw new XsltException ("No stylesheet was loaded.", null);
  125. Outputter outputter = new GenericOutputter (output, s.Outputs, null);
  126. new XslTransformProcessor (s).Process (input, outputter, args, null);
  127. output.Flush ();
  128. }
  129. void Transform (XPathNavigator input, XsltArgumentList args, Stream output)
  130. {
  131. XslOutput xslOutput = (XslOutput)s.Outputs[String.Empty];
  132. Transform (input, args, new StreamWriter (output, xslOutput.Encoding));
  133. }
  134. void Transform (XPathNavigator input, XsltArgumentList args, TextWriter output)
  135. {
  136. if (s == null)
  137. throw new XsltException ("No stylesheet was loaded.", null);
  138. Outputter outputter = new GenericOutputter(output, s.Outputs, output.Encoding);
  139. new XslTransformProcessor (s).Process (input, outputter, args, null);
  140. outputter.Done ();
  141. output.Flush ();
  142. }
  143. #endregion
  144. #region Load
  145. private XmlReader GetXmlReader (string url)
  146. {
  147. XmlResolver res = new XmlUrlResolver ();
  148. Uri uri = res.ResolveUri (null, url);
  149. Stream s = res.GetEntity (uri, null, typeof (Stream)) as Stream;
  150. XmlTextReader xtr = new XmlTextReader (uri.ToString (), s);
  151. xtr.XmlResolver = res;
  152. XmlValidatingReader xvr = new XmlValidatingReader (xtr);
  153. xvr.XmlResolver = res;
  154. xvr.ValidationType = ValidationType.None;
  155. return xvr;
  156. }
  157. public void Load (string url)
  158. {
  159. using (XmlReader r = GetXmlReader (url)) {
  160. Load (r);
  161. }
  162. }
  163. public void Load (XmlReader stylesheet)
  164. {
  165. Load (stylesheet, null, null);
  166. }
  167. public void Load (IXPathNavigable stylesheet)
  168. {
  169. Load (stylesheet.CreateNavigator(), null, null);
  170. }
  171. public void Load (IXPathNavigable stylesheet, XsltSettings settings, XmlResolver resolver)
  172. {
  173. Load (stylesheet.CreateNavigator(), settings, resolver);
  174. }
  175. public void Load (XmlReader stylesheet, XsltSettings settings, XmlResolver resolver)
  176. {
  177. Load (new XPathDocument (stylesheet).CreateNavigator (), settings, resolver);
  178. }
  179. public void Load (string stylesheet, XsltSettings settings, XmlResolver resolver)
  180. {
  181. Load (new XPathDocument (stylesheet).CreateNavigator (), settings, resolver);
  182. }
  183. private void Load (XPathNavigator stylesheet,
  184. XsltSettings settings, XmlResolver resolver)
  185. {
  186. s = new Compiler ().Compile (stylesheet, resolver, null);
  187. }
  188. #endregion
  189. }
  190. }
  191. #endif