Beispiel #1
0
        /// <summary>
        /// Sets the compiler service factory.
        /// </summary>
        /// <param name="factory">The compiler service factory.</param>
        /// <returns>The current configuration builder.</returns>
        public IConfigurationBuilder CompileUsing(ICompilerServiceFactory factory)
        {
            Contract.Requires(factory != null);

            _config.CompilerServiceFactory = factory;
            return(this);
        }
        /// <summary>
        /// Sets the <see cref="ICompilerServiceFactory"/> used to create compiler service instances.
        /// </summary>
        /// <param name="factory">The compiler service factory to use.</param>
        public static void SetCompilerServiceFactory(ICompilerServiceFactory factory)
        {
            Contract.Requires(factory != null);

            lock (sync)
            {
                _factory = factory;
            }
        }
        /// <summary>
        /// Sets the <see cref="ICompilerServiceFactory"/> used to create compiler service instances.
        /// </summary>
        /// <param name="factory">The compiler service factory to use.</param>
        public static void SetCompilerServiceFactory(ICompilerServiceFactory factory)
        {
            //Contract.Requires(factory != null);

            lock (sync)
            {
                _factory = factory;
            }
        }
Beispiel #4
0
        ///// <summary>Sets the activator.</summary>
        ///// <param name="activator">The activator instance.</param>
        ///// <returns>The current configuration builder.</returns>
        //public IConfigurationBuilder ActivateUsing(IActivator activator)
        //{
        //    if (activator == null)
        //        throw new ArgumentNullException("activator");
        //    this._config.Activator = activator;
        //    return (IConfigurationBuilder)this;
        //}

        ///// <summary>Sets the activator.</summary>
        ///// <typeparam name="TActivator">The activator type.</typeparam>
        ///// <returns>The current configuration builder.</returns>
        //public IConfigurationBuilder ActivateUsing<TActivator>() where TActivator : IActivator, new()
        //{
        //    return this.ActivateUsing((IActivator)Activator.CreateInstance<TActivator>());
        //}

        ///// <summary>Sets the activator.</summary>
        ///// <param name="activator">The activator delegate.</param>
        ///// <returns>The current configuration builder.</returns>
        //public IConfigurationBuilder ActivateUsing(Func<InstanceContext, ITemplate> activator)
        //{
        //    if (activator == null)
        //        throw new ArgumentNullException("activator");
        //    this._config.Activator = (IActivator)new DelegateActivator(activator);
        //    return (IConfigurationBuilder)this;
        //}

        ///// <summary>
        ///// Sets that dynamic models should be fault tollerant in accepting missing properties.
        ///// </summary>
        ///// <returns>The current configuration builder.</returns>
        //public IConfigurationBuilder AllowMissingPropertiesOnDynamic()
        //{
        //    this._config.AllowMissingPropertiesOnDynamic = true;
        //    return (IConfigurationBuilder)this;
        //}

        /// <summary>Sets the compiler service factory.</summary>
        /// <param name="factory">The compiler service factory.</param>
        /// <returns>The current configuration builder.</returns>
        public IConfigurationBuilder CompileUsing(ICompilerServiceFactory factory)
        {
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }
            this.config.CompilerServiceFactory = factory;
            return(this);
        }
        /// <summary>
        /// Create a new readonly view (and copy) of the given configuration.
        /// </summary>
        /// <param name="config">the configuration to copy.</param>
        public ReadOnlyTemplateServiceConfiguration(ITemplateServiceConfiguration config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            _allowMissingPropertiesOnDynamic = config.AllowMissingPropertiesOnDynamic;

            _activator = config.Activator;
            if (_activator == null)
            {
                throw new ArgumentNullException("config", "the configured Activator cannot be null!");
            }

            _baseTemplateType = config.BaseTemplateType;
            // Check if the baseTemplateType is valid.
            if (_baseTemplateType != null && (!typeof(ITemplate).IsAssignableFrom(_baseTemplateType) || typeof(ITemplate) == _baseTemplateType))
            {
                throw new ArgumentOutOfRangeException("config", "the configured BaseTemplateType must implement ITemplate!");
            }

            _cachingProvider = config.CachingProvider;
            if (_cachingProvider == null)
            {
                throw new ArgumentNullException("config", "the configured CachingProvider cannot be null!");
            }

#if !RAZOR4
#pragma warning disable 0618 // Backwards Compat.
            _codeInspectors = config.CodeInspectors;
            if (_codeInspectors == null)
            {
                throw new ArgumentNullException("config", "the configured CodeInspectos cannot be null!");
            }
            _codeInspectors = new List <ICodeInspector>(_codeInspectors);
#pragma warning restore 0618 // Backwards Compat.
#endif

            _compilerServiceFactory = config.CompilerServiceFactory;
            if (_compilerServiceFactory == null)
            {
                throw new ArgumentNullException("config", "the configured CompilerServiceFactory cannot be null!");
            }

            _debug = config.Debug;
            _disableTempFileLocking = config.DisableTempFileLocking;
            _encodedStringFactory   = config.EncodedStringFactory;
            if (_encodedStringFactory == null)
            {
                throw new ArgumentNullException("config", "the configured EncodedStringFactory cannot be null!");
            }

            _language   = config.Language;
            _namespaces = config.Namespaces;
            if (_namespaces == null)
            {
                throw new ArgumentNullException("config", "the configured Namespaces cannot be null!");
            }
            _namespaces = new HashSet <string>(_namespaces);

            _referenceResolver = config.ReferenceResolver;
            if (_referenceResolver == null)
            {
                throw new ArgumentNullException("config", "the configured ReferenceResolver cannot be null!");
            }

            _templateManager = config.TemplateManager;

#if NO_CONFIGURATION
            if (_templateManager == null)
            {
                throw new ArgumentNullException("config", "the configured TemplateManager cannot be null!");
            }
#else
#pragma warning disable 0618 // Backwards Compat.
            _resolver = config.Resolver;
            if (_templateManager == null)
            {
                if (_resolver != null)
                {
                    _templateManager = new Xml.WrapperTemplateManager(_resolver);
                }
                else
                {
                    throw new ArgumentNullException("config", "the configured TemplateManager and Resolver cannot be null!");
                }
            }
#pragma warning restore 0618 // Backwards Compat.
#endif
            ConfigureCompilerBuilder = config.ConfigureCompilerBuilder;
        }
Beispiel #6
0
        /// <summary>
        /// Uses the style.
        /// </summary>
        /// <param name="styleName">Name of the style.</param>
        /// <returns></returns>
        internal void UseStyle(string styleName)
        {
            razorCompiler = new DefaultCompilerServiceFactory();
            razor = new TemplateService(razorCompiler.CreateCompilerService(Language.CSharp, false, null), null);
            razor.SetTemplateBase(typeof(TemplateHelperBase));
            razor.AddResolver(this);

            if (!StyleManager.StyleExist(styleName))
                Logger.Fatal("Cannot us style [{0}]. Style doesn't exist", styleName);

            CurrentStyleName = styleName;

            StyleDirectories.Clear();

            var includeBaseStyle = new List<string>();

            var styles = StyleManager.AvailableStyles;
            includeBaseStyle.Add(styleName);

            bool isNotComplete = true;

            // Compute directories to look, by following inheritance
            // In the same order they are declared
            while (isNotComplete)
            {
                isNotComplete = false;
                // Build directories to look for this specific style and all its base style);
                var toRemove = new List<StyleDefinition>();
                foreach (var style in styles)
                {
                    // Apply parameter inherited from style
                    if (style.Name == styleName)
                    {
                        foreach (var parameter in style.Parameters)
                        {
                            ((DynamicParam)Param).Properties.Remove(parameter.Name);
                            ((DynamicParam)Param).Properties.Add(parameter.Name, parameter.value);
                        }
                    }

                    if (includeBaseStyle.Contains(style.Name))
                    {
                        toRemove.Add(style);
                        StyleDirectories.Add(style.DirectoryPath);
                        isNotComplete = true;
                        if (style.HasBaseStyle)
                            includeBaseStyle.Add(style.BaseStyle);
                    }
                }

                // Remove the style that was processed by the previous loop
                foreach (var styleDefinition in toRemove)
                    styles.Remove(styleDefinition);
            }

            foreach (var styleDirectory in StyleDirectories)
            {
                Logger.Message("Using Style Directory [{0}]", styleDirectory);
            }
        }
 /// <summary>
 /// Initialises the <see cref="CompilerServiceBuilder"/> type.
 /// </summary>
 static CompilerServiceBuilder()
 {
     _factory = new DefaultCompilerServiceFactory();
 }
        /// <summary>
        /// Create a new readonly view (and copy) of the given configuration.
        /// </summary>
        /// <param name="config">the configuration to copy.</param>
        public ReadOnlyTemplateServiceConfiguration(ITemplateServiceConfiguration config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            _allowMissingPropertiesOnDynamic = config.AllowMissingPropertiesOnDynamic;

            _activator = config.Activator;
            if (_activator == null)
            {
                throw new ArgumentNullException("config", "the configured Activator cannot be null!");
            }

            _baseTemplateType = config.BaseTemplateType;
            // Check if the baseTemplateType is valid.
            if (_baseTemplateType != null && (!typeof(ITemplate).IsAssignableFrom(_baseTemplateType) || typeof(ITemplate) == _baseTemplateType))
            {
                throw new ArgumentOutOfRangeException("config", "the configured BaseTemplateType must implement ITemplate!");
            }
            
            _cachingProvider = config.CachingProvider;
            if (_cachingProvider == null)
            {
                throw new ArgumentNullException("config", "the configured CachingProvider cannot be null!");
            }

#if !RAZOR4
#pragma warning disable 0618 // Backwards Compat.
            _codeInspectors = config.CodeInspectors;
            if (_codeInspectors == null)
            {
                throw new ArgumentNullException("config", "the configured CodeInspectos cannot be null!");
            }
            _codeInspectors = new List<ICodeInspector>(_codeInspectors);
#pragma warning restore 0618 // Backwards Compat.
#endif

            _compilerServiceFactory = config.CompilerServiceFactory;
            if (_compilerServiceFactory == null)
            {
                throw new ArgumentNullException("config", "the configured CompilerServiceFactory cannot be null!");
            }

            _debug = config.Debug;
            _disableTempFileLocking = config.DisableTempFileLocking;
            _encodedStringFactory = config.EncodedStringFactory;
            if (_encodedStringFactory == null)
            {
                throw new ArgumentNullException("config", "the configured EncodedStringFactory cannot be null!");
            }

            _language = config.Language;
            _namespaces = config.Namespaces;
            if (_namespaces == null)
            {
                throw new ArgumentNullException("config", "the configured Namespaces cannot be null!");
            }
            _namespaces = new HashSet<string>(_namespaces);

            _referenceResolver = config.ReferenceResolver;
            if (_referenceResolver == null)
            {
                throw new ArgumentNullException("config", "the configured ReferenceResolver cannot be null!");
            }

#pragma warning disable 0618 // Backwards Compat.
            _resolver = config.Resolver;
            _templateManager = config.TemplateManager;
            if (_templateManager == null)
            {
                if (_resolver != null)
                {
                    _templateManager = new Xml.WrapperTemplateManager(_resolver);
                }
                else
                {
                    throw new ArgumentNullException("config", "the configured TemplateManager and Resolver cannot be null!");
                }
            }
#pragma warning restore 0618 // Backwards Compat.

        }
 /// <summary>
 /// Initialises the <see cref="CompilerServiceBuilder"/> type.
 /// </summary>
 static CompilerServiceBuilder()
 {
     _factory = new DefaultCompilerServiceFactory();
 }
Beispiel #10
0
        /// <summary>
        /// Uses the style.
        /// </summary>
        /// <param name="styleName">Name of the style.</param>
        /// <returns></returns>
        internal void UseStyle(string styleName)
        {
            razorCompiler = new DefaultCompilerServiceFactory();
            razor         = new TemplateService(razorCompiler.CreateCompilerService(Language.CSharp, false, null), null);
            razor.SetTemplateBase(typeof(TemplateHelperBase));
            razor.AddResolver(this);

            if (!StyleManager.StyleExist(styleName))
            {
                Logger.Fatal("Cannot us style [{0}]. Style doesn't exist", styleName);
            }

            CurrentStyleName = styleName;

            StyleDirectories.Clear();

            var includeBaseStyle = new List <string>();

            var styles = StyleManager.AvailableStyles;

            includeBaseStyle.Add(styleName);

            bool isNotComplete = true;

            // Compute directories to look, by following inheritance
            // In the same order they are declared
            while (isNotComplete)
            {
                isNotComplete = false;
                // Build directories to look for this specific style and all its base style);
                var toRemove = new List <StyleDefinition>();
                foreach (var style in styles)
                {
                    // Apply parameter inherited from style
                    if (style.Name == styleName)
                    {
                        foreach (var parameter in style.Parameters)
                        {
                            ((DynamicParam)Param).Properties.Remove(parameter.Name);
                            ((DynamicParam)Param).Properties.Add(parameter.Name, parameter.value);
                        }
                    }

                    if (includeBaseStyle.Contains(style.Name))
                    {
                        toRemove.Add(style);
                        StyleDirectories.Add(style.DirectoryPath);
                        isNotComplete = true;
                        if (style.HasBaseStyle)
                        {
                            includeBaseStyle.Add(style.BaseStyle);
                        }
                    }
                }

                // Remove the style that was processed by the previous loop
                foreach (var styleDefinition in toRemove)
                {
                    styles.Remove(styleDefinition);
                }
            }

            foreach (var styleDirectory in StyleDirectories)
            {
                Logger.Message("Using Style Directory [{0}]", styleDirectory);
            }
        }
        /// <summary>
        /// Create a new readonly view (and copy) of the given configuration.
        /// </summary>
        /// <param name="config">the configuration to copy.</param>
        public ReadOnlyTemplateServiceConfiguration(ITemplateServiceConfiguration config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            _allowMissingPropertiesOnDynamic = config.AllowMissingPropertiesOnDynamic;

            _activator = config.Activator;
            if (_activator == null)
            {
                throw new ArgumentNullException("config", "the configured Activator cannot be null!");
            }

            _baseTemplateType = config.BaseTemplateType;
            // Check if the baseTemplateType is valid.
            if (_baseTemplateType != null && (!typeof(ITemplate).IsAssignableFrom(_baseTemplateType) || typeof(ITemplate) == _baseTemplateType))
            {
                throw new ArgumentOutOfRangeException("config", "the configured BaseTemplateType must implement ITemplate!");
            }

            _cachingProvider = config.CachingProvider;
            if (_cachingProvider == null)
            {
                throw new ArgumentNullException("config", "the configured CachingProvider cannot be null!");
            }


            _compilerServiceFactory = config.CompilerServiceFactory;
            if (_compilerServiceFactory == null)
            {
                throw new ArgumentNullException("config", "the configured CompilerServiceFactory cannot be null!");
            }

            _debug = config.Debug;
            _disableTempFileLocking = config.DisableTempFileLocking;
            _encodedStringFactory   = config.EncodedStringFactory;
            if (_encodedStringFactory == null)
            {
                throw new ArgumentNullException("config", "the configured EncodedStringFactory cannot be null!");
            }

            _language   = config.Language;
            _namespaces = config.Namespaces;
            if (_namespaces == null)
            {
                throw new ArgumentNullException("config", "the configured Namespaces cannot be null!");
            }
            _namespaces = new HashSet <string>(_namespaces);

            _referenceResolver = config.ReferenceResolver;
            if (_referenceResolver == null)
            {
                throw new ArgumentNullException("config", "the configured ReferenceResolver cannot be null!");
            }

            _templateManager = config.TemplateManager;


            if (_templateManager == null)
            {
                throw new ArgumentNullException("config", "the configured TemplateManager cannot be null!");
            }

            ConfigureCompilerBuilder = config.ConfigureCompilerBuilder;
        }