private void Transform(string sourceFile, IEnumerable<string> tranformFiles, string destinationFile)
        {
            var sourceXmlString = _fileSystem.File.ReadAllText(sourceFile);
            var sourceFileEncoding = GetEncoding(sourceXmlString);
            var intermediateXmlString = sourceXmlString;

            foreach (var transformFile in tranformFiles)
            {
                var sourceDocument = new XmlTransformableDocument();
                var transformXmlString = _fileSystem.File.ReadAllText(transformFile);
                var transformFileEncoding = GetEncoding(transformXmlString);
                var transformXmlBytes = transformFileEncoding.GetBytes(transformXmlString);

                using (var memoryStream = new MemoryStream(transformXmlBytes))
                {
                    var transformation = new XmlTransformation(memoryStream, _xmlTransformationLogger);
                    sourceDocument.LoadXml(intermediateXmlString);
                    transformation.Apply(sourceDocument);
                    intermediateXmlString = sourceDocument.OuterXml;
                }
            }

            var xDocument = XDocument.Parse(intermediateXmlString, LoadOptions.PreserveWhitespace);
            intermediateXmlString = $"{xDocument.Declaration}{Environment.NewLine}{xDocument.Document}";

            _fileSystem.File.WriteAllText(destinationFile, intermediateXmlString, sourceFileEncoding);
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="sourceFile"></param>
 /// <returns></returns>
 public XmlTransformableDocument OpenSourceFile(string sourceFile)
 {
     var xml = File.ReadAllText(sourceFile);
     var document = new XmlTransformableDocument { PreserveWhitespace = true };
     document.LoadXml(xml);
     return document;
 }
Beispiel #3
0
        public static TransformResult Transform(string transformationPath, IDictionary<string, string> parameters, string[] targetFiles, bool preserveWhitespace = true)
        {
            if (!File.Exists(transformationPath)) throw new ArgumentException("Transformation path did not exist");

            if (targetFiles == null || targetFiles.Length == 0) return null;

            foreach (var file in targetFiles)
            {
                if (!File.Exists(file)) throw new ArgumentException("Target file " + file + " did not exist");
            }

            var logger = new CollectionXmlTransformationLogger();
            var transformText = File.ReadAllText(transformationPath);

            transformText = ParameterizeText(transformText, parameters);

            var transformation = new XmlTransformation(transformText, false, logger);

            try
            {
                foreach (var file in targetFiles)
                {
                    var input = File.ReadAllText(file);

                    var document = new XmlTransformableDocument();
                    document.PreserveWhitespace = preserveWhitespace;

                    document.LoadXml(input);

                    transformation.Apply(document);

                    if (logger.HasErrors) break;

                    if (document.IsChanged)
                    {
                        document.Save(file);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new XmlTransformationException(
                    "Transform failed. Log output was: " +
                    string.Join("\n", logger.Messages.Select(x => x.Type.ToString() + ": " + x.Text)), ex);
            }

            return new TransformResult(logger.Messages.ToArray(), !logger.HasErrors);
        }
Beispiel #4
0
        public bool Transform(string source, string transform, string destination)
        {
            if (string.IsNullOrEmpty(destination))
                throw new ArgumentException("destination cannot be null or empty");

            var sourceContent = _fileSystem.File.ReadAllText(source);
            var transformation = new XmlTransformation(_fileSystem.File.OpenRead(transform), null);

            var sourceDocument = new XmlTransformableDocument
            {
                PreserveWhitespace = true
            };

            sourceDocument.LoadXml(sourceContent);
            transformation.Apply(sourceDocument);
            _fileSystem.File.WriteAllText(destination, sourceDocument.OuterXml);

            return true;
        }
        public ActionResult Create(string webConfigXml, string transformXml)
        {
            try
            {
                using (var document = new XmlTransformableDocument())
                {
                    document.PreserveWhitespace = true;
                    document.LoadXml(webConfigXml);

                    using (var transform = new XmlTransformation(transformXml, false, null))
                    {
                        if (transform.Apply(document))
                        {
                            var stringBuilder = new StringBuilder();
                            var xmlWriterSettings = new XmlWriterSettings();
                            xmlWriterSettings.Indent = true;
                            xmlWriterSettings.IndentChars = "    ";
                            using (var xmlTextWriter = XmlTextWriter.Create(stringBuilder, xmlWriterSettings))
                            {
                                document.WriteTo(xmlTextWriter);
                            }
                            return Content(stringBuilder.ToString(), "text/xml");
                        }
                        else
                        {
                            return ErrorXml("Transformation failed for unkown reason");
                        }
                    }
                }
            }
            catch (XmlTransformationException xmlTransformationException)
            {
                return ErrorXml(xmlTransformationException.Message);
            }
            catch (XmlException xmlException)
            {
                return ErrorXml(xmlException.Message);
            }
        }
Beispiel #6
0
        private XmlDocument Transform(XmlDocument sourceXml, XmlDocument patchXml)
        {
            var source = new XmlTransformableDocument();
            source.LoadXml(sourceXml.OuterXml);

            var patch = new XmlTransformation(patchXml.OuterXml, false, null);
            patch.Apply(source);

            return source;
        }
        private XDocument ApplyTransformation(string originalConfiguration, string transformationResourceName)
        {
            XDocument result;
            Stream stream = null;
            try
            {
                stream = typeof(WebConfigTransformTest).Assembly.GetManifestResourceStream(transformationResourceName);
                var document = new XmlTransformableDocument();
                using (var transformation = new XmlTransformation(stream, null))
                {
                    stream = null;
                    document.LoadXml(originalConfiguration);
                    transformation.Apply(document);
                    result = XDocument.Parse(document.OuterXml);
                }
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }

            return result;
        }