// System.Xml.Xsl.XslTransform // // Authors: // Tim Coleman // Gonzalo Paniagua Javier (gonzalo@ximian.com) // // (C) Copyright 2002 Tim Coleman // (c) 2003 Ximian Inc. (http://www.ximian.com) // using System; using System.Xml.XPath; using System.IO; using System.Text; using System.Runtime.InteropServices; namespace System.Xml.Xsl { public sealed class XslTransform { #region Fields XmlResolver xmlResolver; IntPtr stylesheet; #endregion #region Constructors public XslTransform () { stylesheet = IntPtr.Zero; } #endregion #region Properties public XmlResolver XmlResolver { set { xmlResolver = value; } } #endregion #region Methods ~XslTransform () { FreeStylesheetIfNeeded (); } void FreeStylesheetIfNeeded () { if (stylesheet != IntPtr.Zero) { xsltFreeStylesheet (stylesheet); stylesheet = IntPtr.Zero; } } // Loads the XSLT stylesheet contained in the IXPathNavigable. public void Load (IXPathNavigable stylesheet) { Load (stylesheet.CreateNavigator ()); } // Loads the XSLT stylesheet specified by a URL. public void Load (string url) { if (url == null) throw new ArgumentNullException ("url"); FreeStylesheetIfNeeded (); stylesheet = xsltParseStylesheetFile (url); Cleanup (); if (stylesheet == IntPtr.Zero) throw new XmlException ("Error creating stylesheet"); } static IntPtr GetStylesheetFromString (string xml) { IntPtr result = IntPtr.Zero; IntPtr xmlDoc = xmlParseDoc (xml); if (xmlDoc == IntPtr.Zero) { Cleanup (); throw new XmlException ("Error parsing stylesheet"); } result = xsltParseStylesheetDoc (xmlDoc); Cleanup (); if (result == IntPtr.Zero) throw new XmlException ("Error creating stylesheet"); return result; } // Loads the XSLT stylesheet contained in the XmlReader public void Load (XmlReader stylesheet) { FreeStylesheetIfNeeded (); // Create a document for the stylesheet XmlDocument doc = new XmlDocument (); doc.Load (stylesheet); // Store the XML in a StringBuilder StringWriter sr = new UTF8StringWriter (); XmlTextWriter writer = new XmlTextWriter (sr); doc.Save (writer); this.stylesheet = GetStylesheetFromString (sr.GetStringBuilder ().ToString ()); Cleanup (); if (this.stylesheet == IntPtr.Zero) throw new XmlException ("Error creating stylesheet"); } // Loads the XSLT stylesheet contained in the XPathNavigator public void Load (XPathNavigator stylesheet) { FreeStylesheetIfNeeded (); StringWriter sr = new UTF8StringWriter (); Save (stylesheet, sr); this.stylesheet = GetStylesheetFromString (sr.GetStringBuilder ().ToString ()); Cleanup (); if (this.stylesheet == IntPtr.Zero) throw new XmlException ("Error creating stylesheet"); } [MonoTODO("use the resolver")] // Loads the XSLT stylesheet contained in the IXPathNavigable. public void Load (IXPathNavigable stylesheet, XmlResolver resolver) { Load (stylesheet); } [MonoTODO("use the resolver")] // Loads the XSLT stylesheet specified by a URL. public void Load (string url, XmlResolver resolver) { Load (url); } [MonoTODO("use the resolver")] // Loads the XSLT stylesheet contained in the XmlReader public void Load (XmlReader stylesheet, XmlResolver resolver) { Load (stylesheet); } [MonoTODO("use the resolver")] // Loads the XSLT stylesheet contained in the XPathNavigator public void Load (XPathNavigator stylesheet, XmlResolver resolver) { Load (stylesheet); } // Transforms the XML data in the IXPathNavigable using // the specified args and outputs the result to an XmlReader. public XmlReader Transform (IXPathNavigable input, XsltArgumentList args) { if (input == null) throw new ArgumentNullException ("input"); return Transform (input.CreateNavigator (), args); } // Transforms the XML data in the input file and outputs // the result to an output file. public void Transform (string inputfile, string outputfile) { IntPtr xmlDocument = IntPtr.Zero; IntPtr resultDocument = IntPtr.Zero; try { xmlDocument = xmlParseFile (inputfile); if (xmlDocument == IntPtr.Zero) throw new XmlException ("Error parsing input file"); resultDocument = ApplyStylesheet (xmlDocument, null); /* * If I do this, the