/*
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.IO;
using System.Linq;
using System.Text;
using System.Resources;
using System.Reflection;
using System.Globalization;
using System.Collections.Generic;
using System.Security.Permissions;
using System.Collections;
namespace Everest.Library.Resource{
/// <summary>
///
///
/// </summary>
public class EverestResourceManager : ResourceManager
{
static Type RuntimeResourceSet = Type.GetType("System.Resources.RuntimeResourceSet");
string resourceDir;
public string ResourceDir
{
get { return resourceDir; }
set
{
resourceDir = value;
if (!string.IsNullOrEmpty(resourceDir))
{
useResourceFile = true;
}
}
}
bool useResourceFile = false;
Type resourceSetType = null;
public EverestResourceManager(Type resourceSource) : base(resourceSource) { }
public EverestResourceManager(string baseName, Assembly assembly, string resourceDir)
: base(baseName, assembly)
{
this.ResourceDir = resourceDir;
}
public EverestResourceManager(string baseName, Assembly assembly, Type usingResourceSet)
: base(baseName, assembly, usingResourceSet)
{
resourceSetType = usingResourceSet;
}
protected override ResourceSet InternalGetResourceSet(System.Globalization.CultureInfo culture, bool createIfNotExists, bool tryParents)
{
ResourceSet rs = (ResourceSet)ResourceSets[culture];
if (rs != null)
{
return rs;
}
if (useResourceFile)
{
new FileIOPermission(PermissionState.Unrestricted).Assert();
string resourceFileName = this.FindResourceFile(culture);
if (resourceFileName == null)
{
if (tryParents && !culture.Equals(CultureInfo.InvariantCulture))
{
CultureInfo info3 = culture.Parent;
rs = this.InternalGetResourceSet(info3, createIfNotExists, tryParents);
if (rs != null)
{
AddResourceSet(ResourceSets, culture, ref rs);
}
return rs;
}
}
else
{
var fileRs = this.CreateResourceSet(resourceFileName);
if (fileRs != null)
{
//replace the file ResourceSet
rs = new ResourceSet(new ResourceReader(fileRs.GetEnumerator()));
fileRs.Dispose();
AddResourceSet(ResourceSets, culture, ref rs);
}
return rs;
}
}
return base.InternalGetResourceSet(culture, createIfNotExists, tryParents);
}
private string FindResourceFile(CultureInfo culture)
{
string resourceFileName = this.GetResourceFileName(culture);
if (this.ResourceDir != null)
{
string path = Path.Combine(this.ResourceDir, resourceFileName);
if (File.Exists(path))
{
return path;
}
}
if (File.Exists(resourceFileName))
{
return resourceFileName;
}
return null;
}
private static void AddResourceSet(Hashtable localResourceSets, CultureInfo culture, ref ResourceSet rs)
{
lock (localResourceSets)
{
ResourceSet objA = (ResourceSet)localResourceSets[culture];
if (objA != null)
{
if (!object.Equals(objA, rs))
{
rs.Dispose();
rs = objA;
}
}
else
{
localResourceSets.Add(culture, rs);
}
}
}
private ResourceSet CreateResourceSet(string file)
{
ResourceSet set;
object[] args = new object[] { file };
if (this.resourceSetType == null)
{
return CreateRuntimeResourceSet(args);
}
try
{
set = (ResourceSet)Activator.CreateInstance(this.resourceSetType, args);
}
catch (MissingMethodException exception)
{
throw new InvalidOperationException("InvalidOperation_ResMgrBadResSet_Type", exception);
}
return set;
}
private ResourceSet CreateRuntimeResourceSet(object[] args)
{
ConstructorInfo constructor = RuntimeResourceSet.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, CallingConventions.Standard, new Type[] { typeof(string) }, null);
return (ResourceSet)constructor.Invoke(args);
}
}
}
|