Exemplo n.º 1
0
        /// <summary>
        /// Parses the specified resource string and creates a resource store from it
        /// </summary>
        /// <param name="resourceString">The resource string.</param>
        /// <param name="uri">The URI.</param>
        /// <returns></returns>
        /// <exception cref="MetadataException"></exception>
        internal static ResourceStore Parse(string resourceString, string uri)
        {
            ResourceStore resourceStore = new ResourceStore();

            // Deserialise the template xaml
            IResourceContainer rd = null;

            using (var sr = new StringReader(resourceString))
            {
                using (var xr = new XmlTextReader(sr))
                {
                    rd = (IResourceContainer)XamlReader.Load(xr);
                }
            }

            if (rd == null)
            {
                return(resourceStore);
            }

            List <int> elementsForResourceStore = new List <int>();

            int index = 0;

            foreach (var resource in rd.Resources)
            {
                if (resource is CellStyleSelector)
                {
                    resourceStore.Add(resource.Key, ResourceTypeNames.CellStyleSelector, resource, uri);
                }
                //else if (resource is StyleBase)
                //{
                //    resourceStore.Add(resource.Key, ResourceTypeNames.StyleBase, resource, uri);
                //}
                else
                {
                    elementsForResourceStore.Add(index);
                }
                index++;
            }

            index = 0;

            var document = XDocument.Parse(resourceString);

            XElement rootElement = null;

            if (rd is ResourceMetadata)
            {
                rootElement = document.Root;
            }
            else if (rd is ExcelDocumentMetadata)
            {
                // initially check for a ResourceCollection node
                // there will be one of these if there is a MergeResources going on
                rootElement = (from d in document.Descendants()
                               where d.Name.LocalName.CompareTo("ResourceCollection") == 0
                               select d).FirstOrDefault();

                if (rootElement == null)
                {
                    // if no merge resources then look for a ExcelDocumentMetadata.Resources node
                    // all style etc will be located directly beneath this
                    rootElement = (from d in document.Descendants()
                                   where d.Name.LocalName.CompareTo("ExcelDocumentMetadata.Resources") == 0
                                   select d).FirstOrDefault();
                }
            }
            else
            {
                throw new MetadataException(string.Format("Unexpected type in ResourceStore.Parse <{0}>", rd.GetType().ToString()));
            }

            if (rootElement != null)
            {
                foreach (var element in rootElement.Elements())
                {
                    // only store those elements that we havent stored an instance of above
                    // basically everything other that Styles and CellStyleSelectors
                    if (elementsForResourceStore.Contains(index))
                    {
                        var keyAttrib = element.Attribute("Key");
                        if (keyAttrib != null && !string.IsNullOrEmpty(keyAttrib.Value))
                        {
                            resourceStore.Add(keyAttrib.Value, element.Name.LocalName, element.ToString(), rd.DesignerFileName, uri);
                        }
                    }
                    index++;
                }
            }

            return(resourceStore);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Parses the specified resource string and creates a resource store from it
        /// </summary>
        /// <param name="resourceString">The resource string.</param>
        /// <param name="uri">The URI.</param>
        /// <returns></returns>
        /// <exception cref="MetadataException"></exception>
        internal static ResourceStore Parse(string resourceString, string uri)
        {
            ResourceStore resourceStore = new ResourceStore();

            // Deserialise the template xaml
            IResourceContainer resourceContainer = null;

            using (var sr = new StringReader(resourceString))
            {
                using (var xr = new XmlTextReader(sr))
                {
                    resourceContainer = (IResourceContainer)XamlReader.Load(xr);
                }
            }

            if (resourceContainer == null)
            {
                return(resourceStore);
            }

            var indecesOfElementsToAdd = ExtractCellStyleSelectors(uri, resourceStore, resourceContainer);

            int idx      = 0;
            var document = XDocument.Parse(resourceString);

            XElement rootElement = null;

            if (resourceContainer is ResourceMetadata)
            {
                rootElement = document.Root;
            }
            else if (resourceContainer is ExcelDocumentMetadata)
            {
                // initially check for a ResourceCollection node
                // there will be one of these if there is a MergeResources going on
                rootElement = (from d in document.Descendants()
                               where d.Name.LocalName.CompareTo("ResourceCollection") == 0
                               select d).FirstOrDefault();

                if (rootElement == null)
                {
                    // if no merge resources then look for a ExcelDocumentMetadata.Resources node
                    // all style etc will be located directly beneath this
                    rootElement = (from d in document.Descendants()
                                   where d.Name.LocalName.CompareTo("ExcelDocumentMetadata.Resources") == 0
                                   select d).FirstOrDefault();
                }
            }
            else
            {
                throw new MetadataException(string.Format("Unexpected type in ResourceStore.Parse <{0}>", resourceContainer.GetType().ToString()));
            }

            if (rootElement != null)
            {
                foreach (var element in rootElement.Elements())
                {
                    // only store those elements that we havent stored an instance of above
                    // basically everything other that Styles and CellStyleSelectors
                    if (indecesOfElementsToAdd.Contains(idx))
                    {
                        var keyAttrib = element.Attribute("Key");
                        if (keyAttrib != null && !string.IsNullOrEmpty(keyAttrib.Value))
                        {
                            // TODO: Optimisation - DesignerFileName could reference an entry in a string dictionary
                            resourceStore.Add(keyAttrib.Value, element.Name.LocalName, element.ToString(), resourceContainer.DesignerFileName, uri);
                        }
                    }
                    idx++;
                }
            }

            return(resourceStore);
        }