Ejemplo n.º 1
0
        internal string ConstructParentPath(string parentPath, XmlElementContext context, string argumentString)
        {
            Debug.Assert(this.parentPath == null && this.context == null && this.argumentString == null,
                         "Do not call ConstructPath recursively");

            string resultPath = String.Empty;

            if (this.parentPath == null && this.context == null && this.argumentString == null)
            {
                try {
                    this.parentPath     = parentPath;
                    this.context        = context;
                    this.argumentString = argumentString;

                    resultPath = ParentPath;
                }
                finally {
                    this.parentPath     = null;
                    this.context        = null;
                    this.argumentString = null;
                    this.arguments      = null;

                    ReleaseLogger();
                }
            }

            return(resultPath);
        }
Ejemplo n.º 2
0
        private void HandleElement(XmlElementContext context)
        {
            string    str;
            Transform transform = context.ConstructTransform(out str);

            if (transform != null)
            {
                bool         supressWarnings = this.logger.SupressWarnings;
                XmlAttribute namedItem       = context.Element.Attributes.GetNamedItem(SupressWarnings, TransformNamespace) as XmlAttribute;
                if (namedItem != null)
                {
                    bool flag2 = Convert.ToBoolean(namedItem.Value, CultureInfo.InvariantCulture);
                    this.logger.SupressWarnings = flag2;
                }
                try
                {
                    this.OnApplyingTransform();
                    transform.Execute(context, str);
                    this.OnAppliedTransform();
                }
                catch (Exception exception)
                {
                    this.HandleException(exception, context);
                }
                finally
                {
                    this.logger.SupressWarnings = supressWarnings;
                }
            }
            this.TransformLoop(context);
        }
Ejemplo n.º 3
0
        private void HandleMissingTarget(XmlElementContext matchFailureContext, bool existedInOriginal)
        {
            string format = existedInOriginal ? SR.XMLTRANSFORMATION_TransformSourceMatchWasRemoved : SR.XMLTRANSFORMATION_TransformNoMatchingTargetNodes;

            object[] args    = new object[] { matchFailureContext.XPath };
            string   message = string.Format(CultureInfo.CurrentCulture, format, args);

            switch (this.MissingTargetMessage)
            {
            case Microsoft.Web.XmlTransform.MissingTargetMessage.None:
                this.Log.LogMessage(MessageType.Verbose, message, new object[0]);
                return;

            case Microsoft.Web.XmlTransform.MissingTargetMessage.Information:
                this.Log.LogMessage(MessageType.Normal, message, new object[0]);
                return;

            case Microsoft.Web.XmlTransform.MissingTargetMessage.Warning:
                this.Log.LogWarning(matchFailureContext.Node, message, new object[0]);
                return;

            case Microsoft.Web.XmlTransform.MissingTargetMessage.Error:
                throw new XmlNodeException(message, matchFailureContext.Node);
            }
        }
Ejemplo n.º 4
0
        private void HandleMissingTarget(XmlElementContext matchFailureContext, bool existedInOriginal)
        {
            string messageFormat = existedInOriginal
                ? SR.XMLTRANSFORMATION_TransformSourceMatchWasRemoved
                : SR.XMLTRANSFORMATION_TransformNoMatchingTargetNodes;

            string message = string.Format(System.Globalization.CultureInfo.CurrentCulture, messageFormat, matchFailureContext.XPath);

            switch (MissingTargetMessage)
            {
            case MissingTargetMessage.None:
                Log.LogMessage(MessageType.Verbose, message);
                break;

            case MissingTargetMessage.Information:
                Log.LogMessage(MessageType.Normal, message);
                break;

            case MissingTargetMessage.Warning:
                Log.LogWarning(matchFailureContext.Node, message);
                break;

            case MissingTargetMessage.Error:
                throw new XmlNodeException(message, matchFailureContext.Node);
            }
        }
Ejemplo n.º 5
0
 public XmlElementContext(XmlElementContext parent, XmlElement element, XmlDocument xmlTargetDoc, IServiceProvider serviceProvider)
     : base(element)
 {
     this.parentContext   = parent;
     this.xmlTargetDoc    = xmlTargetDoc;
     this.serviceProvider = serviceProvider;
 }
Ejemplo n.º 6
0
        private void PreprocessTransformDocument()
        {
            hasTransformNamespace = false;
            foreach (XmlAttribute attribute in xmlTransformation.SelectNodes("//namespace::*"))
            {
                if (attribute.Value.Equals(TransformNamespace, StringComparison.Ordinal))
                {
                    hasTransformNamespace = true;
                    break;
                }
            }

            if (hasTransformNamespace)
            {
                // This will look for all nodes from our namespace in the document,
                // and do any initialization work
                XmlNamespaceManager namespaceManager = new XmlNamespaceManager(new NameTable());
                namespaceManager.AddNamespace("xdt", TransformNamespace);
                XmlNodeList namespaceNodes = xmlTransformation.SelectNodes("//xdt:*", namespaceManager);

                foreach (XmlNode node in namespaceNodes)
                {
                    XmlElement element = node as XmlElement;
                    if (element == null)
                    {
                        Debug.Fail("The XPath for elements returned something that wasn't an element?");
                        continue;
                    }

                    XmlElementContext context = null;
                    try {
                        switch (element.LocalName)
                        {
                        case "Import":
                            context = CreateElementContext(null, element);
                            PreprocessImportElement(context);
                            break;

                        default:
                            logger.LogWarning(element, SR.XMLTRANSFORMATION_UnknownXdtTag, element.Name);
                            break;
                        }
                    }
                    catch (Exception ex) {
                        if (context != null)
                        {
                            ex = WrapException(ex, context);
                        }

                        logger.LogErrorFromException(ex);
                        throw new XmlTransformationException(SR.XMLTRANSFORMATION_FatalTransformSyntaxError, ex);
                    }
                    finally {
                        context = null;
                    }
                }
            }
        }
Ejemplo n.º 7
0
        private void PreprocessImportElement(XmlElementContext context)
        {
            string assemblyName = null;
            string nameSpace    = null;
            string path         = null;

            foreach (XmlAttribute attribute in context.Element.Attributes)
            {
                if (attribute.NamespaceURI.Length == 0)
                {
                    switch (attribute.Name)
                    {
                    case "assembly":
                        assemblyName = attribute.Value;
                        continue;

                    case "namespace":
                        nameSpace = attribute.Value;
                        continue;

                    case "path":
                        path = attribute.Value;
                        continue;
                    }
                }

                throw new XmlNodeException(string.Format(System.Globalization.CultureInfo.CurrentCulture, SR.XMLTRANSFORMATION_ImportUnknownAttribute, attribute.Name), attribute);
            }

            if (assemblyName != null && path != null)
            {
                throw new XmlNodeException(string.Format(System.Globalization.CultureInfo.CurrentCulture, SR.XMLTRANSFORMATION_ImportAttributeConflict), context.Element);
            }
            else if (assemblyName == null && path == null)
            {
                throw new XmlNodeException(string.Format(System.Globalization.CultureInfo.CurrentCulture, SR.XMLTRANSFORMATION_ImportMissingAssembly), context.Element);
            }
            else if (nameSpace == null)
            {
                throw new XmlNodeException(string.Format(System.Globalization.CultureInfo.CurrentCulture, SR.XMLTRANSFORMATION_ImportMissingNamespace), context.Element);
            }
            else
            {
                if (assemblyName != null)
                {
                    namedTypeFactory.AddAssemblyRegistration(assemblyName, nameSpace);
                }
                else
                {
                    namedTypeFactory.AddPathRegistration(path, nameSpace);
                }
            }
        }
Ejemplo n.º 8
0
 private void PreprocessTransformDocument()
 {
     this.hasTransformNamespace = false;
     foreach (XmlAttribute attribute in this.xmlTransformation.SelectNodes("//namespace::*"))
     {
         if (attribute.Value.Equals(TransformNamespace, StringComparison.Ordinal))
         {
             this.hasTransformNamespace = true;
             break;
         }
     }
     if (this.hasTransformNamespace)
     {
         XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
         nsmgr.AddNamespace("xdt", TransformNamespace);
         foreach (XmlNode node in this.xmlTransformation.SelectNodes("//xdt:*", nsmgr))
         {
             XmlElement element = node as XmlElement;
             if (element != null)
             {
                 XmlElementContext context = null;
                 try
                 {
                     string str;
                     if (((str = element.LocalName) != null) && (str == "Import"))
                     {
                         context = this.CreateElementContext(null, element);
                         this.PreprocessImportElement(context);
                     }
                     else
                     {
                         object[] messageArgs = new object[] { element.Name };
                         this.logger.LogWarning(element, SR.XMLTRANSFORMATION_UnknownXdtTag, messageArgs);
                     }
                 }
                 catch (Exception exception)
                 {
                     if (context != null)
                     {
                         exception = this.WrapException(exception, context);
                     }
                     this.logger.LogErrorFromException(exception);
                     throw new XmlTransformationException(SR.XMLTRANSFORMATION_FatalTransformSyntaxError, exception);
                 }
                 finally
                 {
                     context = null;
                 }
             }
         }
     }
 }
Ejemplo n.º 9
0
        private void PreprocessImportElement(XmlElementContext context)
        {
            string assemblyName = null;
            string nameSpace    = null;
            string path         = null;

            foreach (XmlAttribute attribute in context.Element.Attributes)
            {
                string str4;
                if ((attribute.NamespaceURI.Length == 0) && ((str4 = attribute.Name) != null))
                {
                    if (str4 == "assembly")
                    {
                        assemblyName = attribute.Value;
                        continue;
                    }
                    if (str4 == "namespace")
                    {
                        nameSpace = attribute.Value;
                        continue;
                    }
                    if (str4 == "path")
                    {
                        path = attribute.Value;
                        continue;
                    }
                }
                object[] args = new object[] { attribute.Name };
                throw new XmlNodeException(string.Format(CultureInfo.CurrentCulture, SR.XMLTRANSFORMATION_ImportUnknownAttribute, args), attribute);
            }
            if ((assemblyName != null) && (path != null))
            {
                throw new XmlNodeException(string.Format(CultureInfo.CurrentCulture, SR.XMLTRANSFORMATION_ImportAttributeConflict, new object[0]), context.Element);
            }
            if ((assemblyName == null) && (path == null))
            {
                throw new XmlNodeException(string.Format(CultureInfo.CurrentCulture, SR.XMLTRANSFORMATION_ImportMissingAssembly, new object[0]), context.Element);
            }
            if (nameSpace == null)
            {
                throw new XmlNodeException(string.Format(CultureInfo.CurrentCulture, SR.XMLTRANSFORMATION_ImportMissingNamespace, new object[0]), context.Element);
            }
            if (assemblyName != null)
            {
                this.namedTypeFactory.AddAssemblyRegistration(assemblyName, nameSpace);
            }
            else
            {
                this.namedTypeFactory.AddPathRegistration(path, nameSpace);
            }
        }
Ejemplo n.º 10
0
 internal bool HasTargetParent(out XmlElementContext failedContext, out bool existedInOriginal)
 {
     failedContext     = null;
     existedInOriginal = false;
     if (this.TargetParents.Count != 0)
     {
         return(true);
     }
     failedContext = this;
     while ((failedContext.parentContext != null) && (!string.IsNullOrEmpty(failedContext.parentContext.ParentXPath) && (failedContext.parentContext.TargetParents.Count == 0)))
     {
         failedContext = failedContext.parentContext;
     }
     existedInOriginal = this.ExistedInOriginal(failedContext.XPath);
     return(false);
 }
Ejemplo n.º 11
0
        private void TransformLoop(XmlNodeContext parentContext)
        {
            foreach (XmlNode node in parentContext.Node.ChildNodes)
            {
                XmlElement element = node as XmlElement;
                if (element == null)
                {
                    continue;
                }

                XmlElementContext context = CreateElementContext(parentContext as XmlElementContext, element);
                try {
                    HandleElement(context);
                }
                catch (Exception ex) {
                    HandleException(ex, context);
                }
            }
        }
Ejemplo n.º 12
0
 private void TransformLoop(XmlNodeContext parentContext)
 {
     foreach (XmlNode node in parentContext.Node.ChildNodes)
     {
         XmlElement element = node as XmlElement;
         if (element != null)
         {
             XmlElementContext context = this.CreateElementContext(parentContext as XmlElementContext, element);
             try
             {
                 this.HandleElement(context);
             }
             catch (Exception exception)
             {
                 this.HandleException(exception, context);
             }
         }
     }
 }
Ejemplo n.º 13
0
        internal bool HasTargetNode(out XmlElementContext failedContext, out bool existedInOriginal)
        {
            failedContext     = null;
            existedInOriginal = false;

            if (TargetNodes.Count == 0)
            {
                failedContext = this;
                while (failedContext.parentContext != null &&
                       failedContext.parentContext.TargetNodes.Count == 0)
                {
                    failedContext = failedContext.parentContext;
                }

                existedInOriginal = ExistedInOriginal(failedContext.XPath);
                return(false);
            }

            return(true);
        }
Ejemplo n.º 14
0
        private void HandleElement(XmlElementContext context)
        {
            string    argumentString;
            Transform transform = context.ConstructTransform(out argumentString);

            if (transform != null)
            {
                bool fOriginalSupressWarning = logger.SupressWarnings;

                XmlAttribute SupressWarningsAttribute = context.Element.Attributes.GetNamedItem(XmlTransformation.SupressWarnings, XmlTransformation.TransformNamespace) as XmlAttribute;
                if (SupressWarningsAttribute != null)
                {
                    bool fSupressWarning = System.Convert.ToBoolean(SupressWarningsAttribute.Value, System.Globalization.CultureInfo.InvariantCulture);
                    logger.SupressWarnings = fSupressWarning;
                }

                try
                {
                    OnApplyingTransform();

                    transform.Execute(context, argumentString);

                    OnAppliedTransform();
                }
                catch (Exception ex)
                {
                    HandleException(ex, context);
                }
                finally
                {
                    // reset back the SupressWarnings back per node
                    logger.SupressWarnings = fOriginalSupressWarning;
                }
            }

            // process children
            TransformLoop(context);
        }
Ejemplo n.º 15
0
        internal string ConstructPath(string parentPath, XmlElementContext context, string argumentString)
        {
            string str = string.Empty;

            if ((this.parentPath == null) && ((this.context == null) && (this.argumentString == null)))
            {
                try
                {
                    this.parentPath     = parentPath;
                    this.context        = context;
                    this.argumentString = argumentString;
                    str = this.ConstructPath();
                }
                finally
                {
                    this.parentPath     = null;
                    this.context        = null;
                    this.argumentString = null;
                    this.arguments      = null;
                    this.ReleaseLogger();
                }
            }
            return(str);
        }
Ejemplo n.º 16
0
 private XmlElementContext CreateElementContext(XmlElementContext parentContext, XmlElement element) =>
 new XmlElementContext(parentContext, element, this.xmlTarget, this);
Ejemplo n.º 17
0
 internal void Execute(XmlElementContext context, string argumentString)
 {
     if ((this.context == null) && (this.argumentString == null))
     {
         bool flag  = false;
         bool flag2 = false;
         try
         {
             this.context        = context;
             this.argumentString = argumentString;
             this.arguments      = null;
             if (this.ShouldExecuteTransform())
             {
                 flag2 = true;
                 object[] messageArgs = new object[] { this.TransformNameLong };
                 this.Log.StartSection(MessageType.Verbose, SR.XMLTRANSFORMATION_TransformBeginExecutingMessage, messageArgs);
                 object[] objArray2 = new object[] { context.XPath };
                 this.Log.LogMessage(MessageType.Verbose, SR.XMLTRANSFORMATION_TransformStatusXPath, objArray2);
                 if (this.ApplyTransformToAllTargetNodes)
                 {
                     this.ApplyOnAllTargetNodes();
                 }
                 else
                 {
                     this.ApplyOnce();
                 }
             }
         }
         catch (Exception exception)
         {
             flag = true;
             if (context.TransformAttribute != null)
             {
                 this.Log.LogErrorFromException(XmlNodeException.Wrap(exception, context.TransformAttribute));
             }
             else
             {
                 this.Log.LogErrorFromException(exception);
             }
         }
         finally
         {
             if (!flag2)
             {
                 object[] messageArgs = new object[] { this.TransformNameLong };
                 this.Log.LogMessage(MessageType.Normal, SR.XMLTRANSFORMATION_TransformNotExecutingMessage, messageArgs);
             }
             else if (flag)
             {
                 object[] messageArgs = new object[] { this.TransformNameShort };
                 this.Log.EndSection(MessageType.Verbose, SR.XMLTRANSFORMATION_TransformErrorExecutingMessage, messageArgs);
             }
             else
             {
                 object[] messageArgs = new object[] { this.TransformNameShort };
                 this.Log.EndSection(MessageType.Verbose, SR.XMLTRANSFORMATION_TransformEndExecutingMessage, messageArgs);
             }
             this.context        = null;
             this.argumentString = null;
             this.arguments      = null;
             this.ReleaseLogger();
         }
     }
 }
Ejemplo n.º 18
0
        internal void Execute(XmlElementContext context, string argumentString)
        {
            Debug.Assert(this.context == null && this.argumentString == null, "Don't call Execute recursively");
            Debug.Assert(this.logger == null, "Logger wasn't released from previous execution");

            if (this.context == null && this.argumentString == null)
            {
                bool error          = false;
                bool startedSection = false;

                try {
                    this.context        = context;
                    this.argumentString = argumentString;
                    this.arguments      = null;

                    if (ShouldExecuteTransform())
                    {
                        startedSection = true;

                        Log.StartSection(MessageType.Verbose, SR.XMLTRANSFORMATION_TransformBeginExecutingMessage, TransformNameLong);
                        Log.LogMessage(MessageType.Verbose, SR.XMLTRANSFORMATION_TransformStatusXPath, context.XPath);

                        if (ApplyTransformToAllTargetNodes)
                        {
                            ApplyOnAllTargetNodes();
                        }
                        else
                        {
                            ApplyOnce();
                        }
                    }
                }
                catch (Exception ex) {
                    error = true;
                    if (context.TransformAttribute != null)
                    {
                        Log.LogErrorFromException(XmlNodeException.Wrap(ex, context.TransformAttribute));
                    }
                    else
                    {
                        Log.LogErrorFromException(ex);
                    }
                }
                finally {
                    if (startedSection)
                    {
                        if (error)
                        {
                            Log.EndSection(MessageType.Verbose, SR.XMLTRANSFORMATION_TransformErrorExecutingMessage, TransformNameShort);
                        }
                        else
                        {
                            Log.EndSection(MessageType.Verbose, SR.XMLTRANSFORMATION_TransformEndExecutingMessage, TransformNameShort);
                        }
                    }
                    else
                    {
                        Log.LogMessage(MessageType.Normal, SR.XMLTRANSFORMATION_TransformNotExecutingMessage, TransformNameLong);
                    }

                    this.context        = null;
                    this.argumentString = null;
                    this.arguments      = null;

                    ReleaseLogger();
                }
            }
        }
Ejemplo n.º 19
0
 private XmlElementContext CreateElementContext(XmlElementContext parentContext, XmlElement element)
 {
     return(new XmlElementContext(parentContext, element, xmlTarget, this));
 }