Пример #1
0
 public void LogError(string message, params object[] messageArgs)
 {
     _log.WriteErrorLine(message, messageArgs);
 }
Пример #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>
        /// <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");
            }

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

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

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

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

            try
            {
                Encoding encoding = DefaultEncoding;

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

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

                _log.WriteLine("Transformation task is using encoding '{0}'. Change encoding in source file, or use the 'encoding' parameter if you want to change encoding.", encoding);

                var transformFile = File.ReadAllText(TransformFile, encoding);

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

                    transformFile = parametersTask.ApplyParameters(transformFile);
                }

                XmlTransformation transformation = new XmlTransformation(transformFile, false, _transformationLogger);

                bool result = transformation.Apply(document);

                var outerXml = document.OuterXml;

                if (Indent)
                {
                    outerXml = GetIndentedOuterXml(outerXml, encoding);
                }

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

                File.WriteAllText(destinationFilePath, outerXml, encoding);

                return(result);
            }
            catch (Exception e)
            {
                _log.WriteErrorLine("Exception while transforming: {0}.", e);
                return(false);
            }
        }