XslTransform.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // System.Xml.Xsl.XslTransform
  2. //
  3. // Author: Tim Coleman <[email protected]>
  4. // (C) Copyright 2002 Tim Coleman
  5. using System;
  6. using System.Xml.XPath;
  7. using System.IO;
  8. using System.Text;
  9. using System.Runtime.InteropServices;
  10. namespace System.Xml.Xsl
  11. {
  12. public sealed class XslTransform
  13. {
  14. #region Fields
  15. XmlResolver xmlResolver;
  16. string stylesheet_file;
  17. #endregion
  18. #region Constructors
  19. public XslTransform ()
  20. {
  21. stylesheet_file = String.Empty;
  22. }
  23. #endregion
  24. #region Properties
  25. public XmlResolver XmlResolver {
  26. set { xmlResolver = value; }
  27. }
  28. #endregion
  29. #region Methods
  30. // Loads the XSLT stylesheet contained in the IXPathNavigable.
  31. public void Load (IXPathNavigable stylesheet)
  32. {
  33. Load (stylesheet.CreateNavigator ());
  34. }
  35. // Loads the XSLT stylesheet specified by a URL.
  36. public void Load (string url)
  37. {
  38. stylesheet_file = url;
  39. }
  40. // Loads the XSLT stylesheet contained in the XmlReader
  41. public void Load (XmlReader stylesheet)
  42. {
  43. stylesheet_file = Path.GetTempFileName ();
  44. Save (stylesheet, stylesheet_file);
  45. }
  46. // Loads the XSLT stylesheet contained in the XPathNavigator
  47. public void Load (XPathNavigator stylesheet)
  48. {
  49. stylesheet_file = Path.GetTempFileName ();
  50. Save (stylesheet, stylesheet_file);
  51. }
  52. [MonoTODO]
  53. // Loads the XSLT stylesheet contained in the IXPathNavigable.
  54. public void Load (IXPathNavigable stylesheet, XmlResolver resolver)
  55. {
  56. throw new NotImplementedException ();
  57. }
  58. [MonoTODO]
  59. // Loads the XSLT stylesheet specified by a URL.
  60. public void Load (string url, XmlResolver resolver)
  61. {
  62. throw new NotImplementedException ();
  63. }
  64. [MonoTODO]
  65. // Loads the XSLT stylesheet contained in the XmlReader
  66. public void Load (XmlReader stylesheet, XmlResolver resolver)
  67. {
  68. throw new NotImplementedException ();
  69. }
  70. [MonoTODO]
  71. // Loads the XSLT stylesheet contained in the XPathNavigator
  72. public void Load (XPathNavigator stylesheet, XmlResolver resolver)
  73. {
  74. throw new NotImplementedException ();
  75. }
  76. [DllImport ("libxslt.so")]
  77. static extern IntPtr xsltParseStylesheetFile (string filename);
  78. [DllImport ("libxslt.so")]
  79. static extern IntPtr xsltApplyStylesheet (IntPtr stylePtr, IntPtr DocPtr, string [] parameters);
  80. [DllImport ("libxslt.so")]
  81. static extern IntPtr xmlNewDoc (string version);
  82. [DllImport ("libxslt.so")]
  83. static extern IntPtr xmlParseFile (string filename);
  84. [DllImport ("libxslt.so")]
  85. static extern int xmlSaveFile (string filename, IntPtr cur);
  86. [MonoTODO]
  87. // Transforms the XML data in the IXPathNavigable using
  88. // the specified args and outputs the result to an XmlReader.
  89. public XmlReader Transform (IXPathNavigable input, XsltArgumentList args)
  90. {
  91. throw new NotImplementedException ();
  92. }
  93. [MonoTODO]
  94. // Transforms the XML data in the input file and outputs
  95. // the result to an output file.
  96. public void Transform (string inputfile, string outputfile)
  97. {
  98. IntPtr xmlDocument = xmlParseFile (inputfile);
  99. IntPtr xmlStylesheet = xsltParseStylesheetFile (stylesheet_file);
  100. IntPtr xmlOutput = xmlNewDoc ("1.0");
  101. string [] parameters = new string [] {};
  102. xmlOutput = xsltApplyStylesheet (xmlStylesheet, xmlDocument, parameters);
  103. xmlSaveFile (outputfile, xmlOutput);
  104. }
  105. [MonoTODO]
  106. // Transforms the XML data in the XPathNavigator using
  107. // the specified args and outputs the result to an XmlReader.
  108. public XmlReader Transform (XPathNavigator input, XsltArgumentList args)
  109. {
  110. throw new NotImplementedException ();
  111. }
  112. [MonoTODO]
  113. // Transforms the XML data in the IXPathNavigable using
  114. // the specified args and outputs the result to a Stream.
  115. public void Transform (IXPathNavigable input, XsltArgumentList args, Stream output)
  116. {
  117. throw new NotImplementedException ();
  118. }
  119. [MonoTODO]
  120. // Transforms the XML data in the IXPathNavigable using
  121. // the specified args and outputs the result to a TextWriter.
  122. public void Transform (IXPathNavigable input, XsltArgumentList args, TextWriter output)
  123. {
  124. throw new NotImplementedException ();
  125. }
  126. [MonoTODO]
  127. // Transforms the XML data in the IXPathNavigable using
  128. // the specified args and outputs the result to an XmlWriter.
  129. public void Transform (IXPathNavigable input, XsltArgumentList args, XmlWriter output)
  130. {
  131. throw new NotImplementedException ();
  132. }
  133. [MonoTODO]
  134. // Transforms the XML data in the XPathNavigator using
  135. // the specified args and outputs the result to a Stream.
  136. public void Transform (XPathNavigator input, XsltArgumentList args, Stream output)
  137. {
  138. throw new NotImplementedException ();
  139. }
  140. [MonoTODO]
  141. // Transforms the XML data in the XPathNavigator using
  142. // the specified args and outputs the result to a TextWriter.
  143. public void Transform (XPathNavigator input, XsltArgumentList args, TextWriter output)
  144. {
  145. throw new NotImplementedException ();
  146. }
  147. [MonoTODO]
  148. // Transforms the XML data in the XPathNavigator using
  149. // the specified args and outputs the result to an XmlWriter.
  150. public void Transform (XPathNavigator input, XsltArgumentList args, XmlWriter output)
  151. {
  152. throw new NotImplementedException ();
  153. }
  154. static void Save (XmlReader rdr, string filename)
  155. {
  156. XmlTextWriter writer = new XmlTextWriter (filename, new UTF8Encoding ());
  157. while (rdr.Read ()) {
  158. switch (rdr.NodeType) {
  159. case XmlNodeType.CDATA:
  160. writer.WriteCData (rdr.Value);
  161. break;
  162. case XmlNodeType.Comment:
  163. writer.WriteComment (rdr.Value);
  164. break;
  165. case XmlNodeType.DocumentType:
  166. writer.WriteDocType (rdr.Value, null, null, null);
  167. break;
  168. case XmlNodeType.Element:
  169. writer.WriteStartElement (rdr.Name, rdr.Value);
  170. while (rdr.MoveToNextAttribute ())
  171. writer.WriteAttributes (rdr, true);
  172. break;
  173. case XmlNodeType.EndElement:
  174. writer.WriteEndElement ();
  175. break;
  176. case XmlNodeType.ProcessingInstruction:
  177. writer.WriteProcessingInstruction (rdr.Name, rdr.Value);
  178. break;
  179. case XmlNodeType.Text:
  180. writer.WriteString (rdr.Value);
  181. break;
  182. case XmlNodeType.Whitespace:
  183. writer.WriteWhitespace (rdr.Value);
  184. break;
  185. case XmlNodeType.XmlDeclaration:
  186. writer.WriteStartDocument ();
  187. break;
  188. }
  189. }
  190. writer.Close ();
  191. }
  192. static void Save (XPathNavigator navigator, string filename)
  193. {
  194. XmlTextWriter writer = new XmlTextWriter (filename, new UTF8Encoding ());
  195. XPathNodeType type = XPathNodeType.All;
  196. WriteTree (navigator, writer, type);
  197. }
  198. // Walks the XPathNavigator tree recursively
  199. static void WriteTree (XPathNavigator navigator, XmlTextWriter writer, XPathNodeType type)
  200. {
  201. WriteCurrentNode (navigator, writer, ref type);
  202. if (navigator.HasAttributes) {
  203. navigator.MoveToFirstAttribute ();
  204. do {
  205. WriteCurrentNode (navigator, writer, ref type);
  206. } while ( navigator.MoveToNextAttribute ());
  207. navigator.MoveToParent ();
  208. }
  209. if (navigator.HasChildren) {
  210. navigator.MoveToFirstChild ();
  211. do {
  212. WriteTree (navigator, writer, type);
  213. } while (navigator.MoveToNext ());
  214. navigator.MoveToParent ();
  215. }
  216. }
  217. // Format the output
  218. static void WriteCurrentNode (XPathNavigator navigator, XmlTextWriter writer, ref XPathNodeType current_type)
  219. {
  220. switch (navigator.NodeType) {
  221. case XPathNodeType.Attribute:
  222. current_type = XPathNodeType.Attribute;
  223. writer.WriteAttributeString (navigator.LocalName, navigator.Value);
  224. break;
  225. case XPathNodeType.Comment:
  226. writer.WriteComment (navigator.Value);
  227. break;
  228. case XPathNodeType.Element:
  229. current_type = XPathNodeType.Element;
  230. writer.WriteStartElement (navigator.Name);
  231. break;
  232. case XPathNodeType.ProcessingInstruction:
  233. writer.WriteProcessingInstruction (navigator.Name, navigator.Value);
  234. break;
  235. case XPathNodeType.Text:
  236. writer.WriteString (navigator.Value);
  237. if (current_type == XPathNodeType.Element) {
  238. writer.WriteEndElement ();
  239. current_type = XPathNodeType.All;
  240. }
  241. break;
  242. case XPathNodeType.SignificantWhitespace:
  243. case XPathNodeType.Whitespace:
  244. writer.WriteWhitespace (navigator.Value);
  245. break;
  246. }
  247. }
  248. #endregion
  249. }
  250. }