Click here to Skip to main content
15,867,568 members
Articles / Web Development / CSS

Combres - WebForm & MVC Client-side Resource Combine Library

Rate me:
Please Sign up or sign in to vote.
4.50/5 (8 votes)
1 Nov 2009Apache13 min read 51.9K   3.1K   22  
A .NET library which enables minification, compression, combination, and caching of JavaScript and CSS resources for ASP.NET and ASP.NET MVC web applications. Simply put, it helps your applications rank better with YSlow and PageSpeed.
#region License
// Copyright 2009 Buu Nguyen (http://www.buunguyen.net/blog)
// 
// Licensed under the Apache License, Version 2.0 (the "License"); 
// you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at 
// 
// http://www.apache.org/licenses/LICENSE-2.0 
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
// See the License for the specific language governing permissions and 
// limitations under the License.
// 
// The latest version of this file can be found at http://combres.codeplex.com
#endregion

using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;

namespace Combres
{
    /// <summary>
    /// Represents the resource combination settings.
    /// </summary>
    public class Settings
    {
        /// <summary>
        /// Relative URL of the combiner.   
        /// </summary>
        /// <example>
        /// combres.axd
        /// </example>
        public string Url { get; private set; }

        /// <summary>
        /// Default version which will be used if <see cref="ResourceSet.DurationInDays"/> is 
        /// not specified.
        /// </summary>
        public int? DefaultDuration { get; private set; }

        /// <summary>
        /// Default version which will be used if <see cref="ResourceSet.Version"/> is 
        /// not specified.
        /// </summary>
        public string DefaultVersion { get; private set; }

        /// <summary>
        /// If true, resource sets' resources won't be cached, compressed and minimized 
        /// to facilitate debugging.
        /// </summary>
        public bool DefaultDebugEnabled { get; private set; }

        /// <summary>
        /// Filter types.
        /// </summary>
        private readonly List<Type> filterTypes = new List<Type>();

        /// <summary>
        /// Lists of filter types.
        /// </summary>
        public List<Type> FilterTypes
        {
            get { return filterTypes; }
        }

        /// <summary>
        /// Child resource sets.
        /// </summary>
        private readonly List<ResourceSet> resourceSets = new List<ResourceSet>();

        /// <summary>
        /// Lists of child resource sets.
        /// </summary>
        public List<ResourceSet> ResourceSets
        {
            get { return resourceSets; }
        }

        internal Settings(XmlElement element)
        {
            foreach (var node in element.ChildNodes)
            {
                var child = node as XmlElement;
                if (child == null) continue;
                if (child.Name == NodeConstants.FiltersName)
                    LoadFilters(child);
                else
                    LoadResourceSets(child);
            }
        }

        private void LoadFilters(XmlNode element)
        {
            foreach (var node in element.ChildNodes)
            {
                if (!(node is XmlElement)) continue;
                var typeName = ((node as XmlElement)).GetAttribute(NodeConstants.FilterType);
                var type = Type.GetType(typeName);
                if (type == null)
                    throw new FilterNotFoundException(string.Format(
                                                          "Filter {0} cannot be found", typeName));
                if (!typeof(ICombresFilter).IsAssignableFrom(type))
                    throw new FilterNotFoundException(string.Format(
                                                          "Filter {0} must implements ICombresFilter interface", typeName));
                if (!filterTypes.Contains(type))
                    filterTypes.Add(type);
            }
        }

        private void LoadResourceSets(XmlElement element)
        {
            Url = element.GetAttribute(NodeConstants.SettingUrl);
            DefaultDuration = GetDuration(element, NodeConstants.SettingDefaultDuration);
            var version = element.GetAttribute(NodeConstants.SettingDefaultVersion);
            DefaultVersion = string.IsNullOrEmpty(version)
                ? null
                : version;
            DefaultDebugEnabled = IsDebugEnabled(element, NodeConstants.SettingDefaultDebugEnabled);
            foreach (var node in element.ChildNodes)
            {
                if (node is XmlElement)
                {
                    var rs = new ResourceSet(this, node as XmlElement);
                    if (!resourceSets.Contains(rs))
                        resourceSets.Add(rs);
                }
            }
        }

        private static bool IsDebugEnabled(XmlElement element, string attributeName)
        {
            return element.HasAttribute(attributeName)
                       ? bool.Parse(element.GetAttribute(attributeName))
                       : false;
        }

        private static int? GetDuration(XmlElement element, string attributeName)
        {
            return element.HasAttribute(attributeName)
                       ? (int?) int.Parse(element.GetAttribute(attributeName))
                       : null;
        }

        public ResourceSet this[string setName]
        {
            get
            {
                foreach (var set in resourceSets)
                {
                    if (set.Name == setName)
                    {
                        return set;
                    }
                }
                return null;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Chief Technology Officer KMS Technology
Vietnam Vietnam
You can visit Buu's blog at http://www.buunguyen.net/blog to read about his thoughts on software development.

Comments and Discussions