Esempio 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);
        }
 public XmlElementContext(XmlElementContext parent, XmlElement element, XmlDocument xmlTargetDoc, IServiceProvider serviceProvider)
     : base(element)
 {
     this.parentContext   = parent;
     this.xmlTargetDoc    = xmlTargetDoc;
     this.serviceProvider = serviceProvider;
 }
Esempio n. 3
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);
            }
        }
        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);
                }
            }
        }
        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);
        }
        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);
                }
            }
        }
        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);
        }
        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);
        }
 private XmlElementContext CreateElementContext(XmlElementContext parentContext, XmlElement element)
 {
     return(new XmlElementContext(parentContext, element, xmlTarget, this));
 }
        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("xt", TransformNamespace);
                XmlNodeList namespaceNodes = xmlTransformation.SelectNodes("//xt:*", 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;
                    }
                }
            }
        }
Esempio n. 11
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();
                }
            }
        }