Example #1
0
        public bool TransformXml()
        {
            bool flag = true;
            XmlTransformation        xmlTransformation        = null;
            XmlTransformableDocument xmlTransformableDocument = null;

            try
            {
                try
                {
                    CultureInfo currentCulture = CultureInfo.CurrentCulture;
                    string      bUILDTASKTransformXmlTransformationStart = "Transforming Source File: {0}";
                    object[]    source = new object[] { this.Source };
                    Console.WriteLine(string.Format(currentCulture, bUILDTASKTransformXmlTransformationStart, source));
                    xmlTransformableDocument = this.OpenSourceFile(this.Source);
                    CultureInfo cultureInfo = CultureInfo.CurrentCulture;
                    string      bUILDTASKTransformXmlTransformationApply = "Applying Transform File: {0}";
                    object[]    transform = new object[] { this.Transform };
                    Console.WriteLine(string.Format(cultureInfo, bUILDTASKTransformXmlTransformationApply, this.Transform));
                    xmlTransformation = this.OpenTransformFile(this.Transform);
                    flag = xmlTransformation.Apply(xmlTransformableDocument);
                    if (flag)
                    {
                        CultureInfo currentCulture1 = CultureInfo.CurrentCulture;
                        string      bUILDTASKTransformXmlTransformOutput = "Output File: {0}";
                        Console.WriteLine(string.Format(currentCulture1, bUILDTASKTransformXmlTransformOutput, this.Destination));
                        SaveTransformedFile(xmlTransformableDocument, this.Destination);
                    }
                }
                catch (XmlException xmlException1)
                {
                    XmlException xmlException = xmlException1;
                    string       localPath    = this.Source;
                    if (!string.IsNullOrEmpty(xmlException.SourceUri))
                    {
                        localPath = (new Uri(xmlException.SourceUri)).LocalPath;
                    }
                    Console.WriteLine(string.Format("{0} {1} {2} {3} {4}", localPath, xmlException.LineNumber, xmlException.LinePosition, xmlException.Message));
                    flag = false;
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception);
                    flag = false;
                }
            }
            finally
            {
                Console.WriteLine(string.Format(CultureInfo.CurrentCulture, (flag ? "Transformation succeeded" : "Transformation failed"), new object[0]), new object[0]);
                if (xmlTransformation != null)
                {
                    xmlTransformation.Dispose();
                }
                if (xmlTransformableDocument != null)
                {
                    xmlTransformableDocument.Dispose();
                }
            }
            return(flag);
        }
Example #2
0
        public bool TransformXml(string sourceFile, string transformFile, string destFile)
        {
            FileInfo source    = new FileInfo(sourceFile);
            FileInfo transform = new FileInfo(transformFile);
            FileInfo dest      = new FileInfo(destFile);

            if (!source.Exists)
            {
                throw new FileNotFoundException("source file not found", sourceFile);
            }
            if (!transform.Exists)
            {
                throw new FileNotFoundException("transform file not found", transformFile);
            }


            bool succeeded = true;

            XmlTransformation        transformation = null;
            XmlTransformableDocument document       = null;

            try {
                document       = OpenSourceFile(source.FullName);
                transformation = OpenTransformFile(transform.FullName, null);
                succeeded      = transformation.Apply(document);
                if (succeeded)
                {
                    document.Save(dest.FullName);
                }
            }
            finally {
                if (transformation != null)
                {
                    transformation.Dispose();
                }
                if (document != null)
                {
                    document.Dispose();
                }
            }

            return(succeeded);
        }
Example #3
0
        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);
        }
Example #4
0
        public override bool Execute()
        {
            bool succeeded = true;
            XmlTransformation        transformation = null;
            XmlTransformableDocument document       = null;

            // validate that both Transform and TransformString are not passed in
            if (Transform != null && !string.IsNullOrEmpty(TransformText))
            {
                Log.LogError("Either Transform or TransformString should be passed in, not both.");
                return(false);
            }

            bool loadTransformFromFile = string.IsNullOrEmpty(TransformText);

            try {
                Log.LogMessage(MessageImportance.Low, "Transfroming source file: {0}", Source);

                document = OpenSourceFile(Source.GetMetadata("FullPath"));

                if (loadTransformFromFile)
                {
                    Log.LogMessage(MessageImportance.Low, "Applying Transform File: {0}", Transform);
                    transformation = OpenTransformFile(Transform.GetMetadata("FullPath"), null);
                }
                else
                {
                    Log.LogMessage(MessageImportance.Low, "Applying Transform from TransformText");
                    transformation = OpenTransformText(TransformText, null);
                }

                succeeded = transformation.Apply(document);

                if (succeeded)
                {
                    Log.LogMessage(MessageImportance.Low, "Output File: {0}", Destination);
                    SaveTransformedFile(document, Destination.GetMetadata("FullPath"));
                }
            }
            catch (System.Xml.XmlException ex) {
                string localPath = Source.GetMetadata("FullPath");
                if (!string.IsNullOrEmpty(ex.SourceUri))
                {
                    Uri sourceUri = new Uri(ex.SourceUri);
                    localPath = sourceUri.LocalPath;
                }

                Log.LogErrorFromException(ex);
                succeeded = false;
            }
            catch (Exception ex) {
                Log.LogErrorFromException(ex);
                succeeded = false;
            }
            finally {
                if (transformation != null)
                {
                    transformation.Dispose();
                }
                if (document != null)
                {
                    document.Dispose();
                }
            }

            return(succeeded);
        }