コード例 #1
0
        /// <summary>
        /// Parse string of parameters <paramref name="parametersString"/> separated by semi ';'.
        /// Value should be separated from name by colon ':'.
        /// If value has spaces or semi you can use quotes for value.
        /// You can escape symbols '\' and '"' with \.
        /// </summary>
        /// <param name="parametersString">String of parameters</param>
        /// <param name="parameters">All parameters will be read to current dictionary.</param>
        public void ReadParameters(string parametersString, IDictionary <string, string> parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            if (string.IsNullOrWhiteSpace(parametersString))
            {
                return;
            }

            var source = parametersString.ToCharArray();

            var index = 0;

            var fParameterNameRead       = true;
            var fForceParameterValueRead = false;

            var parameterName  = new StringBuilder();
            var parameterValue = new StringBuilder();

            while (index < source.Length)
            {
                if (fParameterNameRead && source[index] == ':')
                {
                    fParameterNameRead = false;
                    index++;

                    if (index < source.Length && source[index] == '"')
                    {
                        fForceParameterValueRead = true;
                        index++;
                    }

                    continue;
                }

                if (!fForceParameterValueRead && source[index] == ';' ||
                    fForceParameterValueRead && source[index] == '"' &&
                    (index + 1 == source.Length || source[index + 1] == ';'))
                {
                    AddParameter(parameters, parameterName, parameterValue);
                    index++;
                    if (fForceParameterValueRead)
                    {
                        index++;
                    }

                    parameterName.Clear();
                    parameterValue.Clear();
                    fParameterNameRead       = true;
                    fForceParameterValueRead = false;
                    continue;
                }

                // Check is this escape \{ \} \\
                if (source[index] == '\\')
                {
                    var nextIndex = index + 1;
                    if (nextIndex < source.Length)
                    {
                        var nextChar = source[nextIndex];
                        if (nextChar == '"' || nextChar == '\\')
                        {
                            index++;
                        }
                    }
                }

                if (fParameterNameRead)
                {
                    parameterName.Append(source[index]);
                }
                else
                {
                    parameterValue.Append(source[index]);
                }

                index++;
            }

            AddParameter(parameters, parameterName, parameterValue);

            foreach (var parameter in parameters)
            {
                _log.WriteLine("Parameter Name: '{0}', Value: '{1}'", parameter.Key, parameter.Value);
            }
        }
コード例 #2
0
 public void LogMessage(string message, params object[] messageArgs)
 {
     _log.WriteLine(message, messageArgs);
 }
コード例 #3
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);
            }
        }