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