Example #1
0
        private static bool TryGetFromResourceFile(string key, string resourceFile, int portalID,
                                                   CustomizedLocale resourceType, ref string resourceValue)
        {
            var bFound   = Null.NullBoolean;
            var cacheKey = GetResourceFileCacheKey(resourceFile, portalID, resourceType);

            //Get resource file lookup to determine if the resource file even exists
            var resourceFileExistsLookup = GetResourceFileLookupDictionary();

            if (ResourceFileMayExist(resourceFileExistsLookup, cacheKey))
            {
                //File is not in lookup or its value is true so we know it exists
                var dicResources = GetResourceFile(cacheKey);
                if (dicResources != null)
                {
                    bFound = dicResources.TryGetValue(key, out resourceValue);
                }
            }

            return(bFound);
        }
        private static bool TryGetFromResourceFile(string key, string resourceFile, int portalID, CustomizedLocale resourceType, ref string resourceValue)
        {
            bool   bFound           = Null.NullBoolean;
            string resourceFileName = resourceFile;

            switch (resourceType)
            {
            case CustomizedLocale.Host:
                resourceFileName = resourceFile.Replace(".resx", ".Host.resx");
                break;

            case CustomizedLocale.Portal:
                resourceFileName = resourceFile.Replace(".resx", ".Portal-" + portalID + ".resx");
                break;
            }

            if (resourceFileName.StartsWith("desktopmodules", StringComparison.InvariantCultureIgnoreCase) ||
                resourceFileName.StartsWith("admin", StringComparison.InvariantCultureIgnoreCase) ||
                resourceFileName.StartsWith("controls", StringComparison.InvariantCultureIgnoreCase))
            {
                resourceFileName = "~/" + resourceFileName;
            }

            //Local resource files are either named ~/... or <ApplicationPath>/...
            //The following logic creates a cachekey of /....
            string cacheKey = resourceFileName.Replace("~/", "/").ToLowerInvariant();

            if (!String.IsNullOrEmpty(Globals.ApplicationPath))
            {
                if (Globals.ApplicationPath != "/portals")
                {
                    if (cacheKey.StartsWith(Globals.ApplicationPath))
                    {
                        cacheKey = cacheKey.Substring(Globals.ApplicationPath.Length);
                    }
                }
                else
                {
                    cacheKey = "~" + cacheKey;
                    if (cacheKey.StartsWith("~" + Globals.ApplicationPath))
                    {
                        cacheKey = cacheKey.Substring(Globals.ApplicationPath.Length + 1);
                    }
                }
            }

            //Get resource file lookup to determine if the resource file even exists
            SharedDictionary <string, bool> resourceFileExistsLookup = GetResourceFileLookupDictionary();

            if (ResourceFileMayExist(resourceFileExistsLookup, cacheKey))
            {
                //File is not in lookup or its value is true so we know it exists
                Dictionary <string, string> dicResources = GetResourceFile(cacheKey);
                if (dicResources != null)
                {
                    bFound = dicResources.TryGetValue(key, out resourceValue);
                }
            }

            return(bFound);
        }
Example #3
0
        private static string GetResourceFileCacheKey(string resourceFile, int portalID, CustomizedLocale resourceType)
        {
            var resourceFileName = resourceFile;

            switch (resourceType)
            {
            case CustomizedLocale.Host:
                resourceFileName = resourceFile.Replace(".resx", ".Host.resx");
                break;

            case CustomizedLocale.Portal:
                resourceFileName = resourceFile.Replace(".resx", ".Portal-" + portalID + ".resx");
                break;
            }

            if (resourceFileName.StartsWith("desktopmodules", StringComparison.InvariantCultureIgnoreCase) ||
                resourceFileName.StartsWith("admin", StringComparison.InvariantCultureIgnoreCase) ||
                resourceFileName.StartsWith("controls", StringComparison.InvariantCultureIgnoreCase))
            {
                resourceFileName = "~/" + resourceFileName;
            }

            //Local resource files are either named ~/... or <ApplicationPath>/...
            //The following logic creates a cachekey of /....
            var cacheKey = resourceFileName.Replace("~/", "/").ToLowerInvariant();

            if (!string.IsNullOrEmpty(Globals.ApplicationPath))
            {
                if (Globals.ApplicationPath != "/portals")
                {
                    if (cacheKey.StartsWith(Globals.ApplicationPath))
                    {
                        cacheKey = cacheKey.Substring(Globals.ApplicationPath.Length);
                    }
                }
                else
                {
                    cacheKey = "~" + cacheKey;
                    if (cacheKey.StartsWith("~" + Globals.ApplicationPath))
                    {
                        cacheKey = cacheKey.Substring(Globals.ApplicationPath.Length + 1);
                    }
                }
            }
            return(cacheKey);
        }
        /// <summary>
        /// Saves a string to a resource file.
        /// </summary>
        /// <param name="key">The key to save (e.g. "MyWidget.Text").</param>
        /// <param name="value">The text value for the key.</param>
        /// <param name="resourceFileRoot">Relative path for the resource file root (e.g. "DesktopModules/Admin/Lists/App_LocalResources/ListEditor.ascx.resx").</param>
        /// <param name="language">The locale code in lang-region format (e.g. "fr-FR").</param>
        /// <param name="portalSettings">The current portal settings.</param>
        /// <param name="resourceType">Specifies whether to save as portal, host or system resource file.</param>
        /// <param name="createFile">if set to <c>true</c> a new file will be created if it is not found.</param>
        /// <param name="createKey">if set to <c>true</c> a new key will be created if not found.</param>
        /// <returns>If the value could be saved then true will be returned, otherwise false.</returns>
        /// <exception cref="System.Exception">Any file io error or similar will lead to exceptions.</exception>
        public bool SaveString(string key, string value, string resourceFileRoot, string language, PortalSettings portalSettings, CustomizedLocale resourceType, bool createFile, bool createKey)
        {
            try
            {
                if (key.IndexOf(".", StringComparison.Ordinal) < 1)
                {
                    key += ".Text";
                }

                string resourceFileName = GetResourceFileName(resourceFileRoot, language);
                resourceFileName = resourceFileName.Replace("." + language.ToLowerInvariant() + ".", "." + language + ".");
                switch (resourceType)
                {
                case CustomizedLocale.Host:
                    resourceFileName = resourceFileName.Replace(".resx", ".Host.resx");
                    break;

                case CustomizedLocale.Portal:
                    resourceFileName = resourceFileName.Replace(".resx", ".Portal-" + portalSettings.PortalId + ".resx");
                    break;
                }

                resourceFileName = resourceFileName.TrimStart('~', '/', '\\');
                string      filePath = HostingEnvironment.MapPath("~/" + resourceFileName);
                XmlDocument doc      = null;
                if (File.Exists(filePath))
                {
                    doc = new XmlDocument {
                        XmlResolver = null
                    };
                    doc.Load(filePath);
                }
                else
                {
                    if (createFile)
                    {
                        doc = new XmlDocument {
                            XmlResolver = null
                        };
                        doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", "yes"));
                        XmlNode root = doc.CreateElement("root");
                        doc.AppendChild(root);
                        AddResourceFileNode(ref root, "resheader", "resmimetype", "text/microsoft-resx");
                        AddResourceFileNode(ref root, "resheader", "version", "2.0");
                        AddResourceFileNode(ref root, "resheader", "reader", "System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
                        AddResourceFileNode(ref root, "resheader", "writer", "System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
                    }
                }

                if (doc == null)
                {
                    return(false);
                }

                XmlNode reskeyNode = doc.SelectSingleNode("root/data[@name=\"" + key + "\"]");
                if (reskeyNode != null)
                {
                    reskeyNode.SelectSingleNode("value").InnerText = value;
                }
                else
                {
                    if (createKey)
                    {
                        XmlNode root = doc.SelectSingleNode("root");
                        AddResourceFileNode(ref root, "data", key, value);
                    }
                    else
                    {
                        return(false);
                    }
                }

                doc.Save(filePath);
                DataCache.RemoveCache("/" + resourceFileName.ToLowerInvariant());
                return(true);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Error while trying to create resource in {0}", resourceFileRoot), ex);
            }
        }
Example #5
0
        // ReSharper disable SuggestBaseTypeForParameter
        private static IDictionary <string, string> LoadResource(
            IDictionary <string, string> resources, string cacheKey, string resourceFile, CustomizedLocale checkCustomCulture, int portalId)
        {
            //// ReSharper restore SuggestBaseTypeForParameter
            string f = null;

            // Are we looking for customised resources
            switch (checkCustomCulture)
            {
            case CustomizedLocale.None:
                f = resourceFile;
                break;

            case CustomizedLocale.Portal:
                f = resourceFile.Replace(".RESX", ".Portal-" + portalId.ToString(CultureInfo.InvariantCulture) + ".resx");
                break;

            case CustomizedLocale.Host:
                f = resourceFile.Replace(".RESX", ".Host.resx");
                break;
            }

            // If the filename is empty or the file does not exist return the dictionary
            string filePath = HostingEnvironment.MapPath(f);

            if (f == null || !File.Exists(filePath))
            {
                return(resources);
            }

            XPathDocument doc = null;

            using (var dp = new DNNCacheDependency(filePath))
            {
                bool xmlLoaded;
                try
                {
                    // ReSharper disable AssignNullToNotNullAttribute
                    doc = new XPathDocument(filePath);

                    // ReSharper restore AssignNullToNotNullAttribute
                    xmlLoaded = true;
                }
                catch
                {
                    xmlLoaded = false;
                }

                if (xmlLoaded)
                {
                    foreach (XPathNavigator nav in doc.CreateNavigator().Select("root/data"))
                    {
                        if (nav.NodeType != XPathNodeType.Comment)
                        {
                            resources[nav.GetAttribute("name", string.Empty)] = nav.SelectSingleNode("value").Value;
                        }
                    }

                    try
                    {
                        int cacheMinutes = 3 * (int)Host.PerformanceSetting;
                        if (cacheMinutes > 0)
                        {
                            DataCache.SetCache(cacheKey, resources, dp, DateTime.MaxValue, new TimeSpan(0, cacheMinutes, 0));
                        }
                    }

#pragma warning disable 1692
#pragma warning disable EmptyGeneralCatchClause
                    catch
                    {
                    }

#pragma warning restore EmptyGeneralCatchClause
#pragma warning restore 1692
                }
            }

            return(resources);
        }
 private static bool TryGetFromResourceFile(string key, string resourceFile, int portalID, CustomizedLocale resourceType, ref string resourceValue)
 {
     Dictionary<string, string> dicResources = null;
     bool bFound = Null.NullBoolean;
     string resourceFileName = resourceFile;
     switch (resourceType)
     {
         case CustomizedLocale.Host:
             resourceFileName = resourceFile.Replace(".resx", ".Host.resx");
             break;
         case CustomizedLocale.Portal:
             resourceFileName = resourceFile.Replace(".resx", ".Portal-" + portalID.ToString() + ".resx");
             break;
     }
     if (resourceFileName.StartsWith("desktopmodules", StringComparison.InvariantCultureIgnoreCase) || resourceFileName.StartsWith("admin", StringComparison.InvariantCultureIgnoreCase))
     {
         resourceFileName = "~/" + resourceFileName;
     }
     string cacheKey = resourceFileName.Replace("~/", "/").ToLowerInvariant();
     if (!string.IsNullOrEmpty(Globals.ApplicationPath))
     {
         if (Globals.ApplicationPath != "/portals")
         {
             if (cacheKey.StartsWith(Globals.ApplicationPath))
             {
                 cacheKey = cacheKey.Substring(Globals.ApplicationPath.Length);
             }
         }
         else
         {
             cacheKey = "~" + cacheKey;
             if (cacheKey.StartsWith("~" + Globals.ApplicationPath))
             {
                 cacheKey = cacheKey.Substring(Globals.ApplicationPath.Length + 1);
             }
         }
     }
     Dictionary<string, bool> resourceFileExistsLookup = GetResourceFileLookupDictionary();
     if ((!resourceFileExistsLookup.ContainsKey(cacheKey)) || resourceFileExistsLookup[cacheKey])
     {
         dicResources = GetResourceFile(cacheKey);
         if (dicResources != null)
         {
             bFound = dicResources.TryGetValue(key, out resourceValue);
         }
     }
     return bFound;
 }
        // ReSharper disable SuggestBaseTypeForParameter
        private static IDictionary<string, string> LoadResource(
            IDictionary<string, string> resources, string cacheKey, string resourceFile, CustomizedLocale checkCustomCulture, int portalId)
        {
            //// ReSharper restore SuggestBaseTypeForParameter
            string f = null;

            // Are we looking for customised resources
            switch (checkCustomCulture)
            {
                case CustomizedLocale.None:
                    f = resourceFile;
                    break;
                case CustomizedLocale.Portal:
                    f = resourceFile.Replace(".RESX", ".Portal-" + portalId.ToString(CultureInfo.InvariantCulture) + ".resx");
                    break;
                case CustomizedLocale.Host:
                    f = resourceFile.Replace(".RESX", ".Host.resx");
                    break;
            }

            // If the filename is empty or the file does not exist return the dictionary
            string filePath = HostingEnvironment.MapPath(f);
            if (f == null || !File.Exists(filePath))
            {
                return resources;
            }

            XPathDocument doc = null;
            using (var dp = new DNNCacheDependency(filePath))
            {
                bool xmlLoaded;
                try
                {
                    // ReSharper disable AssignNullToNotNullAttribute
                    doc = new XPathDocument(filePath);

                    // ReSharper restore AssignNullToNotNullAttribute
                    xmlLoaded = true;
                }
                catch
                {
                    xmlLoaded = false;
                }

                if (xmlLoaded)
                {
                    foreach (XPathNavigator nav in doc.CreateNavigator().Select("root/data"))
                    {
                        if (nav.NodeType != XPathNodeType.Comment)
                        {
                            resources[nav.GetAttribute("name", string.Empty)] = nav.SelectSingleNode("value").Value;
                        }
                    }

                    try
                    {
                        int cacheMinutes = 3 * (int)Host.PerformanceSetting;
                        if (cacheMinutes > 0)
                        {
                            DataCache.SetCache(cacheKey, resources, dp, DateTime.MaxValue, new TimeSpan(0, cacheMinutes, 0));
                        }
                    }

            #pragma warning disable 1692
            #pragma warning disable EmptyGeneralCatchClause
                    catch
                    {
                    }

            #pragma warning restore EmptyGeneralCatchClause
            #pragma warning restore 1692
                }
            }

            return resources;
        }
Example #8
0
        private static Hashtable LoadResource( Hashtable target, string language, string cacheKey, string resourceFile, CustomizedLocale CheckCustomCulture, PortalSettings portalSettings )
        {
            string filePath;
            string f = Null.NullString;

            //Are we looking for customised resources
            switch (CheckCustomCulture)
            {
                case CustomizedLocale.None:

                    f = resourceFile;
                    break;
                case CustomizedLocale.Portal:

                    f = resourceFile.Replace(".resx", ".Portal-" + portalSettings.PortalId + ".resx");
                    break;
                case CustomizedLocale.Host:

                    f = resourceFile.Replace(".resx", ".Host.resx");
                    break;
            }

            //If the filename is empty or the file does not exist return the Hashtable
            if (f == null || File.Exists(HttpContext.Current.Server.MapPath(f)) == false)
            {
                return target;
            }

            filePath = HttpContext.Current.Server.MapPath(f);

            CacheDependency dp = new CacheDependency(filePath);
            bool xmlLoaded;
            XPathDocument doc = null;
            try
            {
                doc = new XPathDocument( filePath );
                xmlLoaded = true;
            }
            catch //exc As Exception
            {
                xmlLoaded = false;
            }
            if (xmlLoaded)
            {
                foreach (XPathNavigator nav in doc.CreateNavigator().Select("root/data"))
                {
                    if (nav.NodeType != XPathNodeType.Comment)
                    {
                        target[nav.GetAttribute("name", string.Empty)] = nav.SelectSingleNode("value").Value;
                    }
                }

                int cacheMins;
                cacheMins = 3 * Convert.ToInt32(Globals.PerformanceSetting);

                if (cacheMins > 0)
                {
                    DataCache.SetCache(cacheKey, target, dp, DateTime.MaxValue, new TimeSpan(0, cacheMins, 0));
                }
            }
            return target;
        }