XslCompiledTransform.cs 6.6 KB

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