Esempio n. 1
0
        public bool Apply(XmlDocument xmlTarget)
        {
            if (xmlTarget == null)
            {
                throw new ArgumentNullException(nameof(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);
        }
Esempio n. 2
0
        static int Main(string[] args)
        {
            string sourceFilePath = null, outputFilePath = null, transformFilePath = null;
            bool   verbose = false, printUsage = false;

            if (!ParseArguments(args, ref sourceFilePath, ref outputFilePath, ref transformFilePath, ref verbose, ref printUsage))
            {
                if (printUsage)
                {
                    PrintUsage(Console.Out);
                    return(ErrorUsage);
                }

                PrintUsage(Console.Error);
                return(ErrorUsage);
            }

            if (!File.Exists(sourceFilePath))
            {
                LogError($"Source file not found: {sourceFilePath}");
                return(ErrorUsage);
            }

            if (!File.Exists(transformFilePath))
            {
                LogError($"Transform file not found: {transformFilePath}");
                return(ErrorUsage);
            }

            Log($"Transforming '{sourceFilePath}' using '{transformFilePath}' into '{outputFilePath}'");

            try
            {
                var sourceXml = new XmlTransformableDocument {
                    PreserveWhitespace = true
                };

                using (var sourceStream = File.OpenRead(sourceFilePath))
                    using (var transformStream = File.OpenRead(transformFilePath))
                        using (var transformation = new XmlTransformation(transformStream, new ConsoleTransformationLogger(verbose)))
                        {
                            sourceXml.Load(sourceStream);
                            transformation.Apply(sourceXml);
                        }

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

                return(Success);
            }
            catch (Exception ex)
            {
                LogError($"Unexpected error: {ex}");
                return(ErrorFailed);
            }
        }