示例#1
0
        /// <summary>
        /// Make transformation of file <see cref="SourceFilePath"/> with transform file <see cref="TransformFile"/> to <paramref name="destinationFilePath"/>.
        /// </summary>
        /// <param name="destinationFilePath">File path of destination transformation.</param>
        /// <param name="forceParametersTask">Invoke parameters task even if the parameters are not set with <see cref="SetParameters" />.</param>
        /// <returns>Return true if transformation finish successfully, otherwise false.</returns>
        public bool Execute(string destinationFilePath, bool forceParametersTask = false)
        {
            if (string.IsNullOrWhiteSpace(destinationFilePath))
            {
                throw new ArgumentException("Destination file can't be empty.", "destinationFilePath");
            }

            this.log.WriteLine("Start tranformation to '{0}'.", destinationFilePath);

            if (string.IsNullOrWhiteSpace(this.SourceFilePath) || !File.Exists(this.SourceFilePath))
            {
                throw new FileNotFoundException("Can't find source file.", this.SourceFilePath);
            }

            if (string.IsNullOrWhiteSpace(this.TransformFile) || !File.Exists(this.TransformFile))
            {
                throw new FileNotFoundException("Can't find transform  file.", this.TransformFile);
            }

            this.log.WriteLine("Source file: '{0}'.", this.SourceFilePath);
            this.log.WriteLine("Transform  file: '{0}'.", this.TransformFile);

            try
            {
                var transformFile = ReadContent(this.TransformFile);

                if ((this.parameters != null && this.parameters.Count > 0) || forceParametersTask)
                {
                    ParametersTask parametersTask = new ParametersTask();
                    if (this.parameters != null)
                    {
                        parametersTask.AddParameters(this.parameters);
                    }

                    transformFile = parametersTask.ApplyParameters(transformFile);
                }

                XmlDocument document = new XmlDocument();
                document.Load(this.SourceFilePath);

                XmlTransformation transformation = new XmlTransformation(transformFile, false, this.transfomrationLogger);

                bool result = transformation.Apply(document);

                document.Save(destinationFilePath);

                return(result);
            }
            catch (Exception e)
            {
                this.log.WriteLine("Exception while transforming: {0}.", e);
                return(false);
            }
        }
示例#2
0
        /// <summary>
        /// Make transformation of file <see cref="SourceFilePath"/> with transform file <see cref="TransformFile"/> to <paramref name="destinationFilePath"/>.
        /// </summary>
        /// <param name="destinationFilePath">File path of destination transformation.</param>
        /// <returns>Return true if transformation finish successfully, otherwise false.</returns>
        public bool Execute(string destinationFilePath)
        {
            if (string.IsNullOrWhiteSpace(destinationFilePath))
            {
                throw new ArgumentException("Destination file can't be empty.", "destinationFilePath");
            }

            this.log.WriteLine("Start tranformation to '{0}'.", destinationFilePath);

            if (string.IsNullOrWhiteSpace(this.SourceFilePath) || !File.Exists(this.SourceFilePath))
            {
                throw new FileNotFoundException("Can't find source file.", this.SourceFilePath);
            }

            if (string.IsNullOrWhiteSpace(this.TransformFile) || !File.Exists(this.TransformFile))
            {
                throw new FileNotFoundException("Can't find transform  file.", this.TransformFile);
            }

            this.log.WriteLine("Source file: '{0}'.", this.SourceFilePath);
            this.log.WriteLine("Transform  file: '{0}'.", this.TransformFile);

            try
            {
                Encoding encoding = Encoding.Unicode;

                XmlDocument document = new XmlDocument()
                {
                    PreserveWhitespace = this.PreserveWhitespace
                };

                document.Load(this.SourceFilePath);
                bool xmlDeclarationPresent = false;
                if (document.FirstChild.NodeType == XmlNodeType.XmlDeclaration)
                {
                    xmlDeclarationPresent = true;
                    var xmlDeclaration = (XmlDeclaration)document.FirstChild;
                    if (!string.IsNullOrEmpty(xmlDeclaration.Encoding))
                    {
                        encoding = Encoding.GetEncoding(xmlDeclaration.Encoding);
                    }
                }

                var transformEncoding = GetXmlFileEncoding(this.TransformFile);

                this.log.WriteLine("Transformation task is using encoding '{0}'.", transformEncoding);

                var transformFile = File.ReadAllText(this.TransformFile, transformEncoding);

                ParametersTask parametersTask = new ParametersTask();
                if (this.parameters != null)
                {
                    parametersTask.AddParameters(this.parameters);
                }

                transformFile = parametersTask.ApplyParameters(transformFile);

                XmlTransformation transformation =
                    new XmlTransformation(transformFile, false, this.transfomrationLogger);

                bool result = transformation.Apply(document);

                var outerXml = document.OuterXml;

                if (this.Indent)
                {
                    outerXml = this.GetIndentedOuterXml(outerXml, encoding, omitXmlDeclaration: !xmlDeclarationPresent);
                }

                if (this.PreserveWhitespace)
                {
                    outerXml = outerXml.Replace("&#xD;", "\r").Replace("&#xA;", "\n");
                }

                File.WriteAllText(destinationFilePath, outerXml, encoding);

                return(result);
            }
            catch (Exception e)
            {
                this.log.WriteLine("Exception while transforming: {0}.", e);
                return(false);
            }
        }