internal string ConstructParentPath(string parentPath, XmlElementContext context, string argumentString)
        {
            Debug.Assert(_parentPath == null && _context == null && ArgumentString == null,
                         "Do not call ConstructPath recursively");

            string resultPath = string.Empty;

            if (_parentPath == null && _context == null && ArgumentString == null)
            {
                try
                {
                    _parentPath    = parentPath;
                    _context       = context;
                    ArgumentString = argumentString;

                    resultPath = ParentPath;
                }
                finally
                {
                    _parentPath    = null;
                    _context       = null;
                    ArgumentString = null;
                    _arguments     = null;

                    ReleaseLogger();
                }
            }

            return(resultPath);
        }
        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);
            }
        }
 public XmlElementContext(XmlElementContext parent, XmlElement element, XmlDocument xmlTargetDoc, IServiceProvider serviceProvider)
     : base(element)
 {
     _parentContext   = parent;
     TargetDocument   = xmlTargetDoc;
     _serviceProvider = serviceProvider;
 }
Esempio n. 4
0
        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
                var namespaceManager = new XmlNamespaceManager(new NameTable());
                namespaceManager.AddNamespace("xdt", TransformNamespace);
                XmlNodeList namespaceNodes = _xmlTransformation.SelectNodes("//xdt:*", namespaceManager);

                foreach (XmlNode node in namespaceNodes)
                {
                    if (!(node is XmlElement element))
                    {
                        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);
                    }
                }
            }
        }
Esempio n. 5
0
        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);
            }

            if (assemblyName == null && path == null)
            {
                throw new XmlNodeException(string.Format(System.Globalization.CultureInfo.CurrentCulture, SR.XMLTRANSFORMATION_ImportMissingAssembly), context.Element);
            }

            if (nameSpace == null)
            {
                throw new XmlNodeException(string.Format(System.Globalization.CultureInfo.CurrentCulture, SR.XMLTRANSFORMATION_ImportMissingNamespace), context.Element);
            }

            if (assemblyName != null)
            {
                _namedTypeFactory.AddAssemblyRegistration(assemblyName, nameSpace);
            }
            else
            {
                _namedTypeFactory.AddPathRegistration(path, nameSpace);
            }
        }
        internal bool HasTargetParent(out XmlElementContext failedContext, out bool existedInOriginal)
        {
            failedContext     = null;
            existedInOriginal = false;

            if (TargetParents.Count == 0)
            {
                failedContext = this;
                while (!string.IsNullOrEmpty(failedContext._parentContext?.ParentXPath) && failedContext._parentContext.TargetParents.Count == 0)
                {
                    failedContext = failedContext._parentContext;
                }

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

            return(true);
        }
        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);
        }
Esempio n. 8
0
        void TransformLoop(XmlNodeContext parentContext)
        {
            foreach (XmlNode node in parentContext.Node.ChildNodes)
            {
                var element = node as XmlElement;
                if (element == null)
                {
                    continue;
                }

                XmlElementContext context = CreateElementContext(parentContext as XmlElementContext, element);
                try
                {
                    HandleElement(context);
                }
                catch (Exception ex)
                {
                    HandleException(ex, context);
                }
            }
        }
Esempio n. 9
0
        void HandleElement(XmlElementContext context)
        {
            Transform transform = context.ConstructTransform(out string argumentString);

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

                if (context.Element.Attributes.GetNamedItem(SupressWarnings, TransformNamespace) is XmlAttribute supressWarningsAttribute)
                {
                    bool fSupressWarning = 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);
        }
Esempio n. 10
0
 XmlElementContext CreateElementContext(XmlElementContext parentContext, XmlElement element)
 => new XmlElementContext(parentContext, element, _xmlTarget, this);
        internal void Execute(XmlElementContext context, string argumentString)
        {
            Debug.Assert(_context == null && ArgumentString == null, "Don't call Execute recursively");
            Debug.Assert(_logger == null, "Logger wasn't released from previous execution");

            if (_context == null && ArgumentString == null)
            {
                var error          = false;
                var startedSection = false;

                try
                {
                    _context       = context;
                    ArgumentString = argumentString;
                    _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;
                    Log.LogErrorFromException(context.TransformAttribute != null
                        ? XmlNodeException.Wrap(ex, context.TransformAttribute)
                        : ex);
                }
                finally
                {
                    if (startedSection)
                    {
                        Log.EndSection(MessageType.Verbose, error
                            ? SR.XMLTRANSFORMATION_TransformErrorExecutingMessage
                            : SR.XMLTRANSFORMATION_TransformEndExecutingMessage, TransformNameShort);
                    }
                    else
                    {
                        Log.LogMessage(MessageType.Normal, SR.XMLTRANSFORMATION_TransformNotExecutingMessage, TransformNameLong);
                    }

                    _context       = null;
                    ArgumentString = null;
                    _arguments     = null;

                    ReleaseLogger();
                }
            }
        }