コード例 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="XmlTransformer"/> class with an external logger.
        /// </summary>
        /// <param name="logger">External logger. Passed into the transformation</param>
        /// <param name="useSections">Wheter or not to use sections while logging</param>
        public XmlTransformer(ITransformationLogger logger, bool useSections)
        {
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            this.logger = new XmlShimLogger(logger, useSections);
        }
コード例 #2
0
 public static void XdtTransformConfigWithLogger(this ICakeContext context, FilePath sourceFile, FilePath transformFile,
                                                 FilePath targetFile, IXmlTransformationLogger logger)
 {
     if (context == null)
     {
         throw new ArgumentNullException(nameof(context));
     }
     XdtTransformation.TransformConfig(context.FileSystem, sourceFile, transformFile, targetFile, logger);
 }
コード例 #3
0
 public XmlTransformation(Stream transformStream, IXmlTransformationLogger logger)
 {
     this.transformationServiceContainer = new ServiceContainer();
     this.logger            = new XmlTransformationLogger(logger);
     this.transformFile     = string.Empty;
     this.xmlTransformation = new XmlFileInfoDocument();
     this.xmlTransformation.Load(transformStream);
     this.InitializeTransformationServices();
     this.PreprocessTransformDocument();
 }
コード例 #4
0
        public XmlTransformation(Stream transformStream, IXmlTransformationLogger logger)
        {
            this.logger = new XmlTransformationLogger(logger);
            this.transformFile = String.Empty;

            xmlTransformation = new XmlFileInfoDocument();
            xmlTransformation.Load(transformStream);

            InitializeTransformationServices();

            PreprocessTransformDocument();
        }
コード例 #5
0
 public XmlTransformation(string transform, bool isTransformAFile, IXmlTransformationLogger logger)
 {
     this.transformFile = transform;
     this.logger = new XmlTransformationLogger(logger);
     this.xmlTransformation = (XmlDocument)new XmlFileInfoDocument();
     if (isTransformAFile)
         this.xmlTransformation.Load(transform);
     else
         this.xmlTransformation.LoadXml(transform);
     this.InitializeTransformationServices();
     this.PreprocessTransformDocument();
 }
コード例 #6
0
        public XmlTransformation(Stream transformStream, IXmlTransformationLogger logger)
        {
            _logger        = new XmlTransformationLogger(logger);
            _transformFile = string.Empty;

            _xmlTransformation = new XmlFileInfoDocument();
            _xmlTransformation.Load(transformStream);

            InitializeTransformationServices();

            PreprocessTransformDocument();
        }
コード例 #7
0
 private XmlTransformation OpenTransformFile(string transformFile, IXmlTransformationLogger logger) {
     try {
         return new XmlTransformation(transformFile, logger);
     }
     catch (System.Xml.XmlException) {
         throw;
     }
     catch (Exception ex) {
         throw new Exception(
             string.Format(System.Globalization.CultureInfo.CurrentCulture,
             "Could not open Transform file: {0}", ex.Message),
             ex);
     }
 }
コード例 #8
0
 private XmlTransformation OpenTransformFile(string transformFile, IXmlTransformationLogger logger)
 {
     try
     {
         return new XmlTransformation(transformFile, logger);
     }
     catch (XmlException ex)
     {
         throw ex;
     }
     catch (Exception ex)
     {
         throw new Exception(string.Format((IFormatProvider)CultureInfo.CurrentCulture, "Could not open Transform file: {0}", new object[1] { (object) ex.Message }), ex);
     }
 }
コード例 #9
0
 private XmlTransformation OpenTransformFile(string transformFile, IXmlTransformationLogger logger)
 {
     try {
         return(new XmlTransformation(transformFile, logger));
     }
     catch (System.Xml.XmlException) {
         throw;
     }
     catch (Exception ex) {
         throw new Exception(
                   string.Format(System.Globalization.CultureInfo.CurrentCulture,
                                 "Could not open Transform file: {0}", ex.Message),
                   ex);
     }
 }
コード例 #10
0
 public XmlTransformation(string transform, bool isTransformAFile, IXmlTransformationLogger logger)
 {
     this.transformFile     = transform;
     this.logger            = new XmlTransformationLogger(logger);
     this.xmlTransformation = (XmlDocument) new XmlFileInfoDocument();
     if (isTransformAFile)
     {
         this.xmlTransformation.Load(transform);
     }
     else
     {
         this.xmlTransformation.LoadXml(transform);
     }
     this.InitializeTransformationServices();
     this.PreprocessTransformDocument();
 }
コード例 #11
0
 private XmlTransformation OpenTransformFile(string transformFile, IXmlTransformationLogger logger)
 {
     try
     {
         return(new XmlTransformation(transformFile, logger));
     }
     catch (XmlException ex)
     {
         throw ex;
     }
     catch (Exception ex)
     {
         throw new Exception(string.Format((IFormatProvider)CultureInfo.CurrentCulture, "Could not open Transform file: {0}", new object[1] {
             (object)ex.Message
         }), ex);
     }
 }
コード例 #12
0
        public XmlTransformation(Stream transformStream, IXmlTransformationLogger logger)
        {
            if (transformStream == null)
            {
                throw new ArgumentNullException(nameof(transformStream));
            }

            _logger        = new XmlTransformationLogger(logger);
            _transformFile = string.Empty;

            _xmlTransformation = new XmlFileInfoDocument();
            _xmlTransformation.Load(transformStream);

            InitializeTransformationServices();

            PreprocessTransformDocument();
        }
コード例 #13
0
ファイル: TransformXml.cs プロジェクト: vitek-karas/sdk
 private XmlTransformation OpenTransformFile(string transformFile, IXmlTransformationLogger logger)
 {
     try
     {
         return(new XmlTransformation(transformFile, logger));
     }
     catch (System.Xml.XmlException)
     {
         throw;
     }
     catch (Exception ex)
     {
         throw new Exception(
                   string.Format(System.Globalization.CultureInfo.CurrentCulture, Resources.BUILDTASK_TransformXml_TransformLoadFailed, ex.Message),
                   ex);
     }
 }
コード例 #14
0
        public XmlTransformation(string transform, bool isTransformAFile, IXmlTransformationLogger logger)
        {
            _transformFile = transform ?? throw new ArgumentNullException(nameof(transform));
            _logger        = new XmlTransformationLogger(logger);

            _xmlTransformation = new XmlFileInfoDocument();
            if (isTransformAFile)
            {
                _xmlTransformation.Load(transform);
            }
            else
            {
                _xmlTransformation.LoadXml(transform);
            }

            InitializeTransformationServices();

            PreprocessTransformDocument();
        }
コード例 #15
0
        void ApplyTransformation(string configFile, string transformFile, string destinationFile, IXmlTransformationLogger logger)
        {
            var transformation = new XmlTransformation(transformFile, logger);

            var configurationFileDocument = new XmlTransformableDocument()
            {
                PreserveWhitespace = true
            };
            configurationFileDocument.Load(configFile);

            var success = transformation.Apply(configurationFileDocument);
            if (!suppressTransformationErrors && (!success || transformFailed))
            {
                Log.ErrorFormat("The XML configuration file {0} failed with transformation file {1}.", configFile, transformFile);
                throw new CommandException(transformWarning);
            }

            configurationFileDocument.Save(destinationFile);
        }
コード例 #16
0
        private XmlTransformation OpenTransformText(string transformText, IXmlTransformationLogger logger)
        {
            try {
                if (string.IsNullOrEmpty(transformText))
                {
                    throw new ArgumentNullException("transformText");
                }

                transformText = transformText.Trim();
                return(new XmlTransformation(transformText, false, logger));
            }
            catch (System.Xml.XmlException) {
                throw;
            }
            catch (Exception ex) {
                throw new Exception(
                          string.Format(System.Globalization.CultureInfo.CurrentCulture,
                                        "Could not open Transform file: {0}", ex.Message),
                          ex);
            }
        }
コード例 #17
0
        public bool Execute()
        {
            bool flag = true;
            IXmlTransformationLogger logger = (IXmlTransformationLogger) new Log4netTransformationLogger();

            try
            {
                logger.StartSection(string.Format((IFormatProvider)CultureInfo.CurrentCulture, "Transforming Source File: {0}", new object[1] {
                    (object)this.Source
                }), new object[0]);
                XmlTransformableDocument document = this.OpenSourceFile(this.Source);
                logger.LogMessage(string.Format((IFormatProvider)CultureInfo.CurrentCulture, "Applying Transform File: {0}", new object[1] {
                    (object)this.Transform
                }), new object[0]);
                flag = this.OpenTransformFile(this.Transform, logger).Apply((XmlDocument)document);
                if (flag)
                {
                    logger.LogMessage(string.Format((IFormatProvider)CultureInfo.CurrentCulture, "Output File: {0}", new object[1] {
                        (object)this.Destination
                    }), new object[0]);
                    this.SaveTransformedFile(document, this.Destination);
                }
            }
            catch (XmlException ex)
            {
                Uri uri = new Uri(ex.SourceUri);
                logger.LogError(uri.LocalPath, ex.LineNumber, ex.LinePosition, ex.Message, new object[0]);
                flag = false;
            }
            catch (Exception ex)
            {
                logger.LogErrorFromException(ex);
                flag = false;
            }
            finally
            {
                logger.EndSection(string.Format((IFormatProvider)CultureInfo.CurrentCulture, flag ? "Transformation succeeded" : "Transformation failed", new object[0]), new object[0]);
            }
            return(flag);
        }
コード例 #18
0
        public XmlTransformation(string transform, bool isTransformAFile, IXmlTransformationLogger logger)
        {
            _transformFile = transform;
            _logger        = new XmlTransformationLogger(logger);

            _xmlTransformation = new XmlFileInfoDocument();
            if (isTransformAFile)
            {
                using (FileStream stream = File.OpenRead(transform))
                {
                    _xmlTransformation.Load(stream);
                }
            }
            else
            {
                _xmlTransformation.LoadXml(transform);
            }

            InitializeTransformationServices();

            PreprocessTransformDocument();
        }
コード例 #19
0
 public XmlTransformService(IFileSystem fileSystem, IXmlTransformationLogger xmlTransformationLogger)
 {
     _fileSystem = fileSystem;
     _xmlTransformationLogger = xmlTransformationLogger;
 }
コード例 #20
0
        void ApplyTransformation(string configFile, string transformFile, string destinationFile, IXmlTransformationLogger logger)
        {
            var transformation = new XmlTransformation(transformFile, logger);

            var configurationFileDocument = new XmlTransformableDocument()
            {
                PreserveWhitespace = true
            };

            configurationFileDocument.Load(configFile);

            var success = transformation.Apply(configurationFileDocument);

            if (!suppressTransformationErrors && (!success || transformFailed))
            {
                log.ErrorFormat("The XML configuration file {0} failed with transformation file {1}.", configFile, transformFile);
                throw new CommandException(transformWarning);
            }

            if (!configurationFileDocument.ChildNodes.OfType <XmlElement>().Any())
            {
                log.WarnFormat("The XML configuration file {0} no longer has a root element and is invalid after being transformed by {1}", configFile, transformFile);
            }

            configurationFileDocument.Save(destinationFile);
        }
コード例 #21
0
 public XmlTransformation(string transform, IXmlTransformationLogger logger)
     : this(transform, true, logger)
 {
 }
コード例 #22
0
ファイル: TransformXml.cs プロジェクト: vitek-karas/sdk
        public bool RunXmlTransform(bool isLoggingEnabled = true)
        {
            bool succeeded = true;
            IXmlTransformationLogger logger = null;

            if (isLoggingEnabled && !IgnoreError)
            {
                logger = new TaskTransformationLogger(Log, StackTrace);
            }

            XmlTransformation        transformation = null;
            XmlTransformableDocument document       = null;

            try
            {
                logger?.StartSection(string.Format(System.Globalization.CultureInfo.CurrentCulture, Resources.BUILDTASK_TransformXml_TransformationStart, Source));
                document = OpenSourceFile(Source);

                logger?.LogMessage(string.Format(System.Globalization.CultureInfo.CurrentCulture, Resources.BUILDTASK_TransformXml_TransformationApply, Transform));
                transformation = OpenTransformFile(Transform, logger);

                succeeded = transformation.Apply(document);

                if (succeeded)
                {
                    logger?.LogMessage(string.Format(System.Globalization.CultureInfo.CurrentCulture, Resources.BUILDTASK_TransformXml_TransformOutput, Destination));

                    SaveTransformedFile(document, Destination);
                }

                if (IgnoreError)
                {
                    return(true);
                }
            }
            catch (System.Xml.XmlException ex)
            {
                if (IgnoreError)
                {
                    return(true);
                }

                string localPath = Source;
                if (!string.IsNullOrEmpty(ex.SourceUri))
                {
                    Uri sourceUri = new Uri(ex.SourceUri);
                    localPath = sourceUri.LocalPath;
                }

                logger?.LogError(localPath, ex.LineNumber, ex.LinePosition, ex.Message);
                succeeded = false;
            }
            catch (Exception ex)
            {
                if (IgnoreError)
                {
                    return(true);
                }

                logger?.LogErrorFromException(ex);
                succeeded = false;
            }
            finally
            {
                logger?.EndSection(string.Format(System.Globalization.CultureInfo.CurrentCulture, succeeded ?
                                                 Resources.BUILDTASK_TransformXml_TransformationSucceeded :
                                                 Resources.BUILDTASK_TransformXml_TransformationFailed));
                if (transformation != null)
                {
                    transformation.Dispose();
                }
                if (document != null)
                {
                    document.Dispose();
                }
            }

            return(succeeded);
        }
コード例 #23
0
 public Transformer(IXmlTransformationLogger logger, IFilesystemAdapter filesystemAdapter)
 {
     _logger            = logger ?? throw new ArgumentNullException(nameof(logger));
     _filesystemAdapter = filesystemAdapter ?? throw new ArgumentNullException(nameof(filesystemAdapter));
 }
コード例 #24
0
ファイル: TransformationTask.cs プロジェクト: benjamine/CFT
 /// <summary>
 /// Initializes a new instance of the <see cref="TransformationTask"/> class.
 /// </summary>
 /// <param name="transformationLogger">The transformation logger.</param>
 public TransformationTask(IXmlTransformationLogger transformationLogger)
 {
     _transformationLogger = transformationLogger;
 }
コード例 #25
0
 internal XmlTransformationLogger(IXmlTransformationLogger logger)
 {
     this.externalLogger = logger;
 }
コード例 #26
0
 internal XmlTransformationLogger(IXmlTransformationLogger logger) {
     this.externalLogger = logger;
 }
コード例 #27
0
 internal XmlTransformationLogger(IXmlTransformationLogger logger)
 => _externalLogger = logger;
コード例 #28
0
 public XmlTransformation(string transform, IXmlTransformationLogger logger)
     : this(transform, true, logger)
 {
 }
コード例 #29
0
        void ApplyTransformation(string configFile, string transformFile, string destinationFile, IXmlTransformationLogger logger)
        {
            var transformation = new XmlTransformation(transformFile, logger);

            var configurationFileDocument = new XmlTransformableDocument()
            {
                PreserveWhitespace = true
            };

            configurationFileDocument.Load(configFile);

            var success = transformation.Apply(configurationFileDocument);

            if (!suppressTransformationErrors && (!success || transformFailed))
            {
                Log.ErrorFormat("The XML configuration file {0} failed with transformation file {1}.", configFile, transformFile);
                throw new CommandException(transformWarning);
            }

            configurationFileDocument.Save(destinationFile);
        }
コード例 #30
0
        /// <summary>
        /// Transforms config file.
        /// </summary>
        /// <param name="fileSystem">The filesystem.</param>
        /// <param name="sourceFile">Source config file.</param>
        /// <param name="transformFile">Tranformation to apply.</param>
        /// <param name="targetFile">Target config file.</param>
        /// <param name="logger">Logger for the transfomration process</param>
        public static void TransformConfig(IFileSystem fileSystem, FilePath sourceFile, FilePath transformFile, FilePath targetFile, IXmlTransformationLogger logger = null)
        {
            if (fileSystem == null)
            {
                throw new ArgumentNullException(nameof(fileSystem), "File system is null.");
            }
            CheckNulls(sourceFile, transformFile, targetFile);

            IFile
                sourceConfigFile    = fileSystem.GetFile(sourceFile),
                transformConfigFile = fileSystem.GetFile(transformFile),
                targetConfigFile    = fileSystem.GetFile(targetFile);

            using (Stream
                   sourceStream = sourceConfigFile.OpenRead(),
                   transformStream = transformConfigFile.OpenRead(),
                   targetStream = targetConfigFile.OpenWrite())
                using (var document = new XmlTransformableDocument {
                    PreserveWhitespace = true
                })
                    using (var transform = new XmlTransformation(transformStream, logger))
                    {
                        document.Load(sourceStream);

                        if (!transform.Apply(document))
                        {
                            throw new CakeException(
                                      $"Failed to transform \"{sourceFile}\" using \"{transformFile}\" to \"{targetFile}\""
                                      );
                        }

                        document.Save(targetStream);
                    }
        }
コード例 #31
0
        private XmlTransformation OpenTransformText(string transformText, IXmlTransformationLogger logger) {
            try {
                if (string.IsNullOrEmpty(transformText)) { throw new ArgumentNullException("transformText"); }

                transformText = transformText.Trim();
                return new XmlTransformation(transformText, false, logger);
            }
            catch (System.Xml.XmlException) {
                throw;
            }
            catch (Exception ex) {
                throw new Exception(
                    string.Format(System.Globalization.CultureInfo.CurrentCulture,
                    "Could not open Transform file: {0}", ex.Message),
                    ex);
            }
        }
コード例 #32
0
        void ApplyTransformation(string configFile, string transformFile, string destinationFile, IXmlTransformationLogger logger)
        {
            errorEncountered = false;
            var transformation = new XmlTransformation(transformFile, logger);

            var configurationFileDocument = new XmlTransformableDocument()
            {
                PreserveWhitespace = true
            };

            configurationFileDocument.Load(configFile);

            var success = transformation.Apply(configurationFileDocument);

            if (!success || errorEncountered)
            {
                throw new CommandException($"The XML configuration file {configFile} failed with transformation file {transformFile}.");
            }

            if (!configurationFileDocument.ChildNodes.OfType <XmlElement>().Any())
            {
                logger.LogWarning("The XML configuration file {0} no longer has a root element and is invalid after being transformed by {1}", new object[] { configFile, transformFile });
            }

            configurationFileDocument.Save(destinationFile);
        }
コード例 #33
0
 public XmlTransformer(IXmlTransformationLogger logger)
 {
     _logger = logger ?? throw new ArgumentNullException(nameof(logger));
 }