예제 #1
0
        /// <summary>
        /// Apply an XSLT transform on a file to produce a resulting file
        /// </summary>
        /// <param name="sTransformName">full path name of the XSLT transform</param>
        /// <param name="parameterList">list of parameters to pass to the transform</param>
        /// <param name="sInputPath">full path of the input file</param>
        /// <param name="sOutputName">full path of the resulting output file</param>
        public static void TransformFileToFile(string sTransformName, XSLParameter[] parameterList, string sInputPath, string sOutputName)
        {
            #if UsingDotNetTransforms
            #if DEBUG
            DateTime start = DateTime.Now;
            Debug.WriteLine("\tStarting at: " + start.TimeOfDay.ToString());
            #endif
            TextWriter writer = null;
            XmlTextReader reader = null;
            try
            {
                // set up transform
                var transformer = new XslCompiledTransform();
                var settings = XsltSettings.TrustedXslt;
                var resolver = new XmlUrlResolver();
                resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;
                transformer.Load(sTransformName, XsltSettings.TrustedXslt, resolver);

                // add any parameters
                XsltArgumentList args;
                AddParameters(out args, parameterList);

                // setup output file
                writer = File.CreateText(sOutputName);

                // load input file
                reader = new XmlTextReader(sInputPath) {ProhibitDtd = false, EntityHandling = EntityHandling.ExpandEntities};

                // Apply transform
                transformer.Transform(reader, args, writer);
            }
            finally
            {
                if (writer != null)
                    writer.Close();
                if (reader != null)
                    reader.Close();
            }

            /*			TextWriter writer = null;
            try
            {
                // set up transform
                XslTransform transformer = new XslTransform();
                transformer.Load(sTransformName);

                // add any parameters
                XsltArgumentList args;
                AddParameters(out args, parameterList);

                // setup output file
                writer = File.CreateText(sOutputName);

                // load input file
            #if DEBUG
                DateTime beforeDom = DateTime.Now;
                Debug.WriteLine("\t\tBefore loading DOM, time is: " + beforeDom.TimeOfDay.ToString());
            #endif
                XPathDocument inputDOM = new XPathDocument(sInputPath);
            #if DEBUG
                DateTime afterDom = DateTime.Now;
                Debug.WriteLine("\t\tAfter  loading DOM, time is: " + afterDom.TimeOfDay.ToString());
                System.TimeSpan diffDom = afterDom.Subtract(beforeDom);
                Debug.WriteLine("\t\tDom load took " + diffDom.ToString());
            #endif
                // Apply transform
                transformer.Transform(inputDOM, args, writer, null);
            #if DEBUG
                DateTime afterXSLT = DateTime.Now;
                Debug.WriteLine("\t\tAfter    transform, time is: " + afterXSLT.TimeOfDay.ToString());
                System.TimeSpan diffXSLT = afterXSLT.Subtract(afterDom);
                Debug.WriteLine("\t\tTransform took " + diffXSLT.ToString());
            #endif
            }
            finally
            {
                if (writer != null)
                    writer.Close();
            } */
            #if DEBUG
            DateTime end = DateTime.Now;
            Debug.WriteLine("\tEnding at: " + end.TimeOfDay.ToString());
            System.TimeSpan diff = end.Subtract(start);
            Debug.WriteLine("\tProcess took: " + diff.ToString() + " " + sOutputName);
            #endif
            #else // not UsingDotNetTransforms
            //.Net framework XML transform is still slower than something like MSXML2
            // (this is so especially for transforms using xsl:key).
            try
            {
                bool fShowOutput = true;
                //if (sTransformName.Contains("PAWSSKHtmlMapper.xsl"))
                //    fShowOutput = true;
                MessageBox.Show("in transform file");
                MSXML2.XSLTemplate40Class xslt = new MSXML2.XSLTemplate40Class();
                MSXML2.FreeThreadedDOMDocument40Class xslDoc = new
                    MSXML2.FreeThreadedDOMDocument40Class();
                MSXML2.DOMDocument40Class xmlDoc = new MSXML2.DOMDocument40Class();
                MSXML2.IXSLProcessor xslProc;

                xslDoc.async = false;
                //if (fShowOutput)
                //MessageBox.Show("before load transform: " + sTransformName);
                xslDoc.load(sTransformName);
                xslt.stylesheet = xslDoc;
                xmlDoc.async = false;
                //if (fShowOutput)
                  //  MessageBox.Show("before load input path: " + sInputPath);
                xmlDoc.load(sInputPath);
                xslProc = xslt.createProcessor();
                xslProc.input = xmlDoc;
                //if (fShowOutput)
                //MessageBox.Show("before add parameters");
                AddParameters(parameterList, xslProc);

                if (fShowOutput)
                    MessageBox.Show("before transform file");
                xslProc.transform();
                if (fShowOutput)
                    MessageBox.Show("before write output to |" + sOutputName + "|");
                StreamWriter sr = File.CreateText(sOutputName);
                sr.Write(xslProc.output);
                sr.Close();
                if (fShowOutput)
                    MessageBox.Show("after write output to |" + sOutputName + "|");
            }
            catch (Exception exc)
            {
                MessageBox.Show("Exception caught in transform files: " + exc.Message + "inner: " + exc.InnerException.Message);
            }
            #endif // UsingDotNetTransforms
        }
예제 #2
0
        /// <summary>
        /// Apply an XSLT transform on a file to produce a resulting file
        /// </summary>
        /// <param name="sTransformName">full path name of the XSLT transform</param>
        /// <param name="parameterList">list of parameters to pass to the transform</param>
        /// <param name="sInputPath">full path of the input file</param>
        /// <param name="sOutputName">full path of the resulting output file</param>
        public static void TransformFileToFile(string sTransformName, XSLParameter[] parameterList, string sInputPath, string sOutputName)
        {
#if UsingDotNetTransforms
#if DEBUG
            DateTime start = DateTime.Now;
            Debug.WriteLine("\tStarting at: " + start.TimeOfDay.ToString());
#endif
            TextWriter    writer = null;
            XmlTextReader reader = null;
            try
            {
                // set up transform
                var transformer = new XslCompiledTransform();
                var settings    = XsltSettings.TrustedXslt;
                var resolver    = new XmlUrlResolver();
                resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;
                transformer.Load(sTransformName, XsltSettings.TrustedXslt, resolver);


                // add any parameters
                XsltArgumentList args;
                AddParameters(out args, parameterList);

                // setup output file
                writer = File.CreateText(sOutputName);

                // load input file
                reader = new XmlTextReader(sInputPath)
                {
                    ProhibitDtd = false, EntityHandling = EntityHandling.ExpandEntities
                };

                // Apply transform
                transformer.Transform(reader, args, writer);
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
                if (reader != null)
                {
                    reader.Close();
                }
            }

/*			TextWriter writer = null;
 *                      try
 *                      {
 *                              // set up transform
 *                              XslTransform transformer = new XslTransform();
 *                              transformer.Load(sTransformName);
 *
 *                              // add any parameters
 *                              XsltArgumentList args;
 *                              AddParameters(out args, parameterList);
 *
 *                              // setup output file
 *                              writer = File.CreateText(sOutputName);
 *
 *                              // load input file
 #if DEBUG
 *                              DateTime beforeDom = DateTime.Now;
 *                              Debug.WriteLine("\t\tBefore loading DOM, time is: " + beforeDom.TimeOfDay.ToString());
 #endif
 *                              XPathDocument inputDOM = new XPathDocument(sInputPath);
 #if DEBUG
 *                              DateTime afterDom = DateTime.Now;
 *                              Debug.WriteLine("\t\tAfter  loading DOM, time is: " + afterDom.TimeOfDay.ToString());
 *                              System.TimeSpan diffDom = afterDom.Subtract(beforeDom);
 *                              Debug.WriteLine("\t\tDom load took " + diffDom.ToString());
 #endif
 *                              // Apply transform
 *                              transformer.Transform(inputDOM, args, writer, null);
 #if DEBUG
 *                              DateTime afterXSLT = DateTime.Now;
 *                              Debug.WriteLine("\t\tAfter    transform, time is: " + afterXSLT.TimeOfDay.ToString());
 *                              System.TimeSpan diffXSLT = afterXSLT.Subtract(afterDom);
 *                              Debug.WriteLine("\t\tTransform took " + diffXSLT.ToString());
 #endif
 *                      }
 *                      finally
 *                      {
 *                              if (writer != null)
 *                                      writer.Close();
 *                      } */
#if DEBUG
            DateTime end = DateTime.Now;
            Debug.WriteLine("\tEnding at: " + end.TimeOfDay.ToString());
            System.TimeSpan diff = end.Subtract(start);
            Debug.WriteLine("\tProcess took: " + diff.ToString() + " " + sOutputName);
#endif
#else // not UsingDotNetTransforms
            //.Net framework XML transform is still slower than something like MSXML2
            // (this is so especially for transforms using xsl:key).
            try
            {
                bool fShowOutput = true;
                //if (sTransformName.Contains("PAWSSKHtmlMapper.xsl"))
                //    fShowOutput = true;
                MessageBox.Show("in transform file");
                MSXML2.XSLTemplate40Class             xslt   = new MSXML2.XSLTemplate40Class();
                MSXML2.FreeThreadedDOMDocument40Class xslDoc = new
                                                               MSXML2.FreeThreadedDOMDocument40Class();
                MSXML2.DOMDocument40Class xmlDoc = new MSXML2.DOMDocument40Class();
                MSXML2.IXSLProcessor      xslProc;

                xslDoc.async = false;
                //if (fShowOutput)
                //MessageBox.Show("before load transform: " + sTransformName);
                xslDoc.load(sTransformName);
                xslt.stylesheet = xslDoc;
                xmlDoc.async    = false;
                //if (fShowOutput)
                //  MessageBox.Show("before load input path: " + sInputPath);
                xmlDoc.load(sInputPath);
                xslProc       = xslt.createProcessor();
                xslProc.input = xmlDoc;
                //if (fShowOutput)
                //MessageBox.Show("before add parameters");
                AddParameters(parameterList, xslProc);

                if (fShowOutput)
                {
                    MessageBox.Show("before transform file");
                }
                xslProc.transform();
                if (fShowOutput)
                {
                    MessageBox.Show("before write output to |" + sOutputName + "|");
                }
                StreamWriter sr = File.CreateText(sOutputName);
                sr.Write(xslProc.output);
                sr.Close();
                if (fShowOutput)
                {
                    MessageBox.Show("after write output to |" + sOutputName + "|");
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show("Exception caught in transform files: " + exc.Message + "inner: " + exc.InnerException.Message);
            }
#endif // UsingDotNetTransforms
        }