/// <summary>Optimizes the xml document.</summary>
        /// <param name="xDocument">The xml document.</param>
        /// <param name="xamlFile">The xaml file info.</param>
        /// <returns>A result with the optimized <see cref="XDocument"/>, if successful.</returns>
        public OptimizationResult Optimize(XDocument xDocument, IFileReference xamlFile)
        {
            var mergedResourceDictionaries = xDocument.XPathSelectElements(
                Constants.DefaultResourceDictionaryMergedDictionariesDefaultResourceDictionaryXPath,
                this.xamlPlatformInfo.XmlNamespaceResolver);
            var hasBeenOptimized = false;
            var hasSxoNamespace  = false;

            foreach (var xElement in mergedResourceDictionaries.ToList())
            {
                var optimization = OptimizationProvider.GetOptimizationInfo(
                    xElement,
                    this.xamlPlatformInfo.SystemResourceDictionaryName);
                switch (optimization.OptimizationMode)
                {
                case OptimizationMode.Shared:
                    if (!hasSxoNamespace)
                    {
                        xDocument.Root.EnsureXmlNamespaceAttribute(
                            this.xamlPlatformInfo.SundewXamlOptimizationsNamespace,
                            Constants.SxoPrefix,
                            this.xamlPlatformInfo.DefaultInsertAfterNamespaces);
                        hasSxoNamespace = true;
                    }

                    hasBeenOptimized = true;
                    xElement.ReplaceWith(new XElement(
                                             this.sundewXamlResourceDictionaryName,
                                             new XAttribute(Constants.SourceText, optimization.Source)));
                    break;
                }
            }

            return(OptimizationResult.From(hasBeenOptimized, xDocument));
        }
Esempio n. 2
0
        /// <summary>Optimizes the specified xml document.</summary>
        /// <param name="xDocument">The xml document.</param>
        /// <param name="xamlFile">The xaml file reference.</param>
        /// <returns>The result of the xaml optimization.</returns>
        public OptimizationResult Optimize(XDocument xDocument, IFileReference xamlFile)
        {
            var hasBeenOptimized = false;
            var rootElement      = xDocument.Root;

            if (rootElement != null)
            {
                if (rootElement.Name == this.xamlPlatformInfo.SystemResourceDictionaryName)
                {
                    this.TryOptimize(rootElement, xDocument, ref hasBeenOptimized);

                    foreach (var resourcesElement in rootElement
                             .Descendants()
                             .Where(x => x.Name.LocalName.EndsWith(Constants.ResourcesName)))
                    {
                        this.TryOptimize(resourcesElement, xDocument, ref hasBeenOptimized);
                    }
                }

                foreach (var resourcesElement in rootElement
                         .Descendants()
                         .Where(x => x.Name.LocalName.EndsWith(Constants.ResourcesName)))
                {
                    var elementToOptimize = resourcesElement;
                    var firstElement      = resourcesElement.Elements().FirstOrDefault();
                    if (firstElement == null)
                    {
                        break;
                    }

                    if (firstElement.Name == this.xamlPlatformInfo.SystemResourceDictionaryName)
                    {
                        elementToOptimize = firstElement;
                    }

                    this.TryOptimize(elementToOptimize, xDocument, ref hasBeenOptimized);
                }
            }

            return(OptimizationResult.From(hasBeenOptimized, xDocument));
        }