HtmlExtensionUtility.cs :  » Content-Management-Systems-CMS » Kooboo » System » Web » Mvc » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Content Management Systems CMS » Kooboo 
Kooboo » System » Web » Mvc » HtmlExtensionUtility.cs
/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.

This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Reflection;
using System.Web;
using System.Web.Compilation;
using System.IO;
using System.Collections.Specialized;
using System.Linq.Expressions;
using System.Data;

namespace System.Web.Mvc{
    public static class HtmlExtensionUtility
    {

        #region Utility

        internal static bool AreEqual(object o1, object o2)
        {
            bool result = false;

            if (o1 == null && o2 == null)
            {
                result = true;
            }
            else
            {
                if (o1 != null)
                {
                    Type t = o1.GetType();
                    o2 = System.Convert.ChangeType(o2, t);
                    result = o1.Equals(o2);
                }

            }

            return result;
        }

        internal static string EvalHashSetting(Hashtable hash, string key)
        {
            string result = string.Empty;
            if (hash.Keys.Count > 0)
            {
                if (hash.ContainsKey(key))
                {
                    return hash[key].ToString();
                }
            }
            return result;
        }


        public static string RenderPage(System.Web.UI.Page pg)
        {
            //render
            StringBuilder sb = new StringBuilder();
            StringWriter writer = new StringWriter(sb);

            try
            {
                HttpContext.Current.Server.Execute(pg, writer, true);
            }
            catch (Exception x)
            {
                if (x.InnerException != null)
                    throw x.InnerException;
                else
                    throw x;
            }
            string result = sb.ToString();
            return result;

        }


        /// <summary>
        /// Creates a formatted list of items based on the passed in format
        /// </summary>
        /// <param name="list">The item list</param>
        /// <param name="format">The single-place format string to use</param>
        public static string ToFormattedList(this IEnumerable list, string format)
        {
            StringBuilder sb = new StringBuilder();
            IEnumerator en = list.GetEnumerator();

            while (en.MoveNext())
            {
                sb.AppendFormat(format, en.Current.ToString());
            }
            return sb.ToString();

        }

        /// <summary>
        /// Finds a PropertyInfo item inside of a PropertyInfo[] Array regardless of case
        /// </summary>
        internal static PropertyInfo FindProp(string propName, PropertyInfo[] props)
        {
            PropertyInfo result = null;
            foreach (PropertyInfo p in props)
            {
                if (p.Name.ToLower().Trim().Equals(propName.ToLower().Trim()))
                {
                    result = p;
                    break;
                }
            }
            return result;
        }

        /// <summary>
        /// Sets the properties of an object based on a HashTable
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="settings"></param>
        public static void SetPropsFromHash(object instance, Hashtable settings)
        {
            PropertyInfo pInfo = null;
            foreach (string key in settings.Keys)
            {
                //find a matching property
                pInfo = instance.GetType().GetProperty(key, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public);
                if (pInfo == null)
                {
                    throw new RenderViewControlException(string.Format("The property \"{0}\" is not found in \"{1}\",please check if the property is public.", key, instance.GetType()));
                }
                if (pInfo != null)
                {
                    pInfo.SetValue(instance, settings[key], null);
                }
            }
        }

        public static void SetProperties(object instance, object propertySettings)
        {
            if (propertySettings != null)
            {
                var props = HtmlExtensionUtility.GetPropertyHash(propertySettings);
                HtmlExtensionUtility.SetPropsFromHash(instance, props);
            }
        }

        /// <summary>
        /// This is for utility only - used in the InstanceUserControl method
        /// </summary>
        internal class CustomPage : ViewPage
        {
        }

        /// <summary>
        /// Creates a simple {0}='{1}' list based on current object state.
        /// </summary>
        public static string ToAttributeList(this object o)
        {
            StringBuilder sb = new StringBuilder();
            if (o != null)
            {
                Hashtable attributeHash = GetPropertyHash(o);

                string resultFormat = "{0}=\"{1}\" ";
                foreach (string attribute in attributeHash.Keys)
                {
                    sb.AppendFormat(resultFormat, attribute.Replace("_", ""), attributeHash[attribute]);
                }
            }
            return sb.ToString();

        }

        /// <summary>
        /// Creates a simple {0}='{1}' list based on current object state. Ignores the passed-in string[] items
        /// </summary>
        /// <param name="o"></param>
        /// <param name="ignoreList"></param>
        /// <returns></returns>
        public static string ToAttributeList(this object o, params object[] ignoreList)
        {
            Hashtable attributeHash = GetPropertyHash(o);

            string resultFormat = "{0}=\"{1}\" ";
            StringBuilder sb = new StringBuilder();
            foreach (string attribute in attributeHash.Keys)
            {
                if (!ignoreList.Contains(attribute))
                {
                    sb.AppendFormat(resultFormat, attribute, attributeHash[attribute]);
                }
            }
            return sb.ToString();

        }

        /// <summary>
        /// Creates a HashTable based on current object state
        /// </summary>
        /// <param name="properties"></param>
        /// <returns></returns>
        public static Hashtable GetPropertyHash(object properties)
        {

            Hashtable values = null;

            if (properties != null)
            {
                values = new Hashtable();

                PropertyDescriptorCollection props = TypeDescriptor.GetProperties(properties);

                foreach (PropertyDescriptor prop in props)
                {
                    values.Add(prop.Name, prop.GetValue(properties));
                }
            }

            return values;
        }
        #endregion


    }
}

www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.