CachedLocalizedValidatorRegistry.cs :  » Content-Management-Systems-CMS » Cuyahoga » Cuyahoga » Core » Validation » 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 » Cuyahoga 
Cuyahoga » Cuyahoga » Core » Validation » CachedLocalizedValidatorRegistry.cs
using System;
using System.Collections;
using System.Reflection;
using System.Resources;
using System.Threading;
using Castle.Components.Validator;

namespace Cuyahoga.Core.Validation{
  /// <summary>
  /// Validator registry that caches validator attributes. Also, validation messages are localized by translating the error
  /// message. The error message that comes from the attribute is the key for the error message.
  /// </summary>
  public class CachedLocalizedValidatorRegistry : ILocalizedValidatorRegistry
  {
    private static ResourceManager defaultResourceManager;
    private ResourceManager resourceManager;

    private readonly IDictionary propertiesPerType = Hashtable.Synchronized(new Hashtable());
    private readonly IDictionary attrsPerProperty = Hashtable.Synchronized(new Hashtable());

    /// <summary>
    /// Initializes the <see cref="CachedLocalizedValidatorRegistry"/> class.
    /// </summary>
    static CachedLocalizedValidatorRegistry()
    {
      defaultResourceManager =
        new ResourceManager("Castle.Components.Validator.Messages",
                  typeof(CachedValidationRegistry).Assembly);
    }

    /// <summary>
    /// Initializes the <see cref="CachedLocalizedValidatorRegistry"/> class.
    /// </summary>
    public CachedLocalizedValidatorRegistry()
    {}

    /// <summary>
    /// Initializes a new instance of the <see cref="CachedLocalizedValidatorRegistry"/> class.
    /// </summary>
    /// <param name="resourceManager">The resource manager.</param>
    public CachedLocalizedValidatorRegistry(ResourceManager resourceManager)
    {
      this.resourceManager = resourceManager;
    }

    /// <summary>
    /// The resource manager
    /// </summary>
    public ResourceManager ResourceManager
    {
      get { return this.resourceManager; }
      set { this.resourceManager = value; }
    }


    /// <summary>
    /// Gets all validators associated with a <see cref="Type"/>.
    /// <para>
    /// The validators returned are initialized.
    /// </para>
    /// </summary>
    /// <param name="validatorRunner">The validator runner.</param>
    /// <param name="targetType">Target type.</param>
    /// <param name="runWhen">Restrict the set returned to the phase specified</param>
    /// <returns>A Validator array</returns>
    public IValidator[] GetValidators(IValidatorRunner validatorRunner, Type targetType, RunWhen runWhen)
    {
      PropertyInfo[] properties = (PropertyInfo[])propertiesPerType[targetType];

      if (properties == null)
      {
        this.propertiesPerType[targetType] = properties = ResolveProperties(targetType);
      }

      ArrayList list = new ArrayList();

      foreach (PropertyInfo prop in properties)
      {
        list.AddRange(GetValidators(validatorRunner, targetType, prop, runWhen));
      }

      return (IValidator[])list.ToArray(typeof(IValidator));
    }

    /// <summary>
    /// Gets all validators associated with a property.
    /// <para>
    /// The validators returned are initialized.
    /// </para>
    /// </summary>
    /// <param name="validatorRunner">The validator runner.</param>
    /// <param name="targetType">Target type.</param>
    /// <param name="property">The property.</param>
    /// <param name="runWhen">Restrict the set returned to the phase specified</param>
    /// <returns>A Validator array</returns>
    public IValidator[] GetValidators(IValidatorRunner validatorRunner, Type targetType, PropertyInfo property, RunWhen runWhen)
    {
      object[] builders = (object[])this.attrsPerProperty[property];

      if (builders == null)
      {
        builders = property.GetCustomAttributes(typeof(IValidatorBuilder), true);
        this.attrsPerProperty[property] = builders;
      }

      ArrayList validators = new ArrayList();

      foreach (IValidatorBuilder builder in builders)
      {
        IValidator validator = builder.Build(validatorRunner, targetType);

        if (!IsValidatorOnPhase(validator, runWhen)) continue;

        validator.Initialize(this, property);
        // Translate the error message of the validator. This way we can use the error message of a validator as resource key.
        string translatedErrorMessage = TranslateErrorMessage(validator.ErrorMessage);
        if (translatedErrorMessage != null)
        {
          validator.ErrorMessage = translatedErrorMessage;
        }
        validators.Add(validator);
      }

      return (IValidator[])validators.ToArray(typeof(IValidator));
    }

    public Accessor GetPropertyAccessor(PropertyInfo property)
    {
      return AccessorUtil.GetAccessor(property);
    }

    public Accessor GetFieldOrPropertyAccessor(Type targetType, string path)
    {
      return AccessorUtil.GetAccessor(targetType, path);
    }

    /// <summary>
    /// Gets the string from resource by key
    /// </summary>
    /// <param name="key">The key.</param>
    /// <remarks>Only used when there is no error message given via the attribute.</remarks>
    /// <returns></returns>
    public string GetStringFromResource(string key)
    {
      return defaultResourceManager.GetString(key, Thread.CurrentThread.CurrentUICulture);
    }

    /// <summary>
    /// Gets a translated error message from the resource manager.
    /// </summary>
    /// <param name="originalMessage"></param>
    /// <remarks></remarks>
    /// <returns></returns>
    public string TranslateErrorMessage(string originalMessage)
    {
      if (resourceManager != null)
      {
        string result = resourceManager.GetString(originalMessage, Thread.CurrentThread.CurrentUICulture);
        if (result != null)
          return result;
      }
      return originalMessage;
    }

    /// <summary>
    /// Resolve properties that will be inspected for registered validators
    /// </summary>
    /// <param name="targetType">the type to examinate properties for</param>
    /// <returns>resolved properties</returns>
    protected virtual PropertyInfo[] ResolveProperties(Type targetType)
    {
      return targetType.GetProperties();
    }

    private static bool IsValidatorOnPhase(IValidator validator, RunWhen when)
    {
      if (validator.RunWhen == RunWhen.Everytime) return true;

      return ((validator.RunWhen & when) != 0);
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.