Пример #1
0
        private void LoadSettingsFromXDocumentOrElseDefaults(XContainer config)
        {
            try {

                var rootDescendents = config.Descendants(_rootElementName);

                var xElements = rootDescendents as XElement[] ?? rootDescendents.ToArray();
                RootOperator = ConfigElement
                    .Create<RootOperatorElement>()
                    .TryLoadElement(xElements
                        .Descendants("rootOperator")
                        .SingleOrDefault()
                     )
                    .CastTo<RootOperatorElement>();

                Templates = ConfigElement
                    .Create<TemplatesElement>()
                    .TryLoadElement(xElements
                        .Descendants("templates")
                        .SingleOrDefault()
                     )
                    .CastTo<TemplatesElement>();

                Namespaces = xElements.HasClearChildElement("namespaces") ? new List<string>() : CreateDefaultNamespaces();
                References = xElements.HasClearChildElement("references") ? new List<string>() : CreateDefaultReferences();
                ContentProviders = xElements.HasClearChildElement("contentProviders") ? new List<Func<IContentProvider>>() : CreateDefaultContentProviders();

                Namespaces = xElements
                    .Descendants("namespaces")
                    .SingleOrDefault(new XElement("namespaces"))
                    .Descendants("add")
                    .Select(xe => xe.GetAttributeValue("namespace"))
                    .Union(Namespaces)
                    .ToList()
                    .AsReadOnly();

                References = xElements
                    .Descendants("references")
                    .SingleOrDefault(new XElement("references"))
                    .Descendants("add")
                    .Select(xe => xe.GetAttributeValue("reference"))
                    .Union(References, StringComparer.InvariantCultureIgnoreCase)
                    .ToList()
                    .AsReadOnly();

                var contentProviderElements =
                    xElements
                    .Descendants("contentProviders")
                    .SingleOrDefault(new XElement("contentProviders"))
                    .Descendants("add");

                ContentProviders = ContentProviders.ToList();
                foreach (var e in contentProviderElements) {
                    var el = e;
                    ContentProviders.Add(
                        () => Activator.CreateInstance(Type.GetType(el.GetAttributeValue("type"), true), true)
                                .CastTo<IContentProvider>()
                                .InitFromConfig(el)
                    );
                }
                ContentProviders = ContentProviders.CastTo<List<Func<IContentProvider>>>().AsReadOnly();
            }
            catch(TypeLoadException ex){
                throw new TemplateConfigurationException("Unable to load type named '{0}' at your xipton.razor configuration. Please correct the corresponding configuration setting.".FormatWith(ex.TypeName),ex);
            }
            catch(Exception ex){
                throw new TemplateConfigurationException("Unable to load the xipton.razor configuration. {0}. Please correct your xipton.razor configuration. Take a look at the inner exception(s) for details.".FormatWith(ex.Message),ex);
            }
        }
Пример #2
0
 private void LoadDefaults()
 {
     RootOperator = ConfigElement.Create<RootOperatorElement>();
     Templates = ConfigElement.Create<TemplatesElement>();
     Namespaces = CreateDefaultNamespaces();
     References = CreateDefaultReferences();
     ContentProviders = CreateDefaultContentProviders();
 }