protected virtual void Dispose(bool disposing)
        {
            if (_transformationServiceContainer != null)
            {
                _transformationServiceContainer.Dispose();
                _transformationServiceContainer = null;
            }

            if (_documentServiceContainer != null)
            {
                _documentServiceContainer.Dispose();
                _documentServiceContainer = null;
            }

            if (_xmlTransformable != null)
            {
                _xmlTransformable.Dispose();
                _xmlTransformable = null;
            }

            var xmlFileInfoDocument = _xmlTransformation as XmlFileInfoDocument;

            if (xmlFileInfoDocument != null)
            {
                xmlFileInfoDocument.Dispose();
                _xmlTransformation = null;
            }
        }
Пример #2
0
        public bool Apply(XmlDocument xmlTarget)
        {
            Debug.Assert(_xmlTarget == null, "This method should not be called recursively");

            if (_xmlTarget == null)
            {
                // Reset the error state
                _logger.HasLoggedErrors = false;

                _xmlTarget        = xmlTarget;
                _xmlTransformable = xmlTarget as XmlTransformableDocument;
                try
                {
                    if (HasTransformNamespace)
                    {
                        InitializeDocumentServices(xmlTarget);

                        TransformLoop(_xmlTransformation);
                    }
                    else
                    {
                        _logger.LogMessage(MessageType.Normal, "The expected namespace {0} was not found in the transform file", TransformNamespace);
                    }
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
                finally
                {
                    ReleaseDocumentServices();

                    _xmlTarget        = null;
                    _xmlTransformable = null;
                }

                return(!_logger.HasLoggedErrors);
            }
            return(false);
        }
Пример #3
0
        public static int Main(string[] args)
        {
            var app = new CommandLineApplication
            {
                Name        = "dotnet-transform-xdt",
                FullName    = ".NET Core XML Document Transformation",
                Description = "XML Document Transformation for .NET Core applications"
            };

            app.HelpOption("-?|-h|--help");

            CommandOption inputFilePath     = app.Option("--xml|-x", "The path to the XML file to transform", CommandOptionType.SingleValue);
            CommandOption transformFilePath = app.Option("--transform|-t", "The path to the XDT transform file to apply", CommandOptionType.SingleValue);
            CommandOption outputFilePath    = app.Option("--output|-o", "The path where the output (transformed) file will be written", CommandOptionType.SingleValue);
            CommandOption verboseOption     = app.Option("--verbose|-v", "Print verbose messages", CommandOptionType.NoValue);

            app.OnExecute(() =>
            {
                string inputPath     = inputFilePath.Value();
                string transformPath = transformFilePath.Value();
                string outputPath    = outputFilePath.Value();

                if (inputPath == null || transformPath == null || outputPath == null)
                {
                    app.ShowHelp();
                    return(2);
                }

                if (!File.Exists(inputPath))
                {
                    Console.Error.WriteLine($"{Prefix}Input file not found: {inputPath}");
                    return(3);
                }

                if (!File.Exists(transformPath))
                {
                    Console.Error.WriteLine($"{Prefix}Transform file not found: {transformPath}");
                    return(4);
                }

                Console.WriteLine($"{Prefix}Transforming '{inputPath}' using '{transformPath}' into '{outputPath}'");

                var sourceXml = new XmlTransformableDocument {
                    PreserveWhitespace = true
                };

                using (FileStream sourceStream = File.OpenRead(inputPath))
                    using (FileStream transformStream = File.OpenRead(transformPath))
                        using (var transformation = new XmlTransformation(transformStream, new ConsoleTransformationLogger(verboseOption.HasValue())))
                        {
                            sourceXml.Load(sourceStream);
                            transformation.Apply(sourceXml);
                        }

                using (FileStream outputStream = File.Create(outputPath))
                    using (XmlWriter outputWriter = XmlWriter.Create(outputStream, new XmlWriterSettings
                    {
                        Indent = true,
                        Encoding = Encoding.UTF8,
                    }))
                    {
                        sourceXml.WriteTo(outputWriter);
                    }

                return(0);
            });

            if (args == null ||
                args.Length == 0 ||
                args[0].Equals("-?", StringComparison.OrdinalIgnoreCase) ||
                args[0].Equals("-h", StringComparison.OrdinalIgnoreCase) ||
                args[0].Equals("--help", StringComparison.OrdinalIgnoreCase))
            {
                app.ShowHelp();
                return(1);
            }

            try
            {
                return(app.Execute(args));
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(Prefix + "Failed: " + ex.Message);
                return(1);
            }
        }