/// <summary>
        /// Executes the XslTransform task.
        /// </summary>
        /// <returns>true if transformation succeeds.</returns>
        public override bool Execute()
        {
            XmlInput xmlinput;
            XsltInput xsltinput;
            ErrorUtilities.VerifyThrowArgumentNull(_outputPaths, "OutputPath");

            // Load XmlInput, XsltInput parameters
            try
            {
                xmlinput = new XmlInput(_xmlInputPaths, _xmlString);
                xsltinput = new XsltInput(_xsltFile, _xsltString, _xsltCompiledDll, Log);
            }
            catch (Exception e)
            {
                if (ExceptionHandling.IsCriticalException(e))
                {
                    throw;
                }

                Log.LogErrorWithCodeFromResources("XslTransform.ArgumentError", e.Message);
                return false;
            }

            // Check if OutputPath has same number of parameters as xmlInputPaths.
            if (_xmlInputPaths != null && _xmlInputPaths.Length != _outputPaths.Length)
            {
                Log.LogErrorWithCodeFromResources("General.TwoVectorsMustHaveSameLength", _outputPaths.Length, _xmlInputPaths.Length, "XmlContent", "XmlInputPaths");
                return false;
            }

            // Check if OutputPath has 1 parameter if xmlString is specified.
            if (_xmlString != null && _outputPaths.Length != 1)
            {
                Log.LogErrorWithCodeFromResources("General.TwoVectorsMustHaveSameLength", _outputPaths.Length, 1, "XmlContent", "OutputPaths");
                return false;
            }

            XsltArgumentList arguments;

            // Arguments parameters
            try
            {
                arguments = ProcessXsltArguments(_parameters);
            }
            catch (Exception e)
            {
                if (ExceptionHandling.IsCriticalException(e))
                {
                    throw;
                }

                Log.LogErrorWithCodeFromResources("XslTransform.XsltArgumentsError", e.Message);
                return false;
            }

            XslCompiledTransform xslct;

            // Load the XSLT
            try
            {
                xslct = xsltinput.LoadXslt(UseTrustedSettings);
            }
            catch (Exception e)
            {
                if (ExceptionHandling.IsCriticalException(e))
                {
                    throw;
                }

                Log.LogErrorWithCodeFromResources("XslTransform.XsltLoadError", e.Message);
                return false;
            }

            // Do the transformation.
            try
            {
                for (int i = 0; i < xmlinput.Count; i++)
                {
                    using (XmlWriter xmlWriter = XmlWriter.Create(_outputPaths[i].ItemSpec, xslct.OutputSettings))
                    {
                        using (XmlReader xr = xmlinput.CreateReader(i))
                        {
                            xslct.Transform(xr, arguments, xmlWriter);
                        }

                        xmlWriter.Close();
                    }
                }
            }
            catch (Exception e)
            {
                if (ExceptionHandling.IsCriticalException(e))
                {
                    throw;
                }

                Log.LogErrorWithCodeFromResources("XslTransform.TransformError", e.Message);
                return false;
            }

            // Copy Metadata
            if (xmlinput.XmlMode == XmlInput.XmlModes.XmlFile)
            {
                for (int i = 0; i < _xmlInputPaths.Length; i++)
                {
                    _xmlInputPaths[i].CopyMetadataTo(_outputPaths[i]);
                }
            }

            return true;
        }
 public override bool Execute()
 {
     XmlInput input;
     XsltInput input2;
     XsltArgumentList list;
     XslCompiledTransform transform;
     Microsoft.Build.Shared.ErrorUtilities.VerifyThrowArgumentNull(this.outputPaths, "OutputPath");
     try
     {
         input = new XmlInput(this.xmlInputPaths, this.xmlString);
         input2 = new XsltInput(this.xsltFile, this.xsltString, this.xsltCompiledDll);
     }
     catch (Exception exception)
     {
         if (Microsoft.Build.Shared.ExceptionHandling.IsCriticalException(exception))
         {
             throw;
         }
         base.Log.LogErrorWithCodeFromResources("XslTransform.ArgumentError", new object[] { exception.Message });
         return false;
     }
     if ((this.xmlInputPaths != null) && (this.xmlInputPaths.Length != this.outputPaths.Length))
     {
         base.Log.LogErrorWithCodeFromResources("General.TwoVectorsMustHaveSameLength", new object[] { this.outputPaths.Length, this.xmlInputPaths.Length, "XmlContent", "XmlInputPaths" });
         return false;
     }
     if ((this.xmlString != null) && (this.outputPaths.Length != 1))
     {
         base.Log.LogErrorWithCodeFromResources("General.TwoVectorsMustHaveSameLength", new object[] { this.outputPaths.Length, 1, "XmlContent", "OutputPaths" });
         return false;
     }
     try
     {
         list = ProcessXsltArguments(this.parameters);
     }
     catch (Exception exception2)
     {
         if (Microsoft.Build.Shared.ExceptionHandling.IsCriticalException(exception2))
         {
             throw;
         }
         base.Log.LogErrorWithCodeFromResources("XslTransform.XsltArgumentsError", new object[] { exception2.Message });
         return false;
     }
     try
     {
         transform = input2.LoadXslt();
     }
     catch (Exception exception3)
     {
         if (Microsoft.Build.Shared.ExceptionHandling.IsCriticalException(exception3))
         {
             throw;
         }
         base.Log.LogErrorWithCodeFromResources("XslTransform.XsltLoadError", new object[] { exception3.Message });
         return false;
     }
     try
     {
         for (int i = 0; i < input.Count; i++)
         {
             using (XmlWriter writer = XmlWriter.Create(this.outputPaths[i].ItemSpec, transform.OutputSettings))
             {
                 using (XmlReader reader = input.CreateReader(i))
                 {
                     transform.Transform(reader, list, writer);
                 }
                 writer.Close();
             }
         }
     }
     catch (Exception exception4)
     {
         if (Microsoft.Build.Shared.ExceptionHandling.NotExpectedException(exception4))
         {
             throw;
         }
         base.Log.LogErrorWithCodeFromResources("XslTransform.TransformError", new object[] { exception4.Message });
         return false;
     }
     if (input.XmlMode == XmlInput.XmlModes.XmlFile)
     {
         for (int j = 0; j < this.xmlInputPaths.Length; j++)
         {
             this.xmlInputPaths[j].CopyMetadataTo(this.outputPaths[j]);
         }
     }
     return true;
 }
Exemple #3
0
        public override bool Execute()
        {
            XmlInput             input;
            XsltInput            input2;
            XsltArgumentList     list;
            XslCompiledTransform transform;

            Microsoft.Build.Shared.ErrorUtilities.VerifyThrowArgumentNull(this.outputPaths, "OutputPath");
            try
            {
                input  = new XmlInput(this.xmlInputPaths, this.xmlString);
                input2 = new XsltInput(this.xsltFile, this.xsltString, this.xsltCompiledDll);
            }
            catch (Exception exception)
            {
                if (Microsoft.Build.Shared.ExceptionHandling.IsCriticalException(exception))
                {
                    throw;
                }
                base.Log.LogErrorWithCodeFromResources("XslTransform.ArgumentError", new object[] { exception.Message });
                return(false);
            }
            if ((this.xmlInputPaths != null) && (this.xmlInputPaths.Length != this.outputPaths.Length))
            {
                base.Log.LogErrorWithCodeFromResources("General.TwoVectorsMustHaveSameLength", new object[] { this.outputPaths.Length, this.xmlInputPaths.Length, "XmlContent", "XmlInputPaths" });
                return(false);
            }
            if ((this.xmlString != null) && (this.outputPaths.Length != 1))
            {
                base.Log.LogErrorWithCodeFromResources("General.TwoVectorsMustHaveSameLength", new object[] { this.outputPaths.Length, 1, "XmlContent", "OutputPaths" });
                return(false);
            }
            try
            {
                list = ProcessXsltArguments(this.parameters);
            }
            catch (Exception exception2)
            {
                if (Microsoft.Build.Shared.ExceptionHandling.IsCriticalException(exception2))
                {
                    throw;
                }
                base.Log.LogErrorWithCodeFromResources("XslTransform.XsltArgumentsError", new object[] { exception2.Message });
                return(false);
            }
            try
            {
                transform = input2.LoadXslt();
            }
            catch (Exception exception3)
            {
                if (Microsoft.Build.Shared.ExceptionHandling.IsCriticalException(exception3))
                {
                    throw;
                }
                base.Log.LogErrorWithCodeFromResources("XslTransform.XsltLoadError", new object[] { exception3.Message });
                return(false);
            }
            try
            {
                for (int i = 0; i < input.Count; i++)
                {
                    using (XmlWriter writer = XmlWriter.Create(this.outputPaths[i].ItemSpec, transform.OutputSettings))
                    {
                        using (XmlReader reader = input.CreateReader(i))
                        {
                            transform.Transform(reader, list, writer);
                        }
                        writer.Close();
                    }
                }
            }
            catch (Exception exception4)
            {
                if (Microsoft.Build.Shared.ExceptionHandling.NotExpectedException(exception4))
                {
                    throw;
                }
                base.Log.LogErrorWithCodeFromResources("XslTransform.TransformError", new object[] { exception4.Message });
                return(false);
            }
            if (input.XmlMode == XmlInput.XmlModes.XmlFile)
            {
                for (int j = 0; j < this.xmlInputPaths.Length; j++)
                {
                    this.xmlInputPaths[j].CopyMetadataTo(this.outputPaths[j]);
                }
            }
            return(true);
        }