Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        private void HandleMissingTarget(XmlElementContext matchFailureContext, bool existedInOriginal)
        {
            string messageFormat = existedInOriginal
                ? SR.XMLTRANSFORMATION_TransformSourceMatchWasRemoved
                : SR.XMLTRANSFORMATION_TransformNoMatchingTargetNodes;

            string message = string.Format(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.º 3
0
 public XmlElementContext(
     XmlElementContext parent,
     XmlElement element,
     XmlDocument xmlTargetDoc,
     IServiceProvider serviceProvider)
     : base(element)
 {
     _parentContext   = parent;
     TargetDocument   = xmlTargetDoc;
     _serviceProvider = serviceProvider;
 }
Ejemplo n.º 4
0
        internal bool HasTargetNode(out XmlElementContext failedContext, out bool existedInOriginal)
        {
            failedContext     = null;
            existedInOriginal = false;

            if (TargetNodes.Count == 0)
            {
                failedContext = this;
                while (failedContext._parentContext?.TargetNodes.Count == 0)
                {
                    failedContext = failedContext._parentContext;
                }

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

            return(true);
        }
Ejemplo n.º 5
0
        private void TransformLoop(XmlNodeContext parentContext)
        {
            foreach (XmlNode node in parentContext.Node.ChildNodes)
            {
                if (!(node is XmlElement element))
                {
                    continue;
                }

                XmlElementContext context = CreateElementContext(parentContext as XmlElementContext, element);
                try
                {
                    HandleElement(context);
                }
                catch (Exception ex)
                {
                    HandleException(ex, context);
                }
            }
        }
Ejemplo n.º 6
0
        internal bool HasTargetParent(out XmlElementContext failedContext, out bool existedInOriginal)
        {
            failedContext     = null;
            existedInOriginal = false;

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

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

            return(true);
        }
Ejemplo n.º 7
0
        private 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,
                                                             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.º 8
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(CultureInfo.CurrentCulture,
                                                         SR.XMLTRANSFORMATION_ImportUnknownAttribute,
                                                         attribute.Name),
                                           attribute);
            }

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

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

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

            if (assemblyName != null)
            {
                _namedTypeFactory.AddAssemblyRegistration(assemblyName, nameSpace);
            }
            else
            {
                _namedTypeFactory.AddPathRegistration(path, nameSpace);
            }
        }
Ejemplo n.º 9
0
 private XmlElementContext CreateElementContext(XmlElementContext parentContext, XmlElement element)
 {
     return(new XmlElementContext(parentContext, element, _xmlTarget, this));
 }
Ejemplo n.º 10
0
        private void PreprocessTransformDocument()
        {
            HasTransformNamespace = false;
            const string xpath       = "//namespace::*";
            XmlNodeList  xmlNodeList = _xmlTransformation.SelectNodes(xpath);

            if (xmlNodeList is null)
            {
                throw new InvalidOperationException($"Xml node list from xpath pattern {xpath} is null");
            }

            foreach (XmlAttribute attribute in xmlNodeList)
            {
                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);
                const string xdt            = "//xdt:*";
                XmlNodeList  namespaceNodes = _xmlTransformation.SelectNodes(xdt, namespaceManager);

                if (namespaceNodes is null)
                {
                    throw new InvalidOperationException($"Xml node list from xpath pattern {xdt} is null");
                }

                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);
                    }
                    finally
                    {
                        context = null;
                    }
                }
            }
        }
Ejemplo n.º 11
0
        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)
            {
                bool error          = false;
                bool 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;

                    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);
                    }

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

                    ReleaseLogger();
                }
            }
        }