/// <summary> /// Tries the load template resource string. /// </summary> /// <param name="resourceString">The resource string.</param> /// <param name="error">The error.</param> /// <returns>True of no errors loading else false</returns> private bool TryLoadResourceString(string uri, string resourceString, out string error) { try { error = null; // Deserialise the template xaml TemplateCollection templates = TemplateCollection.Deserialize(resourceString); templates.XamlString = resourceString; // ..then we MUST have an assocated template file (Excel Document) if (string.IsNullOrEmpty(templates.TemplateFileName)) { throw new MetadataException(string.Format("<{0}> - Unable to load template without a ExcelTemplateFilePath defined", uri)); } var document = XDocument.Parse(resourceString); foreach (var element in document.Root.Elements()) { if (element.NodeType != XmlNodeType.Element || element.Name == null) { continue; } if (element.Name.LocalName.CompareTo("TemplateCollection.Maps") == 0) { foreach (var mapElement in element.Elements()) { var keyAttrib = mapElement.Attribute("Key"); if (keyAttrib != null && !string.IsNullOrEmpty(keyAttrib.Value)) { this.MapResourceStore.Add(keyAttrib.Value, mapElement.ToString(), templates, uri); } } } else if (element.Name.LocalName.CompareTo("Template") == 0) { var id = element.Attribute("TemplateId").Value; this.TemplateResourceStore.Add(id, element.ToString(), templates, uri); } } } catch (MetadataException mex) { Output(true, mex.Message); error = mex.Message; return(false); } catch (Exception ex) { string errorMessage = string.Format("<{0}> - Failed to load template resource string <{1}>", uri, ex); Output(true, errorMessage); error = errorMessage; return(false); } return(true); }
/// <summary> /// Creates a fully populated <see cref="ResourceStore" /> which contains all resources defined in a <see cref="TemplateCollection" />. /// This is for backward compatibility during processing of a <see cref="TemplateCollection" />. /// </summary> /// <param name="templateCollection">The template collection.</param> /// <param name="templatePackage">The template package.</param> /// <returns> /// A fully populated <see cref="ResourceStore" /> /// </returns> internal static ResourceStore Create(TemplateCollection templateCollection, ExcelTemplatePackage templatePackage) { if (templateCollection == null || string.IsNullOrEmpty(templateCollection.XamlString)) { return(null); } ResourceStore resourceStore = new ResourceStore(); var document = XDocument.Parse(templateCollection.XamlString); // Add in the defined Styles foreach (var resource in templateCollection.StyleResources) { resourceStore.Add(resource.Key, ResourceTypeNames.StyleBase, resource, null); } // Add in CellStyleSelectors foreach (var resource in templateCollection.CellStyleSelectors) { resourceStore.Add(resource.Key, ResourceTypeNames.CellStyleSelector, resource, null); } // Loop over each all elements, adding any that are defined in the Maps collection or identified as being Template resources foreach (var element in document.Root.Elements()) { if (element.NodeType != XmlNodeType.Element || element.Name == null) { continue; } if (element.Name.LocalName.CompareTo("TemplateCollection.Maps") == 0) { foreach (var mapElement in element.Elements()) { AddElementToStore(templateCollection, resourceStore, mapElement); } } else if (element.Name.LocalName.CompareTo("Template") == 0) { var id = element.Attribute("TemplateId").Value; resourceStore.Add(id, element.Name.LocalName, element.ToString(), templateCollection.TemplateFileName, null); } } // Add the TemplateFile as a designer file, which can be used by maps as a resource if (!string.IsNullOrEmpty(templateCollection.TemplateFileName)) { var data = templatePackage.TemplateResourceStore.templateDocumentDictionary[templateCollection.TemplateFileName].Data; resourceStore.AddDesignerFileData(templateCollection.TemplateFileName, data); } // Populate chart and shape models from the supplied resource designer files and defined templates, // such as ChartTemplate and ShapeTempaltes. resourceStore.PopulateModels(); return(resourceStore); }
/// <summary> /// Adds the element to store. /// </summary> /// <param name="templateCollection">The template collection.</param> /// <param name="resourceStore">The resource store.</param> /// <param name="element">The element.</param> private static void AddElementToStore(TemplateCollection templateCollection, ResourceStore resourceStore, XElement element) { var keyAttrib = element.Attribute("Key"); if (keyAttrib != null && !string.IsNullOrEmpty(keyAttrib.Value)) { resourceStore.Add(keyAttrib.Value, element.Name.LocalName, element.ToString(), templateCollection.TemplateFileName, null); } }
/// <summary> /// Add a new set of resource information to the store. /// </summary> /// <param name="key">The unique id of the resource</param> /// <param name="resourceString">A string of Xaml. Will be used to inflate on demand.</param> /// <param name="templateCollection">The parent TemplateCollection stores Style etc</param> /// <param name="templateCollectionUri">Handy to know the TemplateCollection Uri if there are duplicates</param> public void Add(string key, string resourceString, TemplateCollection templateCollection, string templateCollectionUri) { if (resourceInfoDictionary.ContainsKey(key)) { // handy to log where the existing is, so get the xaml uri throw new MetadataException(string.Format("Loading <{0}> - already loaded Map {1} in {2}", templateCollectionUri, key, resourceInfoDictionary[key].TemplateCollectionUri)); } var resourceInfo = new ResourceInfo(key, resourceString, templateCollection, templateCollectionUri); this.resourceInfoDictionary.Add(key, resourceInfo); }
public ResourceInfo(string key, string resourceString, TemplateCollection templateCollection, string templateCollectionUri) { if (string.IsNullOrEmpty(key)) { throw new ArgumentNullException("key"); } if (string.IsNullOrEmpty(resourceString)) { throw new ArgumentNullException("resourceString"); } if (templateCollection == null) { throw new ArgumentNullException("templateCollection"); } if (string.IsNullOrEmpty(templateCollectionUri)) { throw new ArgumentNullException("templateCollectionUri"); } this.Key = key; this.ResourceString = resourceString; this.TemplateCollection = templateCollection; this.TemplateCollectionUri = templateCollectionUri; }