2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.XML / System.Xml.Xsl / XslTransform.cs
index b911afa07de984e6485c10363742b12b7ea52fd2..1aead131cd88827b24322efa8e3f17af48c56ca1 100644 (file)
@@ -1,16 +1,41 @@
-// System.Xml.Xsl.XslTransform\r
+// UnmanagedXslTransform\r
 //\r
 // Authors:\r
 //     Tim Coleman <tim@timcoleman.com>\r
 //     Gonzalo Paniagua Javier (gonzalo@ximian.com)\r
+//     Ben Maurer (bmaurer@users.sourceforge.net)\r
 //\r
 // (C) Copyright 2002 Tim Coleman\r
 // (c) 2003 Ximian Inc. (http://www.ximian.com)\r
+// (C) Ben Maurer 2003\r
 //\r
-\r
+
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+\r
+// DO NOT MOVE THIS FILE. WE WANT HISTORY\r
 using System;\r
 using System.Collections;\r
 using System.IO;\r
+using System.Security.Policy;\r
 using System.Text;\r
 using System.Runtime.InteropServices;\r
 using System.Xml.XPath;\r
@@ -19,36 +44,27 @@ using BF = System.Reflection.BindingFlags;
 \r
 namespace System.Xml.Xsl\r
 {\r
-       public unsafe sealed class XslTransform\r
+       internal unsafe sealed class UnmanagedXslTransform : XslTransformImpl\r
        {\r
 \r
                #region Fields\r
-\r
-               XmlResolver xmlResolver;\r
+               \r
                IntPtr stylesheet;\r
                Hashtable extensionObjectCache = new Hashtable();\r
 \r
                #endregion\r
 \r
                #region Constructors\r
-               public XslTransform ()\r
+               public UnmanagedXslTransform ()\r
                {\r
                        stylesheet = IntPtr.Zero;\r
                }\r
 \r
                #endregion\r
 \r
-               #region Properties\r
-\r
-               public XmlResolver XmlResolver {\r
-                       set { xmlResolver = value; }\r
-               }\r
-\r
-               #endregion\r
-\r
                #region Methods\r
 \r
-               ~XslTransform ()\r
+               ~UnmanagedXslTransform ()\r
                {\r
                        FreeStylesheetIfNeeded ();\r
                }\r
@@ -60,47 +76,17 @@ namespace System.Xml.Xsl
                                stylesheet = IntPtr.Zero;\r
                        }\r
                }\r
-               \r
-               // Loads the XSLT stylesheet contained in the IXPathNavigable.\r
-               public void Load (IXPathNavigable stylesheet)\r
-               {\r
-                       Load (stylesheet.CreateNavigator ());\r
-               }\r
 \r
-               // Loads the XSLT stylesheet specified by a URL.\r
-               public void Load (string url)\r
+               public override void Load (string url, XmlResolver resolver)\r
                {\r
-                       if (url == null)\r
-                               throw new ArgumentNullException ("url");\r
-\r
                        FreeStylesheetIfNeeded ();\r
                        stylesheet = xsltParseStylesheetFile (url);\r
                        Cleanup ();\r
                        if (stylesheet == IntPtr.Zero)\r
                                throw new XmlException ("Error creating stylesheet");\r
                }\r
-\r
-               static IntPtr GetStylesheetFromString (string xml)\r
-               {\r
-                       IntPtr result = IntPtr.Zero;\r
-\r
-                       IntPtr xmlDoc = xmlParseDoc (xml);\r
-\r
-                       if (xmlDoc == IntPtr.Zero) {\r
-                               Cleanup ();\r
-                               throw new XmlException ("Error parsing stylesheet");\r
-                       }\r
-                               \r
-                       result = xsltParseStylesheetDoc (xmlDoc);\r
-                       Cleanup ();\r
-                       if (result == IntPtr.Zero)\r
-                               throw new XmlException ("Error creating stylesheet");\r
-\r
-                       return result;\r
-               }\r
-\r
-               // Loads the XSLT stylesheet contained in the XmlReader\r
-               public void Load (XmlReader stylesheet)\r
+               \r
+               public override void Load (XmlReader stylesheet, XmlResolver resolver, Evidence evidence)\r
                {\r
                        FreeStylesheetIfNeeded ();\r
                        // Create a document for the stylesheet\r
@@ -118,8 +104,7 @@ namespace System.Xml.Xsl
                                throw new XmlException ("Error creating stylesheet");\r
                }\r
 \r
-               // Loads the XSLT stylesheet contained in the XPathNavigator\r
-               public void Load (XPathNavigator stylesheet)\r
+               public override void Load (XPathNavigator stylesheet, XmlResolver resolver, Evidence evidence)\r
                {\r
                        FreeStylesheetIfNeeded ();\r
                        StringWriter sr = new UTF8StringWriter ();\r
@@ -129,76 +114,24 @@ namespace System.Xml.Xsl
                        if (this.stylesheet == IntPtr.Zero)\r
                                throw new XmlException ("Error creating stylesheet");\r
                }\r
-\r
-               [MonoTODO("use the resolver")]\r
-               // Loads the XSLT stylesheet contained in the IXPathNavigable.\r
-               public void Load (IXPathNavigable stylesheet, XmlResolver resolver)\r
-               {\r
-                       Load (stylesheet);\r
-               }\r
-\r
-               [MonoTODO("use the resolver")]\r
-               // Loads the XSLT stylesheet specified by a URL.\r
-               public void Load (string url, XmlResolver resolver)\r
-               {\r
-                       Load (url);\r
-               }\r
-\r
-               [MonoTODO("use the resolver")]\r
-               // Loads the XSLT stylesheet contained in the XmlReader\r
-               public void Load (XmlReader stylesheet, XmlResolver resolver)\r
-               {\r
-                       Load (stylesheet);\r
-               }\r
-\r
-               [MonoTODO("use the resolver")]\r
-               // Loads the XSLT stylesheet contained in the XPathNavigator\r
-               public void Load (XPathNavigator stylesheet, XmlResolver resolver)\r
-               {\r
-                       Load (stylesheet);\r
-               }\r
-\r
-               // Transforms the XML data in the IXPathNavigable using\r
-               // the specified args and outputs the result to an XmlReader.\r
-               public XmlReader Transform (IXPathNavigable input, XsltArgumentList args)\r
-               {\r
-                       if (input == null)\r
-                               throw new ArgumentNullException ("input");\r
-\r
-                       return Transform (input.CreateNavigator (), args);\r
-               }\r
-\r
-               // Transforms the XML data in the input file and outputs\r
-               // the result to an output file.\r
-               public void Transform (string inputfile, string outputfile)\r
+               \r
+               static IntPtr GetStylesheetFromString (string xml)\r
                {\r
-                       IntPtr xmlDocument = IntPtr.Zero;\r
-                       IntPtr resultDocument = IntPtr.Zero;\r
-\r
-                       try {\r
-                               xmlDocument = xmlParseFile (inputfile);\r
-                               if (xmlDocument == IntPtr.Zero)\r
-                                       throw new XmlException ("Error parsing input file");\r
-\r
-                               resultDocument = ApplyStylesheet (xmlDocument, null, null);\r
-\r
-                               /*\r
-                                * If I do this, the <?xml version=... is always present *\r
-                               if (-1 == xsltSaveResultToFilename (outputfile, resultDocument, stylesheet, 0))\r
-                                       throw new XmlException ("Error xsltSaveResultToFilename");\r
-                               */\r
-                               StreamWriter writer = new StreamWriter (new FileStream (outputfile, FileMode.Create));\r
-                               writer.Write (GetStringFromDocument (resultDocument, stylesheet));\r
-                               writer.Close ();\r
-                       } finally {\r
-                               if (xmlDocument != IntPtr.Zero)\r
-                                       xmlFreeDoc (xmlDocument);\r
+                       IntPtr result = IntPtr.Zero;\r
 \r
-                               if (resultDocument != IntPtr.Zero)\r
-                                       xmlFreeDoc (resultDocument);\r
+                       IntPtr xmlDoc = xmlParseDoc (xml);\r
 \r
+                       if (xmlDoc == IntPtr.Zero) {\r
                                Cleanup ();\r
+                               throw new XmlException ("Error parsing stylesheet");\r
                        }\r
+                               \r
+                       result = xsltParseStylesheetDoc (xmlDoc);\r
+                       Cleanup ();\r
+                       if (result == IntPtr.Zero)\r
+                               throw new XmlException ("Error creating stylesheet");\r
+\r
+                       return result;\r
                }\r
 \r
                IntPtr ApplyStylesheet (IntPtr doc, string[] argArr, Hashtable extobjects)\r
@@ -290,8 +223,8 @@ namespace System.Xml.Xsl
 \r
                static void Cleanup ()\r
                {\r
-                       xsltCleanupGlobals ();\r
-                       xmlCleanupParser ();\r
+                       //xsltCleanupGlobals ();\r
+                       //xmlCleanupParser ();\r
                }\r
 \r
                static string GetStringFromDocument (IntPtr doc, IntPtr stylesheet)\r
@@ -311,7 +244,9 @@ namespace System.Xml.Xsl
 \r
                string ApplyStylesheetAndGetString (IntPtr doc, string[] argArr, Hashtable extobjects)\r
                {\r
-                       IntPtr xmlOutput = ApplyStylesheet (doc, argArr, extobjects);\r
+                       IntPtr xmlOutput = ApplyStylesheet (doc,\r
+                               argArr == null ? new string [0] : argArr, \r
+                               extobjects == null ? new Hashtable () : extobjects);\r
                        string strOutput = GetStringFromDocument (xmlOutput, stylesheet);\r
                        xmlFreeDoc (xmlOutput);\r
 \r
@@ -329,78 +264,7 @@ namespace System.Xml.Xsl
                        return xmlInput;\r
                }\r
 \r
-               [MonoTODO("Node Set and Node Fragment Parameters and Extension Objects")]\r
-               // Transforms the XML data in the XPathNavigator using\r
-               // the specified args and outputs the result to an XmlReader.\r
-               public XmlReader Transform (XPathNavigator input, XsltArgumentList args)\r
-               {\r
-                       IntPtr xmlInput = GetDocumentFromNavigator (input);\r
-                       string[] argArr = null;\r
-                       Hashtable extensionObjects = null;\r
-                       if (args != null) {\r
-                               extensionObjects = args.extensionObjects;\r
-                               argArr = new string[args.parameters.Count * 2 + 1];\r
-                               int index = 0;\r
-                               foreach (object key in args.parameters.Keys) {\r
-                                       argArr [index++] = key.ToString();\r
-                                       object value = args.parameters [key];\r
-                                       if (value is Boolean)\r
-                                               argArr [index++] = XmlConvert.ToString((bool) value); // FIXME: How to encode it for libxslt?\r
-                                       else if (value is Double)\r
-                                               argArr [index++] = XmlConvert.ToString((double) value); // FIXME: How to encode infinity's and Nan?\r
-                                       else\r
-                                               argArr [index++] = "'" + value.ToString() + "'"; // FIXME: How to encode "'"?\r
-                               }\r
-                               argArr[index] = null;\r
-                       }\r
-                       string xslOutputString = ApplyStylesheetAndGetString (xmlInput, argArr, extensionObjects);\r
-                       xmlFreeDoc (xmlInput);\r
-                       Cleanup ();\r
-\r
-                       return new XmlTextReader (new StringReader (xslOutputString));\r
-               }\r
-\r
-               // Transforms the XML data in the IXPathNavigable using\r
-               // the specified args and outputs the result to a Stream.\r
-               public void Transform (IXPathNavigable input, XsltArgumentList args, Stream output)\r
-               {\r
-                       if (input == null)\r
-                               throw new ArgumentNullException ("input");\r
-\r
-                       Transform (input.CreateNavigator (), args, new StreamWriter (output));\r
-               }\r
-\r
-               // Transforms the XML data in the IXPathNavigable using\r
-               // the specified args and outputs the result to a TextWriter.\r
-               public void Transform (IXPathNavigable input, XsltArgumentList args, TextWriter output)\r
-               {\r
-                       if (input == null)\r
-                               throw new ArgumentNullException ("input");\r
-\r
-                       Transform (input.CreateNavigator (), args, output);\r
-               }\r
-\r
-               // Transforms the XML data in the IXPathNavigable using\r
-               // the specified args and outputs the result to an XmlWriter.\r
-               public void Transform (IXPathNavigable input, XsltArgumentList args, XmlWriter output)\r
-               {\r
-                       if (input == null)\r
-                               throw new ArgumentNullException ("input");\r
-\r
-                       Transform (input.CreateNavigator (), args, output);\r
-               }\r
-\r
-               // Transforms the XML data in the XPathNavigator using\r
-               // the specified args and outputs the result to a Stream.\r
-               public void Transform (XPathNavigator input, XsltArgumentList args, Stream output)\r
-               {\r
-                       Transform (input, args, new StreamWriter (output));\r
-               }\r
-\r
-               // Transforms the XML data in the XPathNavigator using\r
-               // the specified args and outputs the result to a TextWriter.\r
-               [MonoTODO("Node Set and Node Fragment Parameters and Extension Objects")]\r
-               public void Transform (XPathNavigator input, XsltArgumentList args, TextWriter output)\r
+               public override void Transform (XPathNavigator input, XsltArgumentList args, XmlWriter output, XmlResolver resolver)\r
                {\r
                        if (input == null)\r
                                throw new ArgumentNullException ("input");\r
@@ -408,6 +272,8 @@ namespace System.Xml.Xsl
                        if (output == null)\r
                                throw new ArgumentNullException ("output");\r
 \r
+                       StringWriter writer = new UTF8StringWriter ();\r
+                       \r
                        IntPtr inputDoc = GetDocumentFromNavigator (input);\r
                        string[] argArr = null;\r
                        Hashtable extensionObjects = null;\r
@@ -430,20 +296,44 @@ namespace System.Xml.Xsl
                        string transform = ApplyStylesheetAndGetString (inputDoc, argArr, extensionObjects);\r
                        xmlFreeDoc (inputDoc);\r
                        Cleanup ();\r
-                       output.Write (transform);\r
+                       writer.Write (transform);\r
+                       writer.Flush ();\r
+                       \r
+                       output.WriteRaw (writer.GetStringBuilder ().ToString ());\r
                        output.Flush ();\r
                }\r
+               public override void Transform (XPathNavigator input, XsltArgumentList args, TextWriter output, XmlResolver resolver) \r
+               {\r
+                       Transform(input, args, new XmlTextWriter(output), resolver);\r
+               }\r
 \r
-               // Transforms the XML data in the XPathNavigator using\r
-               // the specified args and outputs the result to an XmlWriter.\r
-               public void Transform (XPathNavigator input, XsltArgumentList args, XmlWriter output)\r
+               public override void Transform(string inputfile, string outputfile, XmlResolver resolver)\r
                {\r
-                       StringWriter writer = new UTF8StringWriter ();\r
-                       Transform (input, args, writer);\r
-                       output.WriteRaw (writer.GetStringBuilder ().ToString ());\r
-                       output.Flush ();\r
+                       IntPtr xmlDocument = IntPtr.Zero;\r
+                       IntPtr resultDocument = IntPtr.Zero;\r
+\r
+                       try {\r
+                               xmlDocument = xmlParseFile (inputfile);\r
+                               if (xmlDocument == IntPtr.Zero)\r
+                                       throw new XmlException ("Error parsing input file");\r
+\r
+                               resultDocument = ApplyStylesheet (xmlDocument, null, null);\r
+\r
+                               if (-1 == xsltSaveResultToFilename (outputfile, resultDocument, stylesheet, 0))\r
+                                       throw new XmlException ("Error in xsltSaveResultToFilename");\r
+                       } finally {\r
+                               if (xmlDocument != IntPtr.Zero)\r
+                                       xmlFreeDoc (xmlDocument);\r
+\r
+                               if (resultDocument != IntPtr.Zero)\r
+                                       xmlFreeDoc (resultDocument);\r
+\r
+                               Cleanup ();\r
+                       }\r
                }\r
 \r
+\r
+\r
                static void Save (XmlReader rdr, TextWriter baseWriter)\r
                {\r
                        XmlTextWriter writer = new XmlTextWriter (baseWriter);\r
@@ -509,6 +399,14 @@ namespace System.Xml.Xsl
                {\r
                        WriteCurrentNode (navigator, writer);\r
 \r
+                       if (navigator.MoveToFirstNamespace (XPathNamespaceScope.Local)) {\r
+                               do {\r
+                                       WriteCurrentNode (navigator, writer);\r
+                               } while (navigator.MoveToNextNamespace (XPathNamespaceScope.Local));\r
+\r
+                               navigator.MoveToParent ();\r
+                       }\r
+\r
                        if (navigator.MoveToFirstAttribute ()) {\r
                                do {\r
                                        WriteCurrentNode (navigator, writer);\r
@@ -537,6 +435,15 @@ namespace System.Xml.Xsl
                        case XPathNodeType.Root:\r
                                writer.WriteStartDocument ();\r
                                break;\r
+                       case XPathNodeType.Namespace:\r
+                               if (navigator.Name == String.Empty)\r
+                                       writer.WriteAttributeString ("xmlns", navigator.Value);\r
+                               else\r
+                                       writer.WriteAttributeString ("xmlns",\r
+                                               navigator.Name,\r
+                                               "http://www.w3.org/2000/xmlns/",\r
+                                               navigator.Value);\r
+                               break;\r
                        case XPathNodeType.Attribute:\r
                                writer.WriteAttributeString (navigator.Name, navigator.Value);\r
                                break;\r